PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/catalog/controller/payment/authorizenet_aim.php

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