shop.balmet.com

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

firstdata_remote.php (13185B)


      1 <?php
      2 class ModelExtensionPaymentFirstdataRemote extends Model {
      3 	public function getMethod($address, $total) {
      4 		$this->load->language('extension/payment/firstdata_remote');
      5 
      6 		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone_to_geo_zone` WHERE `geo_zone_id` = '" . (int)$this->config->get('payment_firstdata_geo_zone_id') . "' AND `country_id` = '" . (int)$address['country_id'] . "' AND (`zone_id` = '" . (int)$address['zone_id'] . "' OR `zone_id` = '0')");
      7 
      8 		if ($this->config->get('firstdata_remote_total') > 0 && $this->config->get('firstdata_remote_total') > $total) {
      9 			$status = false;
     10 		} elseif (!$this->config->get('firstdata_remote_geo_zone_id')) {
     11 			$status = true;
     12 		} elseif ($query->num_rows) {
     13 			$status = true;
     14 		} else {
     15 			$status = false;
     16 		}
     17 
     18 		$method_data = array();
     19 
     20 		if ($status) {
     21 			$method_data = array(
     22 				'code'       => 'firstdata_remote',
     23 				'title'      => $this->language->get('text_title'),
     24 				'terms'      => '',
     25 				'sort_order' => $this->config->get('firstdata_remote_sort_order')
     26 			);
     27 		}
     28 
     29 		return $method_data;
     30 	}
     31 
     32 	public function capturePayment($data, $order_id) {
     33 		$this->load->model('checkout/order');
     34 
     35 		$order_info = $this->model_checkout_order->getOrder($order_id);
     36 
     37 		$order_ref = 'API-' . $order_id . '-' . date("Y-m-d-H-i-s") . '-' . rand(10, 500);
     38 
     39 		$amount = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
     40 
     41 		if ($this->config->get('firstdata_remote_auto_settle') == 1) {
     42 			$type = 'sale';
     43 		} else {
     44 			$type = 'preAuth';
     45 		}
     46 
     47 		$currency = $this->mapCurrency($order_info['currency_code']);
     48 
     49 		$token = '';
     50 		$payment_token = '';
     51 		if ($this->config->get('firstdata_remote_card_storage') == 1) {
     52 			if (isset($this->request->post['cc_choice']) && $this->request->post['cc_choice'] != 'new') {
     53 				$payment_token = $this->request->post['cc_choice'];
     54 			} elseif (isset($this->request->post['cc_store']) && $this->request->post['cc_store'] == 1) {
     55 				$token = sha1($this->customer->getId()  . '-' . date("Y-m-d-H-i-s") . rand(10, 500));
     56 			}
     57 		}
     58 
     59 		$xml = '<?xml version="1.0" encoding="UTF-8"?>';
     60 		$xml .= '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">';
     61 			$xml .= '<SOAP-ENV:Header />';
     62 				$xml .= '<SOAP-ENV:Body>';
     63 					$xml .= '<ipgapi:IPGApiOrderRequest xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1" xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi">';
     64 						$xml .= '<v1:Transaction>';
     65 							$xml .= '<v1:CreditCardTxType>';
     66 								$xml .= '<v1:Type>' . $type . '</v1:Type>';
     67 							$xml .= '</v1:CreditCardTxType>';
     68 							if (empty($payment_token)) {
     69 								$xml .= '<v1:CreditCardData>';
     70 									$xml .= '<v1:CardNumber>' . $data['cc_number'] . '</v1:CardNumber>';
     71 									$xml .= '<v1:ExpMonth>' . $data['cc_expire_date_month'] . '</v1:ExpMonth>';
     72 									$xml .= '<v1:ExpYear>' . $data['cc_expire_date_year'] . '</v1:ExpYear>';
     73 									$xml .= '<v1:CardCodeValue>' . $data['cc_cvv2'] . '</v1:CardCodeValue>';
     74 								$xml .= '</v1:CreditCardData>';
     75 							}
     76 							$xml .= '<v1:Payment>';
     77 								if (!empty($token)) {
     78 									$xml .= '<v1:HostedDataID>' . $token . '</v1:HostedDataID>';
     79 								}
     80 								if (!empty($payment_token)) {
     81 									$xml .= '<v1:HostedDataID>' . $payment_token . '</v1:HostedDataID>';
     82 								}
     83 								$xml .= '<v1:ChargeTotal>' . $amount . '</v1:ChargeTotal>';
     84 								$xml .= '<v1:Currency>' . $currency . '</v1:Currency>';
     85 							$xml .= '</v1:Payment>';
     86 
     87 							$xml .= '<v1:TransactionDetails>';
     88 								$xml .= '<v1:OrderId>' . $order_ref . '</v1:OrderId>';
     89 								$xml .= '<v1:Ip>' . $order_info['ip'] . '</v1:Ip>';
     90 								$xml .= '<v1:TransactionOrigin>ECI</v1:TransactionOrigin>';
     91 								$xml .= '<v1:PONumber>OPENCART2.0' . VERSION . '</v1:PONumber>';
     92 							$xml .= '</v1:TransactionDetails>';
     93 
     94 							$xml .= '<v1:Billing>';
     95 								$xml .= '<v1:CustomerID>' . (int)$this->customer->getId() . '</v1:CustomerID>';
     96 								$xml .= '<v1:Name>' . $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname'] . '</v1:Name>';
     97 								$xml .= '<v1:Company>' . $order_info['payment_company'] . '</v1:Company>';
     98 								$xml .= '<v1:Address1>' . $order_info['payment_address_1'] . '</v1:Address1>';
     99 								$xml .= '<v1:Address2>' . $order_info['payment_address_2'] . '</v1:Address2>';
    100 								$xml .= '<v1:City>' . $order_info['payment_city'] . '</v1:City>';
    101 								$xml .= '<v1:State>' . $order_info['payment_zone'] . '</v1:State>';
    102 								$xml .= '<v1:Zip>' . $order_info['payment_postcode'] . '</v1:Zip>';
    103 								$xml .= '<v1:Country>' . $order_info['payment_iso_code_2'] . '</v1:Country>';
    104 								$xml .= '<v1:Email>' . $order_info['email'] . '</v1:Email>';
    105 							$xml .= '</v1:Billing>';
    106 
    107 							$xml .= '<v1:Shipping>';
    108 								$xml .= '<v1:Name>' . $order_info['shipping_firstname'] . ' ' . $order_info['shipping_lastname'] . '</v1:Name>';
    109 								$xml .= '<v1:Address1>' . $order_info['shipping_address_1'] . '</v1:Address1>';
    110 								$xml .= '<v1:Address2>' . $order_info['shipping_address_2'] . '</v1:Address2>';
    111 								$xml .= '<v1:City>' . $order_info['shipping_city'] . '</v1:City>';
    112 								$xml .= '<v1:State>' . $order_info['shipping_zone'] . '</v1:State>';
    113 								$xml .= '<v1:Zip>' . $order_info['shipping_postcode'] . '</v1:Zip>';
    114 								$xml .= '<v1:Country>' . $order_info['shipping_iso_code_2'] . '</v1:Country>';
    115 							$xml .= '</v1:Shipping>';
    116 
    117 						$xml .= '</v1:Transaction>';
    118 					$xml .= '</ipgapi:IPGApiOrderRequest>';
    119 				$xml .= '</SOAP-ENV:Body>';
    120 		$xml .= '</SOAP-ENV:Envelope>';
    121 
    122 		$xml = simplexml_load_string($this->call($xml));
    123 
    124 		$xml->registerXPathNamespace('ipgapi', 'http://ipg-online.com/ipgapi/schemas/ipgapi');
    125 		$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
    126 
    127 		$fault = $xml->xpath('//soap:Fault');
    128 
    129 		$response['fault'] = '';
    130 		if (!empty($fault[0]) && isset($fault[0]->detail)) {
    131 			$response['fault'] = (string)$fault[0]->detail;
    132 		}
    133 
    134 		$string = $xml->xpath('//ipgapi:CommercialServiceProvider');
    135 		$response['provider'] = isset($string[0]) ? (string)$string[0] : '';
    136 
    137 		$string = $xml->xpath('//ipgapi:TransactionTime');
    138 		$response['transaction_time'] = isset($string[0]) ? (string)$string[0] : '';
    139 
    140 		$string = $xml->xpath('//ipgapi:ProcessorReferenceNumber');
    141 		$response['reference_number'] = isset($string[0]) ? (string)$string[0] : '';
    142 
    143 		$string = $xml->xpath('//ipgapi:ProcessorResponseMessage');
    144 		$response['response_message'] = isset($string[0]) ? (string)$string[0] : '';
    145 
    146 		$string = $xml->xpath('//ipgapi:ProcessorResponseCode');
    147 		$response['response_code'] = isset($string[0]) ? (string)$string[0] : '';
    148 
    149 		$string = $xml->xpath('//ipgapi:ErrorMessage');
    150 		$response['error'] = isset($string[0]) ? (string)$string[0] : '';
    151 
    152 		$string = $xml->xpath('//ipgapi:OrderId');
    153 		$response['order_id'] = isset($string[0]) ? (string)$string[0] : '';
    154 
    155 		$string = $xml->xpath('//ipgapi:ApprovalCode');
    156 		$response['approval_code'] = isset($string[0]) ? (string)$string[0] : '';
    157 
    158 		$string = $xml->xpath('//ipgapi:TDate');
    159 		$response['t_date'] = isset($string[0]) ? (string)$string[0] : '';
    160 
    161 		$string = $xml->xpath('//ipgapi:TransactionResult');
    162 		$response['transaction_result'] = isset($string[0]) ? (string)$string[0] : '';
    163 
    164 		$string = $xml->xpath('//ipgapi:PaymentType');
    165 		$response['payment_type'] = isset($string[0]) ? (string)$string[0] : '';
    166 
    167 		$string = $xml->xpath('//ipgapi:Brand');
    168 		$response['brand'] = isset($string[0]) ? (string)$string[0] : '';
    169 
    170 		$string = $xml->xpath('//ipgapi:Country');
    171 		$response['country'] = isset($string[0]) ? (string)$string[0] : '';
    172 
    173 		$string = $xml->xpath('//ipgapi:ProcessorReceiptNumber');
    174 		$response['receipt_number'] = isset($string[0]) ? (string)$string[0] : '';
    175 
    176 		$string = $xml->xpath('//ipgapi:ProcessorTraceNumber');
    177 		$response['trace_number'] = isset($string[0]) ? (string)$string[0] : '';
    178 
    179 		$string = $xml->xpath('//ipgapi:ProcessorCCVResponse');
    180 		$response['ccv'] = isset($string[0]) ? (string)$string[0] : '';
    181 
    182 		$string = $xml->xpath('//ipgapi:AVSResponse');
    183 		$response['avs'] = isset($string[0]) ? (string)$string[0] : '';
    184 
    185 		$response['card_number_ref'] = (string)substr($data['cc_number'], -4);
    186 
    187 		if (strtoupper($response['transaction_result']) == 'APPROVED' && !empty($token)) {
    188 			$this->storeCard($token, $this->customer->getId(), $response['brand'], $data['cc_expire_date_month'], $data['cc_expire_date_year'], (string)substr($data['cc_number'], -4));
    189 		}
    190 
    191 		$this->logger(print_r($response, 1));
    192 
    193 		return $response;
    194 	}
    195 
    196 	public function call($xml) {
    197 		$ch = curl_init();
    198 		curl_setopt($ch, CURLOPT_URL, "https://test.ipg-online.com/ipgapi/services");
    199 		curl_setopt($ch, CURLOPT_POST, 1);
    200 		curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
    201 		curl_setopt($ch, CURLOPT_HTTPAUTH, 'CURLAUTH_BASIC');
    202 		curl_setopt($ch, CURLOPT_USERPWD, $this->config->get('firstdata_remote_user_id') . ':' . $this->config->get('firstdata_remote_password'));
    203 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    204 		curl_setopt($ch, CURLOPT_CAINFO, $this->config->get('firstdata_remote_ca'));
    205 		curl_setopt($ch, CURLOPT_SSLCERT, $this->config->get('firstdata_remote_certificate'));
    206 		curl_setopt($ch, CURLOPT_SSLKEY, $this->config->get('firstdata_remote_key'));
    207 		curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $this->config->get('firstdata_remote_key_pw'));
    208 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    209 		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    210 		//curl_setopt($ch, CURLOPT_STDERR, fopen(DIR_LOGS . "/headers.txt", "w+"));
    211 		curl_setopt($ch, CURLOPT_VERBOSE, true);
    212 		$response = curl_exec ($ch);
    213 
    214 		$this->logger('Post data: ' . print_r($this->request->post, 1));
    215 		$this->logger('Request: ' . $xml);
    216 		$this->logger('Curl error #: ' . curl_errno($ch));
    217 		$this->logger('Curl error text: ' . curl_error($ch));
    218 		$this->logger('Curl response info: ' . print_r(curl_getinfo($ch), 1));
    219 		$this->logger('Curl response: ' . $response);
    220 
    221 		curl_close ($ch);
    222 
    223 		return $response;
    224 	}
    225 
    226 	public function addOrder($order_info, $capture_result) {
    227 		if ($this->config->get('firstdata_remote_auto_settle') == 1) {
    228 			$settle_status = 1;
    229 		} else {
    230 			$settle_status = 0;
    231 		}
    232 
    233 		$this->db->query("INSERT INTO `" . DB_PREFIX . "firstdata_remote_order` SET `order_id` = '" . (int)$order_info['order_id'] . "', `order_ref` = '" . $this->db->escape($capture_result['order_id']) . "', `authcode` = '" . $this->db->escape($capture_result['approval_code']) . "', `tdate` = '" . $this->db->escape($capture_result['t_date']) . "', `date_added` = now(), `date_modified` = now(), `capture_status` = '" . (int)$settle_status . "', `currency_code` = '" . $this->db->escape($order_info['currency_code']) . "', `total` = '" . $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false) . "'");
    234 
    235 		return $this->db->getLastId();
    236 	}
    237 
    238 	public function addTransaction($firstdata_remote_order_id, $type, $order_info = array()) {
    239 		if (!empty($order_info)) {
    240 			$amount = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
    241 		} else {
    242 			$amount = 0.00;
    243 		}
    244 
    245 		$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)$amount . "'");
    246 	}
    247 
    248 	public function logger($message) {
    249 		if ($this->config->get('firstdata_remote_debug') == 1) {
    250 			$log = new Log('firstdata_remote.log');
    251 			$log->write($message);
    252 		}
    253 	}
    254 
    255 	public function addHistory($order_id, $order_status_id, $comment) {
    256 		$this->db->query("INSERT INTO `" . DB_PREFIX . "order_history` SET `order_id` = '" . (int)$order_id . "', `order_status_id` = '" . (int)$order_status_id . "', `notify` = '0', `comment` = '" . $this->db->escape($comment) . "', `date_added` = NOW()");
    257 	}
    258 
    259 	public function mapCurrency($code) {
    260 		$currency = array(
    261 			'GBP' => 826,
    262 			'USD' => 840,
    263 			'EUR' => 978,
    264 		);
    265 
    266 		if (array_key_exists($code, $currency)) {
    267 			return $currency[$code];
    268 		} else {
    269 			return false;
    270 		}
    271 	}
    272 
    273 	public function getStoredCards() {
    274 		$customer_id = $this->customer->getId();
    275 
    276 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "firstdata_remote_card WHERE customer_id = '" . (int)$customer_id . "'");
    277 
    278 		return $query->rows;
    279 	}
    280 
    281 	public function storeCard($token, $customer_id, $type, $month, $year, $digits) {
    282 		$this->db->query("INSERT INTO `" . DB_PREFIX . "firstdata_remote_card` SET `customer_id` = '" . (int)$customer_id . "', `date_added` = now(), `token` = '" . $this->db->escape($token) . "', `card_type` = '" . $this->db->escape($type) . "', `expire_month` = '" . $this->db->escape($month) . "', `expire_year` = '" . $this->db->escape($year) . "', `digits` = '" . $this->db->escape($digits) . "'");
    283 	}
    284 }