PageRenderTime 84ms CodeModel.GetById 42ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/catalog/controller/payment/sagepay_us.php

https://bitbucket.org/jjasko/opencart_serbian
PHP | 103 lines | 79 code | 24 blank | 0 comment | 7 complexity | 6f8c142fc576cf36f01f94045806b952 MD5 | raw file
  1<?php
  2class ControllerPaymentSagepayUS extends Controller {
  3	protected function index() {
  4    	$this->language->load('payment/sagepay_us');
  5		
  6		$this->data['text_credit_card'] = $this->language->get('text_credit_card');
  7		$this->data['text_wait'] = $this->language->get('text_wait');
  8		
  9		$this->data['entry_cc_owner'] = $this->language->get('entry_cc_owner');
 10		$this->data['entry_cc_number'] = $this->language->get('entry_cc_number');
 11		$this->data['entry_cc_expire_date'] = $this->language->get('entry_cc_expire_date');
 12		$this->data['entry_cc_cvv2'] = $this->language->get('entry_cc_cvv2');
 13		
 14		$this->data['button_confirm'] = $this->language->get('button_confirm');
 15		
 16		$this->data['months'] = array();
 17		
 18		for ($i = 1; $i <= 12; $i++) {
 19			$this->data['months'][] = array(
 20				'text'  => strftime('%B', mktime(0, 0, 0, $i, 1, 2000)), 
 21				'value' => sprintf('%02d', $i)
 22			);
 23		}
 24		
 25		$today = getdate();
 26
 27		$this->data['year_expire'] = array();
 28
 29		for ($i = $today['year']; $i < $today['year'] + 11; $i++) {
 30			$this->data['year_expire'][] = array(
 31				'text'  => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
 32				'value' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)) 
 33			);
 34		}
 35		
 36		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/sagepay_us.tpl')) {
 37			$this->template = $this->config->get('config_template') . '/template/payment/sagepay_us.tpl';
 38		} else {
 39			$this->template = 'default/template/payment/sagepay_us.tpl';
 40		}	
 41		
 42		$this->render();		
 43	}
 44	
 45	public function send() {
 46		$this->load->model('checkout/order');
 47		
 48		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
 49		
 50		$url = 'https://www.sagepayments.net/cgi-bin/eftbankcard.dll?transaction';
 51		
 52		$data  = 'm_id=' . $this->config->get('sagepay_us_merchant_id');
 53		$data .= '&m_key=' . $this->config->get('sagepay_us_merchant_key');
 54		$data .= '&T_amt=' . urlencode($this->currency->format($order_info['total'], $order_info['currency_code'], 1.00000, false));
 55		$data .= '&T_ordernum=' . $this->session->data['order_id'];
 56		$data .= '&C_name=' . urlencode($this->request->post['cc_owner']);
 57		$data .= '&C_address=' . urlencode($order_info['payment_address_1']);
 58		$data .= '&C_state=' . urlencode($order_info['payment_zone']);
 59		$data .= '&C_city=' . urlencode($order_info['payment_city']);
 60		$data .= '&C_cardnumber=' . urlencode($this->request->post['cc_number']);
 61		$data .= '&C_exp=' . urlencode($this->request->post['cc_expire_date_month'] . substr($this->request->post['cc_expire_date_year'], '2'));
 62		$data .= '&C_cvv=' . urlencode($this->request->post['cc_cvv2']);
 63		$data .= '&C_zip=' . urlencode($order_info['payment_postcode']);
 64		$data .= '&C_email=' . urlencode($order_info['email']);
 65		$data .= '&T_code=02';
 66		
 67		$ch = curl_init();
 68		
 69		curl_setopt($ch, CURLOPT_URL, $url);
 70		curl_setopt($ch, CURLOPT_POST, 1);
 71		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 72		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 73
 74		$response = curl_exec($ch);
 75		
 76		curl_close($ch);
 77
 78		$json = array();
 79															
 80		if ($response[1] == 'A') {
 81			$this->model_checkout_order->confirm($this->session->data['order_id'], $this->config->get('config_order_status_id'));
 82
 83			$message  = 'Approval Indicator: ' . $response[1] . "\n";
 84			$message .= 'Approval/Error Code: ' . substr($response, 2, 6) . "\n";
 85			$message .= 'Approval/Error Message: ' . substr($response, 8, 32) . "\n";
 86			$message .= 'Front-End Indicator: ' . substr($response, 40, 2) . "\n";
 87			$message .= 'CVV Indicator: ' . $response[42] . "\n";
 88			$message .= 'AVS Indicator: ' . $response[43] . "\n";
 89			$message .= 'Risk Indicator: ' . substr($response, 44, 2) . "\n";
 90			$message .= 'Reference: ' . substr($response, 46, 10) . "\n";
 91			$message .= 'Order Number: ' . substr($response, strpos($response, chr(28)) + 1, strrpos($response, chr(28) - 1)) . "\n";
 92			
 93			$this->model_checkout_order->update($this->session->data['order_id'], $this->config->get('sagepay_us_order_status_id'), $message, false);
 94
 95			$json['success'] = $this->url->link('checkout/success');
 96		} else {
 97			$json['error'] = substr($response, 8, 32);
 98		}
 99		
100		$this->response->setOutput(json_encode($json));		
101	}	
102}
103?>