ANZ-egate payment method for WP-Commerce Plugin

ANZ-egate payment method for WP-E-commerce plugin. I hope it works for you. Bug-fixing or improvements are welcomed.

How to install: Create a new file (plugins/wp-e-commerce/wpsc-merchants/anz-egate.merchant.php) and place this code into this file. Once saved check the Payment Method section of the WP-ECommerce and it should show options for the ANZ-Egate payment method. It should work similar to other payment methods. Create your own logo for ANZegate or download this one anz-egate payment method for wp-commerce plugin and place in plugin’s images folder and update the following image url option:

$nzshpcrt_gateways[$num]['image'] = WPSC_URL . '/images/anz-egate_checkout.jpg';
<?php
/***
 	ANZ-eGate payment gateway payment method for WP-e-Commerce plugin of wordpress
    Copyright (C) 2012 Arvind K.(arvind.mailto@gmail.com, http://www.devarticles.in)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

$nzshpcrt_gateways[$num]['name'] = 'ANZ eGate 1.0';
$nzshpcrt_gateways[$num]['image'] = WPSC_URL . '/images/anz-egate_checkout.jpg';
$nzshpcrt_gateways[$num]['api_version']    =   1.0;
$nzshpcrt_gateways[$num]['class_name']  =   'wpsc_merchant_anzegate';
$nzshpcrt_gateways[$num]['function'] = 'gateway_anzegate';
$nzshpcrt_gateways[$num]['form'] = "form_anzegate";
$nzshpcrt_gateways[$num]['submit_function'] = "submit_anzegate";
$nzshpcrt_gateways[$num]['is_exclusive'] = true;
$nzshpcrt_gateways[$num]['payment_type'] = "anzegate";
$nzshpcrt_gateways[$num]['internalname'] = 'wpsc_merchant_anzegate';
$nzshpcrt_gateways[$num]['display_name'] = 'ANZ-eGate';

/**
 * WP eCommerce ANZ-eGate Merchant Class
 *
 * This is the ANZ-eGate standard merchant class, it extends the base merchant class
 *
 * @package wp-e-commerce
 * @since 3.7.6
 * @subpackage wpsc-merchants
 */
class wpsc_merchant_anzegate extends wpsc_merchant {
	var $name              = 'ANZ eGate 1.0';
}

function form_anzegate()    {
    
    global $wpdb, $wpsc_gateways;

    $serverType1 = '';
    $serverType2 = '';
    
    if (get_option('anzegate_server_type') == 'test')
            $serverType1="checked='checked'";
    elseif(get_option('anzegate_server_type') == 'live')
            $serverType2 ="checked='checked'";
    
    $anzegate_Version = get_option('anzegate_Version');
    
    //if(is_null($anzegate_Version) || $anzegate_Version="")  
     //   $anzegate_Version = '1';
    
    $output = "
 		<tr>
		  <td>" . __('VPC Version', 'wpsc' ) . "
		  </td>
		  <td>
		  <input type='text' size='40' value='".$anzegate_Version."' name='anzegate_Version' />
                  <br />
                  <small>".__('This is Merchant VPC Version which is 1 by default', 'wpsc' )."</small>
		  </td>
		</tr>
		<tr>
		  <td>" . __('Merchant ID', 'wpsc' ) . "
		  </td>
		  <td>
		  <input type='text' size='40' value='".get_option('anzegate_MerchantID')."' name='anzegate_MerchantID' />
		  </td>
		</tr>
		<tr>
		  <td>" . __('Access Code', 'wpsc' ) . "
		  </td>
		  <td>
		  <input type='text' size='40' value='".get_option('anzegate_AccessCode')."' name='anzegate_AccessCode' />
                  <br />
                  <small>".__('This is Merchant Access Code', 'wpsc' )."</small>
		  </td>
		</tr>
		<tr>
		  <td>" . __('Ref. Code', 'wpsc' ) . "
		  </td>
		  <td>
		  <input type='text' size='40' value='".get_option('anzegate_MerchTxnRef')."' name='anzegate_MerchTxnRef' />
                  <br />
                  <small>".__('This is Merchant Reference Code to identify transactions', 'wpsc' )."</small>
		  </td>
		</tr>
		<tr>
		 <td>" . __('Server Type', 'wpsc' ) . "
		 </td>
		 <td>
			<input $serverType1 type='radio' name='anzegate_server_type' value='test' /> " . __('Test', 'wpsc' ) . "
			<input $serverType2 type='radio' name='anzegate_server_type' value='live' /> " . __('Live', 'wpsc' ) . "
		 </td>
		</tr>
";
    return $output;
}

