Adding a payment gateway to payments manager

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.

  1. Create the basic plugin
  2. Add the admin settings
  3. Add checkout page fields and scripts
  4. Dealing with the checkout submission
  5. 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.

  1. Create a new folder for your plugin, for this tutorial we are calling ours: geodir_payfast_payment_manager
  2. 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
  3. 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.

  1. 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:
  2. 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.

Now if we activate/deactivate our plugin we will see the settings added to the payment manager settings page.payfast1


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.

  1. 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.
  2. Then we add the code to call the function in the correct place with a hook.
  3. 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”.

Now on the GD checkout page if you select PayFast you will be asked to enter your first and last name.

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.

Published by Stiofan O'Connor

Stiofan O'Connor is the co-founder and CEO of AyeCode LTD. With his business partner Paolo, they are the makers of the GeoDirectory, UsersWP and Invoicing plugins for WordPress. He is a Full Stack developer specialized in WordPress development. Stiofan started building websites as a hobby in the early 2000s with PHP at first. He then moved to the CodeIgniter Web Framework for a while before discovering WordPress and ever since he never looked back. Today his WordPress themes and plugins are used with success by +100.000 active websites.