firstdata.php (11668B)
1 <?php 2 class ControllerExtensionPaymentFirstdata extends Controller { 3 public function index() { 4 $this->load->language('extension/payment/firstdata'); 5 6 $this->load->model('checkout/order'); 7 $this->load->model('extension/payment/firstdata'); 8 9 $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); 10 11 if ($this->config->get('payment_firstdata_live_demo') == 1) { 12 $data['action'] = $this->config->get('payment_firstdata_live_url'); 13 } else { 14 $data['action'] = $this->config->get('payment_firstdata_demo_url'); 15 } 16 17 $data['amount'] = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false); 18 $data['currency'] = $this->model_extension_payment_firstdata->mapCurrency($order_info['currency_code']); 19 $data['merchant_id'] = $this->config->get('payment_firstdata_merchant_id'); 20 $data['timestamp'] = date('Y:m:d-H:i:s'); 21 $data['order_id'] = 'CON-' . $this->session->data['order_id'] . 'T' . $data['timestamp'] . mt_rand(1, 999); 22 $data['url_success'] = $this->url->link('checkout/success', '', true); 23 $data['url_fail'] = $this->url->link('extension/payment/firstdata/fail', '', true); 24 $data['url_notify'] = $this->url->link('extension/payment/firstdata/notify', '', true); 25 26 if (preg_match("/Mobile|Android|BlackBerry|iPhone|Windows Phone/", $this->request->server['HTTP_USER_AGENT'])) { 27 $data['mobile'] = true; 28 } else { 29 $data['mobile'] = false; 30 } 31 32 if ($this->config->get('payment_firstdata_auto_settle') == 1) { 33 $data['txntype'] = 'sale'; 34 } else { 35 $data['txntype'] = 'preauth'; 36 } 37 38 $tmp = $data['merchant_id'] . $data['timestamp'] . $data['amount'] . $data['currency'] . $this->config->get('payment_firstdata_secret'); 39 $ascii = bin2hex($tmp); 40 $data['hash'] = sha1($ascii); 41 42 $data['version'] = 'OPENCART-C-' . VERSION; 43 44 $data['bcompany'] = $order_info['payment_company']; 45 $data['bname'] = $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname']; 46 $data['baddr1'] = substr($order_info['payment_address_1'], 0, 30); 47 $data['baddr2'] = substr($order_info['payment_address_2'], 0, 30); 48 $data['bcity'] = substr($order_info['payment_city'], 0, 30); 49 $data['bstate'] = substr($order_info['payment_zone'], 0, 30); 50 $data['bcountry'] = $order_info['payment_iso_code_2']; 51 $data['bzip'] = $order_info['payment_postcode']; 52 $data['email'] = $order_info['email']; 53 54 if ($this->cart->hasShipping()) { 55 $data['sname'] = $order_info['shipping_firstname'] . ' ' . $order_info['shipping_lastname']; 56 $data['saddr1'] = substr($order_info['shipping_address_1'], 0, 30); 57 $data['saddr2'] = substr($order_info['shipping_address_2'], 0, 30); 58 $data['scity'] = substr($order_info['shipping_city'], 0, 30); 59 $data['sstate'] = substr($order_info['shipping_zone'], 0, 30); 60 $data['scountry'] = $order_info['shipping_iso_code_2']; 61 $data['szip'] = $order_info['shipping_postcode']; 62 } else { 63 $data['sname'] = $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname']; 64 $data['saddr1'] = substr($order_info['payment_address_1'], 0, 30); 65 $data['saddr2'] = substr($order_info['payment_address_2'], 0, 30); 66 $data['scity'] = substr($order_info['payment_city'], 0, 30); 67 $data['sstate'] = substr($order_info['payment_zone'], 0, 30); 68 $data['scountry'] = $order_info['payment_iso_code_2']; 69 $data['szip'] = $order_info['payment_postcode']; 70 } 71 72 if ($this->config->get('payment_firstdata_card_storage') == 1 && $this->customer->isLogged()) { 73 $data['card_storage'] = 1; 74 $data['stored_cards'] = $this->model_extension_payment_firstdata->getStoredCards(); 75 $data['new_hosted_id'] = sha1($this->customer->getId() . '-' . date("Y-m-d-H-i-s") . rand(10, 500)); 76 } else { 77 $data['card_storage'] = 0; 78 $data['stored_cards'] = array(); 79 } 80 81 return $this->load->view('extension/payment/firstdata', $data); 82 } 83 84 public function notify() { 85 $this->load->model('extension/payment/firstdata'); 86 87 $this->load->model('checkout/order'); 88 89 $this->load->language('extension/payment/firstdata'); 90 91 $message = ''; 92 93 if ($this->config->get('payment_firstdata_debug') == 1) { 94 $this->model_extension_payment_firstdata->logger(print_r($this->request->post, 1)); 95 } 96 97 if (isset($this->request->post['txntype']) && isset($this->request->post['notification_hash']) && isset($this->request->post['oid'])) { 98 $local_hash = $this->model_extension_payment_firstdata->responseHash($this->request->post['chargetotal'], $this->request->post['currency'], $this->request->post['txndatetime'], $this->request->post['approval_code']); 99 100 if ($local_hash == $this->request->post['notification_hash']) { 101 $order_id_parts = explode('T', $this->request->post['oid']); 102 103 $order_id = str_replace("CON-","",$order_id_parts[0]); 104 105 $order_info = $this->model_checkout_order->getOrder($order_id); 106 107 if ($this->request->post['txntype'] == 'preauth' || $this->request->post['txntype'] == 'sale') { 108 if (isset($this->request->post['approval_code'])) { 109 $response_parts = explode(':', $this->request->post['approval_code']); 110 111 $address_codes = array( 112 'PPX' => $this->language->get('text_address_ppx'), 113 'YYY' => $this->language->get('text_address_yyy'), 114 'YNA' => $this->language->get('text_address_yna'), 115 'NYZ' => $this->language->get('text_address_nyz'), 116 'NNN' => $this->language->get('text_address_nnn'), 117 'YPX' => $this->language->get('text_address_ypx'), 118 'PYX' => $this->language->get('text_address_pyx'), 119 'XXU' => $this->language->get('text_address_xxu') 120 ); 121 122 $cvv_codes = array( 123 'M' => $this->language->get('text_card_code_m'), 124 'N' => $this->language->get('text_card_code_n'), 125 'P' => $this->language->get('text_card_code_p'), 126 'S' => $this->language->get('text_card_code_s'), 127 'U' => $this->language->get('text_card_code_u'), 128 'X' => $this->language->get('text_card_code_x'), 129 'NONE' => $this->language->get('text_card_code_blank') 130 ); 131 132 $card_types = array( 133 'M' => $this->language->get('text_card_type_m'), 134 'V' => $this->language->get('text_card_type_v'), 135 'C' => $this->language->get('text_card_type_c'), 136 'A' => $this->language->get('text_card_type_a'), 137 'MA' => $this->language->get('text_card_type_ma'), 138 'MAESTROUK' => $this->language->get('text_card_type_mauk') 139 ); 140 141 if ($response_parts[0] == 'Y') { 142 if (isset($response_parts[3])) { 143 if (strlen($response_parts[3]) == 4) { 144 $address_pass = strtoupper(substr($response_parts[3], 0, 3)); 145 $cvv_pass = strtoupper(substr($response_parts[3], -1)); 146 147 if (!array_key_exists($cvv_pass, $cvv_codes)) { 148 $cvv_pass = 'NONE'; 149 } 150 } else { 151 $address_pass = $response_parts[3]; 152 $cvv_pass = 'NONE'; 153 } 154 155 $message .= $this->language->get('text_address_response') . $address_codes[$address_pass] . '<br />'; 156 $message .= $this->language->get('text_card_code_verify') . $cvv_codes[$cvv_pass] . '<br />'; 157 $message .= $this->language->get('text_response_code_full') . $this->request->post['approval_code'] . '<br />'; 158 $message .= $this->language->get('text_response_code') . $response_parts[1] . '<br />'; 159 160 if (isset($this->request->post['cardnumber'])) { 161 $message .= $this->language->get('text_response_card') . $this->request->post['cardnumber'] . '<br />'; 162 } 163 164 if (isset($this->request->post['processor_response_code'])) { 165 $message .= $this->language->get('text_response_proc_code') . $this->request->post['processor_response_code'] . '<br />'; 166 } 167 168 if (isset($this->request->post['refnumber'])) { 169 $message .= $this->language->get('text_response_ref') . $this->request->post['refnumber'] . '<br />'; 170 } 171 172 if (isset($this->request->post['paymentMethod'])) { 173 $message .= $this->language->get('text_response_card_type') . $card_types[strtoupper($this->request->post['paymentMethod'])] . '<br />'; 174 } 175 } 176 177 if (isset($this->request->post['hosteddataid']) && $order_info['customer_id'] != 0) { 178 $this->model_extension_payment_firstdata->storeCard($this->request->post['hosteddataid'], $order_info['customer_id'], $this->request->post['expmonth'], $this->request->post['expyear'], $this->request->post['cardnumber']); 179 } 180 181 $fd_order_id = $this->model_extension_payment_firstdata->addOrder($order_info, $this->request->post['oid'], $this->request->post['tdate']); 182 183 if ($this->config->get('payment_firstdata_auto_settle') == 1) { 184 $this->model_extension_payment_firstdata->addTransaction($fd_order_id, 'payment', $order_info); 185 186 $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_firstdata_order_status_success_settled_id'), $message, false); 187 } else { 188 $this->model_extension_payment_firstdata->addTransaction($fd_order_id, 'auth'); 189 190 $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_firstdata_order_status_success_unsettled_id'), $message, false); 191 } 192 } else { 193 $message = $this->request->post['fail_reason'] . '<br />'; 194 $message .= $this->language->get('text_response_code_full') . $this->request->post['approval_code']; 195 196 $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_firstdata_order_status_decline_id'), $message); 197 } 198 } 199 } 200 201 if ($this->request->post['txntype'] == 'void') { 202 if ($this->request->post['status'] == 'DECLINED') { 203 $fd_order = $this->model_extension_payment_firstdata->getOrder($order_id); 204 205 $this->model_extension_payment_firstdata->updateVoidStatus($order_id, 1); 206 207 $this->model_extension_payment_firstdata->addTransaction($fd_order['firstdata_order_id'], 'void'); 208 209 $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_firstdata_order_status_void_id'), $message, false); 210 } 211 } 212 213 if ($this->request->post['txntype'] == 'postauth') { 214 if ($this->request->post['status'] == 'APPROVED') { 215 $fd_order = $this->model_extension_payment_firstdata->getOrder($order_id); 216 217 $this->model_extension_payment_firstdata->updateCaptureStatus($order_id, 1); 218 219 $this->model_extension_payment_firstdata->addTransaction($fd_order['firstdata_order_id'], 'payment', $order_info); 220 221 $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_firstdata_order_status_success_settled_id'), $message, false); 222 } 223 } 224 } else { 225 $this->model_extension_payment_firstdata->logger('Hash does not match! Received: ' . $this->request->post['notification_hash'] . ', calculated: ' . $local_hash); 226 } 227 } else { 228 $this->model_extension_payment_firstdata->logger('Data is missing from request . '); 229 } 230 } 231 232 public function fail() { 233 $this->load->language('extension/payment/firstdata'); 234 235 if (isset($this->request->post['fail_reason']) && !empty($this->request->post['fail_reason'])) { 236 $this->session->data['error'] = $this->request->post['fail_reason']; 237 } else { 238 $this->session->data['error'] = $this->language->get('error_failed'); 239 } 240 241 $this->response->redirect($this->url->link('checkout/checkout', '', true)); 242 } 243 }