function submit_anzegate()    {

    if($_POST['anzegate_Version'] != null) {
        update_option('anzegate_Version', $_POST['anzegate_Version']);
    }

    if($_POST['anzegate_MerchantID'] != null) {
        update_option('anzegate_MerchantID', $_POST['anzegate_MerchantID']);
    }

    if($_POST['anzegate_AccessCode'] != null) {
        update_option('anzegate_AccessCode', $_POST['anzegate_AccessCode']);
    }    
    
    if($_POST['anzegate_MerchTxnRef'] != null) {
        update_option('anzegate_MerchTxnRef', $_POST['anzegate_MerchTxnRef']);
    }
    
    if($_POST['anzegate_server_type'] != null) {
        update_option('anzegate_server_type', $_POST['anzegate_server_type']);
    }
    
    return true;

}

function gateway_anzegate($fromcheckout=false)  {
	
	if(!isset($_REQUEST['custom_gateway']))
		return;
	
	$user_ID=null;
		
	if($_REQUEST['custom_gateway']!='wpsc_merchant_anzegate')
	return;

	error_reporting(1);
	ini_set('error_reporting', E_ALL);
	ini_set('display_errors', 2);
	
    global $wpdb, $wpsc_cart, $wpsc_checkout,$current_user,  $purchlogs;
    
    if(!isset($wpsc_checkout)){
            $wpsc_checkout = new wpsc_checkout();
    }
    if(!isset($_SESSION['wpsc_sessionid'])){
            $sessionid = (mt_rand(100,999).time());
            $_SESSION['wpsc_sessionid'] = $sessionid;
    }

    if($_SESSION['wpsc_delivery_region'] == null && $_SESSION['wpsc_selected_region'] == null){
            $_SESSION['wpsc_delivery_region'] = get_option('base_region');
            $_SESSION['wpsc_selected_region'] = get_option('base_region');
    }

    $wpsc_cart->get_shipping_option();
    $wpsc_cart->get_shipping_quotes();
    $wpsc_cart->get_shipping_method();
    //$wpsc_cart->google_shipping_quotes();
    $subtotal = $wpsc_cart->calculate_subtotal();
    $base_shipping = $wpsc_cart->calculate_total_shipping();
    $tax = $wpsc_cart->calculate_total_tax();
    $total = $wpsc_cart->calculate_total_price();

    
    if($total > 0 ){
            $update = $wpdb->update(
                        WPSC_TABLE_PURCHASE_LOGS,
                        array(
                            'totalprice' => $total,
                            'statusno' => 0,
                            'user_ID' => $user_ID,
                            'date' => time(),
                            'gateway' => 'anzegate',
                            'billing_country' => $wpsc_cart->delivery_country,
                            'shipping_country' => $wpsc_cart->selected_country,
                            'base_shipping' => $base_shipping,
                            'shipping_method' => $wpsc_cart->selected_shipping_method,
                            'shipping_option' => $wpsc_cart->selected_shipping_option,
                            'plugin_version' => WPSC_VERSION,
                            'discount_value' => $wpsc_cart->coupons_amount,
                            'discount_data' => $wpsc_cart->coupons_name
                        ),
                        array(
                            'sessionid' => $_SESSION['wpsc_sessionid']
                        ),
                        array(
                            '%f',
                            '%d',
                            '%d',
                            '%s',
                            '%s',
                            '%s',
                            '%s',
                            '%f',
                            '%s',
                            '%s',
                            '%s',
                            '%f',
                            '%s',
                        ),
                        '%s'
                    );
            $sql = $wpdb->prepare( "SELECT `id` FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE sessionid = %s", $_SESSION['wpsc_sessionid'] );
            $purchase_log_id = $wpdb->get_var($sql);
            if( !empty($purchase_log_id) )  {
                    $sql = $wpdb->prepare( "DELETE FROM  `".WPSC_TABLE_CART_CONTENTS."` WHERE purchaseid = %d", $purchase_log_id );
                    $wpdb->query($sql);
            }
            if( ! $update ){            	
                    $insert = $wpdb->insert(
                        WPSC_TABLE_PURCHASE_LOGS,
                        array(
                            'totalprice' => $total,
                            'statusno' => 0,
                            'sessionid' => $_SESSION['wpsc_sessionid'],
                            'user_ID' => @$user_ID,
                            'date' => time(),
                            'gateway' => 'anzegate',
                            'billing_country' => $wpsc_cart->delivery_country,
                            'shipping_country' => $wpsc_cart->selected_country,
                            'base_shipping' => $base_shipping,
                            'shipping_method' => $wpsc_cart->selected_shipping_method,
                            'shipping_option' => $wpsc_cart->selected_shipping_option,
                            'plugin_version' => WPSC_VERSION,
                            'discount_value' => $wpsc_cart->coupons_amount,
                            'discount_data' => $wpsc_cart->coupons_name
                        ),
                        array(
                            '%f',
                            '%d',
                            '%s',
                            '%d',
                            '%s',
                            '%s',
                            '%s',
                            '%f',
                            '%s',
                            '%s',
                            '%s',
                            '%s',
                            '%f',
                            '%s',
                        ),
                        '%s'
                    );
                    $purchase_log_id = $wpdb->insert_id;
            }
            
            $purchase_log_id;

            $wpsc_cart->save_to_db( $purchase_log_id );

            if( get_option( 'permalink_structure' ) != '' ) {
                    $separator = "?";
            } else {
                    $separator = "&";
            }
            
            anzegate_Usecase("&", $_SESSION['wpsc_sessionid'], $fromcheckout);
    }
}
    
