worldpay.php (7890B)
1 <?php 2 class ControllerExtensionPaymentWorldpay extends Controller { 3 public function index() { 4 $this->load->language('extension/payment/worldpay'); 5 6 $data['worldpay_script'] = 'https://cdn.worldpay.com/v1/worldpay.js'; 7 8 $data['worldpay_client_key'] = $this->config->get('payment_worldpay_client_key'); 9 10 $data['form_submit'] = $this->url->link('extension/payment/worldpay/send', '', true); 11 12 if ($this->config->get('worldpay_card') == '1' && $this->customer->isLogged()) { 13 $data['worldpay_card'] = true; 14 } else { 15 $data['worldpay_card'] = false; 16 } 17 18 $data['existing_cards'] = array(); 19 if ($this->customer->isLogged() && $data['worldpay_card']) { 20 $this->load->model('extension/payment/worldpay'); 21 $data['existing_cards'] = $this->model_extension_payment_worldpay->getCards($this->customer->getId()); 22 } 23 24 $recurring_products = $this->cart->getRecurringProducts(); 25 26 if (!empty($recurring_products)) { 27 $data['recurring_products'] = true; 28 } 29 30 return $this->load->view('extension/payment/worldpay', $data); 31 } 32 33 public function send() { 34 $this->load->language('extension/payment/worldpay'); 35 $this->load->model('checkout/order'); 36 $this->load->model('localisation/country'); 37 $this->load->model('extension/payment/worldpay'); 38 39 $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); 40 41 $recurring_products = $this->cart->getRecurringProducts(); 42 43 if (empty($recurring_products)) { 44 $order_type = 'ECOM'; 45 } else { 46 $order_type = 'RECURRING'; 47 } 48 49 $country_info = $this->model_localisation_country->getCountry($order_info['payment_country_id']); 50 51 $billing_address = array( 52 "address1" => $order_info['payment_address_1'], 53 "address2" => $order_info['payment_address_2'], 54 "address3" => '', 55 "postalCode" => $order_info['payment_postcode'], 56 "city" => $order_info['payment_city'], 57 "state" => $order_info['payment_zone'], 58 "countryCode" => $country_info['iso_code_2'], 59 ); 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 72 $this->model_extension_payment_worldpay->logger($order); 73 74 $response_data = $this->model_extension_payment_worldpay->sendCurl('orders', $order); 75 76 $this->model_extension_payment_worldpay->logger($response_data); 77 78 if (isset($response_data->paymentStatus) && $response_data->paymentStatus == 'SUCCESS') { 79 $this->model_checkout_order->addOrderHistory($order_info['order_id'], $this->config->get('config_order_status_id')); 80 81 $worldpay_order_id = $this->model_extension_payment_worldpay->addOrder($order_info, $response_data->orderCode); 82 83 $this->model_extension_payment_worldpay->addTransaction($worldpay_order_id, 'payment', $order_info); 84 85 if (isset($this->request->post['save-card'])) { 86 $response = $this->model_extension_payment_worldpay->sendCurl('tokens/' . $this->request->post['token']); 87 88 $this->model_extension_payment_worldpay->logger($response); 89 90 $expiry_date = mktime(0, 0, 0, 0, (string)$response->paymentMethod->expiryMonth, (string)$response->paymentMethod->expiryYear); 91 92 if (isset($response->paymentMethod)) { 93 $card_data = array(); 94 $card_data['customer_id'] = $this->customer->getId(); 95 $card_data['Token'] = $response->token; 96 $card_data['Last4Digits'] = (string)$response->paymentMethod->maskedCardNumber; 97 $card_data['ExpiryDate'] = date("m/y", $expiry_date); 98 $card_data['CardType'] = (string)$response->paymentMethod->cardType; 99 $this->model_extension_payment_worldpay->addCard($this->session->data['order_id'], $card_data); 100 } 101 } 102 103 //loop through any products that are recurring items 104 foreach ($recurring_products as $item) { 105 $this->model_extension_payment_worldpay->recurringPayment($item, $this->session->data['order_id'] . rand(), $this->request->post['token']); 106 } 107 108 $this->response->redirect($this->url->link('checkout/success', '', true)); 109 } else { 110 111 $this->session->data['error'] = $this->language->get('error_process_order'); 112 $this->response->redirect($this->url->link('checkout/checkout', '', true)); 113 } 114 } 115 116 public function deleteCard() { 117 $this->load->language('extension/payment/worldpay'); 118 $this->load->model('extension/payment/worldpay'); 119 120 if (isset($this->request->post['token'])) { 121 if ($this->model_extension_payment_worldpay->deleteCard($this->request->post['token'])) { 122 $json['success'] = $this->language->get('text_card_success'); 123 } else { 124 $json['error'] = $this->language->get('text_card_error'); 125 } 126 127 if (count($this->model_extension_payment_worldpay->getCards($this->customer->getId()))) { 128 $json['existing_cards'] = true; 129 } 130 } else { 131 $json['error'] = $this->language->get('text_error'); 132 } 133 134 $this->response->addHeader('Content-Type: application/json'); 135 $this->response->setOutput(json_encode($json)); 136 } 137 138 public function webhook() { 139 if (isset($this->request->get['token']) && hash_equals($this->config->get('worldpay_secret_token'), $this->request->get['token'])) { 140 $this->load->model('extension/payment/worldpay'); 141 $message = json_decode(file_get_contents('php://input'), true); 142 143 if (isset($message['orderCode'])) { 144 $order = $this->model_extension_payment_worldpay->getWorldpayOrder($message['orderCode']); 145 $this->model_extension_payment_worldpay->logger($order); 146 switch ($message['paymentStatus']) { 147 case 'SUCCESS': 148 $order_status_id = $this->config->get('worldpay_entry_success_status_id'); 149 break; 150 case 'FAILED': 151 $order_status_id = $this->config->get('worldpay_entry_failed_status_id'); 152 break; 153 case 'SETTLED': 154 $order_status_id = $this->config->get('worldpay_entry_settled_status_id'); 155 break; 156 case 'REFUNDED': 157 $order_status_id = $this->config->get('worldpay_refunded_status_id'); 158 break; 159 case 'PARTIALLY_REFUNDED': 160 $order_status_id = $this->config->get('worldpay_entry_partially_refunded_status_id'); 161 break; 162 case 'CHARGED_BACK': 163 $order_status_id = $this->config->get('worldpay_entry_charged_back_status_id'); 164 break; 165 case 'INFORMATION_REQUESTED': 166 $order_status_id = $this->config->get('worldpay_entry_information_requested_status_id'); 167 break; 168 case 'INFORMATION_SUPPLIED': 169 $order_status_id = $this->config->get('worldpay_entry_information_supplied_status_id'); 170 break; 171 case 'CHARGEBACK_REVERSED': 172 $order_status_id = $this->config->get('worldpay_entry_chargeback_reversed_status_id'); 173 break; 174 } 175 176 $this->model_extension_payment_worldpay->logger($order_status_id); 177 if (isset($order['order_id'])) { 178 $this->load->model('checkout/order'); 179 $this->model_checkout_order->addOrderHistory($order['order_id'], $order_status_id); 180 } 181 } 182 } 183 184 $this->response->addHeader('HTTP/1.1 200 OK'); 185 $this->response->addHeader('Content-Type: application/json'); 186 } 187 188 public function cron() { 189 if ($this->request->get['token'] == $this->config->get('worldpay_cron_job_token')) { 190 $this->load->model('extension/payment/worldpay'); 191 192 $orders = $this->model_extension_payment_worldpay->cronPayment(); 193 194 $this->model_extension_payment_worldpay->updateCronJobRunTime(); 195 196 $this->model_extension_payment_worldpay->logger($orders); 197 } 198 } 199 200 }