cardconnect.php (12741B)
1 <?php 2 class ModelExtensionPaymentCardConnect extends Model { 3 public function install() { 4 $this->db->query(" 5 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "cardconnect_card` ( 6 `cardconnect_card_id` INT(11) NOT NULL AUTO_INCREMENT, 7 `cardconnect_order_id` INT(11) NOT NULL DEFAULT '0', 8 `customer_id` INT(11) NOT NULL DEFAULT '0', 9 `profileid` VARCHAR(16) NOT NULL DEFAULT '', 10 `token` VARCHAR(19) NOT NULL DEFAULT '', 11 `type` VARCHAR(50) NOT NULL DEFAULT '', 12 `account` VARCHAR(4) NOT NULL DEFAULT '', 13 `expiry` VARCHAR(4) NOT NULL DEFAULT '', 14 `date_added` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', 15 PRIMARY KEY (`cardconnect_card_id`) 16 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci"); 17 18 $this->db->query(" 19 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "cardconnect_order` ( 20 `cardconnect_order_id` INT(11) NOT NULL AUTO_INCREMENT, 21 `order_id` INT(11) NOT NULL DEFAULT '0', 22 `customer_id` INT(11) NOT NULL DEFAULT '0', 23 `payment_method` VARCHAR(255) NOT NULL DEFAULT '', 24 `retref` VARCHAR(12) NOT NULL DEFAULT '', 25 `authcode` VARCHAR(6) NOT NULL DEFAULT '', 26 `currency_code` VARCHAR(3) NOT NULL DEFAULT '', 27 `total` DECIMAL(10, 2) NOT NULL DEFAULT '0.00', 28 `date_added` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', 29 PRIMARY KEY (`cardconnect_order_id`) 30 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci"); 31 32 $this->db->query(" 33 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "cardconnect_order_transaction` ( 34 `cardconnect_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT, 35 `cardconnect_order_id` INT(11) NOT NULL DEFAULT '0', 36 `type` VARCHAR(50) NOT NULL DEFAULT '', 37 `retref` VARCHAR(12) NOT NULL DEFAULT '', 38 `amount` DECIMAL(10, 2) NOT NULL DEFAULT '0.00', 39 `status` VARCHAR(255) NOT NULL DEFAULT '', 40 `date_modified` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', 41 `date_added` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', 42 PRIMARY KEY (`cardconnect_order_transaction_id`) 43 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci"); 44 } 45 46 public function uninstall() { 47 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "cardconnect_card`"); 48 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "cardconnect_order`"); 49 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "cardconnect_order_transaction`"); 50 51 $this->log('Module uninstalled'); 52 } 53 54 public function getOrder($order_id) { 55 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cardconnect_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 56 57 if ($query->num_rows) { 58 $order = $query->row; 59 60 $order['transactions'] = $this->getTransactions($order['cardconnect_order_id']); 61 62 return $order; 63 } else { 64 return false; 65 } 66 } 67 68 private function getTransactions($cardconnect_order_id) { 69 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cardconnect_order_transaction` WHERE `cardconnect_order_id` = '" . (int)$cardconnect_order_id . "'"); 70 71 if ($query->num_rows) { 72 return $query->rows; 73 } else { 74 return array(); 75 } 76 } 77 78 public function getTotalCaptured($cardconnect_order_id) { 79 $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "cardconnect_order_transaction` WHERE `cardconnect_order_id` = '" . (int)$cardconnect_order_id . "' AND (`type` = 'payment' OR `type` = 'refund')"); 80 81 return (float)$query->row['total']; 82 } 83 84 public function inquire($order_info, $retref) { 85 $this->log('Posting inquire to CardConnect'); 86 87 $this->log('Order ID: ' . $order_info['order_id']); 88 89 $url = 'https://' . $this->config->get('cardconnect_site') . '.cardconnect.com:' . (($this->config->get('cardconnect_environment') == 'live') ? 8443 : 6443) . '/cardconnect/rest/inquire/' . $retref . '/' . $this->config->get('payment_cardconnect_merchant_id'); 90 91 $header = array(); 92 93 $header[] = 'Content-type: application/json'; 94 $header[] = 'Authorization: Basic ' . base64_encode($this->config->get('cardconnect_api_username') . ':' . $this->config->get('cardconnect_api_password')); 95 96 $this->model_extension_payment_cardconnect->log('Header: ' . print_r($header, true)); 97 98 $this->model_extension_payment_cardconnect->log('URL: ' . $url); 99 100 $ch = curl_init(); 101 curl_setopt($ch, CURLOPT_URL, $url); 102 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 103 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 104 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 105 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 106 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 107 $response_data = curl_exec($ch); 108 if (curl_errno($ch)) { 109 $this->model_extension_payment_cardconnect->log('cURL error: ' . curl_errno($ch)); 110 } 111 curl_close($ch); 112 113 $response_data = json_decode($response_data, true); 114 115 $this->log('Response: ' . print_r($response_data, true)); 116 117 return $response_data; 118 } 119 120 public function capture($order_info, $amount) { 121 $this->load->model('sale/order'); 122 123 $this->log('Posting capture to CardConnect'); 124 125 $this->log('Order ID: ' . $order_info['order_id']); 126 127 $order = $this->model_sale_order->getOrder($order_info['order_id']); 128 129 $totals = $this->model_sale_order->getOrderTotals($order_info['order_id']); 130 131 $shipping_cost = ''; 132 133 foreach($totals as $total) { 134 if ($total['code'] == 'shipping') { 135 $shipping_cost = $total['value']; 136 } 137 } 138 139 $products = $this->model_sale_order->getOrderProducts($order_info['order_id']); 140 141 $items = array(); 142 143 $i = 1; 144 145 foreach ($products as $product) { 146 $items[] = array( 147 'lineno' => $i, 148 'material' => '', 149 'description' => $product['name'], 150 'upc' => '', 151 'quantity' => $product['quantity'], 152 'uom' => '', 153 'unitcost' => $product['price'], 154 'netamnt' => $product['total'], 155 'taxamnt' => $product['tax'], 156 'discamnt' => '' 157 ); 158 159 $i++; 160 } 161 162 $data = array( 163 'merchid' => $this->config->get('payment_cardconnect_merchant_id'), 164 'retref' => $order_info['retref'], 165 'authcode' => $order_info['authcode'], 166 'ponumber' => $order_info['order_id'], 167 'amount' => round(floatval($amount), 2, PHP_ROUND_HALF_DOWN), 168 'currency' => $order_info['currency_code'], 169 'frtamnt' => $shipping_cost, 170 'dutyamnt' => '', 171 'orderdate' => '', 172 'shiptozip' => $order['shipping_postcode'], 173 'shipfromzip' => '', 174 'shiptocountry' => $order['shipping_iso_code_2'], 175 'Items' => $items 176 ); 177 178 $data_json = json_encode($data); 179 180 $url = 'https://' . $this->config->get('cardconnect_site') . '.cardconnect.com:' . (($this->config->get('cardconnect_environment') == 'live') ? 8443 : 6443) . '/cardconnect/rest/capture'; 181 182 $header = array(); 183 184 $header[] = 'Content-type: application/json'; 185 $header[] = 'Content-length: ' . strlen($data_json); 186 $header[] = 'Authorization: Basic ' . base64_encode($this->config->get('cardconnect_api_username') . ':' . $this->config->get('cardconnect_api_password')); 187 188 $this->model_extension_payment_cardconnect->log('Header: ' . print_r($header, true)); 189 190 $this->model_extension_payment_cardconnect->log('Post Data: ' . print_r($data, true)); 191 192 $this->model_extension_payment_cardconnect->log('URL: ' . $url); 193 194 $ch = curl_init(); 195 curl_setopt($ch, CURLOPT_URL, $url); 196 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 197 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 198 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); 199 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 200 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 201 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 202 $response_data = curl_exec($ch); 203 if (curl_errno($ch)) { 204 $this->model_extension_payment_cardconnect->log('cURL error: ' . curl_errno($ch)); 205 } 206 curl_close($ch); 207 208 $response_data = json_decode($response_data, true); 209 210 $this->log('Response: ' . print_r($response_data, true)); 211 212 return $response_data; 213 } 214 215 public function refund($order_info, $amount) { 216 $this->log('Posting refund to CardConnect'); 217 218 $this->log('Order ID: ' . $order_info['order_id']); 219 220 $data = array( 221 'merchid' => $this->config->get('payment_cardconnect_merchant_id'), 222 'amount' => round(floatval($amount), 2, PHP_ROUND_HALF_DOWN), 223 'currency' => $order_info['currency_code'], 224 'retref' => $order_info['retref'] 225 ); 226 227 $data_json = json_encode($data); 228 229 $url = 'https://' . $this->config->get('cardconnect_site') . '.cardconnect.com:' . (($this->config->get('cardconnect_environment') == 'live') ? 8443 : 6443) . '/cardconnect/rest/refund'; 230 231 $header = array(); 232 233 $header[] = 'Content-type: application/json'; 234 $header[] = 'Content-length: ' . strlen($data_json); 235 $header[] = 'Authorization: Basic ' . base64_encode($this->config->get('cardconnect_api_username') . ':' . $this->config->get('cardconnect_api_password')); 236 237 $this->model_extension_payment_cardconnect->log('Header: ' . print_r($header, true)); 238 239 $this->model_extension_payment_cardconnect->log('Post Data: ' . print_r($data, true)); 240 241 $this->model_extension_payment_cardconnect->log('URL: ' . $url); 242 243 $ch = curl_init(); 244 curl_setopt($ch, CURLOPT_URL, $url); 245 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 246 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 247 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); 248 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 249 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 250 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 251 $response_data = curl_exec($ch); 252 if (curl_errno($ch)) { 253 $this->model_extension_payment_cardconnect->log('cURL error: ' . curl_errno($ch)); 254 } 255 curl_close($ch); 256 257 $response_data = json_decode($response_data, true); 258 259 $this->log('Response: ' . print_r($response_data, true)); 260 261 return $response_data; 262 } 263 264 public function void($order_info, $retref) { 265 $this->log('Posting void to CardConnect'); 266 267 $this->log('Order ID: ' . $order_info['order_id']); 268 269 $data = array( 270 'merchid' => $this->config->get('payment_cardconnect_merchant_id'), 271 'amount' => 0, 272 'currency' => $order_info['currency_code'], 273 'retref' => $retref 274 ); 275 276 $data_json = json_encode($data); 277 278 $url = 'https://' . $this->config->get('cardconnect_site') . '.cardconnect.com:' . (($this->config->get('cardconnect_environment') == 'live') ? 8443 : 6443) . '/cardconnect/rest/void'; 279 280 $header = array(); 281 282 $header[] = 'Content-type: application/json'; 283 $header[] = 'Content-length: ' . strlen($data_json); 284 $header[] = 'Authorization: Basic ' . base64_encode($this->config->get('cardconnect_api_username') . ':' . $this->config->get('cardconnect_api_password')); 285 286 $this->model_extension_payment_cardconnect->log('Header: ' . print_r($header, true)); 287 288 $this->model_extension_payment_cardconnect->log('Post Data: ' . print_r($data, true)); 289 290 $this->model_extension_payment_cardconnect->log('URL: ' . $url); 291 292 $ch = curl_init(); 293 curl_setopt($ch, CURLOPT_URL, $url); 294 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 295 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 296 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); 297 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 298 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 299 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 300 $response_data = curl_exec($ch); 301 if (curl_errno($ch)) { 302 $this->model_extension_payment_cardconnect->log('cURL error: ' . curl_errno($ch)); 303 } 304 curl_close($ch); 305 306 $response_data = json_decode($response_data, true); 307 308 $this->log('Response: ' . print_r($response_data, true)); 309 310 return $response_data; 311 } 312 313 public function updateTransactionStatusByRetref($retref, $status) { 314 $this->db->query("UPDATE `" . DB_PREFIX . "cardconnect_order_transaction` SET `status` = '" . $this->db->escape($status) . "', `date_modified` = NOW() WHERE `retref` = '" . $this->db->escape($retref) . "'"); 315 } 316 317 public function addTransaction($cardconnect_order_id, $type, $retref, $amount, $status) { 318 $this->db->query("INSERT INTO `" . DB_PREFIX . "cardconnect_order_transaction` SET `cardconnect_order_id` = '" . (int)$cardconnect_order_id . "', `type` = '" . $this->db->escape($type) . "', `retref` = '" . $this->db->escape($retref) . "', `amount` = '" . (float)$amount . "', `status` = '" . $this->db->escape($status) . "', `date_modified` = NOW(), `date_added` = NOW()"); 319 } 320 321 public function log($data) { 322 if ($this->config->get('cardconnect_logging')) { 323 $log = new Log('cardconnect.log'); 324 325 $log->write($data); 326 } 327 } 328 }