PageRenderTime 58ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/controller/payment/worldpay.php

https://gitlab.com/shapcy/opencart
PHP | 220 lines | 173 code | 46 blank | 1 comment | 27 complexity | 26803c1ec23f6018a9eec74a895f2b05 MD5 | raw file
  1. <?php
  2. class ControllerPaymentWorldpay extends Controller {
  3. public function index() {
  4. $this->load->language('payment/worldpay');
  5. $data['text_credit_card'] = $this->language->get('text_credit_card');
  6. $data['text_loading'] = $this->language->get('text_loading');
  7. $data['text_card_type'] = $this->language->get('text_card_type');
  8. $data['text_card_name'] = $this->language->get('text_card_name');
  9. $data['text_card_digits'] = $this->language->get('text_card_digits');
  10. $data['text_card_expiry'] = $this->language->get('text_card_expiry');
  11. $data['text_confirm_delete'] = $this->language->get('text_confirm_delete');
  12. $data['entry_card'] = $this->language->get('entry_card');
  13. $data['entry_card_existing'] = $this->language->get('entry_card_existing');
  14. $data['entry_card_new'] = $this->language->get('entry_card_new');
  15. $data['entry_card_save'] = $this->language->get('entry_card_save');
  16. $data['entry_cc_cvc'] = $this->language->get('entry_cc_cvc');
  17. $data['entry_cc_choice'] = $this->language->get('entry_cc_choice');
  18. $data['button_delete_card'] = $this->language->get('button_delete_card');
  19. $data['button_confirm'] = $this->language->get('button_confirm');
  20. $data['worldpay_script'] = 'https://cdn.worldpay.com/v1/worldpay.js';
  21. $data['worldpay_client_key'] = $this->config->get('worldpay_client_key');
  22. $data['form_submit'] = $this->url->link('payment/worldpay/send', '', true);
  23. if ($this->config->get('worldpay_card') == '1' && $this->customer->isLogged()) {
  24. $data['worldpay_card'] = true;
  25. } else {
  26. $data['worldpay_card'] = false;
  27. }
  28. $data['existing_cards'] = array();
  29. if ($this->customer->isLogged() && $data['worldpay_card']) {
  30. $this->load->model('payment/worldpay');
  31. $data['existing_cards'] = $this->model_payment_worldpay->getCards($this->customer->getId());
  32. }
  33. $recurring_products = $this->cart->getRecurringProducts();
  34. if (!empty($recurring_products)) {
  35. $data['recurring_products'] = true;
  36. }
  37. return $this->load->view('payment/worldpay', $data);
  38. }
  39. public function send() {
  40. $this->load->language('payment/worldpay');
  41. $this->load->model('checkout/order');
  42. $this->load->model('localisation/country');
  43. $this->load->model('payment/worldpay');
  44. $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
  45. $recurring_products = $this->cart->getRecurringProducts();
  46. if (empty($recurring_products)) {
  47. $order_type = 'ECOM';
  48. } else {
  49. $order_type = 'RECURRING';
  50. }
  51. $country_info = $this->model_localisation_country->getCountry($order_info['payment_country_id']);
  52. $billing_address = array(
  53. "address1" => $order_info['payment_address_1'],
  54. "address2" => $order_info['payment_address_2'],
  55. "address3" => '',
  56. "postalCode" => $order_info['payment_postcode'],
  57. "city" => $order_info['payment_city'],
  58. "state" => $order_info['payment_zone'],
  59. "countryCode" => $country_info['iso_code_2'],
  60. );
  61. $order = array(
  62. "token" => $this->request->post['token'],
  63. "orderType" => $order_type,
  64. "amount" => round($this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false)*100),
  65. "currencyCode" => $order_info['currency_code'],
  66. "name" => $order_info['firstname'] . ' ' . $order_info['lastname'],
  67. "orderDescription" => $order_info['store_name'] . ' - ' . date('Y-m-d H:i:s'),
  68. "customerOrderCode" => $order_info['order_id'],
  69. "billingAddress" => $billing_address
  70. );
  71. $this->model_payment_worldpay->logger($order);
  72. $response_data = $this->model_payment_worldpay->sendCurl('orders', $order);
  73. $this->model_payment_worldpay->logger($response_data);
  74. if (isset($response_data->paymentStatus) && $response_data->paymentStatus == 'SUCCESS') {
  75. $this->model_checkout_order->addOrderHistory($order_info['order_id'], $this->config->get('config_order_status_id'));
  76. $worldpay_order_id = $this->model_payment_worldpay->addOrder($order_info, $response_data->orderCode);
  77. $this->model_payment_worldpay->addTransaction($worldpay_order_id, 'payment', $order_info);
  78. if (isset($this->request->post['save-card'])) {
  79. $response = $this->model_payment_worldpay->sendCurl('tokens/' . $this->request->post['token']);
  80. $this->model_payment_worldpay->logger($response);
  81. $expiry_date = mktime(0, 0, 0, 0, (string)$response->paymentMethod->expiryMonth, (string)$response->paymentMethod->expiryYear);
  82. if (isset($response->paymentMethod)) {
  83. $card_data = array();
  84. $card_data['customer_id'] = $this->customer->getId();
  85. $card_data['Token'] = $response->token;
  86. $card_data['Last4Digits'] = (string)$response->paymentMethod->maskedCardNumber;
  87. $card_data['ExpiryDate'] = date("m/y", $expiry_date);
  88. $card_data['CardType'] = (string)$response->paymentMethod->cardType;
  89. $this->model_payment_worldpay->addCard($this->session->data['order_id'], $card_data);
  90. }
  91. }
  92. //loop through any products that are recurring items
  93. foreach ($recurring_products as $item) {
  94. $this->model_payment_worldpay->recurringPayment($item, $this->session->data['order_id'] . rand(), $this->request->post['token']);
  95. }
  96. $this->response->redirect($this->url->link('checkout/success', '', true));
  97. } else {
  98. $this->session->data['error'] = $this->language->get('error_process_order');
  99. $this->response->redirect($this->url->link('checkout/checkout', '', true));
  100. }
  101. }
  102. public function deleteCard() {
  103. $this->load->language('payment/worldpay');
  104. $this->load->model('payment/worldpay');
  105. if (isset($this->request->post['token'])) {
  106. if ($this->model_payment_worldpay->deleteCard($this->request->post['token'])) {
  107. $json['success'] = $this->language->get('text_card_success');
  108. } else {
  109. $json['error'] = $this->language->get('text_card_error');
  110. }
  111. if (count($this->model_payment_worldpay->getCards($this->customer->getId()))) {
  112. $json['existing_cards'] = true;
  113. }
  114. } else {
  115. $json['error'] = $this->language->get('text_error');
  116. }
  117. $this->response->addHeader('Content-Type: application/json');
  118. $this->response->setOutput(json_encode($json));
  119. }
  120. public function webhook() {
  121. if (isset($this->request->get['token']) && hash_equals($this->config->get('worldpay_secret_token'), $this->request->get['token'])) {
  122. $this->load->model('payment/worldpay');
  123. $message = json_decode(file_get_contents('php://input'), true);
  124. if (isset($message['orderCode'])) {
  125. $order = $this->model_payment_worldpay->getWorldpayOrder($message['orderCode']);
  126. $this->model_payment_worldpay->logger($order);
  127. switch ($message['paymentStatus']) {
  128. case 'SUCCESS':
  129. $order_status_id = $this->config->get('worldpay_entry_success_status_id');
  130. break;
  131. case 'FAILED':
  132. $order_status_id = $this->config->get('worldpay_entry_failed_status_id');
  133. break;
  134. case 'SETTLED':
  135. $order_status_id = $this->config->get('worldpay_entry_settled_status_id');
  136. break;
  137. case 'REFUNDED':
  138. $order_status_id = $this->config->get('worldpay_refunded_status_id');
  139. break;
  140. case 'PARTIALLY_REFUNDED':
  141. $order_status_id = $this->config->get('worldpay_entry_partially_refunded_status_id');
  142. break;
  143. case 'CHARGED_BACK':
  144. $order_status_id = $this->config->get('worldpay_entry_charged_back_status_id');
  145. break;
  146. case 'INFORMATION_REQUESTED':
  147. $order_status_id = $this->config->get('worldpay_entry_information_requested_status_id');
  148. break;
  149. case 'INFORMATION_SUPPLIED':
  150. $order_status_id = $this->config->get('worldpay_entry_information_supplied_status_id');
  151. break;
  152. case 'CHARGEBACK_REVERSED':
  153. $order_status_id = $this->config->get('worldpay_entry_chargeback_reversed_status_id');
  154. break;
  155. }
  156. $this->model_payment_worldpay->logger($order_status_id);
  157. if (isset($order['order_id'])) {
  158. $this->load->model('checkout/order');
  159. $this->model_checkout_order->addOrderHistory($order['order_id'], $order_status_id);
  160. }
  161. }
  162. }
  163. $this->response->addHeader('HTTP/1.1 200 OK');
  164. $this->response->addHeader('Content-Type: application/json');
  165. }
  166. public function cron() {
  167. if ($this->request->get['token'] == $this->config->get('worldpay_cron_job_token')) {
  168. $this->load->model('payment/worldpay');
  169. $orders = $this->model_payment_worldpay->cronPayment();
  170. $this->model_payment_worldpay->updateCronJobRunTime();
  171. $this->model_payment_worldpay->logger($orders);
  172. }
  173. }
  174. }