shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

realex.php (11345B)


      1 <?php
      2 class ModelExtensionPaymentRealex extends Model {
      3 	public function install() {
      4 		$this->db->query("
      5 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "realex_order` (
      6 			  `realex_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_order_id`)
     23 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     24 
     25 		$this->db->query("
     26 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "realex_order_transaction` (
     27 			  `realex_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT,
     28 			  `realex_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_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_merchant_id');
     42 			$secret = $this->config->get('payment_realex_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_order_id, $status) {
     80 		$this->db->query("UPDATE `" . DB_PREFIX . "realex_order` SET `void_status` = '" . (int)$status . "' WHERE `realex_order_id` = '" . (int)$realex_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_merchant_id');
     89 			$secret = $this->config->get('payment_realex_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 .= '<autosettle flag="1" />';
    122 			$xml .= '<authcode>' . $realex_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($realex_order_id, $status) {
    145 		$this->db->query("UPDATE `" . DB_PREFIX . "realex_order` SET `capture_status` = '" . (int)$status . "' WHERE `realex_order_id` = '" . (int)$realex_order_id . "'");
    146 	}
    147 
    148 	public function updateForRebate($realex_order_id, $pas_ref, $order_ref) {
    149 		$this->db->query("UPDATE `" . DB_PREFIX . "realex_order` SET `order_ref_previous` = '_multisettle_" . $this->db->escape($order_ref) . "', `pasref_previous` = '" . $this->db->escape($pas_ref) . "' WHERE `realex_order_id` = '" . (int)$realex_order_id . "' LIMIT 1");
    150 	}
    151 
    152 	public function rebate($order_id, $amount) {
    153 		$realex_order = $this->getOrder($order_id);
    154 
    155 		if (!empty($realex_order) && $realex_order['rebate_status'] != 1) {
    156 			$timestamp = strftime("%Y%m%d%H%M%S");
    157 			$merchant_id = $this->config->get('payment_realex_merchant_id');
    158 			$secret = $this->config->get('payment_realex_secret');
    159 
    160 			if ($realex_order['settle_type'] == 2) {
    161 				$order_ref = '_multisettle_' . $realex_order['order_ref'];
    162 
    163 				if (empty($realex_order['pasref_previous'])) {
    164 					$pas_ref = $realex_order['pasref'];
    165 				} else {
    166 					$pas_ref = $realex_order['pasref_previous'];
    167 				}
    168 			} else {
    169 				$order_ref = $realex_order['order_ref'];
    170 				$pas_ref = $realex_order['pasref'];
    171 			}
    172 
    173 			$this->logger('Rebate hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . (int)round($amount*100) . '.' . $realex_order['currency_code'] . '.');
    174 
    175 			$tmp = $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . (int)round($amount*100) . '.' . $realex_order['currency_code'] . '.';
    176 			$hash = sha1($tmp);
    177 			$tmp = $hash . '.' . $secret;
    178 			$hash = sha1($tmp);
    179 
    180 			$rebate_hash = sha1($this->config->get('payment_realex_rebate_password'));
    181 
    182 			$xml = '';
    183 			$xml .= '<request type="rebate" timestamp="' . $timestamp . '">';
    184 			$xml .= '<merchantid>' . $merchant_id . '</merchantid>';
    185 			$xml .= '<account>' . $realex_order['account'] . '</account>';
    186 			$xml .= '<orderid>' . $order_ref . '</orderid>';
    187 			$xml .= '<pasref>' . $pas_ref . '</pasref>';
    188 			$xml .= '<authcode>' . $realex_order['authcode'] . '</authcode>';
    189 			$xml .= '<amount currency="' . (string)$realex_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($realex_order_id, $status) {
    213 		$this->db->query("UPDATE `" . DB_PREFIX . "realex_order` SET `rebate_status` = '" . (int)$status . "' WHERE `realex_order_id` = '" . (int)$realex_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 . "realex_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['realex_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($realex_order_id) {
    234 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "realex_order_transaction` WHERE `realex_order_id` = '" . (int)$realex_order_id . "'");
    235 
    236 		if ($qry->num_rows) {
    237 			return $qry->rows;
    238 		} else {
    239 			return false;
    240 		}
    241 	}
    242 
    243 	public function addTransaction($realex_order_id, $type, $total) {
    244 		$this->db->query("INSERT INTO `" . DB_PREFIX . "realex_order_transaction` SET `realex_order_id` = '" . (int)$realex_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_realex_debug') == 1) {
    249 			$log = new Log('realex.log');
    250 			$log->write($message);
    251 		}
    252 	}
    253 
    254 	public function getTotalCaptured($realex_order_id) {
    255 		$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "realex_order_transaction` WHERE `realex_order_id` = '" . (int)$realex_order_id . "' AND (`type` = 'payment' OR `type` = 'rebate')");
    256 
    257 		return (float)$query->row['total'];
    258 	}
    259 
    260 	public function getTotalRebated($realex_order_id) {
    261 		$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "realex_order_transaction` WHERE `realex_order_id` = '" . (int)$realex_order_id . "' AND 'rebate'");
    262 
    263 		return (float)$query->row['total'];
    264 	}
    265 }