shop.balmet.com

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

auspost.php (5227B)


      1 <?php
      2 /**
      3  * @version    N/A, base on AUSPOST API update on 18 April 2016
      4  * @link       https://developers.auspost.com.au/docs/reference
      5  * @since      2.3.0.2   Update on 21 March 2017
      6  */
      7 
      8 class ModelExtensionShippingAusPost extends Model {
      9 	public function getQuote($address) {
     10 		$this->load->language('extension/shipping/auspost');
     11 
     12 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('shipping_auspost_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");
     13 
     14 		if (!$this->config->get('shipping_auspost_geo_zone_id')) {
     15 			$status = true;
     16 		} elseif ($query->num_rows) {
     17 			$status = true;
     18 		} else {
     19 			$status = false;
     20 		}
     21 
     22 		$error = '';
     23 
     24 		$api_key = $this->config->get('shipping_auspost_api');
     25 
     26 		$quote_data = array();
     27 
     28 		if ($status) {
     29 			$weight = $this->weight->convert($this->cart->getWeight(), $this->config->get('config_weight_class_id'), $this->config->get('shipping_auspost_weight_class_id'));
     30 
     31 			$length = 0;
     32 			$width = 0;
     33 			$height = 0;
     34 
     35 			if ($address['iso_code_2'] == 'AU') {
     36 
     37 				foreach ($this->cart->getProducts() as $product) {
     38 					if ($product['height'] > $height) {
     39 						$height = $product['height'];
     40 					}
     41 
     42 					if ($product['width'] > $width) {
     43 						$width = $product['width'];
     44 					}
     45 
     46 					$length += ($product['length']*$product['quantity']);
     47 				}
     48 
     49 				$curl = curl_init();
     50 
     51 				curl_setopt($curl, CURLOPT_HTTPHEADER, array('AUTH-KEY: ' . $api_key));
     52 				curl_setopt($curl, CURLOPT_URL, 'https://digitalapi.auspost.com.au/postage/parcel/domestic/service.json?from_postcode=' . urlencode($this->config->get('shipping_auspost_postcode')) . '&to_postcode=' . urlencode($address['postcode']) . '&height=' . $height . '&width=' . $width . '&length=' . $height . '&weight=' . urlencode($weight));
     53 				curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     54 				curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
     55 				curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     56 
     57 				$response = curl_exec($curl);
     58 
     59 				curl_close($curl);
     60 
     61 				if ($response) {
     62 					$response_info = array();
     63 
     64 					$response_parts = json_decode($response, true);
     65 
     66 					if (isset($response_parts['error'])) {
     67 						$error = $response_parts['error']['errorMessage'];
     68 					} else {
     69 						$response_services = $response_parts['services']['service'];
     70 
     71 						foreach ($response_services as $response_service) {
     72 							$quote_data[$response_service['name']] = array(
     73 								'code'         => 'auspost.' .  $response_service['name'],
     74 								'title'        => $response_service['name'],
     75 								'cost'         => $this->currency->convert($response_service['price'], 'AUD', $this->config->get('config_currency')),
     76 								'tax_class_id' => $this->config->get('shipping_auspost_tax_class_id'),
     77 								'text'         => $this->currency->format($this->tax->calculate($this->currency->convert($response_service['price'], 'AUD', $this->session->data['currency']), $this->config->get('shipping_auspost_tax_class_id'), $this->config->get('config_tax')), $this->session->data['currency'], 1.0000000)
     78 							);
     79 						}
     80 					}
     81 				}
     82 			} else {
     83 				$curl = curl_init();
     84 
     85 				curl_setopt($curl, CURLOPT_HTTPHEADER, array('AUTH-KEY: ' .  $api_key));
     86 				curl_setopt($curl, CURLOPT_URL, 'https://digitalapi.auspost.com.au/postage/parcel/international/service.json?country_code=' . urlencode($address['iso_code_2']) . '&weight=' . urlencode($weight));
     87 				curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     88 				curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
     89 				curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     90 
     91 				$response = curl_exec($curl);
     92 
     93 				curl_close($curl);
     94 
     95 				if ($response) {
     96 					$response_info = array();
     97 
     98 					$response_parts = json_decode($response, true);
     99 
    100 					if (isset($response_parts['error'])) {
    101 						$error = $response_parts['error']['errorMessage'];
    102 					} else {
    103 						$response_services = $response_parts['services']['service'];
    104 
    105 						foreach ($response_services as $response_service) {
    106 							$quote_data[$response_service['name']] = array(
    107 								'code'         => 'auspost.' .  $response_service['name'],
    108 								'title'        => $response_service['name'],
    109 								'cost'         => $this->currency->convert($response_service['price'], 'AUD', $this->config->get('config_currency')),
    110 								'tax_class_id' => $this->config->get('shipping_auspost_tax_class_id'),
    111 								'text'         => $this->currency->format($this->tax->calculate($this->currency->convert($response_service['price'], 'AUD', $this->session->data['currency']), $this->config->get('shipping_auspost_tax_class_id'), $this->config->get('config_tax')), $this->session->data['currency'], 1.0000000)
    112 							);
    113 						}
    114 					}
    115 				}
    116 			}
    117 		}
    118 
    119 		$method_data = array();
    120 
    121 		if ($quote_data) {
    122 			$method_data = array(
    123 				'code'       => 'auspost',
    124 				'title'      => $this->language->get('text_title'),
    125 				'quote'      => $quote_data,
    126 				'sort_order' => $this->config->get('shipping_auspost_sort_order'),
    127 				'error'      => $error
    128 			);
    129 		}
    130 
    131 		return $method_data;
    132 	}
    133 }