function anzegate_Usecase($separator, $sessionid, $fromcheckout) {
    
    if(empty($_POST['card_number']) || empty($_POST['card_code']))
    	return;
	
	
	global $wpdb, $wpsc_cart ;
    
    $doceil=false;

    $purchase_log_sql = $wpdb->prepare( "SELECT * FROM `".WPSC_TABLE_PURCHASE_LOGS."` WHERE `sessionid` = %s  LIMIT 1", $sessionid );
    $purchase_log     = $wpdb->get_results( $purchase_log_sql, ARRAY_A ) ;

    $cart_sql         = $wpdb->prepare( "SELECT * FROM `".WPSC_TABLE_CART_CONTENTS."` WHERE `purchaseid` = %d", $purchase_log[0]['id'] );
    $wp_cart          = $wpdb->get_results($cart_sql,ARRAY_A) ;
    
    $vpc_OrderInfo = "#".$purchase_log[0]['id'] . "-";
    
    /*foreach($wp_cart as $i => $Item) {  
        $vpc_OrderInfo .= "#".$Item['prodid']. "x" . $Item['quantity'] . ",";
    }*/
    
    $vpc_OrderInfo .= $_SESSION['wpsc_checkout_saved_values'][9]; //user email address
    $data = array();
    
    if(isset($_POST['card_number']))    {
        $data['vpc_CardNum'] = $_POST['card_number'];    
    }
     if(isset($_POST['card_code']))    {
        $data['vpc_CardSecurityCode'] = $_POST['card_code'];    
    }
    
    if (get_option('anzegate_server_type') == 'test') {    
    	$data['vpc_Amount'] = 100; //The testing requires 00 at the end of price. so it is $1.00 right now
    }	else	{
    	$data['vpc_Amount'] = (int)($purchase_log[0]['totalprice'] * 100);
    }
    $data['vpc_Version']	= get_option('anzegate_Version'); 
    $data['vpc_Merchant']	= get_option('anzegate_MerchantID');
    $data['vpc_Command'] = 'pay';   
    $data['vpc_AccessCode'] = get_option('anzegate_AccessCode');
    $data['vpc_OrderInfo'] = $vpc_OrderInfo; 
    $data['vpc_MerchTxnRef'] = "#".$purchase_log[0]['id']."-".$_SESSION['wpsc_checkout_saved_values'][9];// . "-" . wp_generate_password(15, false);
    if(isset($_POST['expiry']))    {
        $data['vpc_CardExp'] = substr($_POST['expiry']['year'], 2, 2).$_POST['expiry']['month'];
    }
    
    $transact_url     = get_option('transact_url');
    $returnURL        =  $transact_url.$separator."sessionid=".$sessionid."&gateway=anzegate";
    // Ordered Products
    /*foreach($wpsc_cart->cart_items as $i => $Item) {        
        $data['PROD_NAME'.$i] = $Item->product_name;        
        $data['PROD_AMT'.$i] = number_format($Item->unit_price,2);        
        $data['PROD_NUMBER'.$i]	= $i;        
        $data['PROD_QTY'.$i] = $Item->quantity;        
        $data['PROD_TAXAMT'.$i]	= number_format($Item->tax,2);    
    }*/
    
    $transaction = "";
    $ampersand = "";
    foreach($data as $key => $value) {
        // create the POST data input leaving out any fields that have no value
        if (strlen($value) > 0) {
            $transaction .= $ampersand . urlencode($key) . '=' . urlencode($value);
            $ampersand = "&";
        }
    }
    //$transaction="vpc_CardNum=4005550000000001&vpc_CardSecurityCode=123
&vpc_Amount=100&vpc_Version=1&vpc_Merchant=TESTANZPWAUS
&vpc_Command=pay&vpc_AccessCode=5428B104&vpc_OrderInfo=test1234
&vpc_MerchTxnRef=NTbto2cNK2&vpc_CardExp=1305";
    //now we add all the information in the array into a long string
    //Now we have the information we want to send to the gateway in a nicely formatted string we can setup the cURL
	//ob_start();	
	// initialise Client URL object
    ob_start();
	$ch = curl_init();
	
	//pr($transaction);
	//exit;
	
	// set the URL of the VPC
	curl_setopt($ch, CURLOPT_URL, "https://migs.mastercard.com.au/vpcdps");
	curl_setopt ($ch, CURLOPT_POST, 1);
	curl_setopt ($ch, CURLOPT_POSTFIELDS, $transaction);
	//curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	// (optional) set the proxy IP address and port
	//curl_setopt ($ch, CURLOPT_PROXY, "192.168.21.13:80");
	
	// (optional) certificate validation
	// trusted certificate file
	//curl_setopt($ch, CURLOPT_CAINFO, "c:/temp/ca-bundle.crt");
	
	//turn on/off cert validation
	// 0 = don't verify peer, 1 = do verify
	//curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
	
	// 0 = don't verify hostname, 1 = check for existence of hostame, 2 = verify
	//curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
	
	// connect

	curl_exec ($ch);

	
	// get response
	$response = ob_get_contents();
	
	// turn output buffering off.
	ob_end_clean();    
	
    $r = array();
    $pairs = explode("&",$response);
    foreach($pairs as $pair) {
        list($key,$value) = explode("=",$pair);
        $r[$key] = urldecode($value);
    }
    if($r['vpc_TxnResponseCode']=="00" && $r['vpc_TxnResponseCode'] == "0"){
        $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS.
        "` SET `processed`= '2' WHERE `sessionid`=".$sessionid;
        $wpdb->query($sql);
        $transact_url = get_option('transact_url');
        unset($_SESSION['WpscGatewayErrorMessage']);
        
        header("Location: ".$transact_url."?sessionid=".$sessionid);
    }   else    {
        //redirect back to checkout page with errors
        $sql = "UPDATE `".WPSC_TABLE_PURCHASE_LOGS.
        "` SET `processed`= '5' WHERE `sessionid`=".$sessionid;
        $wpdb->query($sql);
        $transact_url = get_option('checkout_url');
        //die($transact_url);
        $transact_url = "/store/checkout/";
        $_SESSION['WpscGatewayErrorMessage'] =  __('Sorry your transaction did not go through successfully, please try again.<br />Error: ' . $r['vpc_Message']);
        header("Location: ".$transact_url);
    }
}


