PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/controller/extension/payment/globalpay_remote.php

https://gitlab.com/dadangnh/sb1-bon
PHP | 348 lines | 277 code | 62 blank | 9 comment | 99 complexity | f9efc0f745af0c8b890454dfcf7606f9 MD5 | raw file
  1. <?php
  2. class ControllerExtensionPaymentGlobalpayRemote extends Controller {
  3. public function index() {
  4. $this->load->language('extension/payment/globalpay_remote');
  5. $data['text_credit_card'] = $this->language->get('text_credit_card');
  6. $data['text_loading'] = $this->language->get('text_loading');
  7. $data['text_wait'] = $this->language->get('text_wait');
  8. $data['entry_cc_type'] = $this->language->get('entry_cc_type');
  9. $data['entry_cc_number'] = $this->language->get('entry_cc_number');
  10. $data['entry_cc_name'] = $this->language->get('entry_cc_name');
  11. $data['entry_cc_expire_date'] = $this->language->get('entry_cc_expire_date');
  12. $data['entry_cc_cvv2'] = $this->language->get('entry_cc_cvv2');
  13. $data['entry_cc_issue'] = $this->language->get('entry_cc_issue');
  14. $data['help_start_date'] = $this->language->get('help_start_date');
  15. $data['help_issue'] = $this->language->get('help_issue');
  16. $data['button_confirm'] = $this->language->get('button_confirm');
  17. $accounts = $this->config->get('globalpay_remote_account');
  18. $card_types = array(
  19. 'visa' => $this->language->get('text_card_visa'),
  20. 'mc' => $this->language->get('text_card_mc'),
  21. 'amex' => $this->language->get('text_card_amex'),
  22. 'switch' => $this->language->get('text_card_switch'),
  23. 'laser' => $this->language->get('text_card_laser'),
  24. 'diners' => $this->language->get('text_card_diners'),
  25. );
  26. $data['cards'] = array();
  27. foreach ($accounts as $card => $account) {
  28. if (isset($account['enabled']) && $account['enabled'] == 1) {
  29. $data['cards'][] = array(
  30. 'code' => $card,
  31. 'text' => $card_types[$card],
  32. );
  33. }
  34. }
  35. $data['months'] = array();
  36. for ($i = 1; $i <= 12; $i++) {
  37. $data['months'][] = array(
  38. 'text' => strftime('%B', mktime(0, 0, 0, $i, 1, 2000)),
  39. 'value' => sprintf('%02d', $i)
  40. );
  41. }
  42. $today = getdate();
  43. $data['year_expire'] = array();
  44. for ($i = $today['year']; $i < $today['year'] + 11; $i++) {
  45. $data['year_expire'][] = array(
  46. 'text' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
  47. 'value' => strftime('%y', mktime(0, 0, 0, 1, 1, $i))
  48. );
  49. }
  50. return $this->load->view('extension/payment/globalpay_remote', $data);
  51. }
  52. public function send() {
  53. $this->load->model('checkout/order');
  54. $this->load->model('extension/payment/globalpay_remote');
  55. $this->load->language('extension/payment/globalpay_remote');
  56. if ($this->request->post['cc_number'] == '') {
  57. $json['error'] = $this->language->get('error_card_number');
  58. }
  59. if ($this->request->post['cc_name'] == '') {
  60. $json['error'] = $this->language->get('error_card_name');
  61. }
  62. if (strlen($this->request->post['cc_cvv2']) != 3 && strlen($this->request->post['cc_cvv2']) != 4) {
  63. $json['error'] = $this->language->get('error_card_cvv');
  64. }
  65. if (isset($json['error'])) {
  66. $this->response->addHeader('Content-Type: application/json');
  67. $this->response->setOutput(json_encode($json));
  68. die();
  69. }
  70. $order_id = $this->session->data['order_id'];
  71. $order_ref = $order_id . 'T' . strftime("%Y%m%d%H%M%S") . mt_rand(1, 999);
  72. $order_info = $this->model_checkout_order->getOrder($order_id);
  73. $amount = round($this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false) * 100);
  74. $currency = $order_info['currency_code'];
  75. $accounts = $this->config->get('globalpay_remote_account');
  76. if (isset($accounts[$this->request->post['cc_type']]['default']) && $accounts[$this->request->post['cc_type']]['default'] == 1) {
  77. $account = $this->config->get('globalpay_remote_merchant_id');
  78. } else {
  79. $account = $accounts[$this->request->post['cc_type']]['merchant_id'];
  80. }
  81. $eci_ref = '';
  82. $eci = '';
  83. $cavv = '';
  84. $xid = '';
  85. if ($this->config->get('globalpay_remote_3d') == 1) {
  86. if ($this->request->post['cc_type'] == 'visa' || $this->request->post['cc_type'] == 'mc' || $this->request->post['cc_type'] == 'amex') {
  87. $verify_3ds = $this->model_extension_payment_globalpay_remote->checkEnrollment($account, $amount, $currency, $order_ref);
  88. $this->model_extension_payment_globalpay_remote->logger('Verify 3DS result:\r\n' . print_r($verify_3ds, 1));
  89. // Proceed to 3D secure
  90. if (isset($verify_3ds->result) && $verify_3ds->result == '00') {
  91. $enc_data = array(
  92. 'account' => $account,
  93. 'amount' => $amount,
  94. 'currency' => $currency,
  95. 'order_id' => $order_id,
  96. 'order_ref' => $order_ref,
  97. 'cc_number' => $this->request->post['cc_number'],
  98. 'cc_expire' => $this->request->post['cc_expire_date_month'] . $this->request->post['cc_expire_date_year'],
  99. 'cc_name' => $this->request->post['cc_name'],
  100. 'cc_type' => $this->request->post['cc_type'],
  101. 'cc_cvv2' => $this->request->post['cc_cvv2'],
  102. 'cc_issue' => $this->request->post['cc_issue']
  103. );
  104. $md = $this->encryption->encrypt(json_encode($enc_data));
  105. $json = array();
  106. $json['ACSURL'] = (string)$verify_3ds->url;
  107. $json['MD'] = $md;
  108. $json['PaReq'] = (string)$verify_3ds->pareq;
  109. $json['TermUrl'] = $this->url->link('extension/payment/globalpay_remote/acsReturn', '', true);
  110. $this->response->addHeader('Content-Type: application/json');
  111. $this->response->setOutput(json_encode($json));
  112. $this->response->output();
  113. die();
  114. }
  115. // Cardholder Not Enrolled. Shift in liability. ECI = 6
  116. if (isset($verify_3ds->result) && $verify_3ds->result == '110' && isset($verify_3ds->enrolled) && $verify_3ds->enrolled == 'N') {
  117. $eci_ref = 1;
  118. $xid = '';
  119. $cavv = '';
  120. if ($this->request->post['cc_type'] == 'mc') {
  121. $eci = 1;
  122. } else {
  123. $eci = 6;
  124. }
  125. }
  126. // Unable to Verify Enrollment. No shift in liability. ECI = 7
  127. if (isset($verify_3ds->result) && $verify_3ds->result == '110' && isset($verify_3ds->enrolled) && $verify_3ds->enrolled == 'U') {
  128. if ($this->config->get('globalpay_remote_liability') != 1) {
  129. $this->load->language('extension/payment/globalpay_remote');
  130. $json['error'] = $this->language->get('error_3d_unable');
  131. $this->response->addHeader('Content-Type: application/json');
  132. $this->response->setOutput(json_encode($json));
  133. $this->response->output();
  134. die();
  135. } else {
  136. $eci_ref = 2;
  137. $xid = '';
  138. $cavv = '';
  139. if ($this->request->post['cc_type'] == 'mc') {
  140. $eci = 0;
  141. } else {
  142. $eci = 7;
  143. }
  144. }
  145. }
  146. // Invalid response from Enrollment Server. No shift in liability. ECI = 7
  147. if (isset($verify_3ds->result) && $verify_3ds->result >= 500 && $verify_3ds->result < 600) {
  148. if ($this->config->get('globalpay_remote_liability') != 1) {
  149. $this->load->language('extension/payment/globalpay_remote');
  150. $json['error'] = (string)$verify_3ds->message;
  151. $this->response->addHeader('Content-Type: application/json');
  152. $this->response->setOutput(json_encode($json));
  153. $this->response->output();
  154. die();
  155. } else {
  156. $eci_ref = 3;
  157. if ($this->request->post['cc_type'] == 'mc') {
  158. $eci = 0;
  159. } else {
  160. $eci = 7;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. $capture_result = $this->model_extension_payment_globalpay_remote->capturePayment(
  167. $account,
  168. $amount,
  169. $currency,
  170. $order_id,
  171. $order_ref,
  172. $this->request->post['cc_number'],
  173. $this->request->post['cc_expire_date_month'] . $this->request->post['cc_expire_date_year'],
  174. $this->request->post['cc_name'],
  175. $this->request->post['cc_type'],
  176. $this->request->post['cc_cvv2'],
  177. $this->request->post['cc_issue'],
  178. $eci_ref,
  179. $eci,
  180. $cavv,
  181. $xid
  182. );
  183. $this->model_extension_payment_globalpay_remote->logger('Capture result:\r\n' . print_r($capture_result, 1));
  184. if ($capture_result->result != '00') {
  185. $json['error'] = (string)$capture_result->message . ' (' . (int)$capture_result->result . ')';
  186. } else {
  187. $json['success'] = $this->url->link('checkout/success');
  188. }
  189. $this->response->addHeader('Content-Type: application/json');
  190. $this->response->setOutput(json_encode($json));
  191. }
  192. public function acsReturn() {
  193. if (isset($this->session->data['order_id'])) {
  194. $this->load->model('checkout/order');
  195. $this->load->model('extension/payment/globalpay_remote');
  196. $post = $this->request->post;
  197. $md = json_decode($this->encryption->decrypt($post['MD']), true);
  198. $signature_result = $this->model_extension_payment_globalpay_remote->enrollmentSignature($md['account'], $md['amount'], $md['currency'], $md['order_ref'], $md['cc_number'], $md['cc_expire'], $md['cc_type'], $md['cc_name'], $post['PaRes']);
  199. $this->model_extension_payment_globalpay_remote->logger('Signature result:\r\n' . print_r($signature_result, 1));
  200. if ($signature_result->result == '00' && (strtoupper($signature_result->threedsecure->status) == 'Y' || strtoupper($signature_result->threedsecure->status) == 'A')) {
  201. if (strtoupper($signature_result->threedsecure->status) == 'Y') {
  202. $eci_ref = 5;
  203. } else {
  204. $eci_ref = 6;
  205. }
  206. $eci = (string)$signature_result->threedsecure->eci;
  207. $cavv = (string)$signature_result->threedsecure->cavv;
  208. $xid = (string)$signature_result->threedsecure->xid;
  209. } else {
  210. if ($md['cc_type'] == 'mc') {
  211. $eci = 0;
  212. } else {
  213. $eci = 7;
  214. }
  215. // Enrolled but invalid response from ACS. No shift in liability. ECI = 7
  216. if ($signature_result->result == '110' && strtoupper($signature_result->threedsecure->status) == 'Y') {
  217. $eci_ref = 4;
  218. $cavv = (string)$signature_result->threedsecure->cavv;
  219. $xid = (string)$signature_result->threedsecure->xid;
  220. }
  221. // Incorrect password entered. No shift in liability. ECI = 7
  222. if ($signature_result->result == '00' && strtoupper($signature_result->threedsecure->status) == 'N') {
  223. $eci_ref = 7;
  224. $xid = (string)$signature_result->threedsecure->xid;
  225. $cavv = '';
  226. }
  227. // Authentication Unavailable. No shift in liability. ECI = 7
  228. if ($signature_result->result == '00' && strtoupper($signature_result->threedsecure->status) == 'U') {
  229. $eci_ref = 8;
  230. $xid = (string)$signature_result->threedsecure->xid;
  231. $cavv = '';
  232. }
  233. // Invalid response from ACS. No shift in liability. ECI = 7
  234. if (isset($signature_result->result) && $signature_result->result >= 500 && $signature_result->result < 600) {
  235. $eci_ref = 9;
  236. $xid = '';
  237. $cavv = '';
  238. }
  239. if ($this->config->get('globalpay_remote_liability') != 1) {
  240. // this is the check for liability shift - if the merchant does not want to accept, redirect to checkout with message
  241. $this->load->language('extension/payment/globalpay_remote');
  242. $message = $this->language->get('error_3d_unsuccessful');
  243. $message .= '<br /><strong>' . $this->language->get('text_eci') . ':</strong> (' . $eci . ') ' . $this->language->get('text_3d_s' . (int)$eci_ref);
  244. $message .= '<br /><strong>' . $this->language->get('text_timestamp') . ':</strong> ' . (string)strftime("%Y%m%d%H%M%S");
  245. $message .= '<br /><strong>' . $this->language->get('text_order_ref') . ':</strong> ' . (string)$md['order_ref'];
  246. if ($this->config->get('globalpay_remote_card_data_status') == 1) {
  247. $message .= '<br /><strong>' . $this->language->get('entry_cc_type') . ':</strong> ' . (string)$md['cc_type'];
  248. $message .= '<br /><strong>' . $this->language->get('text_last_digits') . ':</strong> ' . (string)substr($md['cc_number'], -4);
  249. $message .= '<br /><strong>' . $this->language->get('entry_cc_expire_date') . ':</strong> ' . (string)$md['cc_expire'];
  250. $message .= '<br /><strong>' . $this->language->get('entry_cc_name') . ':</strong> ' . (string)$md['cc_name'];
  251. }
  252. $this->model_extension_payment_globalpay_remote->addHistory($md['order_id'], $this->config->get('globalpay_remote_order_status_decline_id'), $message);
  253. $this->session->data['error'] = $this->language->get('error_3d_unsuccessful');
  254. $this->response->redirect($this->url->link('checkout/checkout', '', true));
  255. die();
  256. }
  257. }
  258. $capture_result = $this->model_extension_payment_globalpay_remote->capturePayment(
  259. $md['account'],
  260. $md['amount'],
  261. $md['currency'],
  262. $md['order_id'],
  263. $md['order_ref'],
  264. $md['cc_number'],
  265. $md['cc_expire'],
  266. $md['cc_name'],
  267. $md['cc_type'],
  268. $md['cc_cvv2'],
  269. $md['cc_issue'],
  270. $eci_ref,
  271. $eci,
  272. $cavv,
  273. $xid
  274. );
  275. $this->model_extension_payment_globalpay_remote->logger('Capture result:\r\n' . print_r($capture_result, 1));
  276. if ($capture_result->result != '00') {
  277. $this->session->data['error'] = (string)$capture_result->message . ' (' . (int)$capture_result->result . ')';
  278. $this->response->redirect($this->url->link('checkout/checkout', '', true));
  279. } else {
  280. $this->response->redirect($this->url->link('checkout/success'));
  281. }
  282. } else {
  283. $this->response->redirect($this->url->link('account/login', '', true));
  284. }
  285. }
  286. }