Working on ANZ-egate payment method for WP-Commerce Plugin

Working on ANZ-egate payment method for WP-E-commerce plugin (which is completed and here is the link to download).

Helpful resources:

There are certain issues so i strongly discourage using this plugin right now. I am busy with something else at the moment so if you are a php developer you can try and extend this plugin to make it complete.

Current status: Live, doing good.

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';

 */

$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 = "
 		
		  " . __('VPC Version', 'wpsc' ) . "
		  
		  
		  
                  
".__('This is Merchant VPC Version which is 1 by default', 'wpsc' )." " . __('Merchant ID', 'wpsc' ) . " " . __('Access Code', 'wpsc' ) . "
".__('This is Merchant Access Code', 'wpsc' )." " . __('Ref. Code', 'wpsc' ) . "
".__('This is Merchant Reference Code to identify transactions', 'wpsc' )." " . __('Server Type', 'wpsc' ) . " " . __('Test', 'wpsc' ) . " " . __('Live', 'wpsc' ) . " "; 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.
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 .= "\r\n"; $curryear++; } $gateway_checkout_form_fields[$nzshpcrt_gateways[$num]['internalname']] = "

We do not capture, store or handle card information.
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.

" . __( 'Credit Card Number *', 'wpsc' ) . " " . __( 'Credit Card Expiry *', 'wpsc' ) . " " . __( 'CVV *', 'wpsc' ) . " " . __( 'Card Type *', 'wpsc' ) . " "; }

3 thoughts on “Working on ANZ-egate payment method for WP-Commerce Plugin

  1. I integrated this script on a client’s site and successfully connected with ANZ E-Gate Merchant Facility. I am however encountering some problems:
    – Transactions are being double billed with ANZ?
    – If incorrected card details are supplied, the user is redirected to the “transaction-results”, where a large number of PHP errors and notices appear at the beginning and ending of the page. They cite problems with Line 379 of the script. I believe the issue is that the variables “vpc_AcqResponseCode” and “vpc_TxnResponseCode” are undefined as they exist in an array and are only defined on a successful transaction?

    Can you give me some clues?

Leave a Reply