globalpay.php (11624B)
1 <?php 2 class ModelExtensionPaymentGlobalpay extends Model { 3 public function install() { 4 $this->db->query(" 5 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "globalpay_order` ( 6 `globalpay_order_id` INT(11) NOT NULL AUTO_INCREMENT, 7 `order_id` INT(11) NOT NULL, 8 `order_ref` CHAR(50) NOT NULL, 9 `order_ref_previous` CHAR(50) NOT NULL, 10 `pasref` VARCHAR(50) NOT NULL, 11 `pasref_previous` VARCHAR(50) NOT NULL, 12 `date_added` DATETIME NOT NULL, 13 `date_modified` DATETIME NOT NULL, 14 `capture_status` INT(1) DEFAULT NULL, 15 `void_status` INT(1) DEFAULT NULL, 16 `settle_type` INT(1) DEFAULT NULL, 17 `rebate_status` INT(1) DEFAULT NULL, 18 `currency_code` CHAR(3) NOT NULL, 19 `authcode` VARCHAR(30) NOT NULL, 20 `account` VARCHAR(30) NOT NULL, 21 `total` DECIMAL( 10, 2 ) NOT NULL, 22 PRIMARY KEY (`globalpay_order_id`) 23 ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;"); 24 25 $this->db->query(" 26 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "globalpay_order_transaction` ( 27 `globalpay_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT, 28 `globalpay_order_id` INT(11) NOT NULL, 29 `date_added` DATETIME NOT NULL, 30 `type` ENUM('auth', 'payment', 'rebate', 'void') DEFAULT NULL, 31 `amount` DECIMAL( 10, 2 ) NOT NULL, 32 PRIMARY KEY (`globalpay_order_transaction_id`) 33 ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;"); 34 } 35 36 public function void($order_id) { 37 $globalpay_order = $this->getOrder($order_id); 38 39 if (!empty($globalpay_order)) { 40 $timestamp = strftime("%Y%m%d%H%M%S"); 41 $merchant_id = $this->config->get('payment_globalpay_merchant_id'); 42 $secret = $this->config->get('payment_globalpay_secret'); 43 44 $this->logger('Void hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '...'); 45 46 $tmp = $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '...'; 47 $hash = sha1($tmp); 48 $tmp = $hash . '.' . $secret; 49 $hash = sha1($tmp); 50 51 $xml = ''; 52 $xml .= '<request type="void" timestamp="' . $timestamp . '">'; 53 $xml .= '<merchantid>' . $merchant_id . '</merchantid>'; 54 $xml .= '<account>' . $globalpay_order['account'] . '</account>'; 55 $xml .= '<orderid>' . $globalpay_order['order_ref'] . '</orderid>'; 56 $xml .= '<pasref>' . $globalpay_order['pasref'] . '</pasref>'; 57 $xml .= '<authcode>' . $globalpay_order['authcode'] . '</authcode>'; 58 $xml .= '<sha1hash>' . $hash . '</sha1hash>'; 59 $xml .= '</request>'; 60 61 $this->logger('Void XML request:\r\n' . print_r(simplexml_load_string($xml), 1)); 62 63 $ch = curl_init(); 64 curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi"); 65 curl_setopt($ch, CURLOPT_POST, 1); 66 curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION); 67 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 68 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 69 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 70 $response = curl_exec ($ch); 71 curl_close ($ch); 72 73 return simplexml_load_string($response); 74 } else { 75 return false; 76 } 77 } 78 79 public function updateVoidStatus($globalpay_order_id, $status) { 80 $this->db->query("UPDATE `" . DB_PREFIX . "globalpay_order` SET `void_status` = '" . (int)$status . "' WHERE `globalpay_order_id` = '" . (int)$globalpay_order_id . "'"); 81 } 82 83 public function capture($order_id, $amount) { 84 $globalpay_order = $this->getOrder($order_id); 85 86 if (!empty($globalpay_order) && $globalpay_order['capture_status'] == 0) { 87 $timestamp = strftime("%Y%m%d%H%M%S"); 88 $merchant_id = $this->config->get('payment_globalpay_merchant_id'); 89 $secret = $this->config->get('payment_globalpay_secret'); 90 91 if ($globalpay_order['settle_type'] == 2) { 92 $this->logger('Capture hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$globalpay_order['currency_code'] . '.'); 93 94 $tmp = $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$globalpay_order['currency_code'] . '.'; 95 $hash = sha1($tmp); 96 $tmp = $hash . '.' . $secret; 97 $hash = sha1($tmp); 98 99 $settle_type = 'multisettle'; 100 $xml_amount = '<amount currency="' . (string)$globalpay_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>'; 101 } else { 102 //$this->logger('Capture hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '...'); 103 $this->logger('Capture hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$globalpay_order['currency_code'] . '.'); 104 105 $tmp = $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$globalpay_order['currency_code'] . '.'; 106 $hash = sha1($tmp); 107 $tmp = $hash . '.' . $secret; 108 $hash = sha1($tmp); 109 110 $settle_type = 'settle'; 111 $xml_amount = '<amount currency="' . (string)$globalpay_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>'; 112 } 113 114 $xml = ''; 115 $xml .= '<request type="' . $settle_type . '" timestamp="' . $timestamp . '">'; 116 $xml .= '<merchantid>' . $merchant_id . '</merchantid>'; 117 $xml .= '<account>' . $globalpay_order['account'] . '</account>'; 118 $xml .= '<orderid>' . $globalpay_order['order_ref'] . '</orderid>'; 119 $xml .= $xml_amount; 120 $xml .= '<pasref>' . $globalpay_order['pasref'] . '</pasref>'; 121 $xml .= '<autosettle flag="1" />'; 122 $xml .= '<authcode>' . $globalpay_order['authcode'] . '</authcode>'; 123 $xml .= '<sha1hash>' . $hash . '</sha1hash>'; 124 $xml .= '</request>'; 125 126 $this->logger('Settle XML request:\r\n' . print_r(simplexml_load_string($xml), 1)); 127 128 $ch = curl_init(); 129 curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi"); 130 curl_setopt($ch, CURLOPT_POST, 1); 131 curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION); 132 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 133 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 134 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 135 $response = curl_exec ($ch); 136 curl_close ($ch); 137 138 return simplexml_load_string($response); 139 } else { 140 return false; 141 } 142 } 143 144 public function updateCaptureStatus($globalpay_order_id, $status) { 145 $this->db->query("UPDATE `" . DB_PREFIX . "globalpay_order` SET `capture_status` = '" . (int)$status . "' WHERE `globalpay_order_id` = '" . (int)$globalpay_order_id . "'"); 146 } 147 148 public function updateForRebate($globalpay_order_id, $pas_ref, $order_ref) { 149 $this->db->query("UPDATE `" . DB_PREFIX . "globalpay_order` SET `order_ref_previous` = '_multisettle_" . $this->db->escape($order_ref) . "', `pasref_previous` = '" . $this->db->escape($pas_ref) . "' WHERE `globalpay_order_id` = '" . (int)$globalpay_order_id . "' LIMIT 1"); 150 } 151 152 public function rebate($order_id, $amount) { 153 $globalpay_order = $this->getOrder($order_id); 154 155 if (!empty($globalpay_order) && $globalpay_order['rebate_status'] != 1) { 156 $timestamp = strftime("%Y%m%d%H%M%S"); 157 $merchant_id = $this->config->get('payment_globalpay_merchant_id'); 158 $secret = $this->config->get('payment_globalpay_secret'); 159 160 if ($globalpay_order['settle_type'] == 2) { 161 $order_ref = '_multisettle_' . $globalpay_order['order_ref']; 162 163 if (empty($globalpay_order['pasref_previous'])) { 164 $pas_ref = $globalpay_order['pasref']; 165 } else { 166 $pas_ref = $globalpay_order['pasref_previous']; 167 } 168 } else { 169 $order_ref = $globalpay_order['order_ref']; 170 $pas_ref = $globalpay_order['pasref']; 171 } 172 173 $this->logger('Rebate hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . (int)round($amount*100) . '.' . $globalpay_order['currency_code'] . '.'); 174 175 $tmp = $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . (int)round($amount*100) . '.' . $globalpay_order['currency_code'] . '.'; 176 $hash = sha1($tmp); 177 $tmp = $hash . '.' . $secret; 178 $hash = sha1($tmp); 179 180 $rebate_hash = sha1($this->config->get('payment_globalpay_rebate_password')); 181 182 $xml = ''; 183 $xml .= '<request type="rebate" timestamp="' . $timestamp . '">'; 184 $xml .= '<merchantid>' . $merchant_id . '</merchantid>'; 185 $xml .= '<account>' . $globalpay_order['account'] . '</account>'; 186 $xml .= '<orderid>' . $order_ref . '</orderid>'; 187 $xml .= '<pasref>' . $pas_ref . '</pasref>'; 188 $xml .= '<authcode>' . $globalpay_order['authcode'] . '</authcode>'; 189 $xml .= '<amount currency="' . (string)$globalpay_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>'; 190 $xml .= '<refundhash>' . $rebate_hash . '</refundhash>'; 191 $xml .= '<sha1hash>' . $hash . '</sha1hash>'; 192 $xml .= '</request>'; 193 194 $this->logger('Rebate XML request:\r\n' . print_r(simplexml_load_string($xml), 1)); 195 196 $ch = curl_init(); 197 curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi"); 198 curl_setopt($ch, CURLOPT_POST, 1); 199 curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION); 200 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 201 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 202 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 203 $response = curl_exec ($ch); 204 curl_close ($ch); 205 206 return simplexml_load_string($response); 207 } else { 208 return false; 209 } 210 } 211 212 public function updateRebateStatus($globalpay_order_id, $status) { 213 $this->db->query("UPDATE `" . DB_PREFIX . "globalpay_order` SET `rebate_status` = '" . (int)$status . "' WHERE `globalpay_order_id` = '" . (int)$globalpay_order_id . "'"); 214 } 215 216 public function getOrder($order_id) { 217 $this->logger('getOrder - ' . $order_id); 218 219 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "globalpay_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 220 221 if ($qry->num_rows) { 222 $order = $qry->row; 223 $order['transactions'] = $this->getTransactions($order['globalpay_order_id']); 224 225 $this->logger(print_r($order, 1)); 226 227 return $order; 228 } else { 229 return false; 230 } 231 } 232 233 private function getTransactions($globalpay_order_id) { 234 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "globalpay_order_transaction` WHERE `globalpay_order_id` = '" . (int)$globalpay_order_id . "'"); 235 236 if ($qry->num_rows) { 237 return $qry->rows; 238 } else { 239 return false; 240 } 241 } 242 243 public function addTransaction($globalpay_order_id, $type, $total) { 244 $this->db->query("INSERT INTO `" . DB_PREFIX . "globalpay_order_transaction` SET `globalpay_order_id` = '" . (int)$globalpay_order_id . "', `date_added` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . (float)$total . "'"); 245 } 246 247 public function logger($message) { 248 if ($this->config->get('payment_globalpay_debug') == 1) { 249 $log = new Log('globalpay.log'); 250 $log->write($message); 251 } 252 } 253 254 public function getTotalCaptured($globalpay_order_id) { 255 $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "globalpay_order_transaction` WHERE `globalpay_order_id` = '" . (int)$globalpay_order_id . "' AND (`type` = 'payment' OR `type` = 'rebate')"); 256 257 return (float)$query->row['total']; 258 } 259 260 public function getTotalRebated($globalpay_order_id) { 261 $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "globalpay_order_transaction` WHERE `globalpay_order_id` = '" . (int)$globalpay_order_id . "' AND 'rebate'"); 262 263 return (float)$query->row['total']; 264 } 265 }