PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/controller/payment/firstdata.php

https://gitlab.com/shapcy/opencart
PHP | 247 lines | 197 code | 50 blank | 0 comment | 57 complexity | ac2b7023aabc6e336ec665788ba51105 MD5 | raw file
  1. <?php
  2. class ControllerPaymentFirstdata extends Controller {
  3. public function index() {
  4. $this->load->language('payment/firstdata');
  5. $data['button_confirm'] = $this->language->get('button_confirm');
  6. $data['text_new_card'] = $this->language->get('text_new_card');
  7. $data['text_store_card'] = $this->language->get('text_store_card');
  8. $this->load->model('checkout/order');
  9. $this->load->model('payment/firstdata');
  10. $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
  11. if ($this->config->get('firstdata_live_demo') == 1) {
  12. $data['action'] = $this->config->get('firstdata_live_url');
  13. } else {
  14. $data['action'] = $this->config->get('firstdata_demo_url');
  15. }
  16. $data['amount'] = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
  17. $data['currency'] = $this->model_payment_firstdata->mapCurrency($order_info['currency_code']);
  18. $data['merchant_id'] = $this->config->get('firstdata_merchant_id');
  19. $data['timestamp'] = date('Y:m:d-H:i:s');
  20. $data['order_id'] = 'CON-' . $this->session->data['order_id'] . 'T' . $data['timestamp'] . mt_rand(1, 999);
  21. $data['url_success'] = $this->url->link('checkout/success', '', true);
  22. $data['url_fail'] = $this->url->link('payment/firstdata/fail', '', true);
  23. $data['url_notify'] = $this->url->link('payment/firstdata/notify', '', true);
  24. if (preg_match("/Mobile|Android|BlackBerry|iPhone|Windows Phone/", $this->request->server['HTTP_USER_AGENT'])) {
  25. $data['mobile'] = true;
  26. } else {
  27. $data['mobile'] = false;
  28. }
  29. if ($this->config->get('firstdata_auto_settle') == 1) {
  30. $data['txntype'] = 'sale';
  31. } else {
  32. $data['txntype'] = 'preauth';
  33. }
  34. $tmp = $data['merchant_id'] . $data['timestamp'] . $data['amount'] . $data['currency'] . $this->config->get('firstdata_secret');
  35. $ascii = bin2hex($tmp);
  36. $data['hash'] = sha1($ascii);
  37. $data['version'] = 'OPENCART-C-' . VERSION;
  38. $data['bcompany'] = $order_info['payment_company'];
  39. $data['bname'] = $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname'];
  40. $data['baddr1'] = substr($order_info['payment_address_1'], 0, 30);
  41. $data['baddr2'] = substr($order_info['payment_address_2'], 0, 30);
  42. $data['bcity'] = substr($order_info['payment_city'], 0, 30);
  43. $data['bstate'] = substr($order_info['payment_zone'], 0, 30);
  44. $data['bcountry'] = $order_info['payment_iso_code_2'];
  45. $data['bzip'] = $order_info['payment_postcode'];
  46. $data['email'] = $order_info['email'];
  47. if ($this->cart->hasShipping()) {
  48. $data['sname'] = $order_info['shipping_firstname'] . ' ' . $order_info['shipping_lastname'];
  49. $data['saddr1'] = substr($order_info['shipping_address_1'], 0, 30);
  50. $data['saddr2'] = substr($order_info['shipping_address_2'], 0, 30);
  51. $data['scity'] = substr($order_info['shipping_city'], 0, 30);
  52. $data['sstate'] = substr($order_info['shipping_zone'], 0, 30);
  53. $data['scountry'] = $order_info['shipping_iso_code_2'];
  54. $data['szip'] = $order_info['shipping_postcode'];
  55. } else {
  56. $data['sname'] = $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname'];
  57. $data['saddr1'] = substr($order_info['payment_address_1'], 0, 30);
  58. $data['saddr2'] = substr($order_info['payment_address_2'], 0, 30);
  59. $data['scity'] = substr($order_info['payment_city'], 0, 30);
  60. $data['sstate'] = substr($order_info['payment_zone'], 0, 30);
  61. $data['scountry'] = $order_info['payment_iso_code_2'];
  62. $data['szip'] = $order_info['payment_postcode'];
  63. }
  64. if ($this->config->get('firstdata_card_storage') == 1 && $this->customer->isLogged()) {
  65. $data['card_storage'] = 1;
  66. $data['stored_cards'] = $this->model_payment_firstdata->getStoredCards();
  67. $data['new_hosted_id'] = sha1($this->customer->getId() . '-' . date("Y-m-d-H-i-s") . rand(10, 500));
  68. } else {
  69. $data['card_storage'] = 0;
  70. $data['stored_cards'] = array();
  71. }
  72. return $this->load->view('payment/firstdata', $data);
  73. }
  74. public function notify() {
  75. $this->load->model('payment/firstdata');
  76. $this->load->model('checkout/order');
  77. $this->load->language('payment/firstdata');
  78. $message = '';
  79. if ($this->config->get('firstdata_debug') == 1) {
  80. $this->model_payment_firstdata->logger(print_r($this->request->post, 1));
  81. }
  82. if (isset($this->request->post['txntype']) && isset($this->request->post['notification_hash']) && isset($this->request->post['oid'])) {
  83. $local_hash = $this->model_payment_firstdata->responseHash($this->request->post['chargetotal'], $this->request->post['currency'], $this->request->post['txndatetime'], $this->request->post['approval_code']);
  84. if ($local_hash == $this->request->post['notification_hash']) {
  85. $order_id_parts = explode('T', $this->request->post['oid']);
  86. $order_id = str_replace("CON-","",$order_id_parts[0]);
  87. $order_info = $this->model_checkout_order->getOrder($order_id);
  88. if ($this->request->post['txntype'] == 'preauth' || $this->request->post['txntype'] == 'sale') {
  89. if (isset($this->request->post['approval_code'])) {
  90. $response_parts = explode(':', $this->request->post['approval_code']);
  91. $address_codes = array(
  92. 'PPX' => $this->language->get('text_address_ppx'),
  93. 'YYY' => $this->language->get('text_address_yyy'),
  94. 'YNA' => $this->language->get('text_address_yna'),
  95. 'NYZ' => $this->language->get('text_address_nyz'),
  96. 'NNN' => $this->language->get('text_address_nnn'),
  97. 'YPX' => $this->language->get('text_address_ypx'),
  98. 'PYX' => $this->language->get('text_address_pyx'),
  99. 'XXU' => $this->language->get('text_address_xxu')
  100. );
  101. $cvv_codes = array(
  102. 'M' => $this->language->get('text_card_code_m'),
  103. 'N' => $this->language->get('text_card_code_n'),
  104. 'P' => $this->language->get('text_card_code_p'),
  105. 'S' => $this->language->get('text_card_code_s'),
  106. 'U' => $this->language->get('text_card_code_u'),
  107. 'X' => $this->language->get('text_card_code_x'),
  108. 'NONE' => $this->language->get('text_card_code_blank')
  109. );
  110. $card_types = array(
  111. 'M' => $this->language->get('text_card_type_m'),
  112. 'V' => $this->language->get('text_card_type_v'),
  113. 'C' => $this->language->get('text_card_type_c'),
  114. 'A' => $this->language->get('text_card_type_a'),
  115. 'MA' => $this->language->get('text_card_type_ma'),
  116. 'MAESTROUK' => $this->language->get('text_card_type_mauk')
  117. );
  118. if ($response_parts[0] == 'Y') {
  119. if (isset($response_parts[3])) {
  120. if (strlen($response_parts[3]) == 4) {
  121. $address_pass = strtoupper(substr($response_parts[3], 0, 3));
  122. $cvv_pass = strtoupper(substr($response_parts[3], -1));
  123. if (!array_key_exists($cvv_pass, $cvv_codes)) {
  124. $cvv_pass = 'NONE';
  125. }
  126. } else {
  127. $address_pass = $response_parts[3];
  128. $cvv_pass = 'NONE';
  129. }
  130. $message .= $this->language->get('text_address_response') . $address_codes[$address_pass] . '<br />';
  131. $message .= $this->language->get('text_card_code_verify') . $cvv_codes[$cvv_pass] . '<br />';
  132. $message .= $this->language->get('text_response_code_full') . $this->request->post['approval_code'] . '<br />';
  133. $message .= $this->language->get('text_response_code') . $response_parts[1] . '<br />';
  134. if (isset($this->request->post['cardnumber'])) {
  135. $message .= $this->language->get('text_response_card') . $this->request->post['cardnumber'] . '<br />';
  136. }
  137. if (isset($this->request->post['processor_response_code'])) {
  138. $message .= $this->language->get('text_response_proc_code') . $this->request->post['processor_response_code'] . '<br />';
  139. }
  140. if (isset($this->request->post['refnumber'])) {
  141. $message .= $this->language->get('text_response_ref') . $this->request->post['refnumber'] . '<br />';
  142. }
  143. if (isset($this->request->post['paymentMethod'])) {
  144. $message .= $this->language->get('text_response_card_type') . $card_types[strtoupper($this->request->post['paymentMethod'])] . '<br />';
  145. }
  146. }
  147. if (isset($this->request->post['hosteddataid']) && $order_info['customer_id'] != 0) {
  148. $this->model_payment_firstdata->storeCard($this->request->post['hosteddataid'], $order_info['customer_id'], $this->request->post['expmonth'], $this->request->post['expyear'], $this->request->post['cardnumber']);
  149. }
  150. $fd_order_id = $this->model_payment_firstdata->addOrder($order_info, $this->request->post['oid'], $this->request->post['tdate']);
  151. if ($this->config->get('firstdata_auto_settle') == 1) {
  152. $this->model_payment_firstdata->addTransaction($fd_order_id, 'payment', $order_info);
  153. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_success_settled_id'), $message, false);
  154. } else {
  155. $this->model_payment_firstdata->addTransaction($fd_order_id, 'auth');
  156. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_success_unsettled_id'), $message, false);
  157. }
  158. } else {
  159. $message = $this->request->post['fail_reason'] . '<br />';
  160. $message .= $this->language->get('text_response_code_full') . $this->request->post['approval_code'];
  161. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_decline_id'), $message);
  162. }
  163. }
  164. }
  165. if ($this->request->post['txntype'] == 'void') {
  166. if ($this->request->post['status'] == 'DECLINED') {
  167. $fd_order = $this->model_payment_firstdata->getOrder($order_id);
  168. $this->model_payment_firstdata->updateVoidStatus($order_id, 1);
  169. $this->model_payment_firstdata->addTransaction($fd_order['firstdata_order_id'], 'void');
  170. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_void_id'), $message, false);
  171. }
  172. }
  173. if ($this->request->post['txntype'] == 'postauth') {
  174. if ($this->request->post['status'] == 'APPROVED') {
  175. $fd_order = $this->model_payment_firstdata->getOrder($order_id);
  176. $this->model_payment_firstdata->updateCaptureStatus($order_id, 1);
  177. $this->model_payment_firstdata->addTransaction($fd_order['firstdata_order_id'], 'payment', $order_info);
  178. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_success_settled_id'), $message, false);
  179. }
  180. }
  181. } else {
  182. $this->model_payment_firstdata->logger('Hash does not match! Received: ' . $this->request->post['notification_hash'] . ', calculated: ' . $local_hash);
  183. }
  184. } else {
  185. $this->model_payment_firstdata->logger('Data is missing from request . ');
  186. }
  187. }
  188. public function fail() {
  189. $this->load->language('payment/firstdata');
  190. if (isset($this->request->post['fail_reason']) && !empty($this->request->post['fail_reason'])) {
  191. $this->session->data['error'] = $this->request->post['fail_reason'];
  192. } else {
  193. $this->session->data['error'] = $this->language->get('error_failed');
  194. }
  195. $this->response->redirect($this->url->link('checkout/checkout', '', true));
  196. }
  197. }