code = 'authorizenet';
if (IS_ADMIN_FLAG === true) {
$this->title = MODULE_PAYMENT_AUTHORIZENET_TEXT_ADMIN_TITLE; // Payment module title in Admin
if (MODULE_PAYMENT_AUTHORIZENET_STATUS == 'True' && (MODULE_PAYMENT_AUTHORIZENET_LOGIN == 'testing' || MODULE_PAYMENT_AUTHORIZENET_TXNKEY == 'Test' || MODULE_PAYMENT_AUTHORIZENET_MD5HASH == '*Set A Hash Value at AuthNet Admin*')) {
$this->title .= ' (Not Configured)';
} elseif (MODULE_PAYMENT_AUTHORIZENET_TESTMODE == 'Test') {
$this->title .= ' (in Testing mode)';
}
} else {
$this->title = MODULE_PAYMENT_AUTHORIZENET_TEXT_CATALOG_TITLE; // Payment module title in Catalog
}
$this->description = MODULE_PAYMENT_AUTHORIZENET_TEXT_DESCRIPTION;
$this->enabled = ((MODULE_PAYMENT_AUTHORIZENET_STATUS == 'True') ? true : false);
$this->sort_order = MODULE_PAYMENT_AUTHORIZENET_SORT_ORDER;
if ((int)MODULE_PAYMENT_AUTHORIZENET_ORDER_STATUS_ID > 0) {
$this->order_status = MODULE_PAYMENT_AUTHORIZENET_ORDER_STATUS_ID;
}
if (is_object($order)) $this->update_status();
$this->form_action_url = 'https://secure.authorize.net/gateway/transact.dll';
if (AUTHORIZENET_DEVELOPER_MODE == 'on') $this->form_action_url = 'https://test.authorize.net/gateway/transact.dll';
if (AUTHORIZENET_DEVELOPER_MODE == 'echo' || MODULE_PAYMENT_AUTHORIZENET_DEBUGGING == 'echo') $this->form_action_url = 'https://developer.authorize.net/param_dump.asp';
if (AUTHORIZENET_DEVELOPER_MODE == 'certify') $this->form_action_url = 'https://certification.authorize.net/gateway/transact.dll';
$this->gateway_mode = MODULE_PAYMENT_AUTHORIZENET_GATEWAY_MODE;
$this->_logDir = defined('DIR_FS_LOGS') ? DIR_FS_LOGS : DIR_FS_SQL_CACHE;
// verify table structure
if (IS_ADMIN_FLAG === true) $this->tableCheckup();
}
// Authorize.net utility functions
// DISCLAIMER:
// This code 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.
// Main Interfaces:
//
// function InsertFP ($loginid, $txnkey, $amount, $sequence) - Insert HTML form elements required for SIM
// function CalculateFP ($loginid, $txnkey, $amount, $sequence, $tstamp) - Returns Fingerprint.
// compute HMAC-MD5
// Uses PHP mhash extension. Be sure to enable the extension
// function hmac ($key, $data) {
// return (bin2hex (mhash(MHASH_MD5, $data, $key)));
//}
// Thanks is lance from http://www.php.net/manual/en/function.mhash.php
//lance_rushing at hot* spamfree *mail dot com
//27-Nov-2002 09:36
//
/**
* compute HMAC-MD5
*
* @param string $key
* @param string $data
* @return string
*/
function hmac ($key, $data)
{
// RFC 2104 HMAC implementation for php.
// Creates an md5 HMAC.
// Eliminates the need to install mhash to compute a HMAC
// Hacked by Lance Rushing
$b = 64; // byte length for md5
if (strlen($key) > $b) {
$key = pack("H*",md5($key));
}
$key = str_pad($key, $b, chr(0x00));
$ipad = str_pad('', $b, chr(0x36));
$opad = str_pad('', $b, chr(0x5c));
$k_ipad = $key ^ $ipad ;
$k_opad = $key ^ $opad;
return md5($k_opad . pack("H*",md5($k_ipad . $data)));
}
// end code from lance (resume authorize.net code)
/**
* Inserts the hidden variables in the HTML FORM required for SIM
* Invokes hmac function to calculate fingerprint.
*
* @param string $loginid
* @param string $txnkey
* @param float $amount
* @param string $sequence
* @param float $currency
* @return string
*/
function InsertFP ($loginid, $txnkey, $amount, $sequence, $currency = "") {
$tstamp = time ();
$fingerprint = $this->hmac ($txnkey, $loginid . "^" . $sequence . "^" . $tstamp . "^" . $amount . "^" . $currency);
$security_array = array('x_fp_sequence' => $sequence,
'x_fp_timestamp' => $tstamp,
'x_fp_hash' => $fingerprint);
return $security_array;
}
// end authorize.net-provided code
// class methods
/**
* Calculate zone matches and flag settings to determine whether this module should display to customers or not
*/
function update_status() {
global $order, $db;
if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_AUTHORIZENET_ZONE > 0) ) {
$check_flag = false;
$check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_AUTHORIZENET_ZONE . "' and zone_country_id = '" . $order->billing['country']['id'] . "' order by zone_id");
while (!$check->EOF) {
if ($check->fields['zone_id'] < 1) {
$check_flag = true;
break;
} elseif ($check->fields['zone_id'] == $order->billing['zone_id']) {
$check_flag = true;
break;
}
$check->MoveNext();
}
if ($check_flag == false) {
$this->enabled = false;
}
}
}
/**
* JS validation which does error-checking of data-entry if this module is selected for use
* (Number, Owner Lengths)
*
* @return string
*/
function javascript_validation() {
if ($this->gateway_mode == 'offsite') return '';
$js = ' if (payment_value == "' . $this->code . '") {' . "\n" .
' var cc_owner = document.checkout_payment.authorizenet_cc_owner.value;' . "\n" .
' var cc_number = document.checkout_payment.authorizenet_cc_number.value;' . "\n";
if (MODULE_PAYMENT_AUTHORIZENET_USE_CVV == 'True') {
$js .= ' var cc_cvv = document.checkout_payment.authorizenet_cc_cvv.value;' . "\n";
}
$js .= ' if (cc_owner == "" || cc_owner.length < ' . CC_OWNER_MIN_LENGTH . ') {' . "\n" .
' error_message = error_message + "' . MODULE_PAYMENT_AUTHORIZENET_TEXT_JS_CC_OWNER . '";' . "\n" .
' error = 1;' . "\n" .
' }' . "\n" .
' if (cc_number == "" || cc_number.length < ' . CC_NUMBER_MIN_LENGTH . ') {' . "\n" .
' error_message = error_message + "' . MODULE_PAYMENT_AUTHORIZENET_TEXT_JS_CC_NUMBER . '";' . "\n" .
' error = 1;' . "\n" .
' }' . "\n";
if (MODULE_PAYMENT_AUTHORIZENET_USE_CVV == 'True') {
$js .= ' if (cc_cvv == "" || cc_cvv.length < "3" || cc_cvv.length > "4") {' . "\n".
' error_message = error_message + "' . MODULE_PAYMENT_AUTHORIZENET_TEXT_JS_CC_CVV . '";' . "\n" .
' error = 1;' . "\n" .
' }' . "\n" ;
}
$js .= ' }' . "\n";
return $js;
}
/**
* Display Credit Card Information Submission Fields on the Checkout Payment Page
*
* @return array
*/
function selection() {
global $order;
for ($i=1; $i<13; $i++) {
$expires_month[] = array('id' => sprintf('%02d', $i), 'text' => strftime('%B - (%m)',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)));
}
$onFocus = ' onfocus="methodSelect(\'pmt-' . $this->code . '\')"';
if ($this->gateway_mode == 'offsite') {
$selection = array('id' => $this->code,
'module' => $this->title);
} else {
$selection = array('id' => $this->code,
'module' => $this->title,
'fields' => array(array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_OWNER,
'field' => zen_draw_input_field('authorizenet_cc_owner', $order->billing['firstname'] . ' ' . $order->billing['lastname'], 'id="'.$this->code.'-cc-owner"' . $onFocus . ' autocomplete="off"'),
'tag' => $this->code.'-cc-owner'),
array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_NUMBER,
'field' => zen_draw_input_field('authorizenet_cc_number', '', 'id="'.$this->code.'-cc-number"' . $onFocus . ' autocomplete="off"'),
'tag' => $this->code.'-cc-number'),
array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_EXPIRES,
'field' => zen_draw_pull_down_menu('authorizenet_cc_expires_month', $expires_month, strftime('%m'), 'id="'.$this->code.'-cc-expires-month"' . $onFocus) . ' ' . zen_draw_pull_down_menu('authorizenet_cc_expires_year', $expires_year, '', 'id="'.$this->code.'-cc-expires-year"' . $onFocus),
'tag' => $this->code.'-cc-expires-month')));
if (MODULE_PAYMENT_AUTHORIZENET_USE_CVV == 'True') {
$selection['fields'][] = array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CVV,
'field' => zen_draw_input_field('authorizenet_cc_cvv', '', 'size="4" maxlength="4"' . ' id="'.$this->code.'-cc-cvv"' . $onFocus . ' autocomplete="off"') . ' ' . '' . MODULE_PAYMENT_AUTHORIZENET_TEXT_POPUP_CVV_LINK . '',
'tag' => $this->code.'-cc-cvv');
}
}
return $selection;
}
/**
* Evaluates the Credit Card Type for acceptance and the validity of the Credit Card Number & Expiration Date
*
*/
function pre_confirmation_check() {
global $messageStack;
if (isset($_POST['authorizenet_cc_number'])) {
include(DIR_WS_CLASSES . 'cc_validation.php');
$cc_validation = new cc_validation();
$result = $cc_validation->validate($_POST['authorizenet_cc_number'], $_POST['authorizenet_cc_expires_month'], $_POST['authorizenet_cc_expires_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) ) {
$messageStack->add_session('checkout_payment', $error . '', 'error');
zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL', true, false));
}
$this->cc_card_type = $cc_validation->cc_type;
$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;
}
}
/**
* Display Credit Card Information on the Checkout Confirmation Page
*
* @return array
*/
function confirmation() {
if (isset($_POST['authorizenet_cc_number'])) {
$confirmation = array('title' => $this->title . ': ' . $this->cc_card_type,
'fields' => array(array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_OWNER,
'field' => $_POST['authorizenet_cc_owner']),
array('title' => MODULE_PAYMENT_AUTHORIZENET_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_AUTHORIZENET_TEXT_CREDIT_CARD_EXPIRES,
'field' => strftime('%B, %Y', mktime(0,0,0,$_POST['authorizenet_cc_expires_month'], 1, '20' . $_POST['authorizenet_cc_expires_year'])))));
} else {
$confirmation = array(); //array('title' => $this->title);
}
return $confirmation;
}
/**
* Build the data and actions to process when the "Submit" button is pressed on the order-confirmation screen.
* This sends the data to the payment gateway for processing.
* (These are hidden fields on the checkout confirmation page)
*
* @return string
*/
function process_button() {
global $order;
$sequence = rand(1, 1000);
$submit_data_core = array(
'x_login' => MODULE_PAYMENT_AUTHORIZENET_LOGIN,
'x_amount' => number_format($order->info['total'], 2),
//'x_currency_code' => $_SESSION['currency'],
'x_version' => '3.1',
'x_method' => ((MODULE_PAYMENT_AUTHORIZENET_METHOD == 'Credit Card') ? 'CC' : 'ECHECK'),
'x_type' => MODULE_PAYMENT_AUTHORIZENET_AUTHORIZATION_TYPE == 'Authorize' ? 'AUTH_ONLY': 'AUTH_CAPTURE',
'x_cust_ID' => $_SESSION['customer_id'],
'x_email_customer' => ((MODULE_PAYMENT_AUTHORIZENET_EMAIL_CUSTOMER == 'True') ? 'TRUE': 'FALSE'),
'x_company' => $order->billing['company'],
'x_first_name' => $order->billing['firstname'],
'x_last_name' => $order->billing['lastname'],
'x_address' => $order->billing['street_address'],
'x_city' => $order->billing['city'],
'x_state' => $order->billing['state'],
'x_zip' => $order->billing['postcode'],
'x_country' => $order->billing['country']['title'],
'x_phone' => $order->customer['telephone'],
'x_fax' => $order->customer['fax'],
'x_email' => $order->customer['email_address'],
'x_ship_to_company' => $order->delivery['company'],
'x_ship_to_first_name' => $order->delivery['firstname'],
'x_ship_to_last_name' => $order->delivery['lastname'],
'x_ship_to_address' => $order->delivery['street_address'],
'x_ship_to_city' => $order->delivery['city'],
'x_ship_to_state' => $order->delivery['state'],
'x_ship_to_zip' => $order->delivery['postcode'],
'x_ship_to_country' => $order->delivery['country']['title'],
'x_Customer_IP' => zen_get_ip_address(),
'x_relay_response' => 'TRUE',
'x_relay_URL' => zen_href_link(FILENAME_CHECKOUT_PROCESS, 'action=confirm', 'SSL', true, false),
'x_invoice_num' => '',
'x_duplicate_window' => '120',
'x_allow_partial_Auth' => 'FALSE', // unable to accept partial authorizations at this time
'x_description' => 'Website Purchase from ' . str_replace('"',"'", STORE_NAME),
);
// force conversion to USD
if ($_SESSION['currency'] != 'USD') {
global $currencies;
$submit_data_core['x_amount'] = number_format($order->info['total'] * $currencies->get_value('USD'), 2);
$submit_data_core['x_currency_code'] = 'USD';
unset($submit_data_core['x_tax'], $submit_data_core['x_freight']);
}
$submit_data_security = $this->InsertFP(MODULE_PAYMENT_AUTHORIZENET_LOGIN, MODULE_PAYMENT_AUTHORIZENET_TXNKEY, number_format($order->info['total'], 2), $sequence);
$submit_data_offline = array(
'x_show_form' => 'PAYMENT_FORM',
'x_receipt_link_method' => 'POST',
'x_receipt_link_text' => 'Click here to complete your order.',
'x_receipt_link_url' => zen_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL', false)
);
//The following can (and SHOULD) be set in the authnet account admin area instead of here
$submit_data_extras = array(
// 'x_header_email_receipt' => '',
// 'x_footer_email_receipt' => '',
// 'x_header_html_payment_form' => '',
// 'x_footer_html_payment_form' => '',
// 'x_header_html_receipt' => '',
// 'x_footer_html_receipt' => '',
// 'x_logo_url' => '',
// 'x_background_url' => '',
// 'x_color_link' => '',
// 'x_color_background' => '',
// 'x_color_text' => ''
);
$submit_data_onsite = array(
'x_card_num' => $this->cc_card_number,
'x_exp_date' => $this->cc_expiry_month . substr($this->cc_expiry_year, -2));
if (MODULE_PAYMENT_AUTHORIZENET_USE_CVV == 'True') {
if ($this->gateway_mode == 'onsite') $submit_data_onsite['x_card_code'] = $_POST['authorizenet_cc_cvv'];
}
if ($this->gateway_mode == 'onsite') {
$submit_data = array_merge($submit_data_core, $submit_data_security, $submit_data_onsite);
} else {
$submit_data = array_merge($submit_data_core, $submit_data_security, $submit_data_offline, $submit_data_extras);
}
if (MODULE_PAYMENT_AUTHORIZENET_TESTMODE == 'Test') $submit_data['x_Test_Request'] = 'TRUE';
$submit_data[zen_session_name()] = zen_session_id();
$process_button_string = "\n";
foreach($submit_data as $key => $value) {
$process_button_string .= zen_draw_hidden_field($key, $value) . "\n";
}
// prepare a copy of submitted data for error-reporting purposes
$this->reportable_submit_data = $submit_data;
$this->reportable_submit_data['x_login'] = '*******';
if (isset($this->reportable_submit_data['x_tran_key'])) $this->reportable_submit_data['x_tran_key'] = '*******';
if (isset($this->reportable_submit_data['x_card_num'])) $this->reportable_submit_data['x_card_num'] = str_repeat('X', strlen($this->reportable_submit_data['x_card_num'] - 4)) . substr($this->reportable_submit_data['x_card_num'], -4);
if (isset($this->reportable_submit_data['x_card_code'])) $this->reportable_submit_data['x_card_code'] = '*******';
$this->reportable_submit_data['url'] = $url;
$this->_debugActions($this->reportable_submit_data, 'Submit-Data', '', zen_session_id());
return $process_button_string;
}
/**
* Store the CC info to the order and process any results that come back from the payment gateway
*
*/
function before_process() {
global $messageStack;
$this->authorize = $_POST;
unset($this->authorize['btn_submit_x'], $this->authorize['btn_submit_y']);
$this->authorize['HashValidationValue'] = $this->calc_md5_response($this->authorize['x_trans_id'], $this->authorize['x_amount']);
$this->authorize['HashMatchStatus'] = ($this->authorize['x_MD5_Hash'] == $this->authorize['HashValidationValue']) ? 'PASS' : 'FAIL';
$this->_debugActions($this->authorize, 'Response-Data', '', zen_session_id());
// if in 'echo' mode, dump the returned data to the browser and stop execution
if (AUTHORIZENET_DEVELOPER_MODE == 'echo' || MODULE_PAYMENT_AUTHORIZENET_DEBUGGING == 'echo') {
echo 'Returned Response Codes:
' . print_r($_POST, true) . '