Code:
<?php
/*
$Id: ecardon.php 1739 2009-05-24 05:16:16Z hpdl $
Ecardon Payments Class for Open Source osCommerce
http://www.oscommerce.com
Copyright (c) 2009 James Berry
Released under the GNU General Public License
For installation or technical assistance: 1-801-983-6088
*/
class ecardon {
var $code, $title, $description, $enabled;
function ecardon () {
global $order;
$this->code = 'ecardon';
$this->host = MODULE_PAYMENT_ECARDON_SERVER;
$this->path = MODULE_PAYMENT_ECARDON_PATH;
$this->sender = MODULE_PAYMENT_ECARDON_SENDER;
$this->channel = MODULE_PAYMENT_ECARDON_CHANNEL;
$this->userid = MODULE_PAYMENT_ECARDON_USER_ID;
$this->userpwd = MODULE_PAYMENT_ECARDON_PASSWORD;
$this->transaction_mode = MODULE_PAYMENT_ECARDON_TRANSACTION_MODE;
$this->transaction_response = MODULE_PAYMENT_ECARDON_RESPONSE;
$this->title = MODULE_PAYMENT_ECARDON_TEXT_TITLE;
$this->description = MODULE_PAYMENT_ECARDON_TEXT_DESCRIPTION;
$this->sort_order = MODULE_PAYMENT_ECARDON_SORT_ORDER;
$this->enabled = ((MODULE_PAYMENT_ECARDON_STATUS == 'True') ? true : false);
if ((int)MODULE_PAYMENT_ECARDON_ORDER_STATUS_ID > 0) {
$this->order_status = MODULE_PAYMENT_ECARDON_ORDER_STATUS_ID;
}
if (is_object($order)) $this->update_status();
$this->user_agent="php ecardonpost";
$this->request_version="1.0";
$this->payment_code = "CC.DB"; //CREDIT CARD DEBIT
if (MODULE_PAYMENT_ECARDON_GATEWAY_SERVER == 'Live') {
$this->host = 'ctpe.io/payment/ctpe';
$this->transaction_mode = 'LIVE';
} else {
$this->host = 'test.ctpe.io/payment/ctpe';
$this->transaction_mode = 'CONNECTOR_TEST';
}
}
// class methods
function update_status() {
global $order;
if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_ECARDON_ZONE > 0) ) {
$check_flag = false;
$check_query = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_ECARDON_ZONE . "' and zone_country_id = '" . $order->billing['country']['id'] . "' order by zone_id");
while (!$check -> EOF) {
if ($check['zone_id'] < 1) {
$check_flag = true;
break;
} elseif ($check['zone_id'] == $order->billing['zone_id']) {
$check_flag = true;
break;
}
$check->MoveNext();
}
if ($check_flag == false) {
$this->enabled = false;
}
}
}
function javascript_validation() {
$js = 'if (payment_value == "' . $this->code . '") {' . "\n" .
' var number = document.checkout_payment.ecardon_cc_number.value;' . "\n" .
' var cvd = document.checkout_payment.ecardon_cvd_number.value;'. "\n".
' if ( number == "" || number.length < ' . CC_NUMBER_MIN_LENGTH . ') {' . "\n" .
' error_message = error_message + "' . MODULE_PAYMENT_ECARDON_TEXT_JS_CC_NUMBER . '";' . "\n" .
' error = 1;' . "\n" . '}' . "\n" .
' if (cvd == "" || cvd.length < 3 ) {'."\n".
' error_message = error_message + "' . MODULE_PAYMENT_ECARDON_TEXT_JS_CC_CVD . '";'."\n".
' error = 1;'. "\n".
' }' . "\n".
// Optional CC Validation can be done here. Processed by cc_validation so not required
' }' . "\n";
return $js;
}
function selection() {
global $order;
for ($i=1; $i<13; $i++) {
$expires_month[] = array('id' => sprintf('%02d', $i), 'text' => strftime('%B',mktime(0,0,0,$i,1,2000)));
}
$today = getdate();
for ($i=$today['year']; $i < $today['year']+10; $i++) {
$expires_year[] = array('id' => strftime('%y',mktime(0,0,0,1,1,$i)), 'text' => strftime('%Y',mktime(0,0,0,1,1,$i)));
}
$selection = array('id' => $this->code,
'module' => $this->title,
'fields' =>
array(
array('title' => MODULE_PAYMENT_ECARDON_TEXT_CREDIT_CARD_OWNER,
'field' => zen_draw_input_field('ecardon[cc_owner]', $order->billing['firstname'] . ' ' . $order->billing['lastname'])),
array('title' => MODULE_PAYMENT_ECARDON_TEXT_CREDIT_CARD_NUMBER,
'field' => zen_draw_input_field('ecardon[cardNum]')),
array('title' => MODULE_PAYMENT_ECARDON_TEXT_CREDIT_CVV,
'field'=> zen_draw_input_field('ecardon[cvd]')),
array('title' => MODULE_PAYMENT_ECARDON_TEXT_CREDIT_CARD_EXPIRES,
'field' => zen_draw_pull_down_menu('ecardon[month]', $expires_month) . ' ' . zen_draw_pull_down_menu('ecardon[year]', $expires_year))));
return $selection;
}
function pre_confirmation_check() {
global $HTTP_POST_VARS, $order;
include(DIR_WS_CLASSES . 'cc_validation.php');
$cc_validation = new cc_validation();
if(strlen($HTTP_POST_VARS['ecardon']['year']) >2){
$year = substr($HTTP_POST_VARS['ecardon']['year'], 2, 2);
}
else{
$year = $HTTP_POST_VARS['ecardon']['year'];
}
$result = $cc_validation->validate($HTTP_POST_VARS['ecardon']['cardNum'], $HTTP_POST_VARS['ecardon']['month'], $year );
$error = '';
switch ($result) {
case -1:
$error = sprintf(TEXT_CCVAL_ERROR_UNKNOWN_CARD, substr($cc_validation->cc_number, 0, 4));
break;
case -2:
case -3:
case -4:
$error = TEXT_CCVAL_ERROR_INVALID_DATE;
break;
case false:
$error = TEXT_CCVAL_ERROR_INVALID_NUMBER;
break;
}
if ( ($result == false) || ($result < 1) ) {
$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode($error) . '&ecardon_cc_owner=' . urlencode($HTTP_POST_VARS['ecardon']['cc_owner']) . '&ecardon_cc_expires_month=' . $HTTP_POST_VARS['ecardon']['month'] . '&ecardon_cc_expires_year=' . $HTTP_POST_VARS['ecardon']['year'];
zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
}
switch($cc_validation->cc_type){
case 'Visa':
$this->cc_card_type = 'VISA';
break;
case 'Master Card':
$this->cc_card_type = 'MASTER';
break;
case 'American Express':
$this->cc_card_type = 'AMEX';
break;
case 'Diners Club':
$this->cc_card_type = 'DINERS';
break;
case 'Discover':
$this->cc_card_type = 'DISCOVER';
break;
case 'Switch/Solo':
$this->cc_card_type = 'SOLO';
break;
}
$this->cc_card_number = $cc_validation->cc_number;
$this->cc_expiry_month = $cc_validation->cc_expiry_month;
$this->cc_expiry_year = $cc_validation->cc_expiry_year;
$this->cc_owner = $HTTP_POST_VARS['ecardon']['cc_owner'];
$this->cc_cvd = $HTTP_POST_VARS['ecardon']['cvd'];
}
function confirmation() {
global $HTTP_POST_VARS;
$confirmation = array('title' => $this->title . ': ' . $this->cc_card_type,
'fields' => array(
array('title' => MODULE_PAYMENT_ECARDON_TEXT_CREDIT_CARD_OWNER,
'field' => $this->cc_owner),
array('title' => MODULE_PAYMENT_ECARDON_TEXT_CREDIT_CARD_NUMBER,
'field' => substr($this->cc_card_number, 0, 4) . str_repeat('X', (strlen($this->cc_card_number) - 8)) . substr($this->cc_card_number, -4)),
array('title' => MODULE_PAYMENT_ECARDON_TEXT_CREDIT_CVV,
'field' => $this->cc_cvd),
array('title' => MODULE_PAYMENT_ECARDON_TEXT_CREDIT_CARD_EXPIRES,
'field' => $this->cc_expiry_month . " / " . $this->cc_expiry_year)
)
);
//MODULE_PAYMENT_ECARDON_TEXT_CREDIT_CVV CVD Number
return $confirmation;
}
function process_button() {
global $HTTP_SERVER_VARS, $HTTP_POST_VARS, $order, $customer_id, $currency;
$currency="USD";
$bstate = zen_get_zone_code($order->billing['country']['id'], $order->billing['zone_id'],0);
$sstate = zen_get_zone_code($order->delivery['country']['id'], $order->delivery['zone_id'],0);
$process_button_string =
zen_draw_hidden_field('ecardon[merchantRefNum]', $customer_id . '-' . date('Ymdhis')) .
zen_draw_hidden_field('ecardon[cardNum]', $this->cc_card_number) .
zen_draw_hidden_field('ecardon[card_owner]', $this->cc_owner) .
zen_draw_hidden_field('ecardon[cardType]', $this->cc_card_type) .
zen_draw_hidden_field('ecardon[cvd]', $this->cc_cvd) .
zen_draw_hidden_field('ecardon[month]', $this->cc_expiry_month) .
zen_draw_hidden_field('ecardon[year]', $this->cc_expiry_year) .
zen_draw_hidden_field('ecardon[amount]', $order->info['total']) .
zen_draw_hidden_field('ecardon[currency]', $currency) .
// billing info:
zen_draw_hidden_field('ecardon[billingfirstName]', $order->billing['firstname']) .
zen_draw_hidden_field('ecardon[billinglastName]', $order->billing['lastname']) .
zen_draw_hidden_field('ecardon[billingstreet]', $order->billing['street_address']) .
zen_draw_hidden_field('ecardon[billingstreet2]', $order->billing['street_address2']) .
zen_draw_hidden_field('ecardon[billingcity]', $order->billing['city']) .
zen_draw_hidden_field('ecardon[billingregion]', $bstate) .
zen_draw_hidden_field('ecardon[billingzip]', $order->billing['postcode']) .
zen_draw_hidden_field('ecardon[billingcountry]', $order->billing['country']['iso_code_2']) .
zen_draw_hidden_field('ecardon[billingphone]', $order->customer['telephone']) .
zen_draw_hidden_field('ecardon[billingemail]', $order->customer['email_address']) .
//shipping info:
zen_draw_hidden_field('ecardon[shippingfirstName]', $order->delivery['firstname']) .
zen_draw_hidden_field('ecardon[shippinglastName]', $order->delivery['lastname']) .
zen_draw_hidden_field('ecardon[shippingstreet]', $order->delivery['street_address']) .
zen_draw_hidden_field('ecardon[shippingstreet2]', $order->delivery['street_address2']) .
zen_draw_hidden_field('ecardon[shippingcity]', $order->delivery['city']) .
zen_draw_hidden_field('ecardon[shippingregion]', $sstate) .
zen_draw_hidden_field('ecardon[shippingzip]', $order->delivery['postcode']) .
zen_draw_hidden_field('ecardon[shippingcountry]', $order->delivery['country']['iso_code_2']);
$process_button_string .= zen_draw_hidden_field(zen_session_name(), zen_session_id());
return $process_button_string;
}
function before_process(){
global $HTTP_POST_VARS, $currency, $order;
$ecardon_input = (array)$HTTP_POST_VARS['ecardon'];
$xml_bits = $this->_build_request($ecardon_input);
$ecardon_output = $this->_post_request($xml_bits);
if ($ecardon_output) {
if($ecardon_output == 'ACK'){
if(strlen($order->info['comments']))
$order->info['comments'] .= "\n\n";
$order->info['comments'] .= "Output: ".$ecardon_output."\n\n";
$order->info['optimalpayments_cc_number'] = $HTTP_POST_VARS['ecardon']['cardNum'];
$order->info['optimalpayments_cvd_number'] = $HTTP_POST_VARS['ecardon']['cvd'];
$order->info['optimalpayments_cc_expires_month'] = $HTTP_POST_VARS['ecardon']['month'];
$order->info['optimalpayments_cc_expires_year'] = $HTTP_POST_VARS['ecardon']['year'];
$order->info['op_id'] = $HTTP_POST_VARS['ecardon']['merchantRefNum'];
//ENTER OW WHEN COMPLETED TESTING
return;
}
else{
zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode($ecardon_output), 'SSL', true, false));
}
}
else{
zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(print_r($ecardon_output,true)).urlencode(MODULE_PAYMENT_ECARDON_TEXT_ERROR_MESSAGE) , 'SSL', true, false));
}
}
/*
using HTTP/POST send message to ctpe server
*/
function _post_request($xml_bits) {
$cpt = curl_init();
$xmlpost="load=".urlencode($xml_bits);
$url = "https://".$this->host;
curl_setopt($cpt, CURLOPT_URL, $url);
curl_setopt($cpt, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($cpt, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($cpt, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cpt, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($cpt, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($cpt, CURLOPT_POST, 1);
curl_setopt($cpt, CURLOPT_POSTFIELDS, $xmlpost);
$this->resultURL = curl_exec($cpt);
$this->error = curl_error($cpt);
$this->info = curl_getinfo($cpt);
curl_close($cpt);
if ($this->resultURL){
return $this->parserResult($this->resultURL);
}
else{
return false;
}
}
function _build_request($fpinput) {
$strXML = "<?xml version=\"1.0\" ?>";
// set account and user information.
$strXML .= "<Request version=\"$this->request_version\">";
$strXML .= "<Header>";
$strXML .= "<Security sender=\"$this->sender\" token=\"token\" />";
$strXML .= "</Header>";
$strXML .= "<Transaction response=\"$this->transaction_response\" channel=\"$this->channel\" mode=\"$this->transaction_mode\">";
$strXML .= "<User pwd=\"$this->userpwd\" login=\"$this->userid\" />";
$strXML .= "<Identification>";
$strXML .= "<TransactionID>".$fpinput['merchantRefNum']."</TransactionID>";
$strXML .= "</Identification>";
// set account information
$strXML .= "<Account>";
$strXML .= "<Holder>".$fpinput['card_owner']."</Holder>";
$strXML .= "<Number>".$fpinput['cardNum']."</Number>";
$strXML .= "<Brand>".$fpinput['cardType']."</Brand>";
$strXML .= "<Expiry month=\"".$fpinput['month']."\" year=\"".$fpinput['year']."\"></Expiry>";
$strXML .= "<Verification>".$fpinput['cvd']."</Verification>";
$strXML .= "</Account>";
// set payment information
$strXML .= "<Payment code=\"$this->payment_code\">";
$strXML .= "<Presentation>";
$strXML .= "<Amount>".$fpinput['amount']."</Amount>";
$strXML .= "<Usage>".$fpinput['merchantRefNum']."</Usage>";
$strXML .= "<Currency>".$fpinput['currency']."</Currency>";
$strXML .= "</Presentation>";
$strXML .= "</Payment>";
// set customer information
$strXML .= "<Customer>";
$strXML .= "<Contact>";
$strXML .= "<Email>".$fpinput['billingemail']."</Email>";
$strXML .= "<Mobile></Mobile>";
$strXML .= "<Ip>".$_SERVER["REMOTE_ADDR"]."</Ip>";
$strXML .= "<Phone>".$fpinput['billingphone']."</Phone>";
$strXML .= "</Contact>";
$strXML .= "<Address>";
$strXML .= "<Street>".$fpinput['billingstreet']."</Street>";
$strXML .= "<Zip>".$fpinput['billingzip']."</Zip>";
$strXML .= "<City>".$fpinput['billingcity']."</City>";
$strXML .= "<State>".$fpinput['billingregion']."</State>";
$strXML .= "<Country>".$fpinput['billingcountry']."</Country>";
$strXML .= "</Address>";
$strXML .= "<Name>";
$strXML .= "<Salutation></Salutation>";
$strXML .= "<Title></Title>";
$strXML .= "<Given>".$fpinput['billingfirstName']."</Given>";
$strXML .= "<Family>".$fpinput['billinglastName']."</Family>";
$strXML .= "<Company>".$fpinput['company']."</Company>";
$strXML .= "</Name>";
$strXML .= "</Customer>";
$strXML .= "</Transaction>";
$strXML .= "</Request>";
return $strXML;
echo $strXML;
exit;
}
/*
Parser XML message returned by Ecardon server.
*/
function parserResult($resultURL)
{
$resultXML=urldecode($resultURL);
$processingResult = substr($resultXML, strpos($resultXML, "<Result>")+strlen("<Result>"), strpos($resultXML, "</Result>")-strlen("<Result>")-strpos($resultXML, "<Result>"));
if ($processingResult == "ACK")
{
return $processingResult;
}
else
{
$errorCode = substr($resultXML, strpos($resultXML, "<Return code=")+strlen("<Return code=\"000.000.000\">"), strpos($resultXML, "</Return>")-strlen("<Return code=\"000.000.000\">")-strpos($resultXML, "<Return code"));
return $errorCode;
}
/*
uncomment the following line for debug output (whole XML)
*/
print "<br>$resultXML";
}
function after_process() {
return false;
}
function get_error() {
global $HTTP_GET_VARS;
$error = array('title' => MODULE_PAYMENT_ECARDON_TEXT_ERROR,
'error' => stripslashes(urldecode($HTTP_GET_VARS['error'])));
return $error;
}
function check() {
global $db;
if (!isset($this->_check)) {
$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_ECARDON_STATUS'");
$this->_check = $check_query->RecordCount();
}
return $this->_check;
}
function install() {
global $db;
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Ecardon Module', 'MODULE_PAYMENT_ECARDON_STATUS', 'True', 'Do you want to accept Ecardon payments?', '6', '1', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Module Mode', 'MODULE_PAYMENT_ECARDON_GATEWAY_SERVER', 'Test', 'Test or Live Mode?', '6', '2', 'zen_cfg_select_option(array(\'Test\', \'Live\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Set Order Status', 'MODULE_PAYMENT_ECARDON_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '3', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_ECARDON_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '4', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Payment Zone', 'MODULE_PAYMENT_ECARDON_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '5', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sender', 'MODULE_PAYMENT_ECARDON_SENDER', '8a90818a1c767c4e011c8eca17bd05b0', 'The Sender Value', '6', '6', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Channel', 'MODULE_PAYMENT_ECARDON_CHANNEL', '8a90818a1c767c4e011c8eca17be05b5', 'Channel Value', '6', '7', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('User ID', 'MODULE_PAYMENT_ECARDON_USER_ID', '8a90818a1c767c4e011c8eca17be05b4', 'User ID', '6', '8', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Password', 'MODULE_PAYMENT_ECARDON_PASSWORD', 'demo', 'Password', '6', '9', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('API Response', 'MODULE_PAYMENT_ECARDON_RESPONSE', 'SYNC', 'Response Type', '6', '10', now())");
}
function remove() {
global $db;
$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
}
function keys() {
return array('MODULE_PAYMENT_ECARDON_STATUS', 'MODULE_PAYMENT_ECARDON_GATEWAY_SERVER','MODULE_PAYMENT_ECARDON_ORDER_STATUS_ID', 'MODULE_PAYMENT_ECARDON_SORT_ORDER','MODULE_PAYMENT_ECARDON_ZONE','MODULE_PAYMENT_ECARDON_SENDER','MODULE_PAYMENT_ECARDON_CHANNEL','MODULE_PAYMENT_ECARDON_USER_ID','MODULE_PAYMENT_ECARDON_PASSWORD','MODULE_PAYMENT_ECARDON_RESPONSE');
}
}
?>
Bookmarks