shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

authorizenet_aim.php (8198B)


      1 <?php
      2 class ControllerExtensionPaymentAuthorizeNetAim extends Controller {
      3 	public function index() {
      4 		$this->load->language('extension/payment/authorizenet_aim');
      5 
      6 		$data['months'] = array();
      7 
      8 		for ($i = 1; $i <= 12; $i++) {
      9 			$data['months'][] = array(
     10 				'text'  => strftime('%B', mktime(0, 0, 0, $i, 1, 2000)),
     11 				'value' => sprintf('%02d', $i)
     12 			);
     13 		}
     14 
     15 		$today = getdate();
     16 
     17 		$data['year_expire'] = array();
     18 
     19 		for ($i = $today['year']; $i < $today['year'] + 11; $i++) {
     20 			$data['year_expire'][] = array(
     21 				'text'  => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
     22 				'value' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i))
     23 			);
     24 		}
     25 
     26 		return $this->load->view('extension/payment/authorizenet_aim', $data);
     27 	}
     28 
     29 	public function send() {
     30 		if ($this->config->get('payment_authorizenet_aim_server') == 'live') {
     31 			$url = 'https://secure.authorize.net/gateway/transact.dll';
     32 		} elseif ($this->config->get('payment_authorizenet_aim_server') == 'test') {
     33 			$url = 'https://test.authorize.net/gateway/transact.dll';
     34 		}
     35 
     36 		//$url = 'https://secure.networkmerchants.com/gateway/transact.dll';
     37 
     38 		$this->load->model('checkout/order');
     39 
     40 		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     41 
     42 		$data = array();
     43 
     44 		$data['x_login'] = $this->config->get('payment_authorizenet_aim_login');
     45 		$data['x_tran_key'] = $this->config->get('payment_authorizenet_aim_key');
     46 		$data['x_version'] = '3.1';
     47 		$data['x_delim_data'] = 'true';
     48 		$data['x_delim_char'] = '|';
     49 		$data['x_encap_char'] = '"';
     50 		$data['x_relay_response'] = 'false';
     51 		$data['x_first_name'] = html_entity_decode($order_info['payment_firstname'], ENT_QUOTES, 'UTF-8');
     52 		$data['x_last_name'] = html_entity_decode($order_info['payment_lastname'], ENT_QUOTES, 'UTF-8');
     53 		$data['x_company'] = html_entity_decode($order_info['payment_company'], ENT_QUOTES, 'UTF-8');
     54 		$data['x_address'] = html_entity_decode($order_info['payment_address_1'], ENT_QUOTES, 'UTF-8');
     55 		$data['x_city'] = html_entity_decode($order_info['payment_city'], ENT_QUOTES, 'UTF-8');
     56 		$data['x_state'] = html_entity_decode($order_info['payment_zone'], ENT_QUOTES, 'UTF-8');
     57 		$data['x_zip'] = html_entity_decode($order_info['payment_postcode'], ENT_QUOTES, 'UTF-8');
     58 		$data['x_country'] = html_entity_decode($order_info['payment_country'], ENT_QUOTES, 'UTF-8');
     59 		$data['x_phone'] = $order_info['telephone'];
     60 		$data['x_customer_ip'] = $this->request->server['REMOTE_ADDR'];
     61 		$data['x_email'] = $order_info['email'];
     62 		$data['x_description'] = html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8');
     63 		$data['x_amount'] = $this->currency->format($order_info['total'], $order_info['currency_code'], 1.00000, false);
     64 		$data['x_currency_code'] = $this->session->data['currency'];
     65 		$data['x_method'] = 'CC';
     66 		$data['x_type'] = ($this->config->get('payment_authorizenet_aim_method') == 'capture') ? 'AUTH_CAPTURE' : 'AUTH_ONLY';
     67 		$data['x_card_num'] = str_replace(' ', '', $this->request->post['cc_number']);
     68 		$data['x_exp_date'] = $this->request->post['cc_expire_date_month'] . $this->request->post['cc_expire_date_year'];
     69 		$data['x_card_code'] = $this->request->post['cc_cvv2'];
     70 		$data['x_invoice_num'] = $this->session->data['order_id'];
     71 		$data['x_solution_id'] = 'A1000015';
     72 
     73 		/* Customer Shipping Address Fields */
     74 		if ($order_info['shipping_method']) {
     75 			$data['x_ship_to_first_name'] = html_entity_decode($order_info['shipping_firstname'], ENT_QUOTES, 'UTF-8');
     76 			$data['x_ship_to_last_name'] = html_entity_decode($order_info['shipping_lastname'], ENT_QUOTES, 'UTF-8');
     77 			$data['x_ship_to_company'] = html_entity_decode($order_info['shipping_company'], ENT_QUOTES, 'UTF-8');
     78 			$data['x_ship_to_address'] = html_entity_decode($order_info['shipping_address_1'], ENT_QUOTES, 'UTF-8') . ' ' . html_entity_decode($order_info['shipping_address_2'], ENT_QUOTES, 'UTF-8');
     79 			$data['x_ship_to_city'] = html_entity_decode($order_info['shipping_city'], ENT_QUOTES, 'UTF-8');
     80 			$data['x_ship_to_state'] = html_entity_decode($order_info['shipping_zone'], ENT_QUOTES, 'UTF-8');
     81 			$data['x_ship_to_zip'] = html_entity_decode($order_info['shipping_postcode'], ENT_QUOTES, 'UTF-8');
     82 			$data['x_ship_to_country'] = html_entity_decode($order_info['shipping_country'], ENT_QUOTES, 'UTF-8');
     83 		} else {
     84 			$data['x_ship_to_first_name'] = html_entity_decode($order_info['payment_firstname'], ENT_QUOTES, 'UTF-8');
     85 			$data['x_ship_to_last_name'] = html_entity_decode($order_info['payment_lastname'], ENT_QUOTES, 'UTF-8');
     86 			$data['x_ship_to_company'] = html_entity_decode($order_info['payment_company'], ENT_QUOTES, 'UTF-8');
     87 			$data['x_ship_to_address'] = html_entity_decode($order_info['payment_address_1'], ENT_QUOTES, 'UTF-8') . ' ' . html_entity_decode($order_info['payment_address_2'], ENT_QUOTES, 'UTF-8');
     88 			$data['x_ship_to_city'] = html_entity_decode($order_info['payment_city'], ENT_QUOTES, 'UTF-8');
     89 			$data['x_ship_to_state'] = html_entity_decode($order_info['payment_zone'], ENT_QUOTES, 'UTF-8');
     90 			$data['x_ship_to_zip'] = html_entity_decode($order_info['payment_postcode'], ENT_QUOTES, 'UTF-8');
     91 			$data['x_ship_to_country'] = html_entity_decode($order_info['payment_country'], ENT_QUOTES, 'UTF-8');
     92 		}
     93 
     94 		if ($this->config->get('payment_authorizenet_aim_mode') == 'test') {
     95 			$data['x_test_request'] = 'true';
     96 		}
     97 
     98 		$curl = curl_init($url);
     99 
    100 		curl_setopt($curl, CURLOPT_PORT, 443);
    101 		curl_setopt($curl, CURLOPT_HEADER, 0);
    102 		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    103 		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    104 		curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
    105 		curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
    106 		curl_setopt($curl, CURLOPT_POST, 1);
    107 		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    108 		curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    109 		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));
    110 
    111 		$response = curl_exec($curl);
    112 
    113 		$json = array();
    114 
    115 		if (curl_error($curl)) {
    116 			$json['error'] = 'CURL ERROR: ' . curl_errno($curl) . '::' . curl_error($curl);
    117 
    118 			$this->log->write('AUTHNET AIM CURL ERROR: ' . curl_errno($curl) . '::' . curl_error($curl));
    119 		} elseif ($response) {
    120 			$i = 1;
    121 
    122 			$response_info = array();
    123 
    124 			$results = explode('|', $response);
    125 
    126 			foreach ($results as $result) {
    127 				$response_info[$i] = trim($result, '"');
    128 
    129 				$i++;
    130 			}
    131 
    132 			if ($response_info[1] == '1') {
    133 				$message = '';
    134 
    135 				if (isset($response_info['5'])) {
    136 					$message .= 'Authorization Code: ' . $response_info['5'] . "\n";
    137 				}
    138 
    139 				if (isset($response_info['6'])) {
    140 					$message .= 'AVS Response: ' . $response_info['6'] . "\n";
    141 				}
    142 
    143 				if (isset($response_info['7'])) {
    144 					$message .= 'Transaction ID: ' . $response_info['7'] . "\n";
    145 				}
    146 
    147 				if (isset($response_info['39'])) {
    148 					$message .= 'Card Code Response: ' . $response_info['39'] . "\n";
    149 				}
    150 
    151 				if (isset($response_info['40'])) {
    152 					$message .= 'Cardholder Authentication Verification Response: ' . $response_info['40'] . "\n";
    153 				}
    154 
    155 				if (!$this->config->get('payment_authorizenet_aim_hash') || (strtoupper($response_info[38]) == strtoupper(md5($this->config->get('payment_authorizenet_aim_hash') . $this->config->get('payment_authorizenet_aim_login') . $response_info[7] . $this->currency->format($order_info['total'], $order_info['currency_code'], 1.00000, false))))) {
    156 					$this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $this->config->get('payment_authorizenet_aim_order_status_id'), $message, false);
    157 				} else {
    158 					$this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $this->config->get('config_order_status_id'));
    159 				}
    160 
    161 				$json['redirect'] = $this->url->link('checkout/success', '', true);
    162 			} else {
    163 				$json['error'] = $response_info[4];
    164 			}
    165 		} else {
    166 			$json['error'] = 'Empty Gateway Response';
    167 
    168 			$this->log->write('AUTHNET AIM CURL ERROR: Empty Gateway Response');
    169 		}
    170 
    171 		curl_close($curl);
    172 
    173 		$this->response->addHeader('Content-Type: application/json');
    174 		$this->response->setOutput(json_encode($json));
    175 	}
    176 }