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

/catalog/controller/payment/globalpay_remote.php

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