[ Index ] |
|
Code source de Zen Cart E-Commerce Shopping Cart 1.3.7.1 |
1 <?php 2 /** 3 * authorize.net SIM payment method class 4 * 5 * @package paymentMethod 6 * @copyright Copyright 2003-2006 Zen Cart Development Team 7 * @copyright Portions Copyright 2003 osCommerce 8 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0 9 * @version $Id: authorizenet.php 4960 2006-11-20 01:46:46Z drbyte $ 10 */ 11 /** 12 * Enter description here... 13 * 14 */ 15 class authorizenet extends base { 16 /** 17 * $code determines the internal 'code' name used to designate "this" payment module 18 * 19 * @var string 20 */ 21 var $code; 22 /** 23 * $title is the displayed name for this payment method 24 * 25 * @var string 26 */ 27 var $title; 28 /** 29 * $description is a soft name for this payment method 30 * 31 * @var string 32 */ 33 var $description; 34 /** 35 * $enabled determines whether this module shows or not... in catalog. 36 * 37 * @var boolean 38 */ 39 var $enabled; 40 /** 41 * @return authorizenet 42 */ 43 function authorizenet() { 44 global $order; 45 46 $this->code = 'authorizenet'; 47 if ($_GET['main_page'] != '') { 48 $this->title = MODULE_PAYMENT_AUTHORIZENET_TEXT_CATALOG_TITLE; // Payment module title in Catalog 49 } else { 50 $this->title = MODULE_PAYMENT_AUTHORIZENET_TEXT_ADMIN_TITLE; // Payment module title in Admin 51 } 52 $this->description = MODULE_PAYMENT_AUTHORIZENET_TEXT_DESCRIPTION; 53 $this->enabled = ((MODULE_PAYMENT_AUTHORIZENET_STATUS == 'True') ? true : false); 54 $this->sort_order = MODULE_PAYMENT_AUTHORIZENET_SORT_ORDER; 55 56 if ((int)MODULE_PAYMENT_AUTHORIZENET_ORDER_STATUS_ID > 0) { 57 $this->order_status = MODULE_PAYMENT_AUTHORIZENET_ORDER_STATUS_ID; 58 } 59 60 if (is_object($order)) $this->update_status(); 61 62 $this->form_action_url = 'https://secure.authorize.net/gateway/transact.dll'; 63 } 64 65 // Authorize.net utility functions 66 // DISCLAIMER: 67 // This code is distributed in the hope that it will be useful, but without any warranty; 68 // without even the implied warranty of merchantability or fitness for a particular purpose. 69 70 // Main Interfaces: 71 // 72 // function InsertFP ($loginid, $txnkey, $amount, $sequence) - Insert HTML form elements required for SIM 73 // function CalculateFP ($loginid, $txnkey, $amount, $sequence, $tstamp) - Returns Fingerprint. 74 75 // compute HMAC-MD5 76 // Uses PHP mhash extension. Pl sure to enable the extension 77 // function hmac ($key, $data) { 78 // return (bin2hex (mhash(MHASH_MD5, $data, $key))); 79 //} 80 81 // Thanks is lance from http://www.php.net/manual/en/function.mhash.php 82 //lance_rushing at hot* spamfree *mail dot com 83 //27-Nov-2002 09:36 84 // 85 //Want to Create a md5 HMAC, but don't have hmash installed? 86 // 87 //Use this: 88 /** 89 * compute HMAC-MD5 90 * 91 * @param string $key 92 * @param string $data 93 * @return string 94 */ 95 function hmac ($key, $data) 96 { 97 // RFC 2104 HMAC implementation for php. 98 // Creates an md5 HMAC. 99 // Eliminates the need to install mhash to compute a HMAC 100 // Hacked by Lance Rushing 101 102 $b = 64; // byte length for md5 103 if (strlen($key) > $b) { 104 $key = pack("H*",md5($key)); 105 } 106 $key = str_pad($key, $b, chr(0x00)); 107 $ipad = str_pad('', $b, chr(0x36)); 108 $opad = str_pad('', $b, chr(0x5c)); 109 $k_ipad = $key ^ $ipad ; 110 $k_opad = $key ^ $opad; 111 112 return md5($k_opad . pack("H*",md5($k_ipad . $data))); 113 } 114 // end code from lance (resume authorize.net code) 115 116 // Calculate and return fingerprint 117 // Use when you need control on the HTML output 118 function CalculateFP ($loginid, $txnkey, $amount, $sequence, $tstamp, $currency = "") { 119 return ($this->hmac ($txnkey, $loginid . "^" . $sequence . "^" . $tstamp . "^" . $amount . "^" . $currency)); 120 } 121 /** 122 * Inserts the hidden variables in the HTML FORM required for SIM 123 * Invokes hmac function to calculate fingerprint. 124 * 125 * @param string $loginid 126 * @param string $txnkey 127 * @param float $amount 128 * @param string $sequence 129 * @param float $currency 130 * @return string 131 */ 132 133 function InsertFP ($loginid, $txnkey, $amount, $sequence, $currency = "") { 134 $tstamp = time (); 135 $fingerprint = $this->hmac ($txnkey, $loginid . "^" . $sequence . "^" . $tstamp . "^" . $amount . "^" . $currency); 136 137 $str = zen_draw_hidden_field('x_fp_sequence', $sequence) . 138 zen_draw_hidden_field('x_fp_timestamp', $tstamp) . 139 zen_draw_hidden_field('x_fp_hash', $fingerprint); 140 141 return $str; 142 } 143 // end authorize.net-provided code 144 145 // class methods 146 /** 147 * calculate zone matches and flag settings to determine whether this module should display to customers or not 148 * 149 */ 150 function update_status() { 151 global $order, $db; 152 153 if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_AUTHORIZENET_ZONE > 0) ) { 154 $check_flag = false; 155 $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"); 156 while (!$check->EOF) { 157 if ($check->fields['zone_id'] < 1) { 158 $check_flag = true; 159 break; 160 } elseif ($check->fields['zone_id'] == $order->billing['zone_id']) { 161 $check_flag = true; 162 break; 163 } 164 $check->MoveNext(); 165 } 166 167 if ($check_flag == false) { 168 $this->enabled = false; 169 } 170 } 171 } 172 /** 173 * JS validation which does error-checking of data-entry if this module is selected for use 174 * (Number, Owner, and CVV Lengths) 175 * 176 * @return string 177 */ 178 function javascript_validation() { 179 $js = ' if (payment_value == "' . $this->code . '") {' . "\n" . 180 ' var cc_owner = document.checkout_payment.authorizenet_cc_owner.value;' . "\n" . 181 ' var cc_number = document.checkout_payment.authorizenet_cc_number.value;' . "\n" . 182 ' if (cc_owner == "" || cc_owner.length < ' . CC_OWNER_MIN_LENGTH . ') {' . "\n" . 183 ' error_message = error_message + "' . MODULE_PAYMENT_AUTHORIZENET_TEXT_JS_CC_OWNER . '";' . "\n" . 184 ' error = 1;' . "\n" . 185 ' }' . "\n" . 186 ' if (cc_number == "" || cc_number.length < ' . CC_NUMBER_MIN_LENGTH . ') {' . "\n" . 187 ' error_message = error_message + "' . MODULE_PAYMENT_AUTHORIZENET_TEXT_JS_CC_NUMBER . '";' . "\n" . 188 ' error = 1;' . "\n" . 189 ' }' . "\n" . 190 ' }' . "\n"; 191 192 return $js; 193 } 194 /** 195 * Display Credit Card Information Submission Fields on the Checkout Payment Page 196 * 197 * @return array 198 */ 199 function selection() { 200 global $order; 201 202 for ($i=1; $i<13; $i++) { 203 $expires_month[] = array('id' => sprintf('%02d', $i), 'text' => strftime('%B',mktime(0,0,0,$i,1,2000))); 204 } 205 206 $today = getdate(); 207 for ($i=$today['year']; $i < $today['year']+10; $i++) { 208 $expires_year[] = array('id' => strftime('%y',mktime(0,0,0,1,1,$i)), 'text' => strftime('%Y',mktime(0,0,0,1,1,$i))); 209 } 210 211 $onFocus = ' onfocus="methodSelect(\'pmt-' . $this->code . '\')"'; 212 213 $selection = array('id' => $this->code, 214 'module' => $this->title, 215 'fields' => array(array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_OWNER, 216 'field' => zen_draw_input_field('authorizenet_cc_owner', $order->billing['firstname'] . ' ' . $order->billing['lastname'], 'id="'.$this->code.'-cc-owner"' . $onFocus), 217 'tag' => $this->code.'-cc-owner'), 218 array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_NUMBER, 219 'field' => zen_draw_input_field('authorizenet_cc_number', '', 'id="'.$this->code.'-cc-number"' . $onFocus), 220 'tag' => $this->code.'-cc-number'), 221 array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_EXPIRES, 222 'field' => zen_draw_pull_down_menu('authorizenet_cc_expires_month', $expires_month, '', '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), 223 'tag' => $this->code.'-cc-expires-month') 224 )); 225 226 return $selection; 227 } 228 /** 229 * Evaluates the Credit Card Type for acceptance and the validity of the Credit Card Number & Expiration Date 230 * 231 */ 232 function pre_confirmation_check() { 233 global $_POST, $messageStack; 234 235 include(DIR_WS_CLASSES . 'cc_validation.php'); 236 237 $cc_validation = new cc_validation(); 238 $result = $cc_validation->validate($_POST['authorizenet_cc_number'], $_POST['authorizenet_cc_expires_month'], $_POST['authorizenet_cc_expires_year']); 239 $error = ''; 240 switch ($result) { 241 case -1: 242 $error = sprintf(TEXT_CCVAL_ERROR_UNKNOWN_CARD, substr($cc_validation->cc_number, 0, 4)); 243 break; 244 case -2: 245 case -3: 246 case -4: 247 $error = TEXT_CCVAL_ERROR_INVALID_DATE; 248 break; 249 case false: 250 $error = TEXT_CCVAL_ERROR_INVALID_NUMBER; 251 break; 252 } 253 254 if ( ($result == false) || ($result < 1) ) { 255 $payment_error_return = 'payment_error=' . $this->code . 'authorizenet_cc_owner=' . urlencode($_POST['authorizenet_cc_owner']) . '&authorizenet_cc_expires_month=' . $_POST['authorizenet_cc_expires_month'] . '&authorizenet_cc_expires_year=' . $_POST['authorizenet_cc_expires_year']; 256 $messageStack->add_session('checkout_payment', $error . '<!-- ['.$this->code.'] -->', 'error'); 257 zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false)); 258 } 259 260 $this->cc_card_type = $cc_validation->cc_type; 261 $this->cc_card_number = $cc_validation->cc_number; 262 $this->cc_expiry_month = $cc_validation->cc_expiry_month; 263 $this->cc_expiry_year = $cc_validation->cc_expiry_year; 264 } 265 /** 266 * Display Credit Card Information on the Checkout Confirmation Page 267 * 268 * @return array 269 */ 270 function confirmation() { 271 global $_POST; 272 273 $confirmation = array('title' => $this->title . ': ' . $this->cc_card_type, 274 'fields' => array(array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_OWNER, 275 'field' => $_POST['authorizenet_cc_owner']), 276 array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_NUMBER, 277 'field' => substr($this->cc_card_number, 0, 4) . str_repeat('X', (strlen($this->cc_card_number) - 8)) . substr($this->cc_card_number, -4)), 278 array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_CREDIT_CARD_EXPIRES, 279 'field' => strftime('%B, %Y', mktime(0,0,0,$_POST['authorizenet_cc_expires_month'], 1, '20' . $_POST['authorizenet_cc_expires_year']))))); 280 281 return $confirmation; 282 } 283 /** 284 * Build the data and actions to process when the "Submit" button is pressed on the order-confirmation screen. 285 * This sends the data to the payment gateway for processing. 286 * (These are hidden fields on the checkout confirmation page) 287 * 288 * @return string 289 */ 290 function process_button() { 291 global $_SERVER, $order; 292 293 $sequence = rand(1, 1000); 294 $process_button_string = zen_draw_hidden_field('x_Login', MODULE_PAYMENT_AUTHORIZENET_LOGIN) . 295 zen_draw_hidden_field('x_Card_Num', $this->cc_card_number) . 296 zen_draw_hidden_field('x_Exp_Date', $this->cc_expiry_month . substr($this->cc_expiry_year, -2)) . 297 zen_draw_hidden_field('x_Amount', number_format($order->info['total'], 2)) . 298 // zen_draw_hidden_field('x_currency_code', $_SESSION['currency']) . 299 zen_draw_hidden_field('x_Relay_URL', zen_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL', false)) . 300 zen_draw_hidden_field('x_Method', ((MODULE_PAYMENT_AUTHORIZENET_METHOD == 'Credit Card') ? 'CC' : 'ECHECK')) . 301 zen_draw_hidden_field('x_Version', '3.0') . 302 zen_draw_hidden_field('x_Cust_ID', $_SESSION['customer_id']) . 303 zen_draw_hidden_field('x_Email_Customer', ((MODULE_PAYMENT_AUTHORIZENET_EMAIL_CUSTOMER == 'True') ? 'TRUE': 'FALSE')) . 304 zen_draw_hidden_field('x_first_name', $order->billing['firstname']) . 305 zen_draw_hidden_field('x_last_name', $order->billing['lastname']) . 306 zen_draw_hidden_field('x_address', $order->billing['street_address']) . 307 zen_draw_hidden_field('x_city', $order->billing['city']) . 308 zen_draw_hidden_field('x_state', $order->billing['state']) . 309 zen_draw_hidden_field('x_zip', $order->billing['postcode']) . 310 zen_draw_hidden_field('x_country', $order->billing['country']['title']) . 311 zen_draw_hidden_field('x_phone', $order->customer['telephone']) . 312 zen_draw_hidden_field('x_email', $order->customer['email_address']) . 313 zen_draw_hidden_field('x_ship_to_first_name', $order->delivery['firstname']) . 314 zen_draw_hidden_field('x_ship_to_last_name', $order->delivery['lastname']) . 315 zen_draw_hidden_field('x_ship_to_address', $order->delivery['street_address']) . 316 zen_draw_hidden_field('x_ship_to_city', $order->delivery['city']) . 317 zen_draw_hidden_field('x_ship_to_state', $order->delivery['state']) . 318 zen_draw_hidden_field('x_ship_to_zip', $order->delivery['postcode']) . 319 zen_draw_hidden_field('x_ship_to_country', $order->delivery['country']['title']) . 320 zen_draw_hidden_field('x_Customer_IP', $_SERVER['REMOTE_ADDR']) . 321 $this->InsertFP(MODULE_PAYMENT_AUTHORIZENET_LOGIN, MODULE_PAYMENT_AUTHORIZENET_TXNKEY, number_format($order->info['total'], 2), $sequence); 322 if (MODULE_PAYMENT_AUTHORIZENET_TESTMODE == 'Test') $process_button_string .= zen_draw_hidden_field('x_Test_Request', 'TRUE'); 323 324 $process_button_string .= zen_draw_hidden_field(zen_session_name(), zen_session_id()); 325 326 return $process_button_string; 327 } 328 /** 329 * Store the CC info to the order and process any results that come back from the payment gateway 330 * 331 */ 332 function before_process() { 333 global $_POST, $messageStack; 334 335 if ($_POST['x_response_code'] == '1') return; 336 if ($_POST['x_response_code'] == '2') { 337 $messageStack->add_session('checkout_payment', MODULE_PAYMENT_AUTHORIZENET_TEXT_DECLINED_MESSAGE, 'error'); 338 zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL', true, false)); 339 } 340 // Code 3 is an error - but anything else is an error too (IMHO) 341 $messageStack->add_session('checkout_payment', MODULE_PAYMENT_AUTHORIZENET_TEXT_ERROR_MESSAGE, 'error'); 342 zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL', true, false)); 343 } 344 /** 345 * Post-processing activities 346 * 347 * @return boolean 348 */ 349 function after_process() { 350 return false; 351 } 352 /** 353 * Used to display error message details 354 * 355 * @return array 356 */ 357 function get_error() { 358 global $_GET; 359 360 $error = array('title' => MODULE_PAYMENT_AUTHORIZENET_TEXT_ERROR, 361 'error' => stripslashes(urldecode($_GET['error']))); 362 363 return $error; 364 } 365 /** 366 * Check to see whether module is installed 367 * 368 * @return boolean 369 */ 370 function check() { 371 global $db; 372 if (!isset($this->_check)) { 373 $check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_AUTHORIZENET_STATUS'"); 374 $this->_check = $check_query->RecordCount(); 375 } 376 return $this->_check; 377 } 378 /** 379 * Install the payment module and its configuration settings 380 * 381 */ 382 function install() { 383 global $db; 384 $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 Authorize.net Module', 'MODULE_PAYMENT_AUTHORIZENET_STATUS', 'True', 'Do you want to accept Authorize.net payments?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())"); 385 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Login ID', 'MODULE_PAYMENT_AUTHORIZENET_LOGIN', 'testing', 'The API Login ID used for the Authorize.net service', '6', '0', now())"); 386 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Transaction Key', 'MODULE_PAYMENT_AUTHORIZENET_TXNKEY', 'Test', 'Transaction Key used for encrypting TP data', '6', '0', now())"); 387 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Transaction Mode', 'MODULE_PAYMENT_AUTHORIZENET_TESTMODE', 'Test', 'Transaction mode used for processing orders', '6', '0', 'zen_cfg_select_option(array(\'Test\', \'Production\'), ', now())"); 388 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Transaction Method', 'MODULE_PAYMENT_AUTHORIZENET_METHOD', 'Credit Card', 'Transaction method used for processing orders', '6', '0', 'zen_cfg_select_option(array(\'Credit Card\', \'eCheck\'), ', now())"); 389 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Customer Notifications', 'MODULE_PAYMENT_AUTHORIZENET_EMAIL_CUSTOMER', 'False', 'Should Authorize.Net email a receipt to the customer?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())"); 390 $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_AUTHORIZENET_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())"); 391 $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_AUTHORIZENET_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())"); 392 $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_AUTHORIZENET_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '0', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())"); 393 } 394 /** 395 * Remove the module and all its settings 396 * 397 */ 398 function remove() { 399 global $db; 400 $db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')"); 401 } 402 /** 403 * Internal list of configuration keys used for configuration of the module 404 * 405 * @return array 406 */ 407 function keys() { 408 return array('MODULE_PAYMENT_AUTHORIZENET_STATUS', 'MODULE_PAYMENT_AUTHORIZENET_LOGIN', 'MODULE_PAYMENT_AUTHORIZENET_TXNKEY', 'MODULE_PAYMENT_AUTHORIZENET_TESTMODE', 'MODULE_PAYMENT_AUTHORIZENET_METHOD', 'MODULE_PAYMENT_AUTHORIZENET_EMAIL_CUSTOMER', 'MODULE_PAYMENT_AUTHORIZENET_ZONE', 'MODULE_PAYMENT_AUTHORIZENET_ORDER_STATUS_ID', 'MODULE_PAYMENT_AUTHORIZENET_SORT_ORDER'); 409 } 410 } 411 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 16:45:43 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |