PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/catalog/controller/payment/firstdata.php

https://gitlab.com/firstrate/firstrate
PHP | 251 lines | 201 code | 50 blank | 0 comment | 59 complexity | f8a42fc8c06d0c9998e637326520109a 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', '', 'SSL');
  22. $data['url_fail'] = $this->url->link('payment/firstdata/fail', '', 'SSL');
  23. $data['url_notify'] = $this->url->link('payment/firstdata/notify', '', 'SSL');
  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. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/firstdata.tpl')) {
  73. return $this->load->view($this->config->get('config_template') . '/template/payment/firstdata.tpl', $data);
  74. } else {
  75. return $this->load->view('default/template/payment/firstdata.tpl', $data);
  76. }
  77. }
  78. public function notify() {
  79. $this->load->model('payment/firstdata');
  80. $this->load->model('checkout/order');
  81. $this->load->language('payment/firstdata');
  82. $message = '';
  83. if ($this->config->get('firstdata_debug') == 1) {
  84. $this->model_payment_firstdata->logger(print_r($this->request->post, 1));
  85. }
  86. if (isset($this->request->post['txntype']) && isset($this->request->post['notification_hash']) && isset($this->request->post['oid'])) {
  87. $local_hash = $this->model_payment_firstdata->responseHash($this->request->post['chargetotal'], $this->request->post['currency'], $this->request->post['txndatetime'], $this->request->post['approval_code']);
  88. if ($local_hash == $this->request->post['notification_hash']) {
  89. $order_id_parts = explode('T', $this->request->post['oid']);
  90. $order_id = str_replace("CON-","",$order_id_parts[0]);
  91. $order_info = $this->model_checkout_order->getOrder($order_id);
  92. if ($this->request->post['txntype'] == 'preauth' || $this->request->post['txntype'] == 'sale') {
  93. if (isset($this->request->post['approval_code'])) {
  94. $response_parts = explode(':', $this->request->post['approval_code']);
  95. $address_codes = array(
  96. 'PPX' => $this->language->get('text_address_ppx'),
  97. 'YYY' => $this->language->get('text_address_yyy'),
  98. 'YNA' => $this->language->get('text_address_yna'),
  99. 'NYZ' => $this->language->get('text_address_nyz'),
  100. 'NNN' => $this->language->get('text_address_nnn'),
  101. 'YPX' => $this->language->get('text_address_ypx'),
  102. 'PYX' => $this->language->get('text_address_pyx'),
  103. 'XXU' => $this->language->get('text_address_xxu')
  104. );
  105. $cvv_codes = array(
  106. 'M' => $this->language->get('text_card_code_m'),
  107. 'N' => $this->language->get('text_card_code_n'),
  108. 'P' => $this->language->get('text_card_code_p'),
  109. 'S' => $this->language->get('text_card_code_s'),
  110. 'U' => $this->language->get('text_card_code_u'),
  111. 'X' => $this->language->get('text_card_code_x'),
  112. 'NONE' => $this->language->get('text_card_code_blank')
  113. );
  114. $card_types = array(
  115. 'M' => $this->language->get('text_card_type_m'),
  116. 'V' => $this->language->get('text_card_type_v'),
  117. 'C' => $this->language->get('text_card_type_c'),
  118. 'A' => $this->language->get('text_card_type_a'),
  119. 'MA' => $this->language->get('text_card_type_ma'),
  120. 'MAESTROUK' => $this->language->get('text_card_type_mauk')
  121. );
  122. if ($response_parts[0] == 'Y') {
  123. if (isset($response_parts[3])) {
  124. if (strlen($response_parts[3]) == 4) {
  125. $address_pass = strtoupper(substr($response_parts[3], 0, 3));
  126. $cvv_pass = strtoupper(substr($response_parts[3], -1));
  127. if (!array_key_exists($cvv_pass, $cvv_codes)) {
  128. $cvv_pass = 'NONE';
  129. }
  130. } else {
  131. $address_pass = $response_parts[3];
  132. $cvv_pass = 'NONE';
  133. }
  134. $message .= $this->language->get('text_address_response') . $address_codes[$address_pass] . '<br />';
  135. $message .= $this->language->get('text_card_code_verify') . $cvv_codes[$cvv_pass] . '<br />';
  136. $message .= $this->language->get('text_response_code_full') . $this->request->post['approval_code'] . '<br />';
  137. $message .= $this->language->get('text_response_code') . $response_parts[1] . '<br />';
  138. if (isset($this->request->post['cardnumber'])) {
  139. $message .= $this->language->get('text_response_card') . $this->request->post['cardnumber'] . '<br />';
  140. }
  141. if (isset($this->request->post['processor_response_code'])) {
  142. $message .= $this->language->get('text_response_proc_code') . $this->request->post['processor_response_code'] . '<br />';
  143. }
  144. if (isset($this->request->post['refnumber'])) {
  145. $message .= $this->language->get('text_response_ref') . $this->request->post['refnumber'] . '<br />';
  146. }
  147. if (isset($this->request->post['paymentMethod'])) {
  148. $message .= $this->language->get('text_response_card_type') . $card_types[strtoupper($this->request->post['paymentMethod'])] . '<br />';
  149. }
  150. }
  151. if (isset($this->request->post['hosteddataid']) && $order_info['customer_id'] != 0) {
  152. $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']);
  153. }
  154. $fd_order_id = $this->model_payment_firstdata->addOrder($order_info, $this->request->post['oid'], $this->request->post['tdate']);
  155. if ($this->config->get('firstdata_auto_settle') == 1) {
  156. $this->model_payment_firstdata->addTransaction($fd_order_id, 'payment', $order_info);
  157. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_success_settled_id'), $message, false);
  158. } else {
  159. $this->model_payment_firstdata->addTransaction($fd_order_id, 'auth');
  160. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_success_unsettled_id'), $message, false);
  161. }
  162. } else {
  163. $message = $this->request->post['fail_reason'] . '<br />';
  164. $message .= $this->language->get('text_response_code_full') . $this->request->post['approval_code'];
  165. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_decline_id'), $message);
  166. }
  167. }
  168. }
  169. if ($this->request->post['txntype'] == 'void') {
  170. if ($this->request->post['status'] == 'DECLINED') {
  171. $fd_order = $this->model_payment_firstdata->getOrder($order_id);
  172. $this->model_payment_firstdata->updateVoidStatus($order_id, 1);
  173. $this->model_payment_firstdata->addTransaction($fd_order['firstdata_order_id'], 'void');
  174. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_void_id'), $message, false);
  175. }
  176. }
  177. if ($this->request->post['txntype'] == 'postauth') {
  178. if ($this->request->post['status'] == 'APPROVED') {
  179. $fd_order = $this->model_payment_firstdata->getOrder($order_id);
  180. $this->model_payment_firstdata->updateCaptureStatus($order_id, 1);
  181. $this->model_payment_firstdata->addTransaction($fd_order['firstdata_order_id'], 'payment', $order_info);
  182. $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('firstdata_order_status_success_settled_id'), $message, false);
  183. }
  184. }
  185. } else {
  186. $this->model_payment_firstdata->logger('Hash does not match! Received: ' . $this->request->post['notification_hash'] . ', calculated: ' . $local_hash);
  187. }
  188. } else {
  189. $this->model_payment_firstdata->logger('Data is missing from request . ');
  190. }
  191. }
  192. public function fail() {
  193. $this->load->language('payment/firstdata');
  194. if (isset($this->request->post['fail_reason']) && !empty($this->request->post['fail_reason'])) {
  195. $this->session->data['error'] = $this->request->post['fail_reason'];
  196. } else {
  197. $this->session->data['error'] = $this->language->get('error_failed');
  198. }
  199. $this->response->redirect($this->url->link('checkout/checkout', '', 'SSL'));
  200. }
  201. }