shop.balmet.com

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

eway.php (11466B)


      1 <?php
      2 class ControllerExtensionPaymentEway extends Controller {
      3 	public function index() {
      4 		$this->load->language('extension/payment/eway');
      5 
      6 		$data['payment_type'] = $this->config->get('payment_eway_payment_type');
      7 
      8 		$data['months'] = array();
      9 
     10 		for ($i = 1; $i <= 12; $i++) {
     11 			$data['months'][] = array(
     12 				'text' => sprintf('%02d', $i),
     13 				'value' => sprintf('%02d', $i)
     14 			);
     15 		}
     16 
     17 		$today = getdate();
     18 
     19 		$data['year_expire'] = array();
     20 
     21 		for ($i = $today['year']; $i < $today['year'] + 11; $i++) {
     22 			$data['year_expire'][] = array(
     23 				'text' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
     24 				'value' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i))
     25 			);
     26 		}
     27 
     28 		$this->load->model('checkout/order');
     29 		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     30 
     31 		$amount = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
     32 
     33 		if ($this->config->get('payment_eway_test')) {
     34 			$data['text_testing'] = $this->language->get('text_testing');
     35 			$data['Endpoint'] = 'Sandbox';
     36 		} else {
     37 			$data['Endpoint'] = 'Production';
     38 		}
     39 
     40 		$request = new stdClass();
     41 
     42 		$request->Customer = new stdClass();
     43 		$request->Customer->Title = 'Mr.';
     44 		$request->Customer->FirstName = (string)substr($order_info['payment_firstname'], 0, 50);
     45 		$request->Customer->LastName = (string)substr($order_info['payment_lastname'], 0, 50);
     46 		$request->Customer->CompanyName = (string)substr($order_info['payment_company'], 0, 50);
     47 		$request->Customer->Street1 = (string)substr($order_info['payment_address_1'], 0, 50);
     48 		$request->Customer->Street2 = (string)substr($order_info['payment_address_2'], 0, 50);
     49 		$request->Customer->City = (string)substr($order_info['payment_city'], 0, 50);
     50 		$request->Customer->State = (string)substr($order_info['payment_zone'], 0, 50);
     51 		$request->Customer->PostalCode = (string)substr($order_info['payment_postcode'], 0, 30);
     52 		$request->Customer->Country = strtolower($order_info['payment_iso_code_2']);
     53 		$request->Customer->Email = $order_info['email'];
     54 		$request->Customer->Phone = (string)substr($order_info['telephone'], 0, 32);
     55 
     56 		$request->ShippingAddress = new stdClass();
     57 		$request->ShippingAddress->FirstName = (string)substr($order_info['shipping_firstname'], 0, 50);
     58 		$request->ShippingAddress->LastName = (string)substr($order_info['shipping_lastname'], 0, 50);
     59 		$request->ShippingAddress->Street1 = (string)substr($order_info['shipping_address_1'], 0, 50);
     60 		$request->ShippingAddress->Street2 = (string)substr($order_info['shipping_address_2'], 0, 50);
     61 		$request->ShippingAddress->City = (string)substr($order_info['shipping_city'], 0, 50);
     62 		$request->ShippingAddress->State = (string)substr($order_info['shipping_zone'], 0, 50);
     63 		$request->ShippingAddress->PostalCode = (string)substr($order_info['shipping_postcode'], 0, 30);
     64 		$request->ShippingAddress->Country = strtolower($order_info['shipping_iso_code_2']);
     65 		$request->ShippingAddress->Email = $order_info['email'];
     66 		$request->ShippingAddress->Phone = (string)substr($order_info['telephone'], 0, 32);
     67 		$request->ShippingAddress->ShippingMethod = "Unknown";
     68 
     69 		$invoice_desc = '';
     70 		foreach ($this->cart->getProducts() as $product) {
     71 			$item_price = $this->currency->format($product['price'], $order_info['currency_code'], false, false);
     72 			$item_total = $this->currency->format($product['total'], $order_info['currency_code'], false, false);
     73 			$item = new stdClass();
     74 			$item->SKU = (string)substr($product['product_id'], 0, 12);
     75 			$item->Description = (string)substr($product['name'], 0, 26);
     76 			$item->Quantity = strval($product['quantity']);
     77 			$item->UnitCost = strval($item_price * 100);
     78 			$item->Total = strval($item_total * 100);
     79 			$request->Items[] = $item;
     80 			$invoice_desc .= $product['name'] . ', ';
     81 		}
     82 		$invoice_desc = (string)substr($invoice_desc, 0, -2);
     83 		if (strlen($invoice_desc) > 64) {
     84 			$invoice_desc = (string)substr($invoice_desc, 0, 61) . '...';
     85 		}
     86 
     87 		$shipping = $this->currency->format($order_info['total'] - $this->cart->getSubTotal(), $order_info['currency_code'], false, false);
     88 
     89 		if ($shipping > 0) {
     90 			$item = new stdClass();
     91 			$item->SKU = '';
     92 			$item->Description = (string)substr($this->language->get('text_shipping'), 0, 26);
     93 			$item->Quantity = 1;
     94 			$item->UnitCost = $shipping * 100;
     95 			$item->Total = $shipping * 100;
     96 			$request->Items[] = $item;
     97 		}
     98 
     99 		$opt1 = new stdClass();
    100 		$opt1->Value = $order_info['order_id'];
    101 		$request->Options = array($opt1);
    102 
    103 		$request->Payment = new stdClass();
    104 		$request->Payment->TotalAmount = number_format($amount, 2, '.', '') * 100;
    105 		$request->Payment->InvoiceNumber = $this->session->data['order_id'];
    106 		$request->Payment->InvoiceDescription = $invoice_desc;
    107 		$request->Payment->InvoiceReference = (string)substr($this->config->get('config_name'), 0, 40) . ' - #' . $order_info['order_id'];
    108 		$request->Payment->CurrencyCode = $order_info['currency_code'];
    109 
    110 		$request->RedirectUrl = $this->url->link('extension/payment/eway/callback', '', true);
    111 		if ($this->config->get('payment_eway_transaction_method') == 'auth') {
    112 			$request->Method = 'Authorise';
    113 		} else {
    114 			$request->Method = 'ProcessPayment';
    115 		}
    116 		$request->TransactionType = 'Purchase';
    117 		$request->DeviceID = 'opencart-' . VERSION . ' eway-trans-2.1.2';
    118 		$request->CustomerIP = $this->request->server['REMOTE_ADDR'];
    119 
    120 		$this->load->model('extension/payment/eway');
    121 		$template = 'eway';
    122 		if ($this->config->get('payment_eway_paymode') == 'iframe') {
    123 			$request->CancelUrl = 'http://www.example.org';
    124 			$request->CustomerReadOnly = true;
    125 			$result = $this->model_extension_payment_eway->getSharedAccessCode($request);
    126 
    127 			$template = 'eway_iframe';
    128 		} else {
    129 			$result = $this->model_extension_payment_eway->getAccessCode($request);
    130 		}
    131 
    132 		// Check if any error returns
    133 		if (isset($result->Errors)) {
    134 			$error_array = explode(",", $result->Errors);
    135 			$lbl_error = "";
    136 			foreach ($error_array as $error) {
    137 				$error = $this->language->get('text_card_message_' . $error);
    138 				$lbl_error .= $error . "<br />\n";
    139 			}
    140 			$this->log->write('eWAY Payment error: ' . $lbl_error);
    141 		}
    142 
    143 		if (isset($lbl_error)) {
    144 			$data['error'] = $lbl_error;
    145 		} else {
    146 			if ($this->config->get('payment_eway_paymode') == 'iframe') {
    147 				$data['callback'] = $this->url->link('extension/payment/eway/callback', 'AccessCode=' . $result->AccessCode, true);
    148 				$data['SharedPaymentUrl'] = $result->SharedPaymentUrl;
    149 			}
    150 			$data['action'] = $result->FormActionURL;
    151 			$data['AccessCode'] = $result->AccessCode;
    152 		}
    153 
    154 		return $this->load->view('extension/payment/' . $template, $data);
    155 	}
    156 
    157 	public function callback() {
    158 		$this->load->language('extension/payment/eway');
    159 
    160 		if (isset($this->request->get['AccessCode']) || isset($this->request->get['amp;AccessCode'])) {
    161 
    162 			$this->load->model('extension/payment/eway');
    163 
    164 			if (isset($this->request->get['amp;AccessCode'])) {
    165 				$access_code = $this->request->get['amp;AccessCode'];
    166 			} else {
    167 				$access_code = $this->request->get['AccessCode'];
    168 			}
    169 
    170 			$result = $this->model_extension_payment_eway->getAccessCodeResult($access_code);
    171 
    172 			$is_error = false;
    173 
    174 			// Check if any error returns
    175 			if (isset($result->Errors)) {
    176 				$error_array = explode(",", $result->Errors);
    177 				$is_error = true;
    178 				$lbl_error = '';
    179 				foreach ($error_array as $error) {
    180 					$error = $this->language->get('text_card_message_' . $error);
    181 					$lbl_error .= $error . ", ";
    182 				}
    183 				$this->log->write('eWAY error: ' . $lbl_error);
    184 			}
    185 			if (!$is_error) {
    186 				$fraud = false;
    187 				if (!$result->TransactionStatus) {
    188 					$error_array = explode(", ", $result->ResponseMessage);
    189 					$is_error = true;
    190 					$lbl_error = '';
    191 					$log_error = '';
    192 					foreach ($error_array as $error) {
    193 						// Don't show fraud issues to customers
    194 						if (stripos($error, 'F') === false) {
    195 							$lbl_error .= $this->language->get('text_card_message_' . $error);
    196 						} else {
    197 							$fraud = true;
    198 						}
    199 						$log_error .= $this->language->get('text_card_message_' . $error) . ", ";
    200 					}
    201 					$log_error = substr($log_error, 0, -2);
    202 					$this->log->write('eWAY payment failed: ' . $log_error);
    203 				}
    204 			}
    205 
    206 			$this->load->model('checkout/order');
    207 
    208 			if ($is_error) {
    209 				if ($fraud) {
    210 					$this->response->redirect($this->url->link('checkout/failure', '', true));
    211 				} else {
    212 					$this->session->data['error'] = $this->language->get('text_transaction_failed');
    213 					$this->response->redirect($this->url->link('checkout/checkout', '', true));
    214 				}
    215 			} else {
    216 				$order_id = $result->Options[0]->Value;
    217 
    218 				$order_info = $this->model_checkout_order->getOrder($order_id);
    219 
    220 				$this->load->model('extension/payment/eway');
    221 				$eway_order_data = array(
    222 					'order_id' => $order_id,
    223 					'transaction_id' => $result->TransactionID,
    224 					'amount' => $result->TotalAmount / 100,
    225 					'currency_code' => $order_info['currency_code'],
    226 					'debug_data' => json_encode($result)
    227 				);
    228 
    229 				$error_array = explode(", ", $result->ResponseMessage);
    230 				$log_error = '';
    231 				foreach ($error_array as $error) {
    232 					if (stripos($error, 'F') !== false) {
    233 						$fraud = true;
    234 						$log_error .= $this->language->get('text_card_message_' . $error) . ", ";
    235 					}
    236 				}
    237 				$log_error = substr($log_error, 0, -2);
    238 
    239 				$eway_order_id = $this->model_extension_payment_eway->addOrder($eway_order_data);
    240 				$this->model_extension_payment_eway->addTransaction($eway_order_id, $this->config->get('payment_eway_transaction_method'), $result->TransactionID, $order_info);
    241 
    242 				if ($fraud) {
    243 					$message = 'Suspected fraud order: ' . $log_error . "\n";
    244 				} else {
    245 					$message = "eWAY Payment accepted\n";
    246 				}
    247 				$message .= 'Transaction ID: ' . $result->TransactionID . "\n";
    248 				$message .= 'Authorisation Code: ' . $result->AuthorisationCode . "\n";
    249 				$message .= 'Card Response Code: ' . $result->ResponseCode . "\n";
    250 
    251 				if ($fraud) {
    252 					$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_eway_order_status_fraud_id'), $message);
    253 				} elseif ($this->config->get('payment_eway_transaction_method') == 'payment') {
    254 					$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_eway_order_status_id'), $message);
    255 				} else {
    256 					$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_eway_order_status_auth_id'), $message);
    257 				}
    258 
    259 				if (!empty($result->Customer->TokenCustomerID) && $this->customer->isLogged() && !$this->model_checkout_order->checkToken($result->Customer->TokenCustomerID)) {
    260 					$card_data = array();
    261 					$card_data['customer_id'] = $this->customer->getId();
    262 					$card_data['Token'] = $result->Customer->TokenCustomerID;
    263 					$card_data['Last4Digits'] = substr(str_replace(' ', '', $result->Customer->CardDetails->Number), -4, 4);
    264 					$card_data['ExpiryDate'] = $result->Customer->CardDetails->ExpiryMonth . '/' . $result->Customer->CardDetails->ExpiryYear;
    265 					$card_data['CardType'] = '';
    266 					$this->model_extension_payment_eway->addFullCard($this->session->data['order_id'], $card_data);
    267 				}
    268 
    269 				$this->response->redirect($this->url->link('checkout/success', '', true));
    270 			}
    271 		}
    272 	}
    273 
    274 }