shop.balmet.com

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

securetrading_ws.php (17212B)


      1 <?php
      2 class ControllerExtensionPaymentSecureTradingWs extends Controller {
      3 	public function index() {
      4 		$this->load->model('checkout/order');
      5 		$this->load->language('extension/payment/securetrading_ws');
      6 
      7 		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
      8 
      9 		if ($order_info) {
     10 			$data['entry_type'] = $this->language->get('entry_type');
     11 			$data['entry_number'] = $this->language->get('entry_number');
     12 			$data['entry_expire_date'] = $this->language->get('entry_expire_date');
     13 			$data['entry_cvv2'] = $this->language->get('entry_cvv2');
     14 
     15 			$data['text_card_details'] = $this->language->get('text_card_details');
     16 			$data['text_wait'] = $this->language->get('text_wait');
     17 
     18 			$data['button_confirm'] = $this->language->get('button_confirm');
     19 
     20 			$cards = array(
     21 				'AMEX' => 'American Express',
     22 				'VISA' => 'Visa',
     23 				'DELTA' => 'Visa Debit',
     24 				'ELECTRON' => 'Visa Electron',
     25 				'PURCHASING' => 'Visa Purchasing',
     26 				'VPAY' => 'V Pay',
     27 				'MASTERCARD' => 'MasterCard',
     28 				'MASTERCARDDEBIT' => 'MasterCard Debit',
     29 				'MAESTRO' => 'Maestro',
     30 				'PAYPAL' => 'PayPal',
     31 			);
     32 
     33 			for ($i = 1; $i <= 12; $i++) {
     34 				$data['months'][] = array(
     35 					'text' => strftime('%B', mktime(0, 0, 0, $i, 1, 2000)),
     36 					'value' => sprintf('%02d', $i)
     37 				);
     38 			}
     39 
     40 			$today = getdate();
     41 
     42 			$data['year_expire'] = array();
     43 
     44 			for ($i = $today['year']; $i < $today['year'] + 11; $i++) {
     45 				$data['year_expire'][] = array(
     46 					'text' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
     47 					'value' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i))
     48 				);
     49 			}
     50 
     51 			$data['cards'] = array();
     52 
     53 			foreach ($this->config->get('payment_securetrading_ws_cards_accepted') as $card_type) {
     54 				$data['cards'][$card_type] = $cards[$card_type];
     55 			}
     56 
     57 			return $this->load->view('extension/payment/securetrading_ws', $data);
     58 		}
     59 	}
     60 
     61 	public function process() {
     62 		$this->load->model('checkout/order');
     63 		$this->load->model('localisation/country');
     64 		$this->load->model('extension/payment/securetrading_ws');
     65 		$this->load->language('extension/payment/securetrading_ws');
     66 
     67 		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     68 
     69 		if ($order_info) {
     70 			if ($this->config->get('payment_securetrading_ws_3d_secure')) {
     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', 'THREEDQUERY');
     77 
     78 				$merchant_node = $request_node->addChild('merchant');
     79 				$merchant_node->addChild('orderreference', $order_info['order_id']);
     80 				$merchant_node->addChild('termurl', $this->url->link('extension/payment/securetrading_ws/threedreturn', '', true));
     81 
     82 				$settlement_node = $request_node->addChild('settlement');
     83 				$settlement_date = date('Y-m-d', strtotime(date('Y-m-d') . ' +' . $this->config->get('payment_securetrading_ws_settle_due_date') . ' days'));
     84 				$settlement_node->addChild('settleduedate', $settlement_date);
     85 				$settlement_node->addChild('settlestatus', $this->config->get('payment_securetrading_ws_settle_status'));
     86 
     87 				$customer_node = $request_node->addChild('customer');
     88 				$customer_node->addChild('useragent', $order_info['user_agent']);
     89 				$customer_node->addChild('accept', $this->request->server['HTTP_ACCEPT']);
     90 
     91 				$billing_node = $request_node->addChild('billing');
     92 				$amount_node = $billing_node->addChild('amount', str_replace('.', '', $this->model_extension_payment_securetrading_ws->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'])));
     93 				$amount_node->addAttribute('currencycode', $order_info['currency_code']);
     94 
     95 				$billing_node->addChild('premise', $order_info['payment_address_1']);
     96 				$billing_node->addChild('postcode', $order_info['payment_postcode']);
     97 
     98 				$name_node = $billing_node->addChild('name');
     99 				$name_node->addChild('first', $order_info['payment_firstname']);
    100 				$name_node->addChild('last', $order_info['payment_lastname']);
    101 
    102 				$payment_node = $billing_node->addChild('payment');
    103 				$payment_node->addAttribute('type', $this->request->post['type']);
    104 				$payment_node->addChild('pan', $this->request->post['number']);
    105 				$payment_node->addChild('expirydate', $this->request->post['expire_month'] . '/' . $this->request->post['expire_year']);
    106 				$payment_node->addChild('securitycode', $this->request->post['cvv2']);
    107 
    108 				$operation_node = $request_node->addChild('operation');
    109 				$operation_node->addChild('sitereference', $this->config->get('payment_securetrading_ws_site_reference'));
    110 				$operation_node->addChild('accounttypedescription', 'ECOM');
    111 
    112 				$response = $this->model_extension_payment_securetrading_ws->call($requestblock_xml->asXML());
    113 
    114 				if ($response !== false) {
    115 					$response_xml = simplexml_load_string($response);
    116 
    117 					if ($response_xml->response['type'] == 'THREEDQUERY') {
    118 						$error_code = (int)$response_xml->response->error->code;
    119 
    120 						if ($error_code == 0) {
    121 							$enrolled = (string)$response_xml->response->threedsecure->enrolled;
    122 
    123 							if ($enrolled == 'Y') {
    124 								$acs_url = (string)$response_xml->response->threedsecure->acsurl;
    125 								$md = (string)$response_xml->response->threedsecure->md;
    126 								$pareq = (string)$response_xml->response->threedsecure->pareq;
    127 
    128 								$this->model_extension_payment_securetrading_ws->addMd($order_info['order_id'], $md);
    129 
    130 								$json['status'] = 1;
    131 								$json['acs_url'] = $acs_url;
    132 								$json['md'] = $md;
    133 								$json['pareq'] = $pareq;
    134 								$json['term_url'] = $this->url->link('extension/payment/securetrading_ws/threedreturn', '', true);
    135 							} else {
    136 								$requestblock_xml = new SimpleXMLElement('<requestblock></requestblock>');
    137 								$requestblock_xml->addAttribute('version', '3.67');
    138 								$requestblock_xml->addChild('alias', $this->config->get('payment_securetrading_ws_username'));
    139 
    140 								$request_node = $requestblock_xml->addChild('request');
    141 								$request_node->addAttribute('type', 'AUTH');
    142 
    143 								$request_node->addChild('merchant')->addChild('orderreference', $order_info['order_id']);
    144 
    145 								$operation_node = $request_node->addChild('operation');
    146 								$operation_node->addChild('parenttransactionreference', (string)$response_xml->response->transactionreference);
    147 								$operation_node->addChild('sitereference', $this->config->get('payment_securetrading_ws_site_reference'));
    148 
    149 								$response = $this->model_extension_payment_securetrading_ws->call($requestblock_xml->asXML());
    150 
    151 								$json = $this->processAuthResponse($response, $order_info['order_id']);
    152 							}
    153 						} else {
    154 							$json['message'] = $this->language->get('text_transaction_declined');
    155 							$json['status'] = 0;
    156 						}
    157 					} else {
    158 						$json['message'] = $this->language->get('text_transaction_failed');
    159 						$json['status'] = 0;
    160 					}
    161 				} else {
    162 					$json['message'] = $this->language->get('text_connection_error');
    163 					$json['status'] = 0;
    164 				}
    165 			} else {
    166 				$country = $this->model_localisation_country->getCountry($order_info['payment_country_id']);
    167 
    168 				$json = array();
    169 
    170 				$requestblock_xml = new SimpleXMLElement('<requestblock></requestblock>');
    171 				$requestblock_xml->addAttribute('version', '3.67');
    172 				$requestblock_xml->addChild('alias', $this->config->get('payment_securetrading_ws_username'));
    173 
    174 				$request_node = $requestblock_xml->addChild('request');
    175 				$request_node->addAttribute('type', 'AUTH');
    176 				$operation_node = $request_node->addChild('operation');
    177 				$operation_node->addChild('sitereference', $this->config->get('payment_securetrading_ws_site_reference'));
    178 				$operation_node->addChild('accounttypedescription', 'ECOM');
    179 
    180 				$merchant_node = $request_node->addChild('merchant');
    181 				$merchant_node->addChild('orderreference', $order_info['order_id']);
    182 
    183 				$settlement_node = $request_node->addChild('settlement');
    184 				$settlement_date = date('Y-m-d', strtotime(date('Y-m-d') . ' +' . $this->config->get('payment_securetrading_ws_settle_due_date') . ' days'));
    185 				$settlement_node->addChild('settleduedate', $settlement_date);
    186 				$settlement_node->addChild('settlestatus', $this->config->get('payment_securetrading_ws_settle_status'));
    187 
    188 				$billing_node = $request_node->addChild('billing');
    189 				$billing_node->addChild('premise', $order_info['payment_address_1']);
    190 				$billing_node->addChild('street', $order_info['payment_address_2']);
    191 				$billing_node->addChild('town', $order_info['payment_city']);
    192 				$billing_node->addChild('county', $order_info['payment_zone']);
    193 				$billing_node->addChild('country', $country['iso_code_2']);
    194 				$billing_node->addChild('postcode', $order_info['payment_postcode']);
    195 				$billing_node->addChild('email', $order_info['email']);
    196 				$name_node = $billing_node->addChild('name');
    197 
    198 				$name_node->addChild('first', $order_info['payment_firstname']);
    199 				$name_node->addChild('last', $order_info['payment_lastname']);
    200 
    201 				$amount_node = $billing_node->addChild('amount', str_replace('.', '', $this->model_extension_payment_securetrading_ws->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'])));
    202 				$amount_node->addAttribute('currencycode', $order_info['currency_code']);
    203 
    204 				$payment_node = $billing_node->addChild('payment');
    205 				$payment_node->addAttribute('type', $this->request->post['type']);
    206 				$payment_node->addChild('pan', $this->request->post['number']);
    207 				$payment_node->addChild('expirydate', $this->request->post['expire_month'] . '/' . $this->request->post['expire_year']);
    208 				$payment_node->addChild('securitycode', $this->request->post['cvv2']);
    209 
    210 				$response = $this->model_extension_payment_securetrading_ws->call($requestblock_xml->asXML());
    211 
    212 				$json = $this->processAuthResponse($response, $order_info['order_id']);
    213 			}
    214 			$this->response->setOutput(json_encode($json));
    215 		}
    216 	}
    217 
    218 	public function threedreturn() {
    219 		$this->load->model('checkout/order');
    220 		$this->load->model('extension/payment/securetrading_ws');
    221 		$this->load->language('extension/payment/securetrading_ws');
    222 
    223 		// Using unmodified $_POST to access values as per Secure Trading's requirements
    224 		if (isset($_POST['PaRes']) && !empty($_POST['PaRes']) && isset($_POST['MD']) && !empty($_POST['MD'])) {
    225 			$md = $_POST['MD'];
    226 			$pares = $_POST['PaRes'];
    227 
    228 			$order_id = $this->model_extension_payment_securetrading_ws->getOrderId($md);
    229 
    230 			if ($order_id) {
    231 				$requestblock_xml = new SimpleXMLElement('<requestblock></requestblock>');
    232 				$requestblock_xml->addAttribute('version', '3.67');
    233 				$requestblock_xml->addChild('alias', $this->config->get('payment_securetrading_ws_username'));
    234 
    235 				$request_node = $requestblock_xml->addChild('request');
    236 				$request_node->addAttribute('type', 'AUTH');
    237 
    238 				$request_node->addChild('merchant')->addChild('orderreference', $order_id);
    239 
    240 				$operation_node = $request_node->addChild('operation');
    241 				$operation_node->addChild('md', $md);
    242 				$operation_node->addChild('pares', $pares);
    243 
    244 				$response = $this->model_extension_payment_securetrading_ws->call($requestblock_xml->asXML());
    245 
    246 				if ($response) {
    247 					$response_xml = simplexml_load_string($response);
    248 
    249 					$error_code = (int)$response_xml->response->error->code;
    250 
    251 					if ($error_code == 0) {
    252 						$postcode_status = (int)$response_xml->response->security->postcode;
    253 						$security_code_status = (int)$response_xml->response->security->securitycode;
    254 						$address_status = (int)$response_xml->response->security->address;
    255 						$authcode = (string)$response_xml->response->authcode;
    256 						$threed_status = (string)$response_xml->response->threedsecure->status;
    257 
    258 						$status_code_mapping = array(
    259 							0 => $this->language->get('text_not_given'),
    260 							1 => $this->language->get('text_not_checked'),
    261 							2 => $this->language->get('text_match'),
    262 							4 => $this->language->get('text_not_match'),
    263 						);
    264 
    265 						$threed_status_mapping = array(
    266 							'Y' => $this->language->get('text_authenticated'),
    267 							'N' => $this->language->get('text_not_authenticated'),
    268 							'A' => $this->language->get('text_authentication_not_completed'),
    269 							'U' => $this->language->get('text_unable_to_perform'),
    270 						);
    271 
    272 						$message = sprintf($this->language->get('text_auth_code'), $authcode) . "\n";
    273 						$message .= sprintf($this->language->get('text_postcode_check'), $status_code_mapping[$postcode_status]) . "\n";
    274 						$message .= sprintf($this->language->get('text_security_code_check'), $status_code_mapping[$security_code_status]) . "\n";
    275 						$message .= sprintf($this->language->get('text_address_check'), $status_code_mapping[$address_status]) . "\n";
    276 						$message .= sprintf($this->language->get('text_3d_secure_check'), $threed_status_mapping[$threed_status]) . "\n";
    277 
    278 						$transaction_reference = (string)$response_xml->response->transactionreference;
    279 						$this->model_extension_payment_securetrading_ws->updateReference($order_id, $transaction_reference);
    280 
    281 						$this->model_extension_payment_securetrading_ws->confirmOrder($order_id, $this->config->get('payment_securetrading_ws_order_status_id'));
    282 						$this->model_extension_payment_securetrading_ws->updateOrder($order_id, $this->config->get('payment_securetrading_ws_order_status_id'), $message);
    283 
    284 						$this->response->redirect($this->url->link('checkout/success', '', true));
    285 					} else {
    286 						$this->model_extension_payment_securetrading_ws->updateOrder($order_id, $this->config->get('payment_securetrading_ws_declined_order_status_id'));
    287 
    288 						$this->session->data['error'] = $this->language->get('text_transaction_declined');
    289 						$this->response->redirect($this->url->link('checkout/checkout', '', true));
    290 					}
    291 				} else {
    292 					$this->session->data['error'] = $this->language->get('error_failure');
    293 					$this->response->redirect($this->url->link('checkout/checkout', '', true));
    294 				}
    295 			} else {
    296 				$this->session->data['error'] = $this->language->get('error_failure');
    297 				$this->response->redirect($this->url->link('checkout/checkout', '', true));
    298 			}
    299 		} else {
    300 			$this->session->data['error'] = $this->language->get('error_failure');
    301 			$this->response->redirect($this->url->link('checkout/checkout', '', true));
    302 		}
    303 	}
    304 
    305 	private function processAuthResponse($response, $order_id) {
    306 		$json = array();
    307 
    308 		if ($response !== false) {
    309 			$response_xml = simplexml_load_string($response);
    310 
    311 			if ($response_xml->response['type'] == 'AUTH') {
    312 				$error_code = (int)$response_xml->response->error->code;
    313 
    314 				if ($error_code == 0) {
    315 					$postcode_status = (int)$response_xml->response->security->postcode;
    316 					$security_code_status = (int)$response_xml->response->security->securitycode;
    317 					$address_status = (int)$response_xml->response->security->address;
    318 					$authcode = (string)$response_xml->response->authcode;
    319 
    320 					$status_code_mapping = array(
    321 						0 => $this->language->get('text_not_given'),
    322 						1 => $this->language->get('text_not_checked'),
    323 						2 => $this->language->get('text_match'),
    324 						4 => $this->language->get('text_not_match'),
    325 					);
    326 
    327 					$message = sprintf($this->language->get('text_auth_code'), $authcode) . "\n";
    328 					$message .= sprintf($this->language->get('text_postcode_check'), $status_code_mapping[$postcode_status]) . "\n";
    329 					$message .= sprintf($this->language->get('text_security_code_check'), $status_code_mapping[$security_code_status]) . "\n";
    330 					$message .= sprintf($this->language->get('text_address_check'), $status_code_mapping[$address_status]) . "\n";
    331 
    332 					$transaction_reference = (string)$response_xml->response->transactionreference;
    333 					$this->model_extension_payment_securetrading_ws->updateReference($order_id, $transaction_reference);
    334 
    335 					$this->model_extension_payment_securetrading_ws->confirmOrder($order_id, $this->config->get('payment_securetrading_ws_order_status_id'));
    336 					$this->model_extension_payment_securetrading_ws->updateOrder($order_id, $this->config->get('payment_securetrading_ws_order_status_id'), $message);
    337 
    338 					$json['redirect'] = $this->url->link('checkout/success');
    339 					$json['status'] = 1;
    340 				} else {
    341 					$this->model_extension_payment_securetrading_ws->updateOrder($order_id, $this->config->get('payment_securetrading_ws_declined_order_status_id'));
    342 
    343 					$json['message'] = $this->language->get('text_transaction_declined');
    344 					$json['status'] = 0;
    345 				}
    346 			} else {
    347 				$this->model_extension_payment_securetrading_ws->updateOrder($order_id, $this->config->get('payment_securetrading_ws_failed_order_status_id'));
    348 
    349 				$json['message'] = $this->language->get('text_transaction_failed');
    350 				$json['status'] = 0;
    351 			}
    352 		} else {
    353 			$json['message'] = $this->language->get('text_connection_error');
    354 			$json['status'] = 0;
    355 		}
    356 
    357 		return $json;
    358 	}
    359 }