This tutorial will show you how to create a new plugin to integrate a new payment gateway with our new payment manager.
We have made it very easy to integrate almost any payment gateway, in the example below i will be integrating a gateway called “PayFast” which is mainly used in South Africa.
- Create the basic plugin
- Add the admin settings
- Add checkout page fields and scripts
- Dealing with the checkout submission
- Dealing with the IPN/ITN notifications
Creating the basic plugin:
If you are reading this far then you should know how to create a basic plugin but for completeness i am going to list every step in the process.
- Create a new folder for your plugin, for this tutorial we are calling ours: geodir_payfast_payment_manager
- Create a new PHP file inside the folder and call it exactly as the folder name with .php at the end: geodir_payfast_payment_manager.php
- Add the basic text to the file that makes the plugin:
Congratulations, you now have a new plugin, you could install and activate this, it would not do anything but this is the start of our new plugin for adding our payment gateway.
Add the admin settings:
We have made this extremely simple as you will see below.
- First we will need to install and uninstall our settings preferably on the plugin activation and deactivation/uninstall so we add the code to run the functions to add our settings:
- Now we add the functions to add the settings. IMPORTANT: Whatever you use as the “key” in the below code is what will be used at the end of the checkout and ITN/IPN hook names, here we use “payfast” so the hooks we have to use are “geodir_payment_form_handler_payfast” and “geodir_ipn_handler_payfast“, you will replace those with your own key from the below code.
Add checkout page fields and scripts:
Again we have made this very simple. Some payment gateways you will enter your payment info on the screen at checkout and the next step will process it and in other cases you will be sent to the payment gateways website for payment such as PayPal standard, in our case for this example we are sent to the PayFast website but we will also ask for “First Name”, “Last Name” and “Email” at the checkout so we can show how to do both.
- If your gateway needs any extra info then we should ask for it when the payment method is selected, for PayFast we could maybe get away with not asking for any extra but they do ask for first name and last name where as on signup GD asks for a “full name” so this gives us a good opportunity to show how to show extra fields (you may not need this step for your payment gateway): First we create a function that will add the extra fields.
- Then we add the code to call the function in the correct place with a hook.
- PayFast is like paypal standard where payment takes place on the PayFast website, so instead to the normal “Pay & Publish” checkout button we will add a little snippet that changes the GD checkout button to “Proceed to PayFast”.
Dealing with the checkout submission:
Here we use the submitted info to build a form and auto submit it to PayFast.
Dealing with the IPN/ITN notifications:
Most of the code below is taken from the PayFast website that tells us how to verify the ITN (instant Transaction Notification). The code below that is enclosed in “###########################” is the code we use to mark the invoice paid and sent the payment notification emails.
You should now have a working payment gateway. If you have any questions or need pointers for integrating a new payment gateway, just let us know on our support forum and we will give you guidance and tips.