shop.balmet.com

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

klarna_checkout_module.php (5065B)


      1 <?php
      2 class ControllerExtensionModuleKlarnaCheckoutModule extends Controller {
      3 	public function index() {
      4 		$this->load->model('extension/payment/klarna_checkout');
      5 
      6 		// If Payment Method or Module is disabled
      7 		if (!$this->config->get('module_klarna_checkout_status') || !$this->config->get('klarna_checkout_status')) {
      8 			$this->model_extension_payment_klarna_checkout->log('Not shown due to Payment Method or Module being disabled');
      9 			return false;
     10 		}
     11 
     12 		// Validate cart has products and has stock.
     13 		if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
     14 			$this->model_extension_payment_klarna_checkout->log('Not shown due to empty cart');
     15 			return false;
     16 		}
     17 
     18 		// Validate minimum quantity requirements.
     19 		$products = $this->cart->getProducts();
     20 
     21 		foreach ($products as $product) {
     22 			$product_total = 0;
     23 
     24 			foreach ($products as $product_2) {
     25 				if ($product_2['product_id'] == $product['product_id']) {
     26 					$product_total += $product_2['quantity'];
     27 				}
     28 			}
     29 
     30 			if ($product['minimum'] > $product_total) {
     31 				$this->model_extension_payment_klarna_checkout->log('Not shown due to cart not meeting minimum quantity reqs.');
     32 				return false;
     33 			}
     34 		}
     35 
     36 		// Validate cart has recurring products
     37 		if ($this->cart->hasRecurringProducts()) {
     38 			$this->model_extension_payment_klarna_checkout->log('Not shown due to cart having recurring products.');
     39 			return false;
     40 		}
     41 
     42 		list($totals, $taxes, $total) = $this->model_extension_payment_klarna_checkout->getTotals();
     43 
     44 		if ($this->config->get('klarna_checkout_total') > 0 && $this->config->get('klarna_checkout_total') > $total) {
     45 			return false;
     46 		}
     47 
     48 		if ($this->model_extension_payment_klarna_checkout->checkForPaymentTaxes($products)) {
     49 			$this->model_extension_payment_klarna_checkout->log('Payment Address based taxes used.');
     50 			return false;
     51 		}
     52 
     53 		$this->setShipping();
     54 
     55 		list($klarna_account, $connector) = $this->model_extension_payment_klarna_checkout->getConnector($this->config->get('klarna_checkout_account'), $this->session->data['currency']);
     56 
     57 		if (!$klarna_account || !$connector) {
     58 			$this->model_extension_payment_klarna_checkout->log('Couldn\'t secure connection to Klarna API.');
     59 			return false;
     60 		}
     61 
     62 		$data['klarna_checkout'] = $this->url->link('extension/payment/klarna_checkout', '', true);
     63 
     64 		return $this->load->view('extension/module/klarna_checkout_module', $data);
     65 	}
     66 
     67 	private function setShipping() {
     68 		$this->load->model('account/address');
     69 		$this->load->model('localisation/country');
     70 		$this->load->model('localisation/zone');
     71 
     72 		if (isset($this->session->data['shipping_address']) && !empty($this->session->data['shipping_address'])) {
     73 			$this->session->data['shipping_address'] = $this->session->data['shipping_address'];
     74 		} elseif ($this->customer->isLogged() && $this->customer->getAddressId()) {
     75 			$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
     76 		} else {
     77 			$country_info = $this->model_localisation_country->getCountry($this->config->get('config_country_id'));
     78 
     79 			$zone_info = $this->model_localisation_zone->getZone($this->config->get('config_zone_id'));
     80 
     81 			$this->session->data['shipping_address'] = array(
     82 				'address_id'	 => null,
     83 				'firstname'		 => null,
     84 				'lastname'		 => null,
     85 				'company'		 => null,
     86 				'address_1'		 => null,
     87 				'address_2'		 => null,
     88 				'postcode'		 => null,
     89 				'city'			 => null,
     90 				'zone_id'		 => $zone_info['zone_id'],
     91 				'zone'			 => $zone_info['name'],
     92 				'zone_code'		 => $zone_info['code'],
     93 				'country_id'	 => $country_info['country_id'],
     94 				'country'		 => $country_info['name'],
     95 				'iso_code_2'	 => $country_info['iso_code_2'],
     96 				'iso_code_3'	 => $country_info['iso_code_3'],
     97 				'address_format' => '',
     98 				'custom_field'	 => null,
     99 			);
    100 		}
    101 
    102 		if (isset($this->session->data['shipping_address'])) {
    103 			// Shipping Methods
    104 			$method_data = array();
    105 
    106 			$this->load->model('setting/extension');
    107 
    108 			$results = $this->model_setting_extension->getExtensions('shipping');
    109 
    110 			foreach ($results as $result) {
    111 				if ($this->config->get('shipping_' . $result['code'] . '_status')) {
    112 					$this->load->model('extension/shipping/' . $result['code']);
    113 
    114 					$quote = $this->{'model_extension_shipping_' . $result['code']}->getQuote($this->session->data['shipping_address']);
    115 
    116 					if ($quote) {
    117 						$method_data[$result['code']] = array(
    118 							'title'      => $quote['title'],
    119 							'quote'      => $quote['quote'],
    120 							'sort_order' => $quote['sort_order'],
    121 							'error'      => $quote['error']
    122 						);
    123 					}
    124 				}
    125 			}
    126 
    127 			$sort_order = array();
    128 
    129 			foreach ($method_data as $key => $value) {
    130 				$sort_order[$key] = $value['sort_order'];
    131 			}
    132 
    133 			array_multisort($sort_order, SORT_ASC, $method_data);
    134 
    135 			$this->session->data['shipping_methods'] = $method_data;
    136 		}
    137 	}
    138 }