shop.balmet.com

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

pp_braintree.php (66238B)


      1 <?php
      2 class ControllerExtensionPaymentPPBraintree extends Controller {
      3 	private $customer_id_prefix = 'braintree_oc_';
      4 	private $gateway = null;
      5 
      6 	public function index() {
      7 		$this->initialise();
      8 
      9 		$this->load->language('extension/payment/pp_braintree');
     10 
     11 		$data['payment_url'] = $this->url->link('extension/payment/pp_braintree/payment', '', true);
     12 		$data['vaulted_url'] = $this->url->link('extension/payment/pp_braintree/vaulted', '', true);
     13 
     14 		$data['payment_pp_braintree_3ds_status'] = $this->config->get('payment_pp_braintree_3ds_status');
     15 		$data['payment_pp_braintree_vault_cvv_3ds'] = $this->config->get('payment_pp_braintree_vault_cvv_3ds');
     16 		$data['payment_pp_braintree_paypal_option'] = $this->config->get('payment_pp_braintree_paypal_option');
     17 		$data['payment_pp_braintree_vault_cvv'] = $this->config->get('payment_pp_braintree_vault_cvv');
     18 		$data['payment_pp_braintree_settlement_immediate'] = $this->config->get('payment_pp_braintree_settlement_immediate');
     19 		$data['payment_pp_braintree_paypal_button_colour'] = $this->config->get('payment_pp_braintree_paypal_button_colour');
     20 		$data['payment_pp_braintree_paypal_button_size'] = $this->config->get('payment_pp_braintree_paypal_button_size');
     21 		$data['payment_pp_braintree_paypal_button_shape'] = $this->config->get('payment_pp_braintree_paypal_button_shape');
     22 
     23 		if (!$this->session->data['order_id']) {
     24 			return false;
     25 		}
     26 
     27 		$this->load->model('checkout/order');
     28 
     29 		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     30 
     31 		$create_token = array();
     32 		$merchant_id = $this->config->get('payment_pp_braintree_merchant_id');
     33 
     34 		if ($this->gateway == '') {
     35 			$merchant_accounts = $this->config->get('payment_pp_braintree_account');
     36 
     37 			foreach ($merchant_accounts as $merchant_account_currency => $merchant_account) {
     38 				if (($merchant_account_currency == $order_info['currency_code']) && !empty($merchant_account['merchant_account_id'])) {
     39 					$create_token['merchantAccountId'] = $merchant_account['merchant_account_id'];
     40 
     41 					$merchant_id = $merchant_account['merchant_account_id'];
     42 
     43 					break;
     44 				}
     45 			}
     46 		}
     47 
     48 		$data['merchant_id'] = $merchant_id;
     49 
     50 		if ($this->customer->isLogged() && ($this->config->get('payment_pp_braintree_card_vault') || $this->config->get('payment_pp_braintree_paypal_vault'))) {
     51 			$data['payment_pp_braintree_card_vault'] = $this->config->get('payment_pp_braintree_card_vault');
     52 			$data['payment_pp_braintree_paypal_vault'] = $this->config->get('payment_pp_braintree_paypal_vault');
     53 			$data['payment_pp_braintree_card_check_vault'] = $this->config->get('payment_pp_braintree_card_check_vault');
     54 			$data['payment_pp_braintree_paypal_check_vault'] = $this->config->get('payment_pp_braintree_paypal_check_vault');
     55 			$vaulted_customer_info = $this->model_extension_payment_pp_braintree->getCustomer($this->gateway, $this->customer_id_prefix . $this->customer->getId(), false);
     56 		} else {
     57 			$data['payment_pp_braintree_card_vault'] = 0;
     58 			$data['payment_pp_braintree_paypal_vault'] = 0;
     59 			$data['payment_pp_braintree_card_check_vault'] = 0;
     60 			$data['payment_pp_braintree_paypal_check_vault'] = 0;
     61 			$vaulted_customer_info = false;
     62 		}
     63 
     64 		$data['client_token'] = $this->model_extension_payment_pp_braintree->generateToken($this->gateway, $create_token);
     65 		$data['total'] = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
     66 
     67 		$data['currency_code'] = $order_info['currency_code'];
     68 
     69 		// disable paypal option if currency is not in supported array
     70 		if (!in_array($order_info['currency_code'], array('USD', 'EUR', 'GBP', 'CAD', 'AUD', 'DKK', 'NOK', 'PLN', 'SEK', 'CHF', 'TRY'))) {
     71 			$data['payment_pp_braintree_paypal_option'] = false;
     72 		}
     73 
     74 		// pass shipping info to paypal if set
     75 		if ($data['payment_pp_braintree_paypal_option'] && $this->cart->hasShipping()) {
     76 			$data['customer_shipping_address'] = array(
     77 				'name'			=> addslashes($order_info['shipping_firstname']) . ' ' . addslashes($order_info['shipping_lastname']),
     78 				'line_1'		=> addslashes($order_info['shipping_address_1']),
     79 				'line_2'		=> addslashes($order_info['shipping_address_2']),
     80 				'city'			=> addslashes($order_info['shipping_city']),
     81 				'state'			=> addslashes($order_info['shipping_zone_code']),
     82 				'post_code'		=> addslashes($order_info['shipping_postcode']),
     83 				'country_code' 	=> addslashes($order_info['shipping_iso_code_2']),
     84 				'phone'			=> addslashes($order_info['telephone']),
     85 			);
     86 		}
     87 
     88 		$vaulted_payment_methods = array('cards', 'paypal');
     89 		$vaulted_payment_count = 0;
     90 
     91 		if ($vaulted_customer_info) {
     92 			$vaulted_card_count = 0;
     93 			$vaulted_paypal_count = 0;
     94 
     95 			if ($vaulted_customer_info->creditCards && $this->config->get('payment_pp_braintree_card_vault') == 1) {
     96 				$vaulted_card_count = count($vaulted_customer_info->creditCards);
     97 
     98 				foreach ($vaulted_customer_info->creditCards as $credit_card) {
     99 					$vaulted_payment_methods['cards'][] = array(
    100 						'image'	  => $credit_card->imageUrl,
    101 						'name'	  => sprintf($this->language->get('text_vaulted_payment_method_name'), $credit_card->cardType, $credit_card->last4, $credit_card->expirationDate),
    102 						'token'	  => $credit_card->token,
    103 						'expired' => $credit_card->expired,
    104 						'default' => $credit_card->default
    105 					);
    106 				}
    107 			}
    108 
    109 			if ($vaulted_customer_info->paypalAccounts && $this->config->get('payment_pp_braintree_paypal_vault') == 1) {
    110 				$vaulted_paypal_count = count($vaulted_customer_info->paypalAccounts);
    111 
    112 				foreach ($vaulted_customer_info->paypalAccounts as $paypal_account) {
    113 					$vaulted_payment_methods['paypal'][] = array(
    114 						'image'	  => $paypal_account->imageUrl,
    115 						'name'	  => $paypal_account->email,
    116 						'token'	  => $paypal_account->token,
    117 						'default' => $paypal_account->default
    118 					);
    119 				}
    120 			}
    121 
    122 			$vaulted_payment_count = $vaulted_card_count + $vaulted_paypal_count;
    123 		}
    124 
    125 		$data['vaulted_payment_methods'] = $vaulted_payment_methods;
    126 		$data['vaulted_payment_count'] = $vaulted_payment_count;
    127 
    128 		$data['form_styles'] = json_encode("{
    129 		  'input': { 'font-size': '12px', 'font-family': 'Source Sans Pro, sans-serif', 'color': '#7A8494' },
    130 		  'input.invalid': { 'color': 'red' },
    131 		  'input.valid': { 'color': 'green' }
    132 	  	}");
    133 
    134 		if ($this->customer->isLogged()) {
    135 			$data['guest'] = false;
    136 		} else {
    137 			$data['guest'] = true;
    138 		}
    139 
    140 		return $this->load->view('extension/payment/pp_braintree', $data);
    141 	}
    142 
    143 	public function payment() {
    144 		//set_time_limit(120);
    145 
    146 		$this->initialise();
    147 
    148 		$this->load->language('extension/payment/pp_braintree');
    149 
    150 		$this->load->model('checkout/order');
    151 		$this->load->model('extension/payment/pp_braintree');
    152 
    153 		$this->model_extension_payment_pp_braintree->log('Starting payment');
    154 		$this->model_extension_payment_pp_braintree->log($this->request->post);
    155 
    156 		$success = true;
    157 
    158 		if (!$this->session->data['order_id']) {
    159 			$this->model_extension_payment_pp_braintree->log('Session data: order_id not found');
    160 
    161 			$success = false;
    162 		}
    163 
    164 		if (isset($this->request->post['device_data'])) {
    165 			$device_data = $this->request->post['device_data'];
    166 		} else {
    167 			$this->model_extension_payment_pp_braintree->log('Post data: device_data not found');
    168 
    169 			$device_data = '';
    170 
    171 			$success = false;
    172 		}
    173 
    174 		if (isset($this->request->post['payment_method_token'])) {
    175 			$payment_method_token = $this->request->post['payment_method_token'];
    176 		} else {
    177 			$this->model_extension_payment_pp_braintree->log('Post data: payment_method_token not found');
    178 			$payment_method_token = '';
    179 		}
    180 
    181 		if (isset($this->request->post['payment_method_nonce'])) {
    182 			$payment_method_nonce = $this->request->post['payment_method_nonce'];
    183 		} else {
    184 			$this->model_extension_payment_pp_braintree->log('Post data: payment_method_nonce not found');
    185 			$payment_method_nonce = '';
    186 		}
    187 
    188 		if ($payment_method_nonce == '' && $payment_method_token == '') {
    189 			$success = false;
    190 		}
    191 
    192 		//Start creating transaction array
    193 		if ($success) {
    194 			$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
    195 
    196 			$create_sale = array(
    197 				'amount'			 => $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false),
    198 				'channel'			 => 'OpenCart_Cart_vzero',
    199 				'orderId'			 => $order_info['order_id'],
    200 				'deviceData'		 => $device_data,
    201 				'customer'           => array(
    202 					'firstName' => $order_info['firstname'],
    203 					'lastName'  => $order_info['lastname'],
    204 					'phone'		=> $order_info['telephone'],
    205 					'email'		=> $order_info['email']
    206 				),
    207 				'billing'			=> array(
    208 					'firstName'			=> $order_info['payment_firstname'],
    209 					'lastName'			=> $order_info['payment_lastname'],
    210 					'company'			=> $order_info['payment_company'],
    211 					'streetAddress'		=> $order_info['payment_address_1'],
    212 					'extendedAddress'	=> $order_info['payment_address_2'],
    213 					'locality'			=> $order_info['payment_city'],
    214 					'countryCodeAlpha2' => $order_info['payment_iso_code_2'],
    215 					'postalCode'		=> $order_info['payment_postcode'],
    216 					'region'			=> $order_info['payment_zone_code']
    217 				),
    218 				'options' => array('three_d_secure' => array('required' => false))
    219 			);
    220 
    221 			//Add shipping details
    222 			if ($this->cart->hasShipping()) {
    223 				$create_sale['shipping'] = array(
    224 					'firstName'			=> $order_info['shipping_firstname'],
    225 					'lastName'			=> $order_info['shipping_lastname'],
    226 					'company'			=> $order_info['shipping_company'],
    227 					'streetAddress'		=> $order_info['shipping_address_1'],
    228 					'extendedAddress'	=> $order_info['shipping_address_2'],
    229 					'locality'			=> $order_info['shipping_city'],
    230 					'countryCodeAlpha2' => $order_info['shipping_iso_code_2'],
    231 					'postalCode'		=> $order_info['shipping_postcode'],
    232 					'region'			=> $order_info['shipping_zone_code']
    233 				);
    234 			}
    235 
    236 			if ($this->customer->isLogged() && ($this->config->get('payment_pp_braintree_card_vault') || $this->config->get('payment_pp_braintree_paypal_vault'))) {
    237 				$customer_id = $this->customer_id_prefix . $this->customer->getId();
    238 
    239 				$vaulted_customer_info = $this->model_extension_payment_pp_braintree->getCustomer($this->gateway, $customer_id, false);
    240 
    241 				if ($vaulted_customer_info) {
    242 					$create_sale['customerId'] = $customer_id;
    243 				} else {
    244 					$create_sale['customer']['id'] = $customer_id;
    245 				}
    246 
    247 				if (isset($this->request->post['vault_method']) && $this->request->post['vault_method'] == '1') {
    248 					$create_sale['options']['storeInVaultOnSuccess'] = true;
    249 				}
    250 			}
    251 
    252 			if ($payment_method_token != '') {
    253 				$create_sale['paymentMethodToken'] = $payment_method_token;
    254 
    255 				// unset the billing info for a vaulted payment
    256 				$create_sale['billing'] = array();
    257 			}
    258 
    259 			if ($payment_method_nonce != '') {
    260 				$create_sale['paymentMethodNonce'] = $payment_method_nonce;
    261 			}
    262 
    263 			if ($this->gateway == '') {
    264 				$merchant_accounts = $this->config->get('payment_pp_braintree_account');
    265 
    266 				foreach ($merchant_accounts as $merchant_account_currency => $merchant_account) {
    267 					if (($merchant_account_currency == $order_info['currency_code']) && !empty($merchant_account['merchant_account_id'])) {
    268 						$create_sale['merchantAccountId'] = $merchant_account['merchant_account_id'];
    269 					}
    270 				}
    271 			}
    272 
    273 			if ($this->config->get('payment_pp_braintree_settlement_immediate') == 1) {
    274 				$create_sale['options']['submitForSettlement'] = true;
    275 			} else {
    276 				$create_sale['options']['submitForSettlement'] = false;
    277 			}
    278 		}
    279 
    280 		// If the $payment_method_token is not empty it indicates the vaulted payment used CVV or was set to none
    281 		if ($success && (($this->config->get('payment_pp_braintree_3ds_status') == 1 && $payment_method_token == '') || ($this->config->get('payment_pp_braintree_vault_cvv_3ds') == '3ds' && $payment_method_token != ''))) {
    282 			$nonce_info = $this->model_extension_payment_pp_braintree->getPaymentMethodNonce($this->gateway, $payment_method_nonce);
    283 
    284 			$this->model_extension_payment_pp_braintree->log($nonce_info);
    285 
    286 			if ($nonce_info->type == 'CreditCard' && $this->config->get('payment_pp_braintree_3ds_status') == 1) {
    287 				$create_sale['options']['three_d_secure'] = array(
    288 					'required' => true
    289 				);
    290 
    291 				$three_ds_info = array();
    292 
    293 				if (isset($nonce_info->threeDSecureInfo) && !empty($nonce_info->threeDSecureInfo)) {
    294 					$three_ds_info = $nonce_info->threeDSecureInfo;
    295 				}
    296 
    297 				if (!empty($three_ds_info)) {
    298 					$success = false;
    299 
    300 					switch ($three_ds_info->status) {
    301 						case 'unsupported_card':
    302 							if ($nonce_info->details['cardType'] == 'American Express') {
    303 								$success = true;
    304 							} else {
    305 								$success = $this->config->get('payment_pp_braintree_3ds_unsupported_card');
    306 							}
    307 							break;
    308 						case 'lookup_error':
    309 							$success = $this->config->get('payment_pp_braintree_3ds_lookup_error');
    310 							break;
    311 						case 'lookup_enrolled':
    312 							$success = $this->config->get('payment_pp_braintree_3ds_lookup_enrolled');
    313 							break;
    314 						case 'lookup_not_enrolled':
    315 							$success = $this->config->get('payment_pp_braintree_3ds_lookup_not_enrolled');
    316 							break;
    317 						case 'authenticate_successful_issuer_not_participating':
    318 							$success = $this->config->get('payment_pp_braintree_3ds_not_participating');
    319 							break;
    320 						case 'authentication_unavailable':
    321 							$success = $this->config->get('payment_pp_braintree_3ds_unavailable');
    322 							break;
    323 						case 'authenticate_signature_verification_failed':
    324 							$success = $this->config->get('payment_pp_braintree_3ds_signature_failed');
    325 							break;
    326 						case 'authenticate_successful':
    327 							$success = $this->config->get('payment_pp_braintree_3ds_successful');
    328 							break;
    329 						case 'authenticate_attempt_successful':
    330 							$success = $this->config->get('payment_pp_braintree_3ds_attempt_successful');
    331 							break;
    332 						case 'authenticate_failed':
    333 							$success = $this->config->get('payment_pp_braintree_3ds_failed');
    334 							break;
    335 						case 'authenticate_unable_to_authenticate':
    336 							$success = $this->config->get('payment_pp_braintree_3ds_unable_to_auth');
    337 							break;
    338 						case 'authenticate_error':
    339 							$success = $this->config->get('payment_pp_braintree_3ds_error');
    340 							break;
    341 					}
    342 				} else {
    343 					$this->model_extension_payment_pp_braintree->log('Liability shift failed, nonce was not 3D Secured');
    344 
    345 					$success = false;
    346 				}
    347 			}
    348 		}
    349 		$this->model_extension_payment_pp_braintree->log("Success:" . (int)$success);
    350 
    351 		//Create transaction
    352 		if ($success) {
    353 			$transaction = $this->model_extension_payment_pp_braintree->addTransaction($this->gateway, $create_sale);
    354 
    355 			$order_status_id = 0;
    356 			switch ($transaction->transaction->status) {
    357 				case 'authorization_expired':
    358 					$order_status_id = $this->config->get('payment_pp_braintree_authorization_expired_id');
    359 					break;
    360 				case 'authorized':
    361 					$order_status_id = $this->config->get('payment_pp_braintree_authorized_id');
    362 					break;
    363 				case 'authorizing':
    364 					$order_status_id = $this->config->get('payment_pp_braintree_authorizing_id');
    365 					break;
    366 				case 'settlement_pending':
    367 					$order_status_id = $this->config->get('payment_pp_braintree_settlement_pending_id');
    368 					break;
    369 				case 'failed':
    370 					$order_status_id = $this->config->get('payment_pp_braintree_failed_id');
    371 					break;
    372 				case 'gateway_rejected':
    373 					$order_status_id = $this->config->get('payment_pp_braintree_gateway_rejected_id');
    374 					break;
    375 				case 'processor_declined':
    376 					$order_status_id = $this->config->get('payment_pp_braintree_processor_declined_id');
    377 					break;
    378 				case 'settled':
    379 					$order_status_id = $this->config->get('payment_pp_braintree_settled_id');
    380 					break;
    381 				case 'settling':
    382 					$order_status_id = $this->config->get('payment_pp_braintree_settling_id');
    383 					break;
    384 				case 'submitted_for_settlement':
    385 					$order_status_id = $this->config->get('payment_pp_braintree_submitted_for_settlement_id');
    386 					break;
    387 				case 'voided':
    388 					$order_status_id = $this->config->get('payment_pp_braintree_voided_id');
    389 					break;
    390 			}
    391 
    392 			$this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $order_status_id);
    393 
    394 			if ($transaction->success) {
    395 				$this->model_extension_payment_pp_braintree->log('Transaction success, details below');
    396 				$this->model_extension_payment_pp_braintree->log($transaction);
    397 
    398 				$this->response->redirect($this->url->link('checkout/success', '', true));
    399 			} else {
    400 				$this->model_extension_payment_pp_braintree->log('Transaction failed, details below');
    401 				$this->model_extension_payment_pp_braintree->log($transaction);
    402 
    403 				$this->session->data['error'] = $this->language->get('error_process_order');
    404 				$this->response->redirect($this->url->link('checkout/checkout', '', true));
    405 			}
    406 		}
    407 
    408 		//If this is reached, transaction has failed
    409 		$this->model_extension_payment_pp_braintree->log('Transaction reached end of method without being handled, failure');
    410 
    411 		if (isset($this->session->data['order_id'])) {
    412 			$this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $this->config->get('payment_pp_braintree_failed_id'));
    413 		}
    414 
    415 		$this->response->redirect($this->url->link('checkout/failure', '', true));
    416 	}
    417 
    418 	public function nonce() {
    419 		$this->initialise();
    420 
    421 		$this->load->language('extension/payment/pp_braintree');
    422 
    423 		$this->load->model('extension/payment/pp_braintree');
    424 
    425 		$this->model_extension_payment_pp_braintree->log('Starting vaulted');
    426 		$this->model_extension_payment_pp_braintree->log($this->request->post);
    427 
    428 		$json = array();
    429 
    430 		$json['payment_method'] = '';
    431 
    432 		$success = true;
    433 
    434 		if (!isset($this->request->post['vaulted_payment_token'])) {
    435 			$success = false;
    436 		}
    437 
    438 		if ($success) {
    439 			$payment_method = $this->model_extension_payment_pp_braintree->createPaymentMethodNonce($this->gateway, $this->request->post['vaulted_payment_token']);
    440 
    441 			if ($payment_method && $payment_method->success) {
    442 				$json['payment_method'] = array(
    443 					'type'  => $payment_method->paymentMethodNonce->type,
    444 					'nonce' => $payment_method->paymentMethodNonce->nonce
    445 				);
    446 			} else {
    447 				$success = false;
    448 			}
    449 		}
    450 
    451 		$json['success'] = $success;
    452 
    453 		$this->response->addHeader('Content-Type: application/json');
    454 		$this->response->setOutput(json_encode($json));
    455 	}
    456 
    457 	public function remove() {
    458 		$this->initialise();
    459 
    460 		$this->load->language('extension/payment/pp_braintree');
    461 
    462 		$this->load->model('extension/payment/pp_braintree');
    463 
    464 		$json = array();
    465 
    466 		$json['success'] = false;
    467 
    468 		if (isset($this->request->post['vaulted_payment_method'])) {
    469 			$vaulted_payment_method = $this->request->post['vaulted_payment_method'];
    470 		} else {
    471 			$vaulted_payment_method = '';
    472 		}
    473 
    474 		$delete_payment_method = $this->model_extension_payment_pp_braintree->deletePaymentMethod($this->gateway, $vaulted_payment_method);
    475 
    476 		if ($delete_payment_method) {
    477 			$json['success'] = $this->language->get('text_method_removed');
    478 		} else {
    479 			$json['error'] = $this->language->get('text_method_not_removed');
    480 		}
    481 
    482 		$vaulted_customer_info = $this->model_extension_payment_pp_braintree->getCustomer($this->gateway, $this->customer_id_prefix . $this->customer->getId());
    483 
    484 		$vaulted_card_count = 0;
    485 		$vaulted_paypal_count = 0;
    486 
    487 		if ($vaulted_customer_info->creditCards && $this->config->get('payment_pp_braintree_card_vault') == 1) {
    488 			$vaulted_card_count = count($vaulted_customer_info->creditCards);
    489 		}
    490 
    491 		if ($vaulted_customer_info->paypalAccounts && $this->config->get('payment_pp_braintree_paypal_vault') == 1) {
    492 			$vaulted_paypal_count = count($vaulted_customer_info->paypalAccounts);
    493 		}
    494 
    495 		$json['vaulted_payment_count'] = $vaulted_card_count + $vaulted_paypal_count;
    496 
    497 		$this->response->addHeader('Content-Type: application/json');
    498 		$this->response->setOutput(json_encode($json));
    499 	}
    500 
    501 	public function expressSetup() {
    502 		// check checkout can continue due to stock checks or vouchers
    503 		if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
    504 			$json = array();
    505 			$json['error'] = true;
    506 			$json['url'] = $this->url->link('checkout/cart');
    507 
    508 			$this->response->addHeader('Content-Type: application/json');
    509 			$this->response->setOutput(json_encode($json));
    510 		}
    511 
    512 		// if user not logged in check that the guest checkout is allowed
    513 		if (!$this->customer->isLogged() && (!$this->config->get('config_checkout_guest') || $this->config->get('config_customer_price') || $this->cart->hasDownload() || $this->cart->hasRecurringProducts())) {
    514 			$json = array();
    515 			$json['error'] = true;
    516 			$json['url'] = $this->url->link('checkout/checkout');
    517 
    518 			$this->response->addHeader('Content-Type: application/json');
    519 			$this->response->setOutput(json_encode($json));
    520 		} else {
    521 			unset($this->session->data['guest']);
    522 		}
    523 
    524 		unset($this->session->data['shipping_method']);
    525 		unset($this->session->data['shipping_methods']);
    526 		unset($this->session->data['payment_method']);
    527 		unset($this->session->data['payment_methods']);
    528 
    529 		if (!$this->customer->isLogged()) {
    530 			$this->session->data['paypal_braintree']['guest'] = true;
    531 
    532 			$this->session->data['guest']['customer_group_id'] = $this->config->get('config_customer_group_id');
    533 			$this->session->data['guest']['firstname'] = $this->request->post['details']['firstName'];
    534 			$this->session->data['guest']['lastname'] = $this->request->post['details']['lastName'];
    535 			$this->session->data['guest']['email'] = $this->request->post['details']['email'];
    536 
    537 			if (isset($this->request->post['details']['phone'])) {
    538 				$this->session->data['guest']['telephone'] = $this->request->post['details']['phone'];
    539 			} else {
    540 				$this->session->data['guest']['telephone'] = '';
    541 			}
    542 
    543 			$this->session->data['guest']['payment']['company'] = '';
    544 
    545 			$this->session->data['guest']['payment']['firstname'] = $this->request->post['details']['firstName'];
    546 			$this->session->data['guest']['payment']['lastname'] = $this->request->post['details']['lastName'];
    547 
    548 			$this->session->data['guest']['payment']['company_id'] = '';
    549 			$this->session->data['guest']['payment']['tax_id'] = '';
    550 
    551 			if ($this->cart->hasShipping()) {
    552 				$shipping_name = explode(' ', $this->request->post['details']['shippingAddress']['recipientName']);
    553 				$shipping_first_name = $shipping_name[0];
    554 				unset($shipping_name[0]);
    555 				$shipping_last_name = implode(' ', $shipping_name);
    556 
    557 				$this->session->data['guest']['payment']['address_1'] = $this->request->post['details']['shippingAddress']['line1'];
    558 				if (isset($this->request->post['details']['shippingAddress']['line2'])) {
    559 					$this->session->data['guest']['payment']['address_2'] = $this->request->post['details']['shippingAddress']['line2'];
    560 				} else {
    561 					$this->session->data['guest']['payment']['address_2'] = '';
    562 				}
    563 
    564 				$this->session->data['guest']['payment']['postcode'] = $this->request->post['details']['shippingAddress']['postalCode'];
    565 				$this->session->data['guest']['payment']['city'] = $this->request->post['details']['shippingAddress']['city'];
    566 
    567 				$this->session->data['guest']['shipping']['firstname'] = $shipping_first_name;
    568 				$this->session->data['guest']['shipping']['lastname'] = $shipping_last_name;
    569 				$this->session->data['guest']['shipping']['company'] = '';
    570 				$this->session->data['guest']['shipping']['address_1'] = $this->request->post['details']['shippingAddress']['line1'];
    571 
    572 				if (isset($this->request->post['details']['shippingAddress']['line2'])) {
    573 					$this->session->data['guest']['shipping']['address_2'] =$this->request->post['details']['shippingAddress']['line2'];
    574 				} else {
    575 					$this->session->data['guest']['shipping']['address_2'] = '';
    576 				}
    577 
    578 				$this->session->data['guest']['shipping']['postcode'] = $this->request->post['details']['shippingAddress']['postalCode'];
    579 				$this->session->data['guest']['shipping']['city'] = $this->request->post['details']['shippingAddress']['city'];
    580 
    581 				$this->session->data['shipping_postcode'] = $this->request->post['details']['shippingAddress']['postalCode'];
    582 
    583 				$country_info = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE `iso_code_2` = '" . $this->db->escape($this->request->post['details']['shippingAddress']['countryCode']) . "' AND `status` = '1' LIMIT 1")->row;
    584 
    585 				if ($country_info) {
    586 					$this->session->data['guest']['shipping']['country_id'] = $country_info['country_id'];
    587 					$this->session->data['guest']['shipping']['country'] = $country_info['name'];
    588 					$this->session->data['guest']['shipping']['iso_code_2'] = $country_info['iso_code_2'];
    589 					$this->session->data['guest']['shipping']['iso_code_3'] = $country_info['iso_code_3'];
    590 					$this->session->data['guest']['shipping']['address_format'] = $country_info['address_format'];
    591 					$this->session->data['guest']['payment']['country_id'] = $country_info['country_id'];
    592 					$this->session->data['guest']['payment']['country'] = $country_info['name'];
    593 					$this->session->data['guest']['payment']['iso_code_2'] = $country_info['iso_code_2'];
    594 					$this->session->data['guest']['payment']['iso_code_3'] = $country_info['iso_code_3'];
    595 					$this->session->data['guest']['payment']['address_format'] = $country_info['address_format'];
    596 					$this->session->data['shipping_country_id'] = $country_info['country_id'];
    597 
    598 					if (isset($this->request->post['details']['shippingAddress']['state'])) {
    599 						$returned_shipping_zone = $this->request->post['details']['shippingAddress']['state'];
    600 					} else {
    601 						$returned_shipping_zone = '';
    602 					}
    603 
    604 					$zone_info = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE (`name` = '" . $this->db->escape($returned_shipping_zone) . "' OR `code` = '" . $this->db->escape($returned_shipping_zone) . "') AND `status` = '1' AND `country_id` = '" . (int)$country_info['country_id'] . "' LIMIT 1")->row;
    605 				} else {
    606 					$this->session->data['guest']['shipping']['country_id'] = '';
    607 					$this->session->data['guest']['shipping']['country'] = '';
    608 					$this->session->data['guest']['shipping']['iso_code_2'] = '';
    609 					$this->session->data['guest']['shipping']['iso_code_3'] = '';
    610 					$this->session->data['guest']['shipping']['address_format'] = '';
    611 					$this->session->data['guest']['payment']['country_id'] = '';
    612 					$this->session->data['guest']['payment']['country'] = '';
    613 					$this->session->data['guest']['payment']['iso_code_2'] = '';
    614 					$this->session->data['guest']['payment']['iso_code_3'] = '';
    615 					$this->session->data['guest']['payment']['address_format'] = '';
    616 					$this->session->data['shipping_country_id'] = '';
    617 
    618 					$zone_info = array();
    619 				}
    620 
    621 				if ($zone_info) {
    622 					$this->session->data['guest']['shipping']['zone'] = $zone_info['name'];
    623 					$this->session->data['guest']['shipping']['zone_code'] = $zone_info['code'];
    624 					$this->session->data['guest']['shipping']['zone_id'] = $zone_info['zone_id'];
    625 					$this->session->data['guest']['payment']['zone'] = $zone_info['name'];
    626 					$this->session->data['guest']['payment']['zone_code'] = $zone_info['code'];
    627 					$this->session->data['guest']['payment']['zone_id'] = $zone_info['zone_id'];
    628 					$this->session->data['shipping_zone_id'] = $zone_info['zone_id'];
    629 				} else {
    630 					$this->session->data['guest']['shipping']['zone'] = '';
    631 					$this->session->data['guest']['shipping']['zone_code'] = '';
    632 					$this->session->data['guest']['shipping']['zone_id'] = '';
    633 					$this->session->data['guest']['payment']['zone'] = '';
    634 					$this->session->data['guest']['payment']['zone_code'] = '';
    635 					$this->session->data['guest']['payment']['zone_id'] = '';
    636 					$this->session->data['shipping_zone_id'] = '';
    637 				}
    638 
    639 				$this->session->data['guest']['shipping_address'] = true;
    640 			} else {
    641 				$this->session->data['guest']['payment']['address_1'] = '';
    642 				$this->session->data['guest']['payment']['address_2'] = '';
    643 				$this->session->data['guest']['payment']['postcode'] = '';
    644 				$this->session->data['guest']['payment']['city'] = '';
    645 				$this->session->data['guest']['payment']['country_id'] = '';
    646 				$this->session->data['guest']['payment']['country'] = '';
    647 				$this->session->data['guest']['payment']['iso_code_2'] = '';
    648 				$this->session->data['guest']['payment']['iso_code_3'] = '';
    649 				$this->session->data['guest']['payment']['address_format'] = '';
    650 				$this->session->data['guest']['payment']['zone'] = '';
    651 				$this->session->data['guest']['payment']['zone_code'] = '';
    652 				$this->session->data['guest']['payment']['zone_id'] = '';
    653 				$this->session->data['guest']['shipping_address'] = false;
    654 			}
    655 
    656 			$this->session->data['account'] = 'guest';
    657 
    658 			unset($this->session->data['shipping_method']);
    659 			unset($this->session->data['shipping_methods']);
    660 			unset($this->session->data['payment_method']);
    661 			unset($this->session->data['payment_methods']);
    662 		} else {
    663 			$this->session->data['paypal_braintree']['guest'] = false;
    664 
    665 			unset($this->session->data['guest']);
    666 			/**
    667 			 * if the user is logged in, add the address to the account and set the ID.
    668 			 */
    669 
    670 			if ($this->cart->hasShipping()) {
    671 				$this->load->model('account/address');
    672 
    673 				$addresses = $this->model_account_address->getAddresses();
    674 
    675 				/**
    676 				 * Compare all of the user addresses and see if there is a match
    677 				 */
    678 				$match = false;
    679 				foreach($addresses as $address) {
    680 					if (trim(strtolower($address['address_1'])) == trim(strtolower($this->request->post['details']['shippingAddress']['line1'])) && trim(strtolower($address['postcode'])) == trim(strtolower($this->request->post['details']['shippingAddress']['postalCode']))) {
    681 						$match = true;
    682 
    683 						$this->session->data['payment_address_id'] = $address['address_id'];
    684 						$this->session->data['payment_country_id'] = $address['country_id'];
    685 						$this->session->data['payment_zone_id'] = $address['zone_id'];
    686 
    687 						$this->session->data['shipping_address_id'] = $address['address_id'];
    688 						$this->session->data['shipping_country_id'] = $address['country_id'];
    689 						$this->session->data['shipping_zone_id'] = $address['zone_id'];
    690 						$this->session->data['shipping_postcode'] = $address['postcode'];
    691 
    692 						break;
    693 					}
    694 				}
    695 
    696 				/**
    697 				 * If there is no address match add the address and set the info.
    698 				 */
    699 				if ($match == false) {
    700 					$shipping_name = explode(' ', trim($this->request->post['details']['shippingAddress']['recipientName']));
    701 					$shipping_first_name = $shipping_name[0];
    702 					unset($shipping_name[0]);
    703 					$shipping_last_name = implode(' ', $shipping_name);
    704 
    705 					$country_info = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE `iso_code_2` = '" . $this->db->escape($this->request->post['details']['shippingAddress']['countryCode']) . "' AND `status` = '1' LIMIT 1")->row;
    706 					$zone_info = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE (`name` = '" . $this->db->escape($this->request->post['details']['shippingAddress']['state']) . "' OR `code` = '" . $this->db->escape($this->request->post['details']['shippingAddress']['state']) . "') AND `status` = '1' AND `country_id` = '" . (int)$country_info['country_id'] . "'")->row;
    707 
    708 					$address_data = array(
    709 						'firstname'  => $shipping_first_name,
    710 						'lastname'   => $shipping_last_name,
    711 						'company'    => '',
    712 						'company_id' => '',
    713 						'tax_id'     => '',
    714 						'address_1'  => $this->request->post['details']['shippingAddress']['line1'],
    715 						'address_2'  => (isset($this->request->post['details']['shippingAddress']['line2']) ? $this->request->post['details']['shippingAddress']['line2'] : ''),
    716 						'postcode'   => $this->request->post['details']['shippingAddress']['postalCode'],
    717 						'city'       => $this->request->post['details']['shippingAddress']['city'],
    718 						'zone_id'    => (isset($zone_info['zone_id']) ? $zone_info['zone_id'] : 0),
    719 						'country_id' => (isset($country_info['country_id']) ? $country_info['country_id'] : 0)
    720 					);
    721 
    722 					$address_id = $this->model_account_address->addAddress($this->customer->getId(), $address_data);
    723 
    724 					$this->session->data['payment_address_id'] = $address_id;
    725 					$this->session->data['payment_country_id'] = $address_data['country_id'];
    726 					$this->session->data['payment_zone_id'] = $address_data['zone_id'];
    727 
    728 					$this->session->data['shipping_address_id'] = $address_id;
    729 					$this->session->data['shipping_country_id'] = $address_data['country_id'];
    730 					$this->session->data['shipping_zone_id'] = $address_data['zone_id'];
    731 					$this->session->data['shipping_postcode'] = $address_data['postcode'];
    732 				}
    733 			} else {
    734 				$this->session->data['payment_address_id'] = '';
    735 				$this->session->data['payment_country_id'] = '';
    736 				$this->session->data['payment_zone_id'] = '';
    737 			}
    738 		}
    739 
    740 		$this->session->data['paypal_braintree'] = $this->request->post;
    741 
    742 		$json = array(
    743 			'error' => false,
    744 			'url' => ''
    745 		);
    746 
    747 		$this->response->addHeader('Content-Type: application/json');
    748 		$this->response->setOutput(json_encode($json));
    749 	}
    750 
    751 	public function expressConfirm() {
    752 		if (!isset($this->session->data['paypal_braintree']) || !isset($this->session->data['paypal_braintree']['nonce'])) {
    753 			$this->response->redirect($this->url->link('checkout/cart', '', true));
    754 		}
    755 
    756 		$this->load->language('extension/payment/pp_braintree');
    757 		$this->load->language('checkout/cart');
    758 
    759 		$this->load->model('tool/image');
    760 		$this->load->model('extension/payment/pp_braintree');
    761 
    762 		// Coupon
    763 		if (isset($this->request->post['coupon']) && $this->validateCoupon()) {
    764 			$this->session->data['coupon'] = $this->request->post['coupon'];
    765 
    766 			$this->session->data['success'] = $this->language->get('text_coupon');
    767 
    768 			$this->response->redirect($this->url->link('extension/payment/pp_braintree/expressConfirm', '', true));
    769 		}
    770 
    771 		// Voucher
    772 		if (isset($this->request->post['voucher']) && $this->validateVoucher()) {
    773 			$this->session->data['voucher'] = $this->request->post['voucher'];
    774 
    775 			$this->session->data['success'] = $this->language->get('text_voucher');
    776 
    777 			$this->response->redirect($this->url->link('extension/payment/pp_braintree/expressConfirm', '', true));
    778 		}
    779 
    780 		// Reward
    781 		if (isset($this->request->post['reward']) && $this->validateReward()) {
    782 			$this->session->data['reward'] = abs($this->request->post['reward']);
    783 
    784 			$this->session->data['success'] = $this->language->get('text_reward');
    785 
    786 			$this->response->redirect($this->url->link('extension/payment/pp_braintree/expressConfirm', '', true));
    787 		}
    788 
    789 		$this->document->setTitle($this->language->get('text_express_title'));
    790 
    791 		$data['heading_title'] = $this->language->get('text_express_title');
    792 
    793 		$data['breadcrumbs'] = array();
    794 
    795 		$data['breadcrumbs'][] = array(
    796 			'href' => $this->url->link('common/home'),
    797 			'text' => $this->language->get('text_home')
    798 		);
    799 
    800 		$data['breadcrumbs'][] = array(
    801 			'text' => $this->language->get('text_cart'),
    802 			'href' => $this->url->link('checkout/cart')
    803 		);
    804 
    805 		$data['breadcrumbs'][] = array(
    806 			'href' => $this->url->link('extension/payment/pp_braintree/expressConfirm'),
    807 			'text' => $this->language->get('text_express_title')
    808 		);
    809 
    810 		$points_total = 0;
    811 
    812 		foreach ($this->cart->getProducts() as $product) {
    813 			if ($product['points']) {
    814 				$points_total += $product['points'];
    815 			}
    816 		}
    817 
    818 		$data['column_name'] = $this->language->get('column_name');
    819 		$data['column_model'] = $this->language->get('column_model');
    820 		$data['column_quantity'] = $this->language->get('column_quantity');
    821 		$data['column_price'] = $this->language->get('column_price');
    822 		$data['column_total'] = $this->language->get('column_total');
    823 
    824 		$data['button_shipping'] = $this->language->get('button_express_shipping');
    825 		$data['button_confirm'] = $this->language->get('button_express_confirm');
    826 
    827 		if (isset($this->request->post['next'])) {
    828 			$data['next'] = $this->request->post['next'];
    829 		} else {
    830 			$data['next'] = '';
    831 		}
    832 
    833 		$data['action'] = $this->url->link('extension/payment/pp_braintree/expressConfirm', '', true);
    834 
    835 		$this->load->model('tool/upload');
    836 
    837 		$products = $this->cart->getProducts();
    838 
    839 		if (empty($products)) {
    840 			$this->response->redirect($this->url->link('checkout/cart', '', true));
    841 		}
    842 
    843 		foreach ($products as $product) {
    844 			$product_total = 0;
    845 
    846 			foreach ($products as $product_2) {
    847 				if ($product_2['product_id'] == $product['product_id']) {
    848 					$product_total += $product_2['quantity'];
    849 				}
    850 			}
    851 
    852 			if ($product['minimum'] > $product_total) {
    853 				$data['error_warning'] = sprintf($this->language->get('error_minimum'), $product['name'], $product['minimum']);
    854 			}
    855 
    856 			if ($product['image']) {
    857 				$image = $this->model_tool_image->resize($product['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_cart_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_cart_height'));
    858 			} else {
    859 				$image = '';
    860 			}
    861 
    862 			$option_data = array();
    863 
    864 			foreach ($product['option'] as $option) {
    865 				if ($option['type'] != 'file') {
    866 					$value = $option['value'];
    867 				} else {
    868 					$upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
    869 
    870 					if ($upload_info) {
    871 						$value = $upload_info['name'];
    872 					} else {
    873 						$value = '';
    874 					}
    875 				}
    876 
    877 				$option_data[] = array(
    878 					'name'  => $option['name'],
    879 					'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
    880 				);
    881 			}
    882 
    883 			// Display prices
    884 			if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
    885 				$unit_price = $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'));
    886 
    887 				$price = $this->currency->format($unit_price, $this->session->data['currency']);
    888 				$total = $this->currency->format($unit_price * $product['quantity'], $this->session->data['currency']);
    889 			} else {
    890 				$price = false;
    891 				$total = false;
    892 			}
    893 
    894 			$data['products'][] = array(
    895 				'cart_id'               => $product['cart_id'],
    896 				'thumb'                 => $image,
    897 				'name'                  => $product['name'],
    898 				'model'                 => $product['model'],
    899 				'option'                => $option_data,
    900 				'quantity'              => $product['quantity'],
    901 				'stock'                 => $product['stock'] ? true : !(!$this->config->get('config_stock_checkout') || $this->config->get('config_stock_warning')),
    902 				'reward'                => ($product['reward'] ? sprintf($this->language->get('text_points'), $product['reward']) : ''),
    903 				'price'                 => $price,
    904 				'total'                 => $total,
    905 				'href'                  => $this->url->link('product/product', 'product_id=' . $product['product_id']),
    906 				'remove'                => $this->url->link('checkout/cart', 'remove=' . $product['cart_id']),
    907 			);
    908 		}
    909 
    910 		$data['vouchers'] = array();
    911 
    912 		if ($this->cart->hasShipping()) {
    913 			$data['has_shipping'] = true;
    914 			/**
    915 			 * Shipping services
    916 			 */
    917 			if ($this->customer->isLogged()) {
    918 				$this->load->model('account/address');
    919 				$shipping_address = $this->model_account_address->getAddress($this->session->data['shipping_address_id']);
    920 			} elseif (isset($this->session->data['guest'])) {
    921 				$shipping_address = $this->session->data['guest']['shipping'];
    922 			}
    923 
    924 			if (!empty($shipping_address)) {
    925 				// Shipping Methods
    926 				$quote_data = array();
    927 
    928 				$this->load->model('setting/extension');
    929 
    930 				$results = $this->model_setting_extension->getExtensions('shipping');
    931 
    932 				if (!empty($results)) {
    933 					foreach ($results as $result) {
    934 						if ($this->config->get('shipping_' . $result['code'] . '_status')) {
    935 							$this->load->model('extension/shipping/' . $result['code']);
    936 
    937 							$quote = $this->{'model_extension_shipping_' . $result['code']}->getQuote($shipping_address);
    938 
    939 							if ($quote) {
    940 								$quote_data[$result['code']] = array(
    941 									'title'      => $quote['title'],
    942 									'quote'      => $quote['quote'],
    943 									'sort_order' => $quote['sort_order'],
    944 									'error'      => $quote['error']
    945 								);
    946 							}
    947 						}
    948 					}
    949 
    950 					if (!empty($quote_data)) {
    951 						$sort_order = array();
    952 
    953 						foreach ($quote_data as $key => $value) {
    954 							$sort_order[$key] = $value['sort_order'];
    955 						}
    956 
    957 						array_multisort($sort_order, SORT_ASC, $quote_data);
    958 
    959 						$this->session->data['shipping_methods'] = $quote_data;
    960 						$data['shipping_methods'] = $quote_data;
    961 
    962 						if (!isset($this->session->data['shipping_method'])) {
    963 							//default the shipping to the very first option.
    964 							$key1 = key($quote_data);
    965 							$key2 = key($quote_data[$key1]['quote']);
    966 							$this->session->data['shipping_method'] = $quote_data[$key1]['quote'][$key2];
    967 						}
    968 
    969 						$data['code'] = $this->session->data['shipping_method']['code'];
    970 						$data['action_shipping'] = $this->url->link('extension/payment/pp_braintree/shipping', '', true);
    971 					} else {
    972 						unset($this->session->data['shipping_methods']);
    973 						unset($this->session->data['shipping_method']);
    974 						$data['error_no_shipping'] = $this->language->get('error_no_shipping');
    975 					}
    976 				} else {
    977 					unset($this->session->data['shipping_methods']);
    978 					unset($this->session->data['shipping_method']);
    979 					$data['error_no_shipping'] = $this->language->get('error_no_shipping');
    980 				}
    981 			}
    982 		} else {
    983 			$data['has_shipping'] = false;
    984 		}
    985 
    986 		// Totals
    987 		$this->load->model('setting/extension');
    988 
    989 		$totals = array();
    990 		$taxes = $this->cart->getTaxes();
    991 		$total = 0;
    992 
    993 		// Because __call can not keep var references so we put them into an array.
    994 		$total_data = array(
    995 			'totals' => &$totals,
    996 			'taxes'  => &$taxes,
    997 			'total'  => &$total
    998 		);
    999 
   1000 		// Display prices
   1001 		if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
   1002 			$sort_order = array();
   1003 
   1004 			$results = $this->model_setting_extension->getExtensions('total');
   1005 
   1006 			foreach ($results as $key => $value) {
   1007 				$sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order');
   1008 			}
   1009 
   1010 			array_multisort($sort_order, SORT_ASC, $results);
   1011 
   1012 			foreach ($results as $result) {
   1013 				if ($this->config->get('total_' . $result['code'] . '_status')) {
   1014 					$this->load->model('extension/total/' . $result['code']);
   1015 
   1016 					// We have to put the totals in an array so that they pass by reference.
   1017 					$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
   1018 				}
   1019 			}
   1020 
   1021 			$sort_order = array();
   1022 
   1023 			foreach ($totals as $key => $value) {
   1024 				$sort_order[$key] = $value['sort_order'];
   1025 			}
   1026 
   1027 			array_multisort($sort_order, SORT_ASC, $totals);
   1028 		}
   1029 
   1030 		$data['totals'] = array();
   1031 
   1032 		foreach ($totals as $total) {
   1033 			$data['totals'][] = array(
   1034 				'title' => $total['title'],
   1035 				'text'  => $this->currency->format($total['value'], $this->session->data['currency']),
   1036 			);
   1037 		}
   1038 
   1039 		/**
   1040 		 * Payment methods
   1041 		 */
   1042 		if ($this->customer->isLogged() && isset($this->session->data['payment_address_id'])) {
   1043 			$this->load->model('account/address');
   1044 			$payment_address = $this->model_account_address->getAddress($this->session->data['payment_address_id']);
   1045 		} elseif (isset($this->session->data['guest'])) {
   1046 			$payment_address = $this->session->data['guest']['payment'];
   1047 		}
   1048 
   1049 		$method_data = array();
   1050 
   1051 		$this->load->model('setting/extension');
   1052 
   1053 		$results = $this->model_setting_extension->getExtensions('payment');
   1054 
   1055 		$this->model_extension_payment_pp_braintree->log("Payment methods returned based on new data");
   1056 		$this->model_extension_payment_pp_braintree->log($results);
   1057 
   1058 		foreach ($results as $result) {
   1059 			if ($this->config->get('payment_' . $result['code'] . '_status')) {
   1060 				$this->load->model('extension/payment/' . $result['code']);
   1061 
   1062 				$method = $this->{'model_extension_payment_' . $result['code']}->getMethod($payment_address, $total);
   1063 
   1064 				if ($method) {
   1065 					$method_data[$result['code']] = $method;
   1066 				}
   1067 			}
   1068 		}
   1069 
   1070 		$sort_order = array();
   1071 
   1072 		foreach ($method_data as $key => $value) {
   1073 			$sort_order[$key] = $value['sort_order'];
   1074 		}
   1075 
   1076 		array_multisort($sort_order, SORT_ASC, $method_data);
   1077 
   1078 		$this->model_extension_payment_pp_braintree->log("Payment methods again - sorted");
   1079 		$this->model_extension_payment_pp_braintree->log($method_data);
   1080 
   1081 		if (!isset($method_data['pp_braintree'])) {
   1082 			$this->model_extension_payment_pp_braintree->log("Braintree module was no longer an option. Check configured zones or minimum order amount based on user address info");
   1083 			$this->session->data['error_warning'] = $this->language->get('error_unavailable');
   1084 			$this->response->redirect($this->url->link('checkout/checkout', '', true));
   1085 		}
   1086 
   1087 		$this->session->data['payment_methods'] = $method_data;
   1088 		$this->session->data['payment_method'] = $method_data['pp_braintree'];
   1089 
   1090 		$data['action_confirm'] = $this->url->link('extension/payment/pp_braintree/expressComplete', '', true);
   1091 
   1092 		if (isset($this->session->data['error_warning'])) {
   1093 			$data['error_warning'] = $this->session->data['error_warning'];
   1094 			unset($this->session->data['error_warning']);
   1095 		} else {
   1096 			$data['error_warning'] = '';
   1097 		}
   1098 
   1099 		if (isset($this->session->data['success'])) {
   1100 			$data['success'] = $this->session->data['success'];
   1101 			unset($this->session->data['success']);
   1102 		} else {
   1103 			$data['success'] = '';
   1104 		}
   1105 
   1106 		if (isset($this->session->data['attention'])) {
   1107 			$data['attention'] = $this->session->data['attention'];
   1108 			unset($this->session->data['attention']);
   1109 		} else {
   1110 			$data['attention'] = '';
   1111 		}
   1112 
   1113 		$data['coupon'] = $this->load->controller('extension/total/coupon');
   1114 		$data['voucher'] = $this->load->controller('extension/total/voucher');
   1115 		$data['reward'] = $this->load->controller('extension/total/reward');
   1116 		$data['column_left'] = $this->load->controller('common/column_left');
   1117 		$data['column_right'] = $this->load->controller('common/column_right');
   1118 		$data['content_top'] = $this->load->controller('common/content_top');
   1119 		$data['content_bottom'] = $this->load->controller('common/content_bottom');
   1120 		$data['footer'] = $this->load->controller('common/footer');
   1121 		$data['header'] = $this->load->controller('common/header');
   1122 
   1123 		$this->response->setOutput($this->load->view('extension/payment/pp_braintree_confirm', $data));
   1124 	}
   1125 
   1126 	public function expressComplete() {
   1127 		if (!isset($this->session->data['paypal_braintree']) || !isset($this->session->data['paypal_braintree']['nonce'])) {
   1128 			$this->response->redirect($this->url->link('checkout/cart', '', true));
   1129 		}
   1130 
   1131 		$this->load->language('extension/payment/pp_braintree');
   1132 		$redirect = '';
   1133 
   1134 		if ($this->cart->hasShipping()) {
   1135 			// Validate if shipping address has been set.
   1136 			$this->load->model('account/address');
   1137 
   1138 			if ($this->customer->isLogged() && isset($this->session->data['shipping_address_id'])) {
   1139 				$shipping_address = $this->model_account_address->getAddress($this->session->data['shipping_address_id']);
   1140 			} elseif (isset($this->session->data['guest'])) {
   1141 				$shipping_address = $this->session->data['guest']['shipping'];
   1142 			}
   1143 
   1144 			if (empty($shipping_address)) {
   1145 				$redirect = $this->url->link('checkout/checkout', '', true);
   1146 			}
   1147 
   1148 			// Validate if shipping method has been set.
   1149 			if (!isset($this->session->data['shipping_method'])) {
   1150 				$redirect = $this->url->link('checkout/checkout', '', true);
   1151 			}
   1152 		} else {
   1153 			unset($this->session->data['shipping_method']);
   1154 			unset($this->session->data['shipping_methods']);
   1155 		}
   1156 
   1157 		// Validate if payment address has been set.
   1158 		$this->load->model('account/address');
   1159 
   1160 		if ($this->customer->isLogged() && isset($this->session->data['payment_address_id'])) {
   1161 			$payment_address = $this->model_account_address->getAddress($this->session->data['payment_address_id']);
   1162 		} elseif (isset($this->session->data['guest'])) {
   1163 			$payment_address = $this->session->data['guest']['payment'];
   1164 		}
   1165 
   1166 		// Validate if payment method has been set.
   1167 		if (!isset($this->session->data['payment_method'])) {
   1168 			$redirect = $this->url->link('checkout/checkout', '', true);
   1169 		}
   1170 
   1171 		// Validate cart has products and has stock.
   1172 		if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
   1173 			$redirect = $this->url->link('checkout/cart');
   1174 		}
   1175 
   1176 		// Validate minimum quantity requirements.
   1177 		$products = $this->cart->getProducts();
   1178 
   1179 		foreach ($products as $product) {
   1180 			$product_total = 0;
   1181 
   1182 			foreach ($products as $product_2) {
   1183 				if ($product_2['product_id'] == $product['product_id']) {
   1184 					$product_total += $product_2['quantity'];
   1185 				}
   1186 			}
   1187 
   1188 			if ($product['minimum'] > $product_total) {
   1189 				$redirect = $this->url->link('checkout/cart');
   1190 
   1191 				break;
   1192 			}
   1193 		}
   1194 
   1195 		if ($redirect == '') {
   1196 			$totals = array();
   1197 			$taxes = $this->cart->getTaxes();
   1198 			$total = 0;
   1199 
   1200 			// Because __call can not keep var references so we put them into an array.
   1201 			$total_data = array(
   1202 				'totals' => &$totals,
   1203 				'taxes'  => &$taxes,
   1204 				'total'  => &$total
   1205 			);
   1206 
   1207 			$this->load->model('setting/extension');
   1208 
   1209 			$sort_order = array();
   1210 
   1211 			$results = $this->model_setting_extension->getExtensions('total');
   1212 
   1213 			foreach ($results as $key => $value) {
   1214 				$sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order');
   1215 			}
   1216 
   1217 			array_multisort($sort_order, SORT_ASC, $results);
   1218 
   1219 			foreach ($results as $result) {
   1220 				if ($this->config->get('total_' . $result['code'] . '_status')) {
   1221 					$this->load->model('extension/total/' . $result['code']);
   1222 
   1223 					// We have to put the totals in an array so that they pass by reference.
   1224 					$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
   1225 				}
   1226 			}
   1227 
   1228 			$sort_order = array();
   1229 
   1230 			foreach ($totals as $key => $value) {
   1231 				$sort_order[$key] = $value['sort_order'];
   1232 			}
   1233 
   1234 			array_multisort($sort_order, SORT_ASC, $totals);
   1235 
   1236 			$this->load->language('checkout/checkout');
   1237 
   1238 			$data = array();
   1239 
   1240 			$data['invoice_prefix'] = $this->config->get('config_invoice_prefix');
   1241 			$data['store_id'] = $this->config->get('config_store_id');
   1242 			$data['store_name'] = $this->config->get('config_name');
   1243 
   1244 			if ($data['store_id']) {
   1245 				$data['store_url'] = $this->config->get('config_url');
   1246 			} else {
   1247 				$data['store_url'] = HTTP_SERVER;
   1248 			}
   1249 
   1250 			if ($this->customer->isLogged() && isset($this->session->data['payment_address_id'])) {
   1251 				$data['customer_id'] = $this->customer->getId();
   1252 				$data['customer_group_id'] = $this->config->get('config_customer_group_id');
   1253 				$data['firstname'] = $this->customer->getFirstName();
   1254 				$data['lastname'] = $this->customer->getLastName();
   1255 				$data['email'] = $this->customer->getEmail();
   1256 				$data['telephone'] = $this->customer->getTelephone();
   1257 
   1258 				$this->load->model('account/address');
   1259 
   1260 				$payment_address = $this->model_account_address->getAddress($this->session->data['payment_address_id']);
   1261 			} elseif (isset($this->session->data['guest'])) {
   1262 				$data['customer_id'] = 0;
   1263 				$data['customer_group_id'] = $this->session->data['guest']['customer_group_id'];
   1264 				$data['firstname'] = $this->session->data['guest']['firstname'];
   1265 				$data['lastname'] = $this->session->data['guest']['lastname'];
   1266 				$data['email'] = $this->session->data['guest']['email'];
   1267 				$data['telephone'] = $this->session->data['guest']['telephone'];
   1268 
   1269 				$payment_address = $this->session->data['guest']['payment'];
   1270 			}
   1271 
   1272 			$data['payment_firstname'] = isset($payment_address['firstname']) ? $payment_address['firstname'] : '';
   1273 			$data['payment_lastname'] = isset($payment_address['lastname']) ? $payment_address['lastname'] : '';
   1274 			$data['payment_company'] = isset($payment_address['company']) ? $payment_address['company'] : '';
   1275 			$data['payment_company_id'] = isset($payment_address['company_id']) ? $payment_address['company_id'] : '';
   1276 			$data['payment_tax_id'] = isset($payment_address['tax_id']) ? $payment_address['tax_id'] : '';
   1277 			$data['payment_address_1'] = isset($payment_address['address_1']) ? $payment_address['address_1'] : '';
   1278 			$data['payment_address_2'] = isset($payment_address['address_2']) ? $payment_address['address_2'] : '';
   1279 			$data['payment_city'] = isset($payment_address['city']) ? $payment_address['city'] : '';
   1280 			$data['payment_postcode'] = isset($payment_address['postcode']) ? $payment_address['postcode'] : '';
   1281 			$data['payment_zone'] = isset($payment_address['zone']) ? $payment_address['zone'] : '';
   1282 			$data['payment_zone_id'] = isset($payment_address['zone_id']) ? $payment_address['zone_id'] : '';
   1283 			$data['payment_country'] = isset($payment_address['country']) ? $payment_address['country'] : '';
   1284 			$data['payment_country_id'] = isset($payment_address['country_id']) ? $payment_address['country_id'] : '';
   1285 			$data['payment_address_format'] = isset($payment_address['address_format']) ? $payment_address['address_format'] : '';
   1286 
   1287 			$data['payment_method'] = '';
   1288 			if (isset($this->session->data['payment_method']['title'])) {
   1289 				$data['payment_method'] = $this->session->data['payment_method']['title'];
   1290 			}
   1291 
   1292 			$data['payment_code'] = '';
   1293 			if (isset($this->session->data['payment_method']['code'])) {
   1294 				$data['payment_code'] = $this->session->data['payment_method']['code'];
   1295 			}
   1296 
   1297 			if ($this->cart->hasShipping()) {
   1298 				if ($this->customer->isLogged()) {
   1299 					$this->load->model('account/address');
   1300 
   1301 					$shipping_address = $this->model_account_address->getAddress($this->session->data['shipping_address_id']);
   1302 				} elseif (isset($this->session->data['guest'])) {
   1303 					$shipping_address = $this->session->data['guest']['shipping'];
   1304 				}
   1305 
   1306 				$data['shipping_firstname'] = $shipping_address['firstname'];
   1307 				$data['shipping_lastname'] = $shipping_address['lastname'];
   1308 				$data['shipping_company'] = $shipping_address['company'];
   1309 				$data['shipping_address_1'] = $shipping_address['address_1'];
   1310 				$data['shipping_address_2'] = $shipping_address['address_2'];
   1311 				$data['shipping_city'] = $shipping_address['city'];
   1312 				$data['shipping_postcode'] = $shipping_address['postcode'];
   1313 				$data['shipping_zone'] = $shipping_address['zone'];
   1314 				$data['shipping_zone_id'] = $shipping_address['zone_id'];
   1315 				$data['shipping_country'] = $shipping_address['country'];
   1316 				$data['shipping_country_id'] = $shipping_address['country_id'];
   1317 				$data['shipping_address_format'] = $shipping_address['address_format'];
   1318 
   1319 				$data['shipping_method'] = '';
   1320 				if (isset($this->session->data['shipping_method']['title'])) {
   1321 					$data['shipping_method'] = $this->session->data['shipping_method']['title'];
   1322 				}
   1323 
   1324 				$data['shipping_code'] = '';
   1325 				if (isset($this->session->data['shipping_method']['code'])) {
   1326 					$data['shipping_code'] = $this->session->data['shipping_method']['code'];
   1327 				}
   1328 			} else {
   1329 				$data['shipping_firstname'] = '';
   1330 				$data['shipping_lastname'] = '';
   1331 				$data['shipping_company'] = '';
   1332 				$data['shipping_address_1'] = '';
   1333 				$data['shipping_address_2'] = '';
   1334 				$data['shipping_city'] = '';
   1335 				$data['shipping_postcode'] = '';
   1336 				$data['shipping_zone'] = '';
   1337 				$data['shipping_zone_id'] = '';
   1338 				$data['shipping_country'] = '';
   1339 				$data['shipping_country_id'] = '';
   1340 				$data['shipping_address_format'] = '';
   1341 				$data['shipping_method'] = '';
   1342 				$data['shipping_code'] = '';
   1343 			}
   1344 
   1345 			$product_data = array();
   1346 
   1347 			foreach ($this->cart->getProducts() as $product) {
   1348 				$option_data = array();
   1349 
   1350 				foreach ($product['option'] as $option) {
   1351 					$option_data[] = array(
   1352 						'product_option_id'       => $option['product_option_id'],
   1353 						'product_option_value_id' => $option['product_option_value_id'],
   1354 						'option_id'               => $option['option_id'],
   1355 						'option_value_id'         => $option['option_value_id'],
   1356 						'name'                    => $option['name'],
   1357 						'value'                   => $option['value'],
   1358 						'type'                    => $option['type']
   1359 					);
   1360 				}
   1361 
   1362 				$product_data[] = array(
   1363 					'product_id' => $product['product_id'],
   1364 					'name'       => $product['name'],
   1365 					'model'      => $product['model'],
   1366 					'option'     => $option_data,
   1367 					'download'   => $product['download'],
   1368 					'quantity'   => $product['quantity'],
   1369 					'subtract'   => $product['subtract'],
   1370 					'price'      => $product['price'],
   1371 					'total'      => $product['total'],
   1372 					'tax'        => $this->tax->getTax($product['price'], $product['tax_class_id']),
   1373 					'reward'     => $product['reward']
   1374 				);
   1375 			}
   1376 
   1377 			// Gift Voucher
   1378 			$voucher_data = array();
   1379 
   1380 			if (!empty($this->session->data['vouchers'])) {
   1381 				foreach ($this->session->data['vouchers'] as $voucher) {
   1382 					$voucher_data[] = array(
   1383 						'description'      => $voucher['description'],
   1384 						'code'             => token(10),
   1385 						'to_name'          => $voucher['to_name'],
   1386 						'to_email'         => $voucher['to_email'],
   1387 						'from_name'        => $voucher['from_name'],
   1388 						'from_email'       => $voucher['from_email'],
   1389 						'voucher_theme_id' => $voucher['voucher_theme_id'],
   1390 						'message'          => $voucher['message'],
   1391 						'amount'           => $voucher['amount']
   1392 					);
   1393 				}
   1394 			}
   1395 
   1396 			$data['products'] = $product_data;
   1397 			$data['vouchers'] = $voucher_data;
   1398 			$data['totals'] = $totals;
   1399 			$data['total'] = $total;
   1400 			$data['comment'] = '';
   1401 
   1402 			if (isset($this->request->cookie['tracking'])) {
   1403 				$data['tracking'] = $this->request->cookie['tracking'];
   1404 
   1405 				$subtotal = $this->cart->getSubTotal();
   1406 
   1407 				// Affiliate
   1408 				$this->load->model('affiliate/affiliate');
   1409 
   1410 				$affiliate_info = $this->model_affiliate_affiliate->getAffiliateByCode($this->request->cookie['tracking']);
   1411 
   1412 				if ($affiliate_info) {
   1413 					$data['affiliate_id'] = $affiliate_info['affiliate_id'];
   1414 					$data['commission'] = ($subtotal / 100) * $affiliate_info['commission'];
   1415 				} else {
   1416 					$data['affiliate_id'] = 0;
   1417 					$data['commission'] = 0;
   1418 				}
   1419 
   1420 				// Marketing
   1421 				$this->load->model('checkout/marketing');
   1422 
   1423 				$marketing_info = $this->model_checkout_marketing->getMarketingByCode($this->request->cookie['tracking']);
   1424 
   1425 				if ($marketing_info) {
   1426 					$data['marketing_id'] = $marketing_info['marketing_id'];
   1427 				} else {
   1428 					$data['marketing_id'] = 0;
   1429 				}
   1430 			} else {
   1431 				$data['affiliate_id'] = 0;
   1432 				$data['commission'] = 0;
   1433 				$data['marketing_id'] = 0;
   1434 				$data['tracking'] = '';
   1435 			}
   1436 
   1437 			$data['language_id'] = $this->config->get('config_language_id');
   1438 			$data['currency_id'] = $this->currency->getId($this->session->data['currency']);
   1439 			$data['currency_code'] = $this->session->data['currency'];
   1440 			$data['currency_value'] = $this->currency->getValue($this->session->data['currency']);
   1441 			$data['ip'] = $this->request->server['REMOTE_ADDR'];
   1442 
   1443 			if (!empty($this->request->server['HTTP_X_FORWARDED_FOR'])) {
   1444 				$data['forwarded_ip'] = $this->request->server['HTTP_X_FORWARDED_FOR'];
   1445 			} elseif (!empty($this->request->server['HTTP_CLIENT_IP'])) {
   1446 				$data['forwarded_ip'] = $this->request->server['HTTP_CLIENT_IP'];
   1447 			} else {
   1448 				$data['forwarded_ip'] = '';
   1449 			}
   1450 
   1451 			if (isset($this->request->server['HTTP_USER_AGENT'])) {
   1452 				$data['user_agent'] = $this->request->server['HTTP_USER_AGENT'];
   1453 			} else {
   1454 				$data['user_agent'] = '';
   1455 			}
   1456 
   1457 			if (isset($this->request->server['HTTP_ACCEPT_LANGUAGE'])) {
   1458 				$data['accept_language'] = $this->request->server['HTTP_ACCEPT_LANGUAGE'];
   1459 			} else {
   1460 				$data['accept_language'] = '';
   1461 			}
   1462 
   1463 			$this->load->model('account/custom_field');
   1464 			$this->load->model('checkout/order');
   1465 
   1466 			$order_id = $this->model_checkout_order->addOrder($data);
   1467 			$this->session->data['order_id'] = $order_id;
   1468 
   1469 			$this->load->model('extension/payment/pp_braintree');
   1470 
   1471 			$this->initialise();
   1472 
   1473 			$create_sale = [
   1474 				"amount" => $this->currency->format($data['total'], $data['currency_code'], $data['currency_value'], false),
   1475 				"paymentMethodNonce" => $this->session->data['paypal_braintree']['nonce'],
   1476 				"orderId" => $order_id,
   1477 				'channel' => 'OpenCart_Cart_vzero',
   1478 			];
   1479 
   1480 			$transaction = $this->model_extension_payment_pp_braintree->addTransaction($this->gateway, $create_sale);
   1481 
   1482 			//handle order status
   1483 
   1484 			$order_status_id = 0;
   1485 			switch ($transaction->transaction->status) {
   1486 				case 'authorization_expired':
   1487 					$order_status_id = $this->config->get('payment_pp_braintree_authorization_expired_id');
   1488 					break;
   1489 				case 'authorized':
   1490 					$order_status_id = $this->config->get('payment_pp_braintree_authorized_id');
   1491 					break;
   1492 				case 'authorizing':
   1493 					$order_status_id = $this->config->get('payment_pp_braintree_authorizing_id');
   1494 					break;
   1495 				case 'settlement_pending':
   1496 					$order_status_id = $this->config->get('payment_pp_braintree_settlement_pending_id');
   1497 					break;
   1498 				case 'failed':
   1499 					$order_status_id = $this->config->get('payment_pp_braintree_failed_id');
   1500 					break;
   1501 				case 'gateway_rejected':
   1502 					$order_status_id = $this->config->get('payment_pp_braintree_gateway_rejected_id');
   1503 					break;
   1504 				case 'processor_declined':
   1505 					$order_status_id = $this->config->get('payment_pp_braintree_processor_declined_id');
   1506 					break;
   1507 				case 'settled':
   1508 					$order_status_id = $this->config->get('payment_pp_braintree_settled_id');
   1509 					break;
   1510 				case 'settling':
   1511 					$order_status_id = $this->config->get('payment_pp_braintree_settling_id');
   1512 					break;
   1513 				case 'submitted_for_settlement':
   1514 					$order_status_id = $this->config->get('payment_pp_braintree_submitted_for_settlement_id');
   1515 					break;
   1516 				case 'voided':
   1517 					$order_status_id = $this->config->get('payment_pp_braintree_voided_id');
   1518 					break;
   1519 			}
   1520 
   1521 			$this->model_checkout_order->addOrderHistory($this->session->data['order_id'], $order_status_id);
   1522 
   1523 			if ($transaction->success) {
   1524 				$this->model_extension_payment_pp_braintree->log('Transaction success, details below');
   1525 				$this->model_extension_payment_pp_braintree->log($transaction);
   1526 
   1527 				$this->response->redirect($this->url->link('checkout/success', '', true));
   1528 			} else {
   1529 				$this->model_extension_payment_pp_braintree->log('Transaction failed, details below');
   1530 				$this->model_extension_payment_pp_braintree->log($transaction);
   1531 
   1532 				$this->session->data['error'] = $this->language->get('error_process_order');
   1533 				$this->response->redirect($this->url->link('checkout/checkout', '', true));
   1534 			}
   1535 		} else {
   1536 			$this->response->redirect($redirect);
   1537 		}
   1538 	}
   1539 
   1540 	private function initialise() {
   1541 		$this->load->model('extension/payment/pp_braintree');
   1542 
   1543 		if ($this->config->get('payment_pp_braintree_access_token') != '') {
   1544 			$this->gateway = $this->model_extension_payment_pp_braintree->setGateway($this->config->get('payment_pp_braintree_access_token'));
   1545 		} else {
   1546 			$this->model_extension_payment_pp_braintree->setCredentials();
   1547 		}
   1548 	}
   1549 
   1550 	public function shipping() {
   1551 		$this->shippingValidate($this->request->post['shipping_method']);
   1552 
   1553 		$this->response->redirect($this->url->link('extension/payment/pp_braintree/expressConfirm'));
   1554 	}
   1555 
   1556 	protected function shippingValidate($code) {
   1557 		$this->load->language('checkout/cart');
   1558 		$this->load->language('extension/payment/pp_braintree');
   1559 
   1560 		if (empty($code)) {
   1561 			$this->session->data['error_warning'] = $this->language->get('error_shipping');
   1562 			return false;
   1563 		} else {
   1564 			$shipping = explode('.', $code);
   1565 
   1566 			if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
   1567 				$this->session->data['error_warning'] = $this->language->get('error_shipping');
   1568 				return false;
   1569 			} else {
   1570 				$this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
   1571 				$this->session->data['success'] = $this->language->get('text_shipping_updated');
   1572 				return true;
   1573 			}
   1574 		}
   1575 	}
   1576 
   1577 	protected function validateCoupon() {
   1578 		$this->load->model('extension/total/coupon');
   1579 
   1580 		$coupon_info = $this->model_extension_total_coupon->getCoupon($this->request->post['coupon']);
   1581 
   1582 		if ($coupon_info) {
   1583 			return true;
   1584 		} else {
   1585 			$this->session->data['error_warning'] = $this->language->get('error_coupon');
   1586 			return false;
   1587 		}
   1588 	}
   1589 
   1590 	protected function validateVoucher() {
   1591 		$this->load->model('extension/total/coupon');
   1592 
   1593 		$voucher_info = $this->model_extension_total_voucher->getVoucher($this->request->post['voucher']);
   1594 
   1595 		if ($voucher_info) {
   1596 			return true;
   1597 		} else {
   1598 			$this->session->data['error_warning'] = $this->language->get('error_voucher');
   1599 			return false;
   1600 		}
   1601 	}
   1602 
   1603 	protected function validateReward() {
   1604 		$points = $this->customer->getRewardPoints();
   1605 
   1606 		$points_total = 0;
   1607 
   1608 		foreach ($this->cart->getProducts() as $product) {
   1609 			if ($product['points']) {
   1610 				$points_total += $product['points'];
   1611 			}
   1612 		}
   1613 
   1614 		$error = '';
   1615 
   1616 		if (empty($this->request->post['reward'])) {
   1617 			$error = $this->language->get('error_reward');
   1618 		}
   1619 
   1620 		if ($this->request->post['reward'] > $points) {
   1621 			$error = sprintf($this->language->get('error_points'), $this->request->post['reward']);
   1622 		}
   1623 
   1624 		if ($this->request->post['reward'] > $points_total) {
   1625 			$error = sprintf($this->language->get('error_maximum'), $points_total);
   1626 		}
   1627 
   1628 		if (!$error) {
   1629 			return true;
   1630 		} else {
   1631 			$this->session->data['error_warning'] = $error;
   1632 			return false;
   1633 		}
   1634 	}
   1635 }