shop.balmet.com

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

firstdata_remote.php (11775B)


      1 <?php
      2 class ModelExtensionPaymentFirstdataRemote extends Model {
      3 	public function install() {
      4 		$this->db->query("
      5 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "firstdata_remote_order` (
      6 			  `firstdata_remote_order_id` INT(11) NOT NULL AUTO_INCREMENT,
      7 			  `order_id` INT(11) NOT NULL,
      8 			  `order_ref` CHAR(50) NOT NULL,
      9 			  `date_added` DATETIME NOT NULL,
     10 			  `date_modified` DATETIME NOT NULL,
     11 			  `tdate` VARCHAR(30) NOT NULL,
     12 			  `capture_status` INT(1) DEFAULT NULL,
     13 			  `void_status` INT(1) DEFAULT NULL,
     14 			  `refund_status` INT(1) DEFAULT NULL,
     15 			  `currency_code` CHAR(3) NOT NULL,
     16 			  `authcode` VARCHAR(30) NOT NULL,
     17 			  `total` DECIMAL( 10, 2 ) NOT NULL,
     18 			  PRIMARY KEY (`firstdata_remote_order_id`)
     19 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     20 
     21 		$this->db->query("
     22 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "firstdata_remote_order_transaction` (
     23 			  `firstdata_remote_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT,
     24 			  `firstdata_remote_order_id` INT(11) NOT NULL,
     25 			  `date_added` DATETIME NOT NULL,
     26 			  `type` ENUM('auth', 'payment', 'refund', 'void') DEFAULT NULL,
     27 			  `amount` DECIMAL( 10, 2 ) NOT NULL,
     28 			  PRIMARY KEY (`firstdata_remote_order_transaction_id`)
     29 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     30 
     31 		$this->db->query("
     32 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "firstdata_remote_card` (
     33 			  `firstdata_remote_card_id` INT(11) NOT NULL AUTO_INCREMENT,
     34 			  `customer_id` INT(11) NOT NULL,
     35 			  `date_added` DATETIME NOT NULL,
     36 			  `digits` CHAR(4) NOT NULL,
     37 			  `expire_month` INT(2) NOT NULL,
     38 			  `expire_year` INT(2) NOT NULL,
     39 			  `card_type` CHAR(15) NOT NULL,
     40 			  `token` CHAR(64) NOT NULL,
     41 			  PRIMARY KEY (`firstdata_remote_card_id`)
     42 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     43 	}
     44 
     45 	public function uninstall() {
     46 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "firstdata_remote_order`;");
     47 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "firstdata_remote_order_transaction`;");
     48 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "firstdata_remote_card`;");
     49 	}
     50 
     51 	public function call($xml) {
     52 		$ch = curl_init();
     53 		curl_setopt($ch, CURLOPT_URL, "https://test.ipg-online.com/ipgapi/services");
     54 		curl_setopt($ch, CURLOPT_POST, 1);
     55 		curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
     56 		curl_setopt($ch, CURLOPT_HTTPAUTH, 'CURLAUTH_BASIC');
     57 		curl_setopt($ch, CURLOPT_USERPWD, $this->config->get('firstdata_remote_user_id') . ':' . $this->config->get('firstdata_remote_password'));
     58 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
     59 		curl_setopt($ch, CURLOPT_CAINFO, $this->config->get('firstdata_remote_ca'));
     60 		curl_setopt($ch, CURLOPT_SSLCERT, $this->config->get('firstdata_remote_certificate'));
     61 		curl_setopt($ch, CURLOPT_SSLKEY, $this->config->get('firstdata_remote_key'));
     62 		curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $this->config->get('firstdata_remote_key_pw'));
     63 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     64 		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
     65 		//curl_setopt($ch, CURLOPT_STDERR, fopen(DIR_LOGS . "/headers.txt", "w+"));
     66 		curl_setopt($ch, CURLOPT_VERBOSE, true);
     67 		$response = curl_exec ($ch);
     68 
     69 		$this->logger('Post data: ' . print_r($this->request->post, 1));
     70 		$this->logger('Request: ' . $xml);
     71 		$this->logger('Curl error #: ' . curl_errno($ch));
     72 		$this->logger('Curl error text: ' . curl_error($ch));
     73 		$this->logger('Curl response info: ' . print_r(curl_getinfo($ch), 1));
     74 		$this->logger('Curl response: ' . $response);
     75 
     76 		curl_close ($ch);
     77 
     78 		return $response;
     79 	}
     80 
     81 	public function void($order_ref, $tdate) {
     82 		$xml = '<?xml version="1.0" encoding="UTF-8"?>';
     83 		$xml .= '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">';
     84 			$xml .= '<SOAP-ENV:Header />';
     85 			$xml .= '<SOAP-ENV:Body>';
     86 			$xml .= '<ipgapi:IPGApiOrderRequest xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1" xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi">';
     87 			$xml .= '<v1:Transaction>';
     88 				$xml .= '<v1:CreditCardTxType>';
     89 					$xml .= '<v1:Type>void</v1:Type>';
     90 				$xml .= '</v1:CreditCardTxType>';
     91 				$xml .= '<v1:TransactionDetails>';
     92 					$xml .= '<v1:OrderId>' . $order_ref . '</v1:OrderId>';
     93 					$xml .= '<v1:TDate>' . $tdate . '</v1:TDate>';
     94 				$xml .= '</v1:TransactionDetails>';
     95 			$xml .= '</v1:Transaction>';
     96 			$xml .= '</ipgapi:IPGApiOrderRequest>';
     97 			$xml .= '</SOAP-ENV:Body>';
     98 		$xml .= '</SOAP-ENV:Envelope>';
     99 
    100 		$xml = simplexml_load_string($this->call($xml));
    101 
    102 		$xml->registerXPathNamespace('ipgapi', 'http://ipg-online.com/ipgapi/schemas/ipgapi');
    103 		$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
    104 
    105 		$fault = $xml->xpath('//soap:Fault');
    106 
    107 		$response['fault'] = '';
    108 		if (!empty($fault[0]) && isset($fault[0]->detail)) {
    109 			$response['fault'] = (string)$fault[0]->detail;
    110 		}
    111 
    112 		$string = $xml->xpath('//ipgapi:ErrorMessage');
    113 		$response['error'] = isset($string[0]) ? (string)$string[0] : '';
    114 
    115 		$string = $xml->xpath('//ipgapi:TransactionResult');
    116 		$response['transaction_result'] = isset($string[0]) ? (string)$string[0] : '';
    117 
    118 		return $response;
    119 	}
    120 
    121 	public function updateVoidStatus($firstdata_remote_order_id, $status) {
    122 		$this->db->query("UPDATE `" . DB_PREFIX . "firstdata_remote_order` SET `void_status` = '" . (int)$status . "' WHERE `firstdata_remote_order_id` = '" . (int)$firstdata_remote_order_id . "'");
    123 	}
    124 
    125 	public function capture($order_ref, $total, $currency_code) {
    126 		$xml = '<?xml version="1.0" encoding="UTF-8"?>';
    127 		$xml .= '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">';
    128 			$xml .= '<SOAP-ENV:Header />';
    129 			$xml .= '<SOAP-ENV:Body>';
    130 				$xml .= '<ipgapi:IPGApiOrderRequest xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1" xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi">';
    131 					$xml .= '<v1:Transaction>';
    132 						$xml .= '<v1:CreditCardTxType>';
    133 							$xml .= '<v1:Type>postAuth</v1:Type>';
    134 						$xml .= '</v1:CreditCardTxType>';
    135 						$xml .= '<v1:Payment>';
    136 							$xml .= '<v1:ChargeTotal>' . $total . '</v1:ChargeTotal>';
    137 							$xml .= '<v1:Currency>' . $this->mapCurrency($currency_code) . '</v1:Currency>';
    138 						$xml .= '</v1:Payment>';
    139 						$xml .= '<v1:TransactionDetails>';
    140 							$xml .= '<v1:OrderId>' . $order_ref . '</v1:OrderId>';
    141 						$xml .= '</v1:TransactionDetails>';
    142 					$xml .= '</v1:Transaction>';
    143 				$xml .= '</ipgapi:IPGApiOrderRequest>';
    144 			$xml .= '</SOAP-ENV:Body>';
    145 		$xml .= '</SOAP-ENV:Envelope>';
    146 
    147 		$xml = simplexml_load_string($this->call($xml));
    148 
    149 		$xml->registerXPathNamespace('ipgapi', 'http://ipg-online.com/ipgapi/schemas/ipgapi');
    150 		$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
    151 
    152 		$fault = $xml->xpath('//soap:Fault');
    153 
    154 		$response['fault'] = '';
    155 		if (!empty($fault[0]) && isset($fault[0]->detail)) {
    156 			$response['fault'] = (string)$fault[0]->detail;
    157 		}
    158 
    159 		$string = $xml->xpath('//ipgapi:ErrorMessage');
    160 		$response['error'] = isset($string[0]) ? (string)$string[0] : '';
    161 
    162 		$string = $xml->xpath('//ipgapi:TransactionResult');
    163 		$response['transaction_result'] = isset($string[0]) ? (string)$string[0] : '';
    164 
    165 		return $response;
    166 	}
    167 
    168 	public function updateCaptureStatus($firstdata_remote_order_id, $status) {
    169 		$this->db->query("UPDATE `" . DB_PREFIX . "firstdata_remote_order` SET `capture_status` = '" . (int)$status . "' WHERE `firstdata_remote_order_id` = '" . (int)$firstdata_remote_order_id . "'");
    170 	}
    171 
    172 	public function refund($order_ref, $total, $currency_code) {
    173 		$xml = '<?xml version="1.0" encoding="UTF-8"?>';
    174 		$xml .= '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">';
    175 		$xml .= '<SOAP-ENV:Header />';
    176 		$xml .= '<SOAP-ENV:Body>';
    177 		$xml .= '<ipgapi:IPGApiOrderRequest xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1" xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi">';
    178 		$xml .= '<v1:Transaction>';
    179 		$xml .= '<v1:CreditCardTxType>';
    180 		$xml .= '<v1:Type>return</v1:Type>';
    181 		$xml .= '</v1:CreditCardTxType>';
    182 		$xml .= '<v1:Payment>';
    183 		$xml .= '<v1:ChargeTotal>' . $total . '</v1:ChargeTotal>';
    184 		$xml .= '<v1:Currency>' . $this->mapCurrency($currency_code) . '</v1:Currency>';
    185 		$xml .= '</v1:Payment>';
    186 		$xml .= '<v1:TransactionDetails>';
    187 		$xml .= '<v1:OrderId>' . $order_ref . '</v1:OrderId>';
    188 		$xml .= '</v1:TransactionDetails>';
    189 		$xml .= '</v1:Transaction>';
    190 		$xml .= '</ipgapi:IPGApiOrderRequest>';
    191 		$xml .= '</SOAP-ENV:Body>';
    192 		$xml .= '</SOAP-ENV:Envelope>';
    193 
    194 		$xml = simplexml_load_string($this->call($xml));
    195 
    196 		$xml->registerXPathNamespace('ipgapi', 'http://ipg-online.com/ipgapi/schemas/ipgapi');
    197 		$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
    198 
    199 		$fault = $xml->xpath('//soap:Fault');
    200 
    201 		$response['fault'] = '';
    202 		if (!empty($fault[0]) && isset($fault[0]->detail)) {
    203 			$response['fault'] = (string)$fault[0]->detail;
    204 		}
    205 
    206 		$string = $xml->xpath('//ipgapi:ErrorMessage');
    207 		$response['error'] = isset($string[0]) ? (string)$string[0] : '';
    208 
    209 		$string = $xml->xpath('//ipgapi:TransactionResult');
    210 		$response['transaction_result'] = isset($string[0]) ? (string)$string[0] : '';
    211 
    212 		return $response;
    213 	}
    214 
    215 	public function updateRefundStatus($firstdata_remote_order_id, $status) {
    216 		$this->db->query("UPDATE `" . DB_PREFIX . "firstdata_remote_order` SET `refund_status` = '" . (int)$status . "' WHERE `firstdata_remote_order_id` = '" . (int)$firstdata_remote_order_id . "'");
    217 	}
    218 
    219 	public function getOrder($order_id) {
    220 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "firstdata_remote_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
    221 
    222 		if ($qry->num_rows) {
    223 			$order = $qry->row;
    224 			$order['transactions'] = $this->getTransactions($order['firstdata_remote_order_id']);
    225 
    226 			return $order;
    227 		} else {
    228 			return false;
    229 		}
    230 	}
    231 
    232 	private function getTransactions($firstdata_remote_order_id) {
    233 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "firstdata_remote_order_transaction` WHERE `firstdata_remote_order_id` = '" . (int)$firstdata_remote_order_id . "'");
    234 
    235 		if ($qry->num_rows) {
    236 			return $qry->rows;
    237 		} else {
    238 			return false;
    239 		}
    240 	}
    241 
    242 	public function addTransaction($firstdata_remote_order_id, $type, $total) {
    243 		$this->db->query("INSERT INTO `" . DB_PREFIX . "firstdata_remote_order_transaction` SET `firstdata_remote_order_id` = '" . (int)$firstdata_remote_order_id . "', `date_added` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . (float)$total . "'");
    244 	}
    245 
    246 	public function logger($message) {
    247 		if ($this->config->get('firstdata_remote_debug') == 1) {
    248 			$log = new Log('firstdata_remote.log');
    249 			$log->write($message);
    250 		}
    251 	}
    252 
    253 	public function getTotalCaptured($firstdata_order_id) {
    254 		$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "firstdata_remote_order_transaction` WHERE `firstdata_remote_order_id` = '" . (int)$firstdata_order_id . "' AND (`type` = 'payment' OR `type` = 'refund')");
    255 
    256 		return (float)$query->row['total'];
    257 	}
    258 
    259 	public function getTotalRefunded($firstdata_order_id) {
    260 		$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "firstdata_remote_order_transaction` WHERE `firstdata_remote_order_id` = '" . (int)$firstdata_order_id . "' AND 'refund'");
    261 
    262 		return (float)$query->row['total'];
    263 	}
    264 
    265 	public function mapCurrency($code) {
    266 		$currency = array(
    267 			'GBP' => 826,
    268 			'USD' => 840,
    269 			'EUR' => 978,
    270 		);
    271 
    272 		if (array_key_exists($code, $currency)) {
    273 			return $currency[$code];
    274 		} else {
    275 			return false;
    276 		}
    277 	}
    278 }