shop.balmet.com

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

securetrading_ws.php (12919B)


      1 <?php
      2 class ModelExtensionPaymentSecureTradingWs extends Model {
      3 	public function install() {
      4 		$this->db->query("
      5 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "securetrading_ws_order` (
      6 			  `securetrading_ws_order_id` INT(11) NOT NULL AUTO_INCREMENT,
      7 			  `order_id` INT(11) NOT NULL,
      8 			  `md` varchar(1024) DEFAULT NULL,
      9 			  `transaction_reference` varchar(127) DEFAULT NULL,
     10 			  `created` DATETIME NOT NULL,
     11 			  `modified` DATETIME NOT NULL,
     12 			  `release_status` INT(1) DEFAULT NULL,
     13 			  `void_status` INT(1) DEFAULT NULL,
     14 			  `settle_type` INT(1) DEFAULT NULL,
     15 			  `rebate_status` INT(1) DEFAULT NULL,
     16 			  `currency_code` CHAR(3) NOT NULL,
     17 			  `total` DECIMAL( 10, 2 ) NOT NULL,
     18 			  PRIMARY KEY (`securetrading_ws_order_id`)
     19 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     20 
     21 		$this->db->query("
     22 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "securetrading_ws_order_transaction` (
     23 			  `securetrading_ws_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT,
     24 			  `securetrading_ws_order_id` INT(11) NOT NULL,
     25 			  `created` DATETIME NOT NULL,
     26 			  `type` ENUM('auth', 'payment', 'rebate', 'reversed') DEFAULT NULL,
     27 			  `amount` DECIMAL( 10, 2 ) NOT NULL,
     28 			  PRIMARY KEY (`securetrading_ws_order_transaction_id`)
     29 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     30 	}
     31 
     32 	public function uninstall() {
     33 		$this->db->query("DROP TABLE IF EXISTS " . DB_PREFIX . "securetrading_ws_order");
     34 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "securetrading_ws_order_transaction`;");
     35 	}
     36 
     37 	public function void($order_id) {
     38 		$securetrading_ws_order = $this->getOrder($order_id);
     39 
     40 		if (!empty($securetrading_ws_order) && $securetrading_ws_order['release_status'] == 0) {
     41 
     42 			$requestblock_xml = new SimpleXMLElement('<requestblock></requestblock>');
     43 			$requestblock_xml->addAttribute('version', '3.67');
     44 			$requestblock_xml->addChild('alias', $this->config->get('payment_securetrading_ws_username'));
     45 
     46 			$request_node = $requestblock_xml->addChild('request');
     47 			$request_node->addAttribute('type', 'TRANSACTIONUPDATE');
     48 
     49 			$filter_node = $request_node->addChild('filter');
     50 			$filter_node->addChild('sitereference', $this->config->get('payment_securetrading_ws_site_reference'));
     51 			$filter_node->addChild('transactionreference', $securetrading_ws_order['transaction_reference']);
     52 
     53 			$request_node->addChild('updates')->addChild('settlement')->addChild('settlestatus', 3);
     54 
     55 			return $this->call($requestblock_xml->asXML());
     56 		} else {
     57 			return false;
     58 		}
     59 	}
     60 
     61 	public function updateVoidStatus($securetrading_ws_order_id, $status) {
     62 		$this->db->query("UPDATE `" . DB_PREFIX . "securetrading_ws_order` SET `void_status` = '" . (int)$status . "' WHERE `securetrading_ws_order_id` = '" . (int)$securetrading_ws_order_id . "'");
     63 	}
     64 
     65 	public function release($order_id, $amount) {
     66 		$securetrading_ws_order = $this->getOrder($order_id);
     67 		$total_released = $this->getTotalReleased($securetrading_ws_order['securetrading_ws_order_id']);
     68 
     69 		if (!empty($securetrading_ws_order) && $securetrading_ws_order['release_status'] == 0 && $total_released <= $amount) {
     70 
     71 			$requestblock_xml = new SimpleXMLElement('<requestblock></requestblock>');
     72 			$requestblock_xml->addAttribute('version', '3.67');
     73 			$requestblock_xml->addChild('alias', $this->config->get('payment_securetrading_ws_username'));
     74 
     75 			$request_node = $requestblock_xml->addChild('request');
     76 			$request_node->addAttribute('type', 'TRANSACTIONUPDATE');
     77 
     78 			$filter_node = $request_node->addChild('filter');
     79 			$filter_node->addChild('sitereference', $this->config->get('payment_securetrading_ws_site_reference'));
     80 			$filter_node->addChild('transactionreference', $securetrading_ws_order['transaction_reference']);
     81 
     82 			$settlement_node = $request_node->addChild('updates')->addChild('settlement');
     83 			$settlement_node->addChild('settlestatus', 0);
     84 			$settlement_node->addChild('settlemainamount', $amount)->addAttribute('currencycode', $securetrading_ws_order['currency_code']);
     85 
     86 			return $this->call($requestblock_xml->asXML());
     87 		} else {
     88 			return false;
     89 		}
     90 	}
     91 
     92 	public function updateReleaseStatus($securetrading_ws_order_id, $status) {
     93 		$this->db->query("UPDATE `" . DB_PREFIX . "securetrading_ws_order` SET `release_status` = '" . (int)$status . "' WHERE `securetrading_ws_order_id` = '" . (int)$securetrading_ws_order_id . "'");
     94 	}
     95 
     96 	public function updateForRebate($securetrading_ws_order_id, $order_ref) {
     97 		$this->db->query("UPDATE `" . DB_PREFIX . "securetrading_ws_order` SET `order_ref_previous` = '_multisettle_" . $this->db->escape($order_ref) . "' WHERE `securetrading_ws_order_id` = '" . (int)$securetrading_ws_order_id . "' LIMIT 1");
     98 	}
     99 
    100 	public function rebate($order_id, $refunded_amount) {
    101 		$securetrading_ws_order = $this->getOrder($order_id);
    102 
    103 		if (!empty($securetrading_ws_order) && $securetrading_ws_order['rebate_status'] != 1) {
    104 
    105 			$requestblock_xml = new SimpleXMLElement('<requestblock></requestblock>');
    106 			$requestblock_xml->addAttribute('version', '3.67');
    107 			$requestblock_xml->addChild('alias', $this->config->get('payment_securetrading_ws_username'));
    108 
    109 			$request_node = $requestblock_xml->addChild('request');
    110 			$request_node->addAttribute('type', 'REFUND');
    111 
    112 			$request_node->addChild('merchant')->addChild('orderreference', $order_id);
    113 
    114 			$operation_node = $request_node->addChild('operation');
    115 			$operation_node->addChild('accounttypedescription', 'ECOM');
    116 			$operation_node->addChild('parenttransactionreference', $securetrading_ws_order['transaction_reference']);
    117 			$operation_node->addChild('sitereference', $this->config->get('payment_securetrading_ws_site_reference'));
    118 
    119 			$billing_node = $request_node->addChild('billing');
    120 			$billing_node->addAttribute('currencycode', $securetrading_ws_order['currency_code']);
    121 			$billing_node->addChild('amount', str_replace('.', '', $refunded_amount));
    122 
    123 			return $this->call($requestblock_xml->asXML());
    124 		} else {
    125 			return false;
    126 		}
    127 	}
    128 
    129 	public function getOrder($order_id) {
    130 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "securetrading_ws_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
    131 
    132 		if ($qry->num_rows) {
    133 			$order = $qry->row;
    134 			$order['transactions'] = $this->getTransactions($order['securetrading_ws_order_id']);
    135 
    136 			return $order;
    137 		} else {
    138 			return false;
    139 		}
    140 	}
    141 
    142 	private function getTransactions($securetrading_ws_order_id) {
    143 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "securetrading_ws_order_transaction` WHERE `securetrading_ws_order_id` = '" . (int)$securetrading_ws_order_id . "'");
    144 
    145 		if ($qry->num_rows) {
    146 			return $qry->rows;
    147 		} else {
    148 			return false;
    149 		}
    150 	}
    151 
    152 	public function addTransaction($securetrading_ws_order_id, $type, $total) {
    153 		$this->db->query("INSERT INTO `" . DB_PREFIX . "securetrading_ws_order_transaction` SET `securetrading_ws_order_id` = '" . (int)$securetrading_ws_order_id . "', `created` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . (double)$total . "'");
    154 	}
    155 
    156 	public function getTotalReleased($securetrading_ws_order_id) {
    157 		$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "securetrading_ws_order_transaction` WHERE `securetrading_ws_order_id` = '" . (int)$securetrading_ws_order_id . "' AND (`type` = 'payment' OR `type` = 'rebate')");
    158 
    159 		return (double)$query->row['total'];
    160 	}
    161 
    162 	public function getTotalRebated($securetrading_ws_order_id) {
    163 		$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "securetrading_ws_order_transaction` WHERE `securetrading_ws_order_id` = '" . (int)$securetrading_ws_order_id . "' AND 'rebate'");
    164 
    165 		return (double)$query->row['total'];
    166 	}
    167 
    168 	public function increaseRefundedAmount($order_id, $amount) {
    169 		$this->db->query("UPDATE " . DB_PREFIX . "securetrading_ws_order SET refunded = refunded + " . (double)$amount . " WHERE order_id = " . (int)$order_id);
    170 	}
    171 
    172 	public function getCsv($data) {
    173 		$ch = curl_init();
    174 
    175 		$post_data = array();
    176 		$post_data['sitereferences'] = $this->config->get('payment_securetrading_ws_site_reference');
    177 		$post_data['startdate'] = $data['date_from'];
    178 		$post_data['enddate'] = $data['date_to'];
    179 		$post_data['accounttypedescriptions'] = 'ECOM';
    180 
    181 		if ($data['detail']) {
    182 			$post_data['optionalfields'] = array(
    183 				'parenttransactionreference',
    184 				'accounttypedescription',
    185 				'requesttypedescription',
    186 				'mainamount',
    187 				'currencyiso3a',
    188 				'errorcode',
    189 				'authcode',
    190 				'customerip',
    191 				'fraudrating',
    192 				'orderreference',
    193 				'paymenttypedescription',
    194 				'maskedpan',
    195 				'expirydate',
    196 				'settlestatus',
    197 				'settlemainamount',
    198 				'settleduedate',
    199 				'securityresponsesecuritycode',
    200 				'securityresponseaddress',
    201 				'securityresponsepostcode',
    202 				'billingprefixname',
    203 				'billingfirstname',
    204 				'billingmiddlename',
    205 				'billinglastname',
    206 				'billingpremise',
    207 				'billingstreet',
    208 				'billingtown',
    209 				'billingcounty',
    210 				'billingemail',
    211 				'billingcountryiso2a',
    212 				'billingpostcode',
    213 				'billingtelephones',
    214 				'customerprefixname',
    215 				'customerfirstname',
    216 				'customermiddlename',
    217 				'customerlastname',
    218 				'customerpremise',
    219 				'customerstreet',
    220 				'customertown',
    221 				'customercounty',
    222 				'customeremail',
    223 				'customercountryiso2a',
    224 				'customerpostcode',
    225 				'customertelephones',
    226 			);
    227 		} else {
    228 			$post_data['optionalfields'] = array(
    229 				'orderreference',
    230 				'currencyiso3a',
    231 				'errorcode',
    232 				'paymenttypedescription',
    233 				'settlestatus',
    234 				'requesttypedescription',
    235 				'mainamount',
    236 				'billingfirstname',
    237 				'billinglastname',
    238 			);
    239 		}
    240 
    241 		if (isset($data['currency']) && !empty($data['currency'])) {
    242 			$post_data['currencyiso3as'] = $data['currency'];
    243 		}
    244 
    245 		if (isset($data['status']) && !empty($data['status'])) {
    246 			$post_data['errorcodes'] = $data['status'];
    247 		}
    248 
    249 		if (isset($data['payment_type']) && !empty($data['payment_type'])) {
    250 			$post_data['paymenttypedescriptions'] = $data['payment_type'];
    251 		}
    252 
    253 		if (isset($data['request']) && !empty($data['request'])) {
    254 			$post_data['requesttypedescriptions'] = $data['request'];
    255 		}
    256 
    257 		if (isset($data['settle_status']) && !empty($data['settle_status'])) {
    258 			$post_data['settlestatuss'] = $data['settle_status'];
    259 		}
    260 
    261 		$defaults = array(
    262 			CURLOPT_POST => 1,
    263 			CURLOPT_HEADER => 0,
    264 			CURLOPT_SSL_VERIFYPEER => 0,
    265 			CURLOPT_URL => 'https://myst.securetrading.net/auto/transactions/transactionsearch',
    266 			CURLOPT_FRESH_CONNECT => 1,
    267 			CURLOPT_RETURNTRANSFER => 1,
    268 			CURLOPT_FORBID_REUSE => 1,
    269 			CURLOPT_TIMEOUT => 15,
    270 			CURLOPT_HTTPHEADER => array(
    271 				'User-Agent: OpenCart - Secure Trading WS',
    272 				'Authorization: Basic ' . base64_encode($this->config->get('payment_securetrading_ws_csv_username') . ':' . $this->config->get('payment_securetrading_ws_csv_password')),
    273 			),
    274 			CURLOPT_POSTFIELDS => $this->encodePost($post_data),
    275 		);
    276 
    277 		curl_setopt_array($ch, $defaults);
    278 
    279 		$response = curl_exec($ch);
    280 
    281 		if ($response === false) {
    282 			$this->log->write('Secure Trading WS CURL Error: (' . curl_errno($ch) . ') ' . curl_error($ch));
    283 		}
    284 
    285 		curl_close($ch);
    286 
    287 		if (empty($response) || $response === 'No records found for search') {
    288 			return false;
    289 		}
    290 
    291 		if (preg_match('/401 Authorization Required/', $response)) {
    292 			return false;
    293 		}
    294 
    295 		return $response;
    296 	}
    297 
    298 	private function encodePost($data) {
    299 		$params = array();
    300 
    301 		foreach ($data as $key => $value) {
    302 			if (is_array($value)) {
    303 				foreach ($value as $v) {
    304 					$params[] = $key . '=' . rawurlencode($v);
    305 				}
    306 			} else {
    307 				$params[] = $key . '=' . rawurlencode($value);
    308 			}
    309 		}
    310 
    311 		return implode('&', $params);
    312 	}
    313 
    314 	public function call($data) {
    315 		$ch = curl_init();
    316 
    317 		$defaults = array(
    318 			CURLOPT_POST => 1,
    319 			CURLOPT_HEADER => 0,
    320 			CURLOPT_SSL_VERIFYPEER => 0,
    321 			CURLOPT_URL => 'https://webservices.securetrading.net/xml/',
    322 			CURLOPT_FRESH_CONNECT => 1,
    323 			CURLOPT_RETURNTRANSFER => 1,
    324 			CURLOPT_FORBID_REUSE => 1,
    325 			CURLOPT_TIMEOUT => 15,
    326 			CURLOPT_HTTPHEADER => array(
    327 				'User-Agent: OpenCart - Secure Trading WS',
    328 				'Content-Length: ' . strlen($data),
    329 				'Authorization: Basic ' . base64_encode($this->config->get('payment_securetrading_ws_username') . ':' . $this->config->get('payment_securetrading_ws_password')),
    330 			),
    331 			CURLOPT_POSTFIELDS => $data,
    332 		);
    333 
    334 		curl_setopt_array($ch, $defaults);
    335 
    336 		$response = curl_exec($ch);
    337 
    338 		if ($response === false) {
    339 			$this->log->write('Secure Trading WS CURL Error: (' . curl_errno($ch) . ') ' . curl_error($ch));
    340 		}
    341 
    342 		curl_close($ch);
    343 
    344 		return $response;
    345 	}
    346 
    347 	public function logger($message) {
    348 		$log = new Log('securetrading_ws.log');
    349 		$log->write($message);
    350 	}
    351 }