Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Payment Gateway WordPress


I need help with a payment gateway. I have attached documentation that explains what I am trying to do. I need this example to connect to sage payments.

Thanks in advance

How to Add a Payment Gateway
Payment gateway programming takes place in library/payment folder of theme folder. All programming files are kept in single folder and the folder name should be same as “key”. Eg: I want to integrate paypal pro to my site so my key and folder name should be like “paypalpro”. In other way folder name must be same as the “key” value of data stored in database to identify the gateway information.
There are mainly 3 files to manage your payment gateway. File structure is as follows:
• install.php
• paypalpro.php (in case of my folder name is “paypalpro”)
• paypalpro_response.php (incase of my folder name is “paypalpro”)
Coding of install.php should be like this:

$paymentmethodname = paypalpro;
$paymethodinfo = array();
$payOpts = array();
$payOpts[] = array(
"title" => "API User Name",
"fieldname" => "api_username",
"value" => "",
"description" => __('Example')." :",
$payOpts[] = array(
"title" => "API Password",
"fieldname" => "api_password",
"value" => "QFZCWN5HZM8VBG7Q",
"description" => __('Example')." : QFZCWN5HZM8VBG7Q ",
$payOpts[] = array(
"title" => "API Signature",
"fieldname" => "api_signature",
"value" => "A.d9eRKfd1yVkRrtmMfCFLTqa6M9AyodL0SJkhYztxUi8W9pCXF6.4NI",
"description" => __('Example')." : A.d9eRKfd1yVkRrtmMfCFLTqa6M9AyodL0SJkhYztxUi8W9pCXF6.4NI",
$paymethodinfo = array("name" => 'Paypal Pro Direct',
"key" => $paymentmethodname,
"isactive" => '1', // 1->display,0->hide
”payOpts" => $payOpts,
update_option("payment_method_$paymentmethodname", $paymethodinfo );
$install_message = __("Payment Method integrated successfully");
$option_id = $wpdb->get_var("select option_id from $wpdb->options where option_name like "payment_method_$paymentmethodname"");
$install_message = __("this payment method cannot deleted because it is fix, you can deactive it");

Whatever coding you do in HTML or php or JavaScript, it will be effected on checkout page. Just like if you want to allow user to enter credit card information, you need to create the form which contains coding to collect credit card information. Below code is for PayPal pro HTML form which will display on checkout page.
This sample coding is for our “paypal pro” example, which will help you to develop your Payment gateway coding for checkout page:

<?php $paymentType = 'Authorization'; ?>
<tr id="paypal_prooptions" style="display:none;" >
<td colspan="2" >
<script language="JavaScript">
function generateCC(){
var cc_number = new Array(16);
var cc_len = 16;
var start = 0;
var rand_number = Math.random();
switch(document.checkout_frm.creditCardType.value) {
case "Visa":
cc_number[start++] = 4;
case "Discover":
cc_number[start++] = 6;
cc_number[start++] = 0;
cc_number[start++] = 1;
cc_number[start++] = 1;
case "MasterCard":
cc_number[start++] = 5;
cc_number[start++] = Math.floor(Math.random() * 5) + 1;
case "Amex":
cc_number[start++] = 3;
cc_number[start++] = Math.round(Math.random()) ? 7 : 4 ;
cc_len = 15;

for (var i = start; i < (cc_len - 1); i++) {
cc_number[i] = Math.floor(Math.random() * 10);
var sum = 0;
for (var j = 0; j < (cc_len - 1); j++) {
var digit = cc_number[j];
if ((j & 1) == (cc_len & 1)) digit *= 2;
if (digit > 9) digit -= 9;
sum += digit;
var check_digit = new Array(0, 9, 8, 7, 6, 5, 4, 3, 2, 1);
cc_number[cc_len - 1] = check_digit[sum % 10];
document.checkout_frm.creditCardNumber.value = "";
for (var k = 0; k < cc_len; k++) {
document.checkout_frm.creditCardNumber.value += cc_number[k];
<input type=hidden name=paymentType value="<?=$paymentType?>" />
<table width=600>
global $current_user;
<td align=right><?php _e('Card Type');?>:</td>
<td align=left>
<select name=creditCardType onChange="javascript:generateCC(); return false;">
<option value=Visa selected>Visa</option>
<option value=MasterCard>MasterCard</option>
<option value=Discover>Discover</option>
<option value=Amex>American Express</option>
<td align=right><?php _e('Card Number');?>:</td>
<td align=left><input type=text size=19 maxlength=19 name=creditCardNumber></td>
<td align=right><?php _e('Expiration Date');?>:</td>
<td align=left><p>
<select name=expDateMonth>
<option value=1>01</option>
<option value=2>02</option>
<option value=3>03</option>
<option value=4>04</option>
<option value=5>05</option>
<option value=6>06</option>
<option value=7>07</option>
<option value=8>08</option>
<option value=9>09</option>
<option value=10>10</option>
<option value=11>11</option>
<option value=12>12</option>
<select name=expDateYear>
<?php $curryr = date('Y');
for($i=$curryr;$i<$curryr+11;$i++){ ?>
<option value=<?php echo $i;?>><?php echo $i;?></option>
<?php }?></select>
<td align=right><?php _e('Card Verification Number');?>:</td>
<td align=left><input type=text size=3 maxlength=4 name=cvv2Number value=></td>
<script type="text/javascript">generateCC();</script>

This file contains PHP coding mainly. After confirming the order, cart information will be saved to database and user will be redirected to payment gateway. Lets take an example of “PayPal Pro”, we created HTML to collect credit card information and now its time to collect the information and forward to PayPal to transfer payment to merchant account.
Below sample coding is for our “paypal pro” example, which help you to develop your Payment gateway coding after order confirmation.

<?php global $General;
$subject = SUBJECT;
* Get required parameters from the web form for the request
global $General, $Cart;
$paymentOpts = $General->get_payment_optins($_REQUEST['paymentmethod']);
$userInfo = $General->getLoginUserInfo();
$user_address_info = unserialize(get_user_option('user_address_info', $userInfo['ID']));
$taxable_amt_info = $General->get_tax_amount();
$taxable_amt = $taxable_amt_info[0];
$payable_amt = $General->get_payable_amount($_REQUEST['shippingmethod']);

$paymentType =urlencode( $_POST['paymentType']);
$firstName =urlencode( $userInfo['display_name']);
$lastName =urlencode( $user_address_info['last_name']);
$creditCardType =urlencode( $_POST['creditCardType']);
$creditCardNumber = urlencode($_POST['creditCardNumber']);
$expDateMonth =urlencode( $_POST['expDateMonth']);

// Month must be padded with leading zero
$padDateMonth = str_pad($expDateMonth, 2, '0', STR_PAD_LEFT);

$expDateYear =urlencode( $_POST['expDateYear']);
$cvv2Number = urlencode($_POST['cvv2Number']);
$address1 = urlencode($user_address_info['user_add1']);
$address2 = urlencode($user_address_info['user_add2']);
$city = urlencode($user_address_info['user_city']);
$state =urlencode( $user_address_info['user_state']);
$zip = urlencode($user_address_info['user_postalcode']);
$amount = urlencode($payable_amt);

/* Construct the request string that will be sent to PayPal.
The variable $nvpstr contains all the variables and is a
name value pair string with & as a delimiter */
$nvpstr="&PAYMENTACTION=$paymentType&AMT=$amount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber&EXPDATE=". $padDateMonth.$expDateYear."&CVV2=$cvv2Number&FIRSTNAME=$firstName&LASTNAME=$lastName&STREET=$address1&CITY=$city&STATE=$state".

$getAuthModeFromConstantFile = true;
//$getAuthModeFromConstantFile = false;
$nvpHeader = "";

if(!$getAuthModeFromConstantFile) {
$AuthMode = "THIRDPARTY"; //Partner's API Credential and Merchant Email as Subject are required.
} else {
if(!empty($API_UserName) && !empty($API_Password) && !empty($API_Signature) && !empty($subject)) {
$AuthMode = "THIRDPARTY";
}else if(!empty($API_UserName) && !empty($API_Password) && !empty($API_Signature)) {
$AuthMode = "3TOKEN";
}else if(!empty($subject)) {
$AuthMode = "FIRSTPARTY";

switch($AuthMode) {

case "3TOKEN" :
$nvpHeader = "&PWD=".urlencode($API_Password)."&USER=".urlencode($API_UserName)."&SIGNATURE=".urlencode($API_Signature);
$nvpHeader = "&SUBJECT=".urlencode($subject);
$nvpHeader = "&PWD=".urlencode($API_Password)."&USER=".urlencode($API_UserName)."&SIGNATURE=".urlencode($API_Signature)."&SUBJECT=".urlencode($subject);


$nvpstr = $nvpHeader.$nvpstr;

/* Make the API call to PayPal, using API signature.
The API response is stored in an associative array called $resArray */

/* Display the API response back to the browser.
If the response from PayPal was a success, display the response parameters'
If the response was an error, display the errors received using APIError.php.
$ack = strtoupper($resArray["ACK"]);
$paymentFlag = 1;
$_SESSION['display_message'] = $resArray['L_LONGMESSAGE0'];
$redirectUrl = get_option('siteurl')."/?ptype=payment_success&oid=".$orderNumber;
else //Failure
$paymentFlag = 0;
$errorCode= $_SESSION['curl_error_no'] ;
$errorMessage=$_SESSION['curl_error_msg'] ;
$_SESSION['display_message'] = $errorMessage;
$paymentFlag = 0;
$_SESSION['display_message'] = $resArray['L_LONGMESSAGE0'];

if($paymentFlag == 0)
global $General;

Once you complete all above development process, now its time to activate and test what we have done.
Go to wp-admin > Shopping Cart > Payment Options : You can see two options on the page
(a) Current Installed & (b) Install New/All Options
• Click on Second option (Install New/All Options) and you can see listing of all payment method which are there in library/payment folder.
• Find your gateway you have developed. In our case we have developed “PayPal Pro”, so “paypalpro” is seen in the listing and now just click on “Install”.
• Your install.php file will be executed and whatever setting you have in it will be effected and you can see it from “Current Installed” option > settings.
• Make sure you “Activate” this new payment gateway from the Gateway settings
• Now place the order and check it by placing a test order
So that’s it, we developed and integrated new payment gateway for eCommerce Plex framework.

Answers (2)


Just Me answers:

the procedure seems pretty straight forward but without the Sage payment documentation it is not possible to create a payment gateway.

You will have to check their requirements. Find out the format the data needs to be in and what data they exactly need.

You can add the above example to your installation, to see if it works correctly then change it around to Sage payments gateway.

jmmgmm comments:


The documentation is here. Page 23 has the php sample:

Just Me comments:

You will have to compare the two and make changes. For instance the PayPal Pro example uses





where the Sage one needs

$data = "m_id=" . "$mid"; //your eftsecure merchant id. 
$data .= "&m_key=" . "$mkey"; // your eftsecure merchant key;

Not sure if you have the knowledge and time to do that.
Did you try to find someone on the eCommerce forum who already developed a thing like that? I assume you are not the only one looking for this.


Christianto answers:


I believe creating payment gateway not only require you to integrate it with Wordpress but also you must taken care all aspect, most importantly security factor since this related personal information and credentials of your user.

Personally, It's beyond my capabilities..
I would suggest you to raise the prize money and add additional requirement for expert to taken care security factor (like using ssl protocol and other factor). So experts that has ability in security field will answer it.

You can ask for refund if no one can do it for you.

Hope this help