[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/includes/modules/payment/ -> authorizenet_cc_aim.php (source)

   1  <?php
   2  /*
   3    $Id: authorizenet_cc_aim.php 1803 2008-01-11 18:16:37Z user $
   4  
   5    osCMax Power E-Commerce
   6    http://oscdox.com
   7  
   8    Copyright 2008 osCMax
   9  
  10    Released under the GNU General Public License
  11  */
  12  
  13    class authorizenet_cc_aim {
  14      var $code, $title, $description, $enabled;
  15  
  16  // class constructor
  17      function authorizenet_cc_aim() {
  18        global $order;
  19  
  20        $this->signature = 'authorizenet|authorizenet_cc_aim|1.0|2.2';
  21  
  22        $this->code = 'authorizenet_cc_aim';
  23        $this->title = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TEXT_TITLE;
  24        $this->public_title = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TEXT_PUBLIC_TITLE;
  25        $this->description = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TEXT_DESCRIPTION;
  26        $this->sort_order = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_SORT_ORDER;
  27        $this->enabled = ((MODULE_PAYMENT_AUTHORIZENET_CC_AIM_STATUS == 'True') ? true : false);
  28  
  29        if ((int)MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ORDER_STATUS_ID > 0) {
  30          $this->order_status = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ORDER_STATUS_ID;
  31        }
  32  
  33        if (is_object($order)) $this->update_status();
  34      }
  35  
  36  // class methods
  37      function update_status() {
  38        global $order;
  39  
  40        if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ZONE > 0) ) {
  41          $check_flag = false;
  42          $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ZONE . "' and zone_country_id = '" . $order->billing['country']['id'] . "' order by zone_id");
  43          while ($check = tep_db_fetch_array($check_query)) {
  44            if ($check['zone_id'] < 1) {
  45              $check_flag = true;
  46              break;
  47            } elseif ($check['zone_id'] == $order->billing['zone_id']) {
  48              $check_flag = true;
  49              break;
  50            }
  51          }
  52  
  53          if ($check_flag == false) {
  54            $this->enabled = false;
  55          }
  56        }
  57      }
  58  
  59      function javascript_validation() {
  60        return false;
  61      }
  62  
  63      function selection() {
  64        return array('id' => $this->code,
  65                     'module' => $this->public_title);
  66      }
  67  
  68      function pre_confirmation_check() {
  69        return false;
  70      }
  71  
  72      function confirmation() {
  73        global $order;
  74  
  75        for ($i=1; $i<13; $i++) {
  76          $expires_month[] = array('id' => sprintf('%02d', $i), 'text' => strftime('%B',mktime(0,0,0,$i,1,2000)));
  77        }
  78  
  79        $today = getdate(); 
  80        for ($i=$today['year']; $i < $today['year']+10; $i++) {
  81          $expires_year[] = array('id' => strftime('%y',mktime(0,0,0,1,1,$i)), 'text' => strftime('%Y',mktime(0,0,0,1,1,$i)));
  82        }
  83  
  84        $confirmation = array('fields' => array(array('title' => MODULE_PAYMENT_AUTHORIZENET_CC_AIM_CREDIT_CARD_OWNER,
  85                                                      'field' => tep_draw_input_field('cc_owner', $order->billing['firstname'] . ' ' . $order->billing['lastname'])),
  86                                                array('title' => MODULE_PAYMENT_AUTHORIZENET_CC_AIM_CREDIT_CARD_NUMBER,
  87                                                      'field' => tep_draw_input_field('cc_number_nh-dns')),
  88                                                array('title' => MODULE_PAYMENT_AUTHORIZENET_CC_AIM_CREDIT_CARD_EXPIRES,
  89                                                      'field' => tep_draw_pull_down_menu('cc_expires_month', $expires_month) . '&nbsp;' . tep_draw_pull_down_menu('cc_expires_year', $expires_year)),
  90                                                array('title' => MODULE_PAYMENT_AUTHORIZENET_CC_AIM_CREDIT_CARD_CVC,
  91                                                      'field' => tep_draw_input_field('cc_cvc_nh-dns', '', 'size="5" maxlength="4"'))));
  92  
  93        return $confirmation;
  94      }
  95  
  96      function process_button() {
  97        return false;
  98      }
  99  
 100      function before_process() {
 101        global $HTTP_POST_VARS, $customer_id, $order, $sendto, $currency;
 102  
 103        $params = array('x_login' => substr(MODULE_PAYMENT_AUTHORIZENET_CC_AIM_LOGIN_ID, 0, 20),
 104                        'x_tran_key' => substr(MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TRANSACTION_KEY, 0, 16),
 105                        'x_version' => '3.1',
 106                        'x_delim_data' => 'TRUE',
 107                        'x_delim_char' => ',',
 108                        'x_encap_char' => '"',
 109                        'x_relay_response' => 'FALSE',
 110                        'x_first_name' => substr($order->billing['firstname'], 0, 50),
 111                        'x_last_name' => substr($order->billing['lastname'], 0, 50),
 112                        'x_company' => substr($order->billing['company'], 0, 50),
 113                        'x_address' => substr($order->billing['street_address'], 0, 60),
 114                        'x_city' => substr($order->billing['city'], 0, 40),
 115                        'x_state' => substr($order->billing['state'], 0, 40),
 116                        'x_zip' => substr($order->billing['postcode'], 0, 20),
 117                        'x_country' => substr($order->billing['country']['title'], 0, 60),
 118                        'x_phone' => substr($order->customer['telephone'], 0, 25),
 119                        'x_cust_id' => substr($customer_id, 0, 20),
 120                        'x_customer_ip' => tep_get_ip_address(),
 121                        'x_email' => substr($order->customer['email_address'], 0, 255),
 122                        'x_description' => substr(STORE_NAME, 0, 255),
 123                        'x_amount' => substr($this->format_raw($order->info['total']), 0, 15),
 124                        'x_currency_code' => substr($currency, 0, 3),
 125                        'x_method' => 'CC',
 126                        'x_type' => ((MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TRANSACTION_METHOD == 'Capture') ? 'AUTH_CAPTURE' : 'AUTH_ONLY'),
 127                        'x_card_num' => substr($HTTP_POST_VARS['cc_number_nh-dns'], 0, 22),
 128                        'x_exp_date' => $HTTP_POST_VARS['cc_expires_month'] . $HTTP_POST_VARS['cc_expires_year'],
 129                        'x_card_code' => substr($HTTP_POST_VARS['cc_cvc_nh-dns'], 0, 4));
 130  
 131        if (is_numeric($sendto) && ($sendto > 0)) {
 132          $params['x_ship_to_first_name'] = substr($order->delivery['firstname'], 0, 50);
 133          $params['x_ship_to_last_name'] = substr($order->delivery['lastname'], 0, 50);
 134          $params['x_ship_to_company'] = substr($order->delivery['company'], 0, 50);
 135          $params['x_ship_to_address'] = substr($order->delivery['street_address'], 0, 60);
 136          $params['x_ship_to_city'] = substr($order->delivery['city'], 0, 40);
 137          $params['x_ship_to_state'] = substr($order->delivery['state'], 0, 40);
 138          $params['x_ship_to_zip'] = substr($order->delivery['postcode'], 0, 20);
 139          $params['x_ship_to_country'] = substr($order->delivery['country']['title'], 0, 60);
 140        }
 141  
 142        if (MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TRANSACTION_MODE == 'Test') {
 143          $params['x_test_request'] = 'TRUE';
 144        }
 145  
 146        $tax_value = 0;
 147  
 148        foreach ($order->info['tax_groups'] as $key => $value) {
 149          if ($value > 0) {
 150            $tax_value += $this->format_raw($value);
 151          }
 152        }
 153  
 154        if ($tax_value > 0) {
 155          $params['x_tax'] = $this->format_raw($tax_value);
 156        }
 157  
 158        $params['x_freight'] = $this->format_raw($order->info['shipping_cost']);
 159  
 160        $post_string = '';
 161  
 162        foreach ($params as $key => $value) {
 163          $post_string .= $key . '=' . urlencode(trim($value)) . '&';
 164        }
 165  
 166        $post_string = substr($post_string, 0, -1);
 167  
 168        for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
 169          $post_string .= '&x_line_item=' . urlencode($i+1) . '<|>' . urlencode(substr($order->products[$i]['name'], 0, 31)) . '<|>' . urlencode(substr($order->products[$i]['name'], 0, 255)) . '<|>' . urlencode($order->products[$i]['qty']) . '<|>' . urlencode($this->format_raw($order->products[$i]['final_price'])) . '<|>' . urlencode($order->products[$i]['tax'] > 0 ? 'YES' : 'NO');
 170        }
 171  
 172        switch (MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TRANSACTION_SERVER) {
 173          case 'Live':
 174            $gateway_url = 'https://secure.authorize.net/gateway/transact.dll';
 175            break;
 176  
 177          default:
 178            $gateway_url = 'https://test.authorize.net/gateway/transact.dll';
 179            break;
 180        }
 181  
 182        $transaction_response = $this->sendTransactionToGateway($gateway_url, $post_string);
 183  
 184        if (!empty($transaction_response)) {
 185          $regs = preg_split("/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/", $transaction_response);
 186  
 187          foreach ($regs as $key => $value) {
 188            $regs[$key] = substr($value, 1, -1); // remove double quotes
 189          }
 190        } else {
 191          $regs = array('-1', '-1', '-1');
 192        }
 193  
 194        $error = false;
 195  
 196        if ($regs[0] == '1') {
 197          if (tep_not_null(MODULE_PAYMENT_AUTHORIZENET_CC_AIM_MD5_HASH)) {
 198            if (strtoupper($regs[37]) != strtoupper(md5(MODULE_PAYMENT_AUTHORIZENET_CC_AIM_MD5_HASH . MODULE_PAYMENT_AUTHORIZENET_CC_AIM_LOGIN_ID . $regs[6] . $this->format_raw($order->info['total'])))) {
 199              $error = 'general';
 200            }
 201          }
 202        } else {
 203          switch ($regs[2]) {
 204            case '7':
 205              $error = 'invalid_expiration_date';
 206              break;
 207  
 208            case '8':
 209              $error = 'expired';
 210              break;
 211  
 212            case '6':
 213            case '17':
 214            case '28':
 215              $error = 'declined';
 216              break;
 217  
 218            case '78':
 219              $error = 'cvc';
 220              break;
 221  
 222            default:
 223              $error = 'general';
 224              break;
 225          }
 226        }
 227  
 228        if ($error != false) {
 229          tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error=' . $this->code . '&error=' . $error, 'SSL'));
 230        }
 231      }
 232  
 233      function after_process() {
 234        return false;
 235      }
 236  
 237      function get_error() {
 238        global $HTTP_GET_VARS;
 239  
 240        $error_message = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ERROR_GENERAL;
 241  
 242        switch ($HTTP_GET_VARS['error']) {
 243          case 'invalid_expiration_date':
 244            $error_message = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ERROR_INVALID_EXP_DATE;
 245            break;
 246  
 247          case 'expired':
 248            $error_message = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ERROR_EXPIRED;
 249            break;
 250  
 251          case 'declined':
 252            $error_message = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ERROR_DECLINED;
 253            break;
 254  
 255          case 'cvc':
 256            $error_message = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ERROR_CVC;
 257            break;
 258  
 259          default:
 260            $error_message = MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ERROR_GENERAL;
 261            break;
 262        }
 263  
 264        $error = array('title' => MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ERROR_TITLE,
 265                       'error' => $error_message);
 266  
 267        return $error;
 268      }
 269  
 270      function check() {
 271        if (!isset($this->_check)) {
 272          $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_STATUS'");
 273          $this->_check = tep_db_num_rows($check_query);
 274        }
 275        return $this->_check;
 276      }
 277  
 278      function install() {
 279        tep_db_query("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 Credit Card AIM', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_STATUS', 'False', 'Do you want to accept Authorize.net Credit Card AIM payments?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
 280        tep_db_query("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_CC_AIM_LOGIN_ID', '', 'The login ID used for the Authorize.net service', '6', '0', now())");
 281        tep_db_query("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_CC_AIM_TRANSACTION_KEY', '', 'Transaction key used for encrypting data', '6', '0', now())");
 282        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('MD5 Hash', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_MD5_HASH', '', 'The MD5 hash value to verify transactions with', '6', '0', now())");
 283        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Transaction Server', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TRANSACTION_SERVER', 'Live', 'Perform transactions on the live or test server. The test server should only be used by developers with Authorize.net test accounts.', '6', '0', 'tep_cfg_select_option(array(\'Live\', \'Test\'), ', now())");
 284        tep_db_query("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_CC_AIM_TRANSACTION_MODE', 'Live', 'Transaction mode used for processing orders', '6', '0', 'tep_cfg_select_option(array(\'Live\', \'Test\'), ', now())");
 285        tep_db_query("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_CC_AIM_TRANSACTION_METHOD', 'Authorization', 'The processing method to use for each transaction.', '6', '0', 'tep_cfg_select_option(array(\'Authorization\', \'Capture\'), ', now())");
 286        tep_db_query("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_CC_AIM_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
 287        tep_db_query("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_CC_AIM_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
 288        tep_db_query("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_CC_AIM_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '0', 'tep_cfg_pull_down_order_statuses(', 'tep_get_order_status_name', now())");
 289        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('cURL Program Location', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_CURL', '/usr/bin/curl', 'The location to the cURL program application.', '6', '0' , now())");
 290      }
 291  
 292      function remove() {
 293        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
 294      }
 295  
 296      function keys() {
 297        return array('MODULE_PAYMENT_AUTHORIZENET_CC_AIM_STATUS', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_LOGIN_ID', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TRANSACTION_KEY', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_MD5_HASH', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TRANSACTION_SERVER', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TRANSACTION_MODE', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_TRANSACTION_METHOD', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ZONE', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_ORDER_STATUS_ID', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_SORT_ORDER', 'MODULE_PAYMENT_AUTHORIZENET_CC_AIM_CURL');
 298      }
 299  
 300      function _hmac($key, $data) {
 301        if (function_exists('mhash') && defined('MHASH_MD5')) {
 302          return bin2hex(mhash(MHASH_MD5, $data, $key));
 303        }
 304  
 305  // RFC 2104 HMAC implementation for php.
 306  // Creates an md5 HMAC.
 307  // Eliminates the need to install mhash to compute a HMAC
 308  // Hacked by Lance Rushing
 309  
 310        $b = 64; // byte length for md5
 311        if (strlen($key) > $b) {
 312          $key = pack("H*",md5($key));
 313        }
 314  
 315        $key = str_pad($key, $b, chr(0x00));
 316        $ipad = str_pad('', $b, chr(0x36));
 317        $opad = str_pad('', $b, chr(0x5c));
 318        $k_ipad = $key ^ $ipad ;
 319        $k_opad = $key ^ $opad;
 320  
 321        return md5($k_opad . pack("H*",md5($k_ipad . $data)));
 322      }
 323  
 324      function sendTransactionToGateway($url, $parameters) {
 325        $server = parse_url($url);
 326  
 327        if (isset($server['port']) === false) {
 328          $server['port'] = ($server['scheme'] == 'https') ? 443 : 80;
 329        }
 330  
 331        if (isset($server['path']) === false) {
 332          $server['path'] = '/';
 333        }
 334  
 335        if (isset($server['user']) && isset($server['pass'])) {
 336          $header[] = 'Authorization: Basic ' . base64_encode($server['user'] . ':' . $server['pass']);
 337        }
 338  
 339        if (function_exists('curl_init')) {
 340          $curl = curl_init($server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : ''));
 341          curl_setopt($curl, CURLOPT_PORT, $server['port']);
 342          curl_setopt($curl, CURLOPT_HEADER, 0);
 343          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
 344          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 345          curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
 346          curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
 347          curl_setopt($curl, CURLOPT_POST, 1);
 348          curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
 349  
 350          $result = curl_exec($curl);
 351  
 352          curl_close($curl);
 353        } else {
 354          exec(escapeshellarg(MODULE_PAYMENT_AUTHORIZENET_CC_AIM_CURL) . ' -d ' . escapeshellarg($parameters) . ' "' . $server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : '') . '" -P ' . $server['port'] . ' -k', $result);
 355          $result = implode("\n", $result);
 356        }
 357  
 358        return $result;
 359      }
 360  
 361  // format prices without currency formatting
 362      function format_raw($number, $currency_code = '', $currency_value = '') {
 363        global $currencies, $currency;
 364  
 365        if (empty($currency_code) || !$this->is_set($currency_code)) {
 366          $currency_code = $currency;
 367        }
 368  
 369        if (empty($currency_value) || !is_numeric($currency_value)) {
 370          $currency_value = $currencies->currencies[$currency_code]['value'];
 371        }
 372  
 373        return number_format(tep_round($number * $currency_value, $currencies->currencies[$currency_code]['decimal_places']), $currencies->currencies[$currency_code]['decimal_places'], '.', '');
 374      }
 375    }
 376  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7