realex_remote.php (11545B)
1 <?php 2 class ModelExtensionPaymentRealexRemote extends Model { 3 public function install() { 4 $this->db->query(" 5 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "realex_remote_order` ( 6 `realex_remote_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 (`realex_remote_order_id`) 23 ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;"); 24 25 $this->db->query(" 26 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "realex_remote_order_transaction` ( 27 `realex_remote_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT, 28 `realex_remote_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 (`realex_remote_order_transaction_id`) 33 ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;"); 34 } 35 36 public function void($order_id) { 37 $realex_order = $this->getOrder($order_id); 38 39 if (!empty($realex_order)) { 40 $timestamp = strftime("%Y%m%d%H%M%S"); 41 $merchant_id = $this->config->get('payment_realex_remote_merchant_id'); 42 $secret = $this->config->get('payment_realex_remote_secret'); 43 44 $this->logger('Void hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $realex_order['order_ref'] . '...'); 45 46 $tmp = $timestamp . '.' . $merchant_id . '.' . $realex_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>' . $realex_order['account'] . '</account>'; 55 $xml .= '<orderid>' . $realex_order['order_ref'] . '</orderid>'; 56 $xml .= '<pasref>' . $realex_order['pasref'] . '</pasref>'; 57 $xml .= '<authcode>' . $realex_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($realex_remote_order_id, $status) { 80 $this->db->query("UPDATE `" . DB_PREFIX . "realex_remote_order` SET `void_status` = '" . (int)$status . "' WHERE `realex_remote_order_id` = '" . (int)$realex_remote_order_id . "'"); 81 } 82 83 public function capture($order_id, $amount) { 84 $realex_order = $this->getOrder($order_id); 85 86 if (!empty($realex_order) && $realex_order['capture_status'] == 0) { 87 $timestamp = strftime("%Y%m%d%H%M%S"); 88 $merchant_id = $this->config->get('payment_realex_remote_merchant_id'); 89 $secret = $this->config->get('payment_realex_remote_secret'); 90 91 if ($realex_order['settle_type'] == 2) { 92 $this->logger('Capture hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $realex_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$realex_order['currency_code'] . '.'); 93 94 $tmp = $timestamp . '.' . $merchant_id . '.' . $realex_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$realex_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)$realex_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>'; 101 } else { 102 //$this->logger('Capture hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $realex_order['order_ref'] . '...'); 103 $this->logger('Capture hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $realex_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$realex_order['currency_code'] . '.'); 104 105 $tmp = $timestamp . '.' . $merchant_id . '.' . $realex_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$realex_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)$realex_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>' . $realex_order['account'] . '</account>'; 118 $xml .= '<orderid>' . $realex_order['order_ref'] . '</orderid>'; 119 $xml .= $xml_amount; 120 $xml .= '<pasref>' . $realex_order['pasref'] . '</pasref>'; 121 $xml .= '<authcode>' . $realex_order['authcode'] . '</authcode>'; 122 $xml .= '<sha1hash>' . $hash . '</sha1hash>'; 123 $xml .= '</request>'; 124 125 $this->logger('Settle XML request:\r\n' . print_r(simplexml_load_string($xml), 1)); 126 127 $ch = curl_init(); 128 curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi"); 129 curl_setopt($ch, CURLOPT_POST, 1); 130 curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION); 131 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 132 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 133 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 134 $response = curl_exec ($ch); 135 curl_close ($ch); 136 137 return simplexml_load_string($response); 138 } else { 139 return false; 140 } 141 } 142 143 public function updateCaptureStatus($realex_remote_order_id, $status) { 144 $this->db->query("UPDATE `" . DB_PREFIX . "realex_remote_order` SET `capture_status` = '" . (int)$status . "' WHERE `realex_remote_order_id` = '" . (int)$realex_remote_order_id . "'"); 145 } 146 147 public function updateForRebate($realex_remote_order_id, $pas_ref, $order_ref) { 148 $this->db->query("UPDATE `" . DB_PREFIX . "realex_remote_order` SET `order_ref_previous` = '_multisettle_" . $this->db->escape($order_ref) . "', `pasref_previous` = '" . $this->db->escape($pas_ref) . "' WHERE `realex_remote_order_id` = '" . (int)$realex_remote_order_id . "' LIMIT 1"); 149 } 150 151 public function rebate($order_id, $amount) { 152 $realex_order = $this->getOrder($order_id); 153 154 if (!empty($realex_order) && $realex_order['rebate_status'] != 1) { 155 $timestamp = strftime("%Y%m%d%H%M%S"); 156 $merchant_id = $this->config->get('payment_realex_remote_merchant_id'); 157 $secret = $this->config->get('payment_realex_remote_secret'); 158 159 if ($realex_order['settle_type'] == 2) { 160 $order_ref = '_multisettle_' . $realex_order['order_ref']; 161 162 if (empty($realex_order['pasref_previous'])) { 163 $pas_ref = $realex_order['pasref']; 164 } else { 165 $pas_ref = $realex_order['pasref_previous']; 166 } 167 } else { 168 $order_ref = $realex_order['order_ref']; 169 $pas_ref = $realex_order['pasref']; 170 } 171 172 $this->logger('Rebate hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . (int)round($amount*100) . '.' . $realex_order['currency_code'] . '.'); 173 174 $tmp = $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . (int)round($amount*100) . '.' . $realex_order['currency_code'] . '.'; 175 $hash = sha1($tmp); 176 $tmp = $hash . '.' . $secret; 177 $hash = sha1($tmp); 178 179 $rebatehash = sha1($this->config->get('payment_realex_remote_rebate_password')); 180 181 $xml = ''; 182 $xml .= '<request type="rebate" timestamp="' . $timestamp . '">'; 183 $xml .= '<merchantid>' . $merchant_id . '</merchantid>'; 184 $xml .= '<account>' . $realex_order['account'] . '</account>'; 185 $xml .= '<orderid>' . $order_ref . '</orderid>'; 186 $xml .= '<pasref>' . $pas_ref . '</pasref>'; 187 $xml .= '<authcode>' . $realex_order['authcode'] . '</authcode>'; 188 $xml .= '<amount currency="' . (string)$realex_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>'; 189 $xml .= '<refundhash>' . $rebatehash . '</refundhash>'; 190 $xml .= '<sha1hash>' . $hash . '</sha1hash>'; 191 $xml .= '</request>'; 192 193 $this->logger('Rebate XML request:\r\n' . print_r(simplexml_load_string($xml), 1)); 194 195 $ch = curl_init(); 196 curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi"); 197 curl_setopt($ch, CURLOPT_POST, 1); 198 curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION); 199 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 200 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 201 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 202 $response = curl_exec ($ch); 203 curl_close ($ch); 204 205 return simplexml_load_string($response); 206 } else { 207 return false; 208 } 209 } 210 211 public function updateRebateStatus($realex_remote_order_id, $status) { 212 $this->db->query("UPDATE `" . DB_PREFIX . "realex_remote_order` SET `rebate_status` = '" . (int)$status . "' WHERE `realex_remote_order_id` = '" . (int)$realex_remote_order_id . "'"); 213 } 214 215 public function getOrder($order_id) { 216 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "realex_remote_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 217 218 if ($qry->num_rows) { 219 $order = $qry->row; 220 $order['transactions'] = $this->getTransactions($order['realex_remote_order_id']); 221 222 return $order; 223 } else { 224 return false; 225 } 226 } 227 228 private function getTransactions($realex_remote_order_id) { 229 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "realex_remote_order_transaction` WHERE `realex_remote_order_id` = '" . (int)$realex_remote_order_id . "'"); 230 231 if ($qry->num_rows) { 232 return $qry->rows; 233 } else { 234 return false; 235 } 236 } 237 238 public function addTransaction($realex_remote_order_id, $type, $total) { 239 $this->db->query("INSERT INTO `" . DB_PREFIX . "realex_remote_order_transaction` SET `realex_remote_order_id` = '" . (int)$realex_remote_order_id . "', `date_added` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . (float)$total . "'"); 240 } 241 242 public function logger($message) { 243 if ($this->config->get('payment_realex_remote_debug') == 1) { 244 $log = new Log('realex_remote.log'); 245 $log->write($message); 246 } 247 } 248 249 public function getTotalCaptured($realex_order_id) { 250 $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "realex_remote_order_transaction` WHERE `realex_remote_order_id` = '" . (int)$realex_order_id . "' AND (`type` = 'payment' OR `type` = 'rebate')"); 251 252 return (float)$query->row['total']; 253 } 254 255 public function getTotalRebated($realex_order_id) { 256 $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "realex_remote_order_transaction` WHERE `realex_remote_order_id` = '" . (int)$realex_order_id . "' AND 'rebate'"); 257 258 return (double)$query->row['total']; 259 } 260 }