$years = $months = '';

if ( in_array( 'wpsc_merchant_anzegate', (array)get_option( 'custom_gateway_options' ) ) ) {

	$curryear = date( 'Y' );

	//generate year options
	for ( $i = 0; $i < 10; $i++ ) {
		$years .= "<option value='" . $curryear . "'>" . $curryear . "</option>\r\n";
		$curryear++;
	}

	$gateway_checkout_form_fields[$nzshpcrt_gateways[$num]['internalname']] = "
	<tr>
		<td colspan='2'>
			<p><strong>We do not capture, store or handle card information.</strong><br />
SnoreMeds™ do not capture, store or handle card information directly. Snoremeds™ make use of the ANZ bank and their Anzegate secure gateway who handle the payment transactions on our behalf.</p>
		</td>
	</tr>
	<tr>
		<td class='wpsc_CC_details'>" . __( 'Credit Card Number *', 'wpsc' ) . "</td>
		<td>
			<input type='text' value='' name='card_number' />
		</td>
	</tr>
	<tr>
		<td class='wpsc_CC_details'>" . __( 'Credit Card Expiry *', 'wpsc' ) . "</td>
		<td>
			<select class='wpsc_ccBox' name='expiry[month]'>
			" . $months . "
			<option value='01'>01</option>
			<option value='02'>02</option>
			<option value='03'>03</option>
			<option value='04'>04</option>
			<option value='05'>05</option>
			<option value='06'>06</option>
			<option value='07'>07</option>
			<option value='08'>08</option>
			<option value='09'>09</option>
			<option value='10'>10</option>
			<option value='11'>11</option>
			<option value='12'>12</option>
			</select>
			<select class='wpsc_ccBox' name='expiry[year]'>
			" . $years . "
			</select>
		</td>
	</tr>
	<tr>
		<td class='wpsc_CC_details'>" . __( 'CVV *', 'wpsc' ) . "</td>
		<td><input type='text' size='4' value='' maxlength='4' name='card_code' />
		</td>
	</tr>
	<tr>
		<td class='wpsc_CC_details'>" . __( 'Card Type *', 'wpsc' ) . "</td>
		<td>
		<select class='wpsc_ccBox' name='cctype'>
			<option value='Visa'>" . __( 'Visa', 'wpsc' ) . "</option>
			<option value='Mastercard'>" . __( 'MasterCard', 'wpsc' ) . "</option>
			<option value='Discover'>" . __( 'Discover', 'wpsc' ) . "</option>
			<option value='Amex'>" . __( 'Amex', 'wpsc' ) . "</option>
		</select>
		</td>
	</tr>
";

}

One thought on “ANZ-egate payment method for WP-Commerce Plugin

  1. Hi,

    Thanks for sharing code.
    But only one file code could not work. I tried, But nothing to show in banckend plugins.

    I could not found plugin activation code, How could I activate this plugin and if I add new code for plugin activation, where is “wpsc_merchant_anzegate” class file which is extended by “wpsc_merchant” .

    You need to share more files or full plugin source.

    Thanks,
    Hemant Vaniya

Leave a Reply

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