Favourite Host

Interserver

Install Service

Premium Support

Development

Order Custom Development Services

Payment Modules: Creating your own. (Advanced)

This article is aimed at PHP programmers who want to create their own payment modules.

The payment system is loosely designed around the Model-View-Controller (MVC) pattern from OO programming.

The payment module system is controlled by payment/payment_manager.php (Payment Manager)

The Payment Manager is responsible for loading the module files, listing the payment options, and configuration of the payment modules from the Admin.

The configuration data is stored in the ‘config’ table in the database

Each payment module has it’s own payment object in a separate file. Each payment module is responsible for rendering it’s own payment button, rendering configuration forms, loading / saving configuration data and handling the payment gateway integration.

Anatomy of a payment module

A payment module has two sections, the top part is the integration with the payment gateway, and the bottom part is an implementation of the payment object.

Here is an example based around the Paypal module:

<?php
// Change to whatever is your payment class name 
$_PAYMENT_OBJECTS['PayPal'] = new PayPal;

if ( $_POST['txn_id'] != '' ) {
	// insert your gateway handling code here (see paypalIPN.php for further hints)
	// This code should only execute when called by the gateway, in this case this script is called 	
	// by the PayPal IPN system, where a txn_id is posted to the script. 	
	// The script will then do all the PayPal IPN stuff in here. (Validate the request & complete the order)
}

// Change to whatever is your payment object name
class PayPal {
	// Change to whatever is your payment method 	
	var $name = "PayPal";
	
	// Change to whatever you want displayed 	
	var $description = "PayPal Secure Credit Card Payment";
	
	// Change to whatever is your payment class name 	
	var $className = "PayPal";
	
	// A payment object has to implement the following functions: 	
	// Constructor that loads all the configuration from the config table 	
	function __construct() {}

	// returns the payment module's current currency 	
	function get_currency() {}

	// installs all the payment module to the config table 	
	function install() {}

	// removes the payment module form the config table 	
	function uninstall {}

	// renders the payment button for the advertiser 	
	function payment_button() {}

	// config form in the admin 	
	function config_form() {}

	// save_config 	
	function save_config() {}

	function is_enabled() {}

	function is_installed() {}

	function enable() {}

	function disable() {}
}

What is the best way to create a new payment module?

1. Copy paypalIPN.php and name the new file whatever you like.

2. Edit the new file, remove the old gateway handling code, change the class names and $_PAYMENT_OBJECTS variable.

3. Determine which variables you will need for the payment button. Implement a basic payment button.

4. Implement the Admin side of the module, including the config form, installing / enabling, constructor, etc

5. Test your payment module to see that the configuration is saved, payment button is properly displayed.

6. Implement the payment gateway section.

7. Test the payment gateway

Powered by BetterDocs

Submit a Comment

Your email address will not be published. Required fields are marked *