PageRenderTime 145ms CodeModel.GetById 40ms app.highlight 46ms RepoModel.GetById 55ms app.codeStats 0ms

/catalog/controller/payment/sagepay_direct.php

https://bitbucket.org/jjasko/opencart_serbian
PHP | 360 lines | 275 code | 85 blank | 0 comment | 57 complexity | aa9f1d94c4da99b7bdb2c8a941bb69d9 MD5 | raw file
  1<?php
  2class ControllerPaymentSagepayDirect extends Controller {
  3	protected function index() {
  4		$this->language->load('payment/sagepay_direct');
  5		
  6		$this->data['text_credit_card'] = $this->language->get('text_credit_card');
  7		$this->data['text_start_date'] = $this->language->get('text_start_date');
  8		$this->data['text_issue'] = $this->language->get('text_issue');
  9		$this->data['text_wait'] = $this->language->get('text_wait');
 10		
 11		$this->data['entry_cc_owner'] = $this->language->get('entry_cc_owner');
 12		$this->data['entry_cc_type'] = $this->language->get('entry_cc_type');
 13		$this->data['entry_cc_number'] = $this->language->get('entry_cc_number');
 14		$this->data['entry_cc_start_date'] = $this->language->get('entry_cc_start_date');
 15		$this->data['entry_cc_expire_date'] = $this->language->get('entry_cc_expire_date');
 16		$this->data['entry_cc_cvv2'] = $this->language->get('entry_cc_cvv2');
 17		$this->data['entry_cc_issue'] = $this->language->get('entry_cc_issue');
 18		
 19		$this->data['button_confirm'] = $this->language->get('button_confirm');
 20		
 21		$this->data['cards'] = array();
 22
 23		$this->data['cards'][] = array(
 24			'text'  => 'Visa', 
 25			'value' => 'VISA'
 26		);
 27
 28		$this->data['cards'][] = array(
 29			'text'  => 'MasterCard', 
 30			'value' => 'MC'
 31		);
 32
 33		$this->data['cards'][] = array(
 34			'text'  => 'Visa Delta/Debit', 
 35			'value' => 'DELTA'
 36		);
 37		
 38		$this->data['cards'][] = array(
 39			'text'  => 'Solo', 
 40			'value' => 'SOLO'
 41		);	
 42		
 43		$this->data['cards'][] = array(
 44			'text'  => 'Maestro', 
 45			'value' => 'MAESTRO'
 46		);
 47		
 48		$this->data['cards'][] = array(
 49			'text'  => 'Visa Electron UK Debit', 
 50			'value' => 'UKE'
 51		);
 52		
 53		$this->data['cards'][] = array(
 54			'text'  => 'American Express', 
 55			'value' => 'AMEX'
 56		);
 57		
 58		$this->data['cards'][] = array(
 59			'text'  => 'Diners Club', 
 60			'value' => 'DC'
 61		);
 62		
 63		$this->data['cards'][] = array(
 64			'text'  => 'Japan Credit Bureau', 
 65			'value' => 'JCB'
 66		);
 67		
 68		$this->data['months'] = array();
 69		
 70		for ($i = 1; $i <= 12; $i++) {
 71			$this->data['months'][] = array(
 72				'text'  => strftime('%B', mktime(0, 0, 0, $i, 1, 2000)), 
 73				'value' => sprintf('%02d', $i)
 74			);
 75		}
 76		
 77		$today = getdate();
 78		
 79		$this->data['year_valid'] = array();
 80		
 81		for ($i = $today['year'] - 10; $i < $today['year'] + 1; $i++) {	
 82			$this->data['year_valid'][] = array(
 83				'text'  => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)), 
 84				'value' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i))
 85			);
 86		}
 87
 88		$this->data['year_expire'] = array();
 89
 90		for ($i = $today['year']; $i < $today['year'] + 11; $i++) {
 91			$this->data['year_expire'][] = array(
 92				'text'  => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
 93				'value' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)) 
 94			);
 95		}
 96		
 97		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/sagepay_direct.tpl')) {
 98			$this->template = $this->config->get('config_template') . '/template/payment/sagepay_direct.tpl';
 99		} else {
100			$this->template = 'default/template/payment/sagepay_direct.tpl';
101		}	
102		
103		$this->render();		
104	}
105	
106	public function send() {
107		if ($this->config->get('sagepay_direct_test') == 'live') {
108    		$url = 'https://live.sagepay.com/gateway/service/vspdirect-register.vsp';
109		} elseif ($this->config->get('sagepay_direct_test') == 'test') {
110			$url = 'https://test.sagepay.com/gateway/service/vspdirect-register.vsp';		
111		} elseif ($this->config->get('sagepay_direct_test') == 'sim') {
112    		$url = 'https://test.sagepay.com/Simulator/VSPDirectGateway.asp';
113  		} 		
114
115		$this->load->model('checkout/order');
116		
117		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
118		
119        $data = array();
120		
121		$data['VPSProtocol'] = '2.23';
122        $data['ReferrerID'] = 'E511AF91-E4A0-42DE-80B0-09C981A3FB61';
123        $data['Vendor'] = $this->config->get('sagepay_direct_vendor');
124		$data['VendorTxCode'] = $this->session->data['order_id'];
125		$data['Amount'] = $this->currency->format($order_info['total'], $order_info['currency_code'], 1.00000, false);
126		$data['Currency'] = $this->currency->getCode();
127		$data['Description'] = substr($this->config->get('config_name'), 0, 100);
128		$data['CardHolder'] = $this->request->post['cc_owner'];
129		$data['CardNumber'] = $this->request->post['cc_number'];
130		$data['ExpiryDate'] = $this->request->post['cc_expire_date_month'] . substr($this->request->post['cc_expire_date_year'], 2);
131		$data['CardType'] = $this->request->post['cc_type'];
132		$data['TxType'] = $this->config->get('sagepay_direct_transaction');
133		$data['StartDate'] = $this->request->post['cc_start_date_month'] . substr($this->request->post['cc_start_date_year'], 2);
134		$data['IssueNumber'] = $this->request->post['cc_issue'];
135		$data['CV2'] = $this->request->post['cc_cvv2'];
136		
137		$data['BillingSurname'] = substr($order_info['payment_lastname'], 0, 20);
138		$data['BillingFirstnames'] = substr($order_info['payment_firstname'], 0, 20);
139		$data['BillingAddress1'] = substr($order_info['payment_address_1'], 0, 100);
140		
141		if ($order_info['payment_address_2']) {
142        	$data['BillingAddress2'] = $order_info['payment_address_2'];
143		}
144		
145		$data['BillingCity'] = substr($order_info['payment_city'], 0, 40);
146		$data['BillingPostCode'] = substr($order_info['payment_postcode'], 0, 10);
147		$data['BillingCountry'] = $order_info['payment_iso_code_2'];
148
149		if ($order_info['payment_iso_code_2'] == 'US') {
150			$data['BillingState'] = $order_info['payment_zone_code'];
151		}
152		
153		$data['BillingPhone'] = substr($order_info['telephone'], 0, 20);
154		
155		if ($this->cart->hasShipping()) {
156			$data['DeliverySurname'] = substr($order_info['shipping_lastname'], 0, 20);
157			$data['DeliveryFirstnames'] = substr($order_info['shipping_firstname'], 0, 20);
158			$data['DeliveryAddress1'] = substr($order_info['shipping_address_1'], 0, 100);
159			
160			if ($order_info['shipping_address_2']) {
161        		$data['DeliveryAddress2'] = $order_info['shipping_address_2'];
162			}		
163			
164			$data['DeliveryCity'] = substr($order_info['shipping_city'], 0, 40);
165			$data['DeliveryPostCode'] = substr($order_info['shipping_postcode'], 0, 10);
166			$data['DeliveryCountry'] = $order_info['shipping_iso_code_2'];
167			
168			if ($order_info['shipping_iso_code_2'] == 'US') {
169				$data['DeliveryState'] = $order_info['shipping_zone_code'];
170			}
171			
172			$data['CustomerName'] = substr($order_info['firstname'] . ' ' . $order_info['lastname'], 0, 100);
173			$data['DeliveryPhone'] = substr($order_info['telephone'], 0, 20);
174		} else {
175			$data['DeliveryFirstnames'] = $order_info['payment_firstname'];
176        	$data['DeliverySurname'] = $order_info['payment_lastname'];
177        	$data['DeliveryAddress1'] = $order_info['payment_address_1'];
178		
179			if ($order_info['payment_address_2']) {
180        		$data['DeliveryAddress2'] = $order_info['payment_address_2'];
181			}
182		
183        	$data['DeliveryCity'] = $order_info['payment_city'];
184        	$data['DeliveryPostCode'] = $order_info['payment_postcode'];
185        	$data['DeliveryCountry'] = $order_info['payment_iso_code_2'];
186		
187			if ($order_info['payment_iso_code_2'] == 'US') {
188				$data['DeliveryState'] = $order_info['payment_zone_code'];
189			}
190		
191			$data['DeliveryPhone'] = $order_info['telephone'];			
192		}		
193		
194		$data['CustomerEMail'] = substr($order_info['email'], 0, 255);
195		$data['Apply3DSecure'] = '0';
196		$data['ClientIPAddress'] = $this->request->server['REMOTE_ADDR'];
197		
198		$curl = curl_init($url);
199
200		curl_setopt($curl, CURLOPT_PORT, 443);
201		curl_setopt($curl, CURLOPT_HEADER, 0);
202		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
203		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
204		curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
205		curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
206		curl_setopt($curl, CURLOPT_POST, 1);
207		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
208 
209		$response = curl_exec($curl);
210  		
211		curl_close($curl);
212
213		$data = array();
214		
215		$response_data = explode(chr(10), $response);
216
217		foreach ($response_data as $string) {
218			if (strpos($string, '=')) {
219				$parts = explode('=', $string, 2);
220				
221				$data[trim($parts[0])] = trim($parts[1]);
222			}
223		}
224		
225		$json = array();
226      
227		if ($data['Status'] == '3DAUTH') {
228			$json['ACSURL'] = $data['ACSURL'];
229			$json['MD'] = $data['MD'];
230			$json['PaReq'] = $data['PAReq'];
231			$json['TermUrl'] = $this->url->link('payment/sagepay_direct/callback');
232		} elseif ($data['Status'] == 'OK' || $data['Status'] == 'AUTHENTICATED' || $data['Status'] == 'REGISTERED') {
233			$this->model_checkout_order->confirm($this->session->data['order_id'], $this->config->get('config_order_status_id'));
234			
235			$message = '';
236			
237			if (isset($data['TxAuthNo'])) {
238				$message .= 'TxAuthNo: ' . $data['TxAuthNo'] . "\n";
239			}
240
241			if (isset($data['AVSCV2'])) {
242				$message .= 'AVSCV2: ' . $data['AVSCV2'] . "\n";
243			}
244
245			if (isset($data['AddressResult'])) {
246				$message .= 'AddressResult: ' . $data['AddressResult'] . "\n";
247			}
248
249			if (isset($data['PostCodeResult'])) {
250				$message .= 'PostCodeResult: ' . $data['PostCodeResult'] . "\n";
251			}
252
253			if (isset($data['CV2Result'])) {
254				$message .= 'CV2Result: ' . $data['CV2Result'] . "\n";
255			}
256			
257			if (isset($data['3DSecureStatus'])) {
258				$message .= '3DSecureStatus: ' . $data['3DSecureStatus'] . "\n";
259			}
260			
261			if (isset($data['CAVV'])) {
262				$message .= 'CAVV: ' . $data['CAVV'] . "\n";
263			}
264			
265			$this->model_checkout_order->update($this->session->data['order_id'], $this->config->get('sagepay_direct_order_status_id'), $message, false);
266
267			$json['success'] = $this->url->link('checkout/success'); 			
268		} else {
269			$json['error'] = $data['StatusDetail'];
270		}
271
272		$this->response->setOutput(json_encode($json));
273	}	 
274	
275	public function callback() {
276		if (isset($this->session->data['order_id'])) {
277			if ($this->config->get('sagepay_direct_test') == 'live') {
278				$url = 'https://live.sagepay.com/gateway/service/direct3dcallback.vsp';
279			} elseif ($this->config->get('sagepay_direct_test') == 'test') {
280				$url = 'https://test.sagepay.com/gateway/service/direct3dcallback.vsp';		
281			} elseif ($this->config->get('sagepay_direct_test') == 'sim') {
282				$url = 'https://test.sagepay.com/Simulator/VSPDirectCallback.asp';
283			} 	
284			
285			$curl = curl_init($url);
286	
287			curl_setopt($curl, CURLOPT_PORT, 443);
288			curl_setopt($curl, CURLOPT_HEADER, 0);
289			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
290			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
291			curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
292			curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
293			curl_setopt($curl, CURLOPT_POST, 1);
294			curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->request->post));
295	
296			$response = curl_exec($curl);
297			
298			curl_close($curl);
299			
300			$data = array();
301			
302			$response_data = explode(chr(10), $response);
303	
304			foreach ($response_data as $string) {
305				if (strpos($string, '=')) {
306					$parts = explode('=', $string, 2);
307					
308					$data[trim($parts[0])] = trim($parts[1]);
309				}
310			}
311			
312			if ($data['Status'] == 'OK' || $data['Status'] == 'AUTHENTICATED' || $data['Status'] == 'REGISTERED') {
313				$this->load->model('checkout/order');
314				
315				$this->model_checkout_order->confirm($this->session->data['order_id'], $this->config->get('config_order_status_id'));
316				
317				$message = '';
318				
319				if (isset($data['TxAuthNo'])) {
320					$message .= 'TxAuthNo: ' . $data['TxAuthNo'] . "\n";
321				}
322	
323				if (isset($data['AVSCV2'])) {
324					$message .= 'AVSCV2: ' . $data['AVSCV2'] . "\n";
325				}
326	
327				if (isset($data['AddressResult'])) {
328					$message .= 'AddressResult: ' . $data['AddressResult'] . "\n";
329				}
330	
331				if (isset($data['PostCodeResult'])) {
332					$message .= 'PostCodeResult: ' . $data['PostCodeResult'] . "\n";
333				}
334	
335				if (isset($data['CV2Result'])) {
336					$message .= 'CV2Result: ' . $data['CV2Result'] . "\n";
337				}
338				
339				if (isset($data['3DSecureStatus'])) {
340					$message .= '3DSecureStatus: ' . $data['3DSecureStatus'] . "\n";
341				}
342				
343				if (isset($data['CAVV'])) {
344					$message .= 'CAVV: ' . $data['CAVV'] . "\n";
345				}
346				
347				$this->model_checkout_order->update($this->session->data['order_id'], $this->config->get('sagepay_direct_order_status_id'), $message, false);	
348				
349				$this->redirect($this->url->link('checkout/success'));
350			} else {
351				$this->session->data['error'] = $data['StatusDetail'];
352
353				$this->redirect($this->url->link('checkout/checkout', '', 'SSL'));
354			}
355		} else {
356			$this->redirect($this->url->link('account/login', '', 'SSL'));
357		}
358	}
359}
360?>