PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/catalog/controller/payment/realex_remote.php

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