How to add a custom payment gateway to WPdeposit

Download gatewayExample class file

Since WPdeposit version 1.8 it is now possible to add extra payment gateways through external plugins without writing any code in the WPdeposit plugin itself.

This tutorial will teach you how to do this. I of course assume you have a basic knowledge in PHP (and classes). To find examples you can look at the payment gateways currently available in the WPdeposit plugin (plugins/wpdeposit/controllers/gateways).

Create a new plugin

To get started we will create a new plugin that allows us to add our code without modifying the wpdeposit plugin, which is not a good idea.

Create a new folder in the WordPress plugin folder (hopefully you are working on a local development environment and not a live production website!). I will name this folder custom_payment_gateway

In that folder create a new php file named index.php and add the following code at the top (make sure to update the info with your name, url, plugin description, …):

<?php 
/*
Plugin Name: Your new payment gateway
Plugin URI: http://yourpluginlink.org
Description: description here
Author: your name
Version: 1.0
Author URI: http://yourlink.org
*/

[contentBox type=”Information” ]No I did not add the php closing tag. It is rumored to be a bad practice. Read more[/contentBox]

Finally navigate to your WordPress plugin admin page and activate your new plugin.

Let’s start coding

The first thing you need to do is to add a filter for wpdeposit_payment_gateways and specify the callback function.

add_filter('wpdeposit_payment_gateways', 'example_gateway' );

Then add the callback function, it accepts a parameter which is the list of payment gateways in which you need to add yours and then return the list again. The name you pass is the name of the class that you will be using to code your custom payment gateway actions, this is very important!

function example_gateway( $gateway ) {
	$gateway[] = 'WPD_example_Gateway';
	return $gateway;
}

Create the class

Start by creating a class for your custom payment gateway

class WPD_example_Gateway extends WPD_Payment_Gateway {
	/**
	*
	* @var string
	*/
	public $_gatewayName = 'example';

	/**
	*
	* @var string
	*/
	public $_icon;

	/**
	*
	* @var boolean
	*/
	public $_testmode = false;

	/**
	* construct function
	*/
	public function __construct() {
		if (get_option(WPDEPOSIT_NAME . '_test_mode') == 1) {
			$this->_testmode = true;
		}
		parent::__construct();
	}

	/**
	* Function to return the PG icon
	* @return string
	*/
	public function getIcon(){
		$this->_icon = WPDEPOSIT_URL . '/resources/images/example.png';
		return $this->_icon;
	}
}


In the class you need to specify a name and an icon. You can also check in the construct of your class to see if wpdeposit is in test mode or not in case you need to check for this in your code later.

Add admin form fields

Next you need to create a function named getFormFields() where you can add form fields which will be used in the settings of the Payment gateways.

public function getFormFields(){
	return array(
		'title' => 'example Options',
		'fields' => array(
			array(
				'type' => 'textfield',
				'name' => 'test_field', //also used for the id
				'label' => 'Test field',
				'extra_options' => array('size' => 50, 'class' => 'input_field') //extra option for in the field
			),//end of text field
		)
	);//end of main array
}

The list of field types you can pass:

  • textfield
  • checkbox
  • uploadfile (uploadify will be used for this field option)
  • radio
  • textarea
  • title (useful if you need to separate certain form fields)

You can view our gateway example file to see how you need to specify the label, name, … for each form field

Create the init payment process function

Add another function named initPaymentProcess(). This will run after the user has selected your payment gateway and clicked on the submit button. The functions has one parameter which is the amount the user wants to deposit.

public function initPaymentProcess($amount)    {
	try {
		$response = json_encode(array('success' => true,
			'message' => 'Your request is being handled',
			'processtype' => 'redirecturl',
			'redirecturl' => 'your-process-url-here'
		));
	} catch (Exception $e) {
		$response = json_encode(array('success' => false, 'message' => $e->getMessage()));
	}
	return $response;
}


You must return a json encoded array containing:

  1. a message. This will be shown to the user before he is redirect to the payment gateway.
  2. a process type. Here you can specify what should happen once the user clicked on the submit button. ‘processtype’ should be set to ‘redirecturl’ if you wish to redirect a user to a specific page. Or ‘form’ if you need to specify a form to proceed to the payment gateway (like paypal).
  3. depending on the process type an url or a form.

Example of the third parameter:

    • For a redirect – ‘redirecturl’ => WPDEPOSIT_URL . ‘/includes/generate_invoice.php?tid=’ . $params[‘txn_id’]
    • For a form – ‘form’ => self::deposit_form($amount). Check the Paypal gateway class in the WPdeposit plugin for a good example on specifying a form.

Process payment

Finally you can create a function to process the payment. This function can be named whatever you like but to keep it simple use processPayment().
In this function you can add the following code:

$deposit = new DepositOptionsController();
$params = array(
	'userid' => $_POST['custom'],
	'txn_id' => $_POST['ipn_track_id'],
	'amount' => $_POST['payment_gross'],
	'payermail'     => $_POST['payer_email'],
	'status'        => 1,
);
$success = $deposit->processPayment($params);
if ($success) {
	//do some stuff
} else {
	//do some other stuff
}

It is important that you pass the exact same array as in the example above!

Callback url

If you need to process the payment through a callback url (like Paypal’s Instant payment notification) you can use an url like http://yourwpsite.com/?callbackaction and use that as a callback url to run any code you need to process the payment. Add this url wherever you need to specify it for your custom payment gateway.

hook into the init action of wordpress.

add_action('init', array($this, 'init'));

create a init function in your Payment gateway class and check if your query param exist and if it does call the processPayment() function of your class.

public function init(){
	if(isset($_GET['callbackaction'])){
		$class = New WPD_example_Gateway(); $class->processPayment();
	}
}