shop.balmet.com

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

firstdata.php (9216B)


      1 <?php
      2 class ModelExtensionPaymentFirstdata extends Model {
      3 	public function install() {
      4 		$this->db->query("
      5 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "firstdata_order` (
      6 			  `firstdata_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 			  `tdate` DATETIME NOT NULL,
     13 			  `date_added` DATETIME NOT NULL,
     14 			  `date_modified` DATETIME NOT NULL,
     15 			  `capture_status` INT(1) DEFAULT NULL,
     16 			  `void_status` INT(1) DEFAULT NULL,
     17 			  `currency_code` CHAR(3) NOT NULL,
     18 			  `authcode` VARCHAR(30) NOT NULL,
     19 			  `account` VARCHAR(30) NOT NULL,
     20 			  `total` DECIMAL( 10, 2 ) NOT NULL,
     21 			  PRIMARY KEY (`firstdata_order_id`)
     22 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     23 
     24 		$this->db->query("
     25 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "firstdata_order_transaction` (
     26 			  `firstdata_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT,
     27 			  `firstdata_order_id` INT(11) NOT NULL,
     28 			  `date_added` DATETIME NOT NULL,
     29 			  `type` ENUM('auth', 'payment', 'void') DEFAULT NULL,
     30 			  `amount` DECIMAL( 10, 2 ) NOT NULL,
     31 			  PRIMARY KEY (`firstdata_order_transaction_id`)
     32 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     33 
     34 		$this->db->query("
     35 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "firstdata_card` (
     36 			  `firstdata_card_id` INT(11) NOT NULL AUTO_INCREMENT,
     37 			  `customer_id` INT(11) NOT NULL,
     38 			  `date_added` DATETIME NOT NULL,
     39 			  `digits` CHAR(25) NOT NULL,
     40 			  `expire_month` INT(2) NOT NULL,
     41 			  `expire_year` INT(2) NOT NULL,
     42 			  `token` CHAR(64) NOT NULL,
     43 			  PRIMARY KEY (`firstdata_card_id`)
     44 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     45 	}
     46 
     47 	public function uninstall() {
     48 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "firstdata_order`;");
     49 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "firstdata_order_transaction`;");
     50 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "firstdata_card`;");
     51 	}
     52 
     53 	public function void($order_id) {
     54 		$firstdata_order = $this->getOrder($order_id);
     55 
     56 		if (!empty($firstdata_order)) {
     57 			$timestamp = strftime("%Y%m%d%H%M%S");
     58 			$merchant_id = $this->config->get('payment_firstdata_merchant_id');
     59 			$secret = $this->config->get('payment_firstdata_secret');
     60 
     61 			$this->logger('Void hash construct: ' . $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . . . ');
     62 
     63 			$tmp = $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . . . ';
     64 			$hash = sha1($tmp);
     65 			$tmp = $hash . ' . ' . $secret;
     66 			$hash = sha1($tmp);
     67 
     68 			$xml = '';
     69 			$xml .= '<request type="void" timestamp="' . $timestamp . '">';
     70 			$xml .= '<merchantid>' . $merchant_id . '</merchantid>';
     71 			$xml .= '<account>' . $firstdata_order['account'] . '</account>';
     72 			$xml .= '<orderid>' . $firstdata_order['order_ref'] . '</orderid>';
     73 			$xml .= '<pasref>' . $firstdata_order['pasref'] . '</pasref>';
     74 			$xml .= '<authcode>' . $firstdata_order['authcode'] . '</authcode>';
     75 			$xml .= '<sha1hash>' . $hash . '</sha1hash>';
     76 			$xml .= '</request>';
     77 
     78 			$this->logger('Void XML request:\r\n' . print_r(simplexml_load_string($xml), 1));
     79 
     80 			$ch = curl_init();
     81 			curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi");
     82 			curl_setopt($ch, CURLOPT_POST, 1);
     83 			curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
     84 			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     85 			curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
     86 			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     87 			$response = curl_exec ($ch);
     88 			curl_close ($ch);
     89 
     90 			return simplexml_load_string($response);
     91 		} else {
     92 			return false;
     93 		}
     94 	}
     95 
     96 	public function updateVoidStatus($firstdata_order_id, $status) {
     97 		$this->db->query("UPDATE `" . DB_PREFIX . "firstdata_order` SET `void_status` = '" . (int)$status . "' WHERE `firstdata_order_id` = '" . (int)$firstdata_order_id . "'");
     98 	}
     99 
    100 	public function capture($order_id, $amount) {
    101 		$firstdata_order = $this->getOrder($order_id);
    102 
    103 		if (!empty($firstdata_order) && $firstdata_order['capture_status'] == 0) {
    104 			$timestamp = strftime("%Y%m%d%H%M%S");
    105 			$merchant_id = $this->config->get('payment_firstdata_merchant_id');
    106 			$secret = $this->config->get('payment_firstdata_secret');
    107 
    108 			if ($firstdata_order['settle_type'] == 2) {
    109 				$this->logger('Capture hash construct: ' . $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . ' . (int)round($amount*100) . ' . ' . (string)$firstdata_order['currency_code'] . ' . ');
    110 
    111 				$tmp = $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . ' . (int)round($amount*100) . ' . ' . (string)$firstdata_order['currency_code'] . ' . ';
    112 				$hash = sha1($tmp);
    113 				$tmp = $hash . ' . ' . $secret;
    114 				$hash = sha1($tmp);
    115 
    116 				$settle_type = 'multisettle';
    117 				$xml_amount = '<amount currency="' . (string)$firstdata_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>';
    118 			} else {
    119 				//$this->logger('Capture hash construct: ' . $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . . . ');
    120 				$this->logger('Capture hash construct: ' . $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . ' . (int)round($amount*100) . ' . ' . (string)$firstdata_order['currency_code'] . ' . ');
    121 
    122 				$tmp = $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . ' . (int)round($amount*100) . ' . ' . (string)$firstdata_order['currency_code'] . ' . ';
    123 				$hash = sha1($tmp);
    124 				$tmp = $hash . ' . ' . $secret;
    125 				$hash = sha1($tmp);
    126 
    127 				$settle_type = 'settle';
    128 				$xml_amount = '<amount currency="' . (string)$firstdata_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>';
    129 			}
    130 
    131 			$xml = '';
    132 			$xml .= '<request type="' . $settle_type . '" timestamp="' . $timestamp . '">';
    133 			$xml .= '<merchantid>' . $merchant_id . '</merchantid>';
    134 			$xml .= '<account>' . $firstdata_order['account'] . '</account>';
    135 			$xml .= '<orderid>' . $firstdata_order['order_ref'] . '</orderid>';
    136 			$xml .= $xml_amount;
    137 			$xml .= '<pasref>' . $firstdata_order['pasref'] . '</pasref>';
    138 			$xml .= '<autosettle flag="1" />';
    139 			$xml .= '<authcode>' . $firstdata_order['authcode'] . '</authcode>';
    140 			$xml .= '<sha1hash>' . $hash . '</sha1hash>';
    141 			$xml .= '</request>';
    142 
    143 			$this->logger('Settle XML request:\r\n' . print_r(simplexml_load_string($xml), 1));
    144 
    145 			$ch = curl_init();
    146 			curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi");
    147 			curl_setopt($ch, CURLOPT_POST, 1);
    148 			curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
    149 			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    150 			curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    151 			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    152 			$response = curl_exec ($ch);
    153 			curl_close ($ch);
    154 
    155 			return simplexml_load_string($response);
    156 		} else {
    157 			return false;
    158 		}
    159 	}
    160 
    161 	public function updateCaptureStatus($firstdata_order_id, $status) {
    162 		$this->db->query("UPDATE `" . DB_PREFIX . "firstdata_order` SET `capture_status` = '" . (int)$status . "' WHERE `firstdata_order_id` = '" . (int)$firstdata_order_id . "'");
    163 	}
    164 
    165 	public function getOrder($order_id) {
    166 		$this->logger('getOrder - ' . $order_id);
    167 
    168 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "firstdata_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
    169 
    170 		if ($qry->num_rows) {
    171 			$order = $qry->row;
    172 			$order['transactions'] = $this->getTransactions($order['firstdata_order_id']);
    173 
    174 			$this->logger(print_r($order, 1));
    175 
    176 			return $order;
    177 		} else {
    178 			return false;
    179 		}
    180 	}
    181 
    182 	private function getTransactions($firstdata_order_id) {
    183 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "firstdata_order_transaction` WHERE `firstdata_order_id` = '" . (int)$firstdata_order_id . "'");
    184 
    185 		if ($qry->num_rows) {
    186 			return $qry->rows;
    187 		} else {
    188 			return false;
    189 		}
    190 	}
    191 
    192 	public function addTransaction($firstdata_order_id, $type, $total) {
    193 		$this->db->query("INSERT INTO `" . DB_PREFIX . "firstdata_order_transaction` SET `firstdata_order_id` = '" . (int)$firstdata_order_id . "', `date_added` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . (float)$total . "'");
    194 	}
    195 
    196 	public function logger($message) {
    197 		if ($this->config->get('payment_firstdata_debug') == 1) {
    198 			$log = new Log('firstdata.log');
    199 			$log->write($message);
    200 		}
    201 	}
    202 
    203 	public function getTotalCaptured($firstdata_order_id) {
    204 		$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "firstdata_order_transaction` WHERE `firstdata_order_id` = '" . (int)$firstdata_order_id . "' AND (`type` = 'payment' OR `type` = 'refund')");
    205 
    206 		return (float)$query->row['total'];
    207 	}
    208 
    209 	public function mapCurrency($code) {
    210 		$currency = array(
    211 			'GBP' => 826,
    212 			'USD' => 840,
    213 			'EUR' => 978,
    214 		);
    215 
    216 		if (array_key_exists($code, $currency)) {
    217 			return $currency[$code];
    218 		} else {
    219 			return false;
    220 		}
    221 	}
    222 }