shop.balmet.com

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

globalpay_remote.php (17071B)


      1 <?php
      2 class ModelExtensionPaymentGlobalpayRemote extends Model {
      3 	public function getMethod($address, $total) {
      4 		$this->load->language('extension/payment/globalpay_remote');
      5 
      6 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('payment_globalpay_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('payment_globalpay_remote_total') > 0 && $this->config->get('payment_globalpay_remote_total') > $total) {
      9 			$status = false;
     10 		} elseif (!$this->config->get('payment_globalpay_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'       => 'globalpay_remote',
     23 				'title'      => $this->language->get('text_title'),
     24 				'terms'      => '',
     25 				'sort_order' => $this->config->get('payment_globalpay_remote_sort_order')
     26 			);
     27 		}
     28 
     29 		return $method_data;
     30 	}
     31 
     32 	public function checkEnrollment($account, $amount, $currency, $order_ref) {
     33 		$timestamp = strftime("%Y%m%d%H%M%S");
     34 		$merchant_id = $this->config->get('payment_globalpay_remote_merchant_id');
     35 		$secret = $this->config->get('payment_globalpay_remote_secret');
     36 
     37 		$tmp = $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . $amount . '.' . $currency . '.' . $this->request->post['cc_number'];
     38 		$hash = sha1($tmp);
     39 		$tmp = $hash . '.' . $secret;
     40 		$hash = sha1($tmp);
     41 
     42 		$xml = '';
     43 		$xml .= '<request type="3ds-verifyenrolled" timestamp="' . $timestamp . '">';
     44 			$xml .= '<merchantid>' . $merchant_id . '</merchantid>';
     45 			$xml .= '<account>' . $account . '</account>';
     46 			$xml .= '<orderid>' . $order_ref . '</orderid>';
     47 			$xml .= '<amount currency="' . $currency . '">' . $amount . '</amount>';
     48 			$xml .= '<card>';
     49 				$xml .= '<number>' . $this->request->post['cc_number'] . '</number>';
     50 				$xml .= '<expdate>' . $this->request->post['cc_expire_date_month'] . $this->request->post['cc_expire_date_year'] . '</expdate>';
     51 				$xml .= '<type>' . $this->request->post['cc_type'] . '</type>';
     52 				$xml .= '<chname>' . $this->request->post['cc_name'] . '</chname>';
     53 			$xml .= '</card>';
     54 			$xml .= '<sha1hash>' . $hash . '</sha1hash>';
     55 		$xml .= '</request>';
     56 
     57 		$this->logger('checkEnrollment call');
     58 		$this->logger(simplexml_load_string($xml));
     59 		$this->logger($xml);
     60 
     61 		$ch = curl_init();
     62 		curl_setopt($ch, CURLOPT_URL, "https://remote.globaliris.com/realmpi");
     63 		curl_setopt($ch, CURLOPT_POST, 1);
     64 		curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
     65 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     66 		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
     67 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     68 		$response = curl_exec ($ch);
     69 		curl_close ($ch);
     70 
     71 		$this->logger('checkEnrollment xml response');
     72 		$this->logger($response);
     73 
     74 		return simplexml_load_string($response);
     75 	}
     76 
     77 	public function enrollmentSignature($account, $amount, $currency, $order_ref, $card_number, $card_expire, $card_type, $card_name, $pares) {
     78 		$this->load->model('checkout/order');
     79 
     80 		$timestamp = strftime("%Y%m%d%H%M%S");
     81 		$merchant_id = $this->config->get('payment_globalpay_remote_merchant_id');
     82 		$secret = $this->config->get('payment_globalpay_remote_secret');
     83 
     84 		$tmp = $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . $amount . '.' . $currency . '.' . $card_number;
     85 		$hash = sha1($tmp);
     86 		$tmp = $hash . '.' . $secret;
     87 		$hash = sha1($tmp);
     88 
     89 		$xml = '';
     90 		$xml .= '<request type="3ds-verifysig" timestamp="' . $timestamp . '">';
     91 			$xml .= '<merchantid>' . $merchant_id . '</merchantid>';
     92 			$xml .= '<account>' . $account . '</account>';
     93 			$xml .= '<orderid>' . $order_ref . '</orderid>';
     94 			$xml .= '<amount currency="' . $currency . '">' . (int)$amount . '</amount>';
     95 			$xml .= '<card>';
     96 				$xml .= '<number>' . $card_number . '</number>';
     97 				$xml .= '<expdate>' . $card_expire . '</expdate>';
     98 				$xml .= '<type>' . $card_type . '</type>';
     99 				$xml .= '<chname>' . $card_name . '</chname>';
    100 			$xml .= '</card>';
    101 			$xml .= '<pares>' . $pares . '</pares>';
    102 			$xml .= '<sha1hash>' . $hash . '</sha1hash>';
    103 		$xml .= '</request>';
    104 
    105 		$this->logger('enrollmentSignature call');
    106 		$this->logger(simplexml_load_string($xml));
    107 		$this->logger($xml);
    108 
    109 		$ch = curl_init();
    110 		curl_setopt($ch, CURLOPT_URL, "https://remote.globaliris.com/realmpi");
    111 		curl_setopt($ch, CURLOPT_POST, 1);
    112 		curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
    113 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    114 		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    115 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    116 		$response = curl_exec ($ch);
    117 		curl_close ($ch);
    118 
    119 		$this->logger('enrollmentSignature xml response');
    120 		$this->logger($response);
    121 
    122 		return simplexml_load_string($response);
    123 	}
    124 
    125 	public function capturePayment($account, $amount, $currency, $order_id, $order_ref, $card_number, $expire, $name, $type, $cvv, $issue, $eci_ref, $eci = '', $cavv = '', $xid = '') {
    126 		$this->load->model('checkout/order');
    127 
    128 		$timestamp = strftime("%Y%m%d%H%M%S");
    129 		$merchant_id = $this->config->get('payment_globalpay_remote_merchant_id');
    130 		$secret = $this->config->get('payment_globalpay_remote_secret');
    131 
    132 		$tmp = $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . $amount . '.' . $currency . '.' . $card_number;
    133 		$hash = sha1($tmp);
    134 		$tmp = $hash . '.' . $secret;
    135 		$hash = sha1($tmp);
    136 
    137 		$order_info = $this->model_checkout_order->getOrder($order_id);
    138 
    139 		$xml = '';
    140 		$xml .= '<request type="auth" timestamp="' . $timestamp . '">';
    141 			$xml .= '<merchantid>' . $merchant_id . '</merchantid>';
    142 			$xml .= '<account>' . $account . '</account>';
    143 			$xml .= '<orderid>' . $order_ref . '</orderid>';
    144 			$xml .= '<amount currency="' . $currency . '">' . $amount . '</amount>';
    145 			$xml .= '<comments>';
    146 				$xml .= '<comment id="1">OpenCart</comment>';
    147 			$xml .= '</comments>';
    148 			$xml .= '<card>';
    149 				$xml .= '<number>' . $card_number . '</number>';
    150 				$xml .= '<expdate>' . $expire . '</expdate>';
    151 				$xml .= '<type>' . $type . '</type>';
    152 				$xml .= '<chname>' . $name . '</chname>';
    153 				$xml .= '<cvn>';
    154 					$xml .= '<number>' . (int)$cvv . '</number>';
    155 					$xml .= '<presind>2</presind>';
    156 				$xml .= '</cvn>';
    157 				if (!empty($issue)) {
    158 					$xml .= '<issueno>' . (int)$issue . '</issueno>';
    159 				}
    160 			$xml .= '</card>';
    161 
    162 			if ($this->config->get('payment_globalpay_remote_auto_settle') == 0) {
    163 				$xml .= '<autosettle flag="0" />';
    164 			} elseif ($this->config->get('payment_globalpay_remote_auto_settle') == 1) {
    165 				$xml .= '<autosettle flag="1" />';
    166 			} elseif ($this->config->get('payment_globalpay_remote_auto_settle') == 2) {
    167 				$xml .= '<autosettle flag="MULTI" />';
    168 			}
    169 
    170 			if ($eci != '' || $cavv != '' || $xid != '') {
    171 				$xml .= '<mpi>';
    172 					if ($eci != '') {
    173 						$xml .= '<eci>' . (string)$eci . '</eci>';
    174 					}
    175 					if ($cavv != '') {
    176 						$xml .= '<cavv>' . (string)$cavv . '</cavv>';
    177 					}
    178 					if ($xid != '') {
    179 						$xml .= '<xid>' . (string)$xid . '</xid>';
    180 					}
    181 				$xml .= '</mpi>';
    182 			}
    183 
    184 			$xml .= '<sha1hash>' . $hash . '</sha1hash>';
    185 
    186 			if ($this->config->get('payment_globalpay_remote_tss_check') == 1) {
    187 				$xml .= '<tssinfo>';
    188 					$xml .= '<custipaddress>' . $order_info['ip'] . '</custipaddress>';
    189 
    190 					if ($this->customer->getId() > 0) {
    191 						$xml .= '<custnum>' . (int)$this->customer->getId() . '</custnum>';
    192 					}
    193 
    194 					if ((isset($order_info['payment_iso_code_2']) && !empty($order_info['payment_iso_code_2'])) || (isset($order_info['payment_postcode']) && !empty($order_info['payment_postcode']))) {
    195 						$xml .= '<address type="billing">';
    196 						if ((isset($order_info['payment_postcode']) && !empty($order_info['payment_postcode']))) {
    197 							$xml .= '<code>' . filter_var($order_info['payment_postcode'], FILTER_SANITIZE_NUMBER_INT) . '|' . filter_var($order_info['payment_address_1'], FILTER_SANITIZE_NUMBER_INT) . '</code>';
    198 						}
    199 						if ((isset($order_info['payment_iso_code_2']) && !empty($order_info['payment_iso_code_2']))) {
    200 							$xml .= '<country>' . $order_info['payment_iso_code_2'] . '</country>';
    201 						}
    202 						$xml .= '</address>';
    203 					}
    204 					if ((isset($order_info['shipping_iso_code_2']) && !empty($order_info['shipping_iso_code_2'])) || (isset($order_info['shipping_postcode']) && !empty($order_info['shipping_postcode']))) {
    205 						$xml .= '<address type="shipping">';
    206 						if ((isset($order_info['shipping_postcode']) && !empty($order_info['shipping_postcode']))) {
    207 							$xml .= '<code>' . filter_var($order_info['shipping_postcode'], FILTER_SANITIZE_NUMBER_INT) . '|' . filter_var($order_info['shipping_address_1'], FILTER_SANITIZE_NUMBER_INT) . '</code>';
    208 						}
    209 						if ((isset($order_info['shipping_iso_code_2']) && !empty($order_info['shipping_iso_code_2']))) {
    210 							$xml .= '<country>' . $order_info['shipping_iso_code_2'] . '</country>';
    211 						}
    212 						$xml .= '</address>';
    213 					}
    214 				$xml .= '</tssinfo>';
    215 			}
    216 
    217 		$xml .= '</request>';
    218 
    219 		$this->logger('capturePayment call');
    220 		$this->logger(simplexml_load_string($xml));
    221 		$this->logger($xml);
    222 
    223 		$ch = curl_init();
    224 		curl_setopt($ch, CURLOPT_URL, "https://remote.globaliris.com/realauth");
    225 		curl_setopt($ch, CURLOPT_POST, 1);
    226 		curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
    227 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    228 		curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    229 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    230 		$response = curl_exec ($ch);
    231 		curl_close ($ch);
    232 
    233 		$this->logger('capturePayment xml response');
    234 		$this->logger($response);
    235 
    236 		$response = simplexml_load_string($response);
    237 
    238 		$this->load->language('extension/payment/globalpay_remote');
    239 
    240 		$message = '<strong>' . $this->language->get('text_result') . ':</strong> ' . (int)$response->result;
    241 		$message .= '<br /><strong>' . $this->language->get('text_message') . ':</strong> ' . (string)$response->message;
    242 		$message .= '<br /><strong>' . $this->language->get('text_order_ref') . ':</strong> ' . (string)$order_ref;
    243 
    244 		if (isset($response->cvnresult) && !empty($response->cvnresult)) {
    245 			$message .= '<br /><strong>' . $this->language->get('text_cvn_result') . ':</strong> ' . (string)$response->cvnresult;
    246 		}
    247 
    248 		if (isset($response->avspostcoderesponse) && !empty($response->avspostcoderesponse)) {
    249 			$message .= '<br /><strong>' . $this->language->get('text_avs_postcode') . ':</strong> ' . (string)$response->avspostcoderesponse;
    250 		}
    251 
    252 		if (isset($response->avsaddressresponse) && !empty($response->avsaddressresponse)) {
    253 			$message .= '<br /><strong>' . $this->language->get('text_avs_address') . ':</strong> ' . (string)$response->avsaddressresponse;
    254 		}
    255 
    256 		if (isset($response->authcode) && !empty($response->authcode)) {
    257 			$message .= '<br /><strong>' . $this->language->get('text_auth_code') . ':</strong> ' . (string)$response->authcode;
    258 		}
    259 
    260 		if (!empty($eci_ref)) {
    261 			$message .= '<br /><strong>' . $this->language->get('text_eci') . ':</strong> (' . (int)$eci . ') ' . $this->language->get('text_3d_s' . (int)$eci_ref);
    262 		}
    263 
    264 		if (isset($response->tss->result) && !empty($response->tss->result)) {
    265 			$message .= '<br /><strong>' . $this->language->get('text_tss') . ':</strong> ' . (int)$response->tss->result;
    266 		}
    267 
    268 		$message .= '<br /><strong>' . $this->language->get('text_timestamp') . ':</strong> ' . (string)$timestamp;
    269 
    270 		if ($this->config->get('payment_globalpay_remote_card_data_status') == 1) {
    271 			$message .= '<br /><strong>' . $this->language->get('entry_cc_type') . ':</strong> ' . (string)$type;
    272 			$message .= '<br /><strong>' . $this->language->get('text_last_digits') . ':</strong> ' . (string)substr($card_number, -4);
    273 			$message .= '<br /><strong>' . $this->language->get('entry_cc_expire_date') . ':</strong> ' . (string)$expire;
    274 			$message .= '<br /><strong>' . $this->language->get('entry_cc_name') . ':</strong> ' . (string)$name;
    275 
    276 			if (isset($response->cardissuer->bank) && !empty($response->cardissuer->bank)) {
    277 				$message .= '<br /><strong>' . $this->language->get('text_card_bank') . ':</strong> ' . (string)$response->cardissuer->bank;
    278 			}
    279 
    280 			if (isset($response->cardissuer->country) && !empty($response->cardissuer->country)) {
    281 				$message .= '<br /><strong>' . $this->language->get('text_card_country') . ':</strong> ' . (string)$response->cardissuer->country;
    282 			}
    283 
    284 			if (isset($response->cardissuer->region) && !empty($response->cardissuer->region)) {
    285 				$message .= '<br /><strong>' . $this->language->get('text_card_region') . ':</strong> ' . (string)$response->cardissuer->region;
    286 			}
    287 		}
    288 
    289 		if ($response->result == '00') {
    290 			$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('config_order_status_id'));
    291 
    292 			$globalpay_order_id = $this->addOrder($order_info, $response, $account, $order_ref);
    293 
    294 			if ($this->config->get('payment_globalpay_remote_auto_settle') == 1) {
    295 				$this->addTransaction($globalpay_order_id, 'payment', $order_info);
    296 				$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_globalpay_remote_order_status_success_settled_id'), $message);
    297 			} else {
    298 				$this->addTransaction($globalpay_order_id, 'auth', 0);
    299 				$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_globalpay_remote_order_status_success_unsettled_id'), $message);
    300 			}
    301 		} elseif ($response->result == "101") {
    302 			// Decline
    303 			$this->addHistory($order_id, $this->config->get('payment_globalpay_remote_order_status_decline_id'), $message);
    304 		} elseif ($response->result == "102") {
    305 			// Referal B
    306 			$this->addHistory($order_id, $this->config->get('payment_globalpay_remote_order_status_decline_pending_id'), $message);
    307 		} elseif ($response->result == "103") {
    308 			// Referal A
    309 			$this->addHistory($order_id, $this->config->get('payment_globalpay_remote_order_status_decline_stolen_id'), $message);
    310 		} elseif ($response->result == "200") {
    311 			// Error Connecting to Bank
    312 			$this->addHistory($order_id, $this->config->get('payment_globalpay_remote_order_status_decline_bank_id'), $message);
    313 		} elseif ($response->result == "204") {
    314 			// Error Connecting to Bank
    315 			$this->addHistory($order_id, $this->config->get('payment_globalpay_remote_order_status_decline_bank_id'), $message);
    316 		} elseif ($response->result == "205") {
    317 			// Comms Error
    318 			$this->addHistory($order_id, $this->config->get('payment_globalpay_remote_order_status_decline_bank_id'), $message);
    319 		} else {
    320 			// Other
    321 			$this->addHistory($order_id, $this->config->get('payment_globalpay_remote_order_status_decline_id'), $message);
    322 		}
    323 
    324 		return $response;
    325 	}
    326 
    327 	public function addOrder($order_info, $response, $account, $order_ref) {
    328 		if ($this->config->get('payment_globalpay_remote_auto_settle') == 1) {
    329 			$settle_status = 1;
    330 		} else {
    331 			$settle_status = 0;
    332 		}
    333 
    334 		$this->db->query("INSERT INTO `" . DB_PREFIX . "globalpay_remote_order` SET `order_id` = '" . (int)$order_info['order_id'] . "', `settle_type` = '" . (int)$this->config->get('payment_globalpay_remote_auto_settle') . "', `order_ref` = '" . $this->db->escape($order_ref) . "', `order_ref_previous` = '" . $this->db->escape($order_ref) . "', `date_added` = now(), `date_modified` = now(), `capture_status` = '" . (int)$settle_status . "', `currency_code` = '" . $this->db->escape($order_info['currency_code']) . "', `pasref` = '" . $this->db->escape($response->pasref) . "', `pasref_previous` = '" . $this->db->escape($response->pasref) . "', `authcode` = '" . $this->db->escape($response->authcode) . "', `account` = '" . $this->db->escape($account) . "', `total` = '" . $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false) . "'");
    335 
    336 		return $this->db->getLastId();
    337 	}
    338 
    339 	public function addTransaction($globalpay_remote_order_id, $type, $order_info) {
    340 		$this->db->query("INSERT INTO `" . DB_PREFIX . "globalpay_remote_order_transaction` SET `globalpay_remote_order_id` = '" . (int)$globalpay_remote_order_id . "', `date_added` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false) . "'");
    341 	}
    342 
    343 	public function logger($message) {
    344 		if ($this->config->get('payment_globalpay_remote_debug') == 1) {
    345 			$log = new Log('globalpay_remote.log');
    346 			$log->write($message);
    347 		}
    348 	}
    349 
    350 	public function addHistory($order_id, $order_status_id, $comment) {
    351 		$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()");
    352 	}
    353 }