<?php
/**
* @copyright Copyright (c) 2019 Hikashop delyva Shipping Plugin. All rights reserved.
* @license
www.gnu.org/licenses/gpl-2.0.html
GNU/GPL
*/
// no direct access
defined('_JEXEC') or die;
/**
* hikashopshipping - delyva Plugin
*
* @package Joomla.Plugin
* @subpakage Hikashop delyva Shipping Plugin.delyva
*/
class plgHikashopshippingDelyva extends hikashopShippingPlugin {
var $multiple = true;
var $name = 'delyva';
var $use_cache = true;
var $pluginConfig = array(
'customerid' => array('Action ID', 'input'),
'passphrase' => array('API Key', 'input'),
'url' => array('Url v2', 'input'),
'disable' => array('Diabled method (eg.poslaju, nationwide, etc)', 'input'),
'config_sender_address' => array('Pick up Address', 'input'),
'config_sender_postcode' => array('Pick up Postcode', 'input'),
'config_sender_country' => array('Pick up Country', 'input'),
'config_sender_pickup' => array('Pick up (Y/N)', 'input'),
'item_type' => array('Item Type', 'list', array('DOCUMENT','PARCEL','FOOD','FLOWER','CHILL','FROZEN','CAKE','OTHER','TASK', 'BULKY'))
);
public function shippingMethods(&$main) {
$methods = array();
if(!empty($main->shipping_params->methodsList))
$main->shipping_params->methods = hikashop_unserialize($main->shipping_params->methodsList);
if(!empty($main->shipping_params->methods)) {
foreach($main->shipping_params->methods as $method) {
$selected = null;
foreach($this->ups_methods as $ups) {
if($ups == $method) {
$selected = $ups;
break;
}
}
if($selected)
$methods[$main->shipping_id .'-'. $selected['key']] = $selected;
}
}
return $methods;
}
public function onShippingDisplay(&$order,&$methods,&$usable_methods,&$messages)
{
$input = JFactory::getApplication()->input;
// should be article, categories, featured, blog...
$view = $input->get('view');
if(!hikashop_loadUser())
return false;
if(empty($order->shipping_address)) {
$messages = 'No shipping address is configured.';
return true;
}
//if($this->loadShippingCache($order, $usable_methods, $messages))
//return true;
//print_r($methods);
print_r($usable_methods);print_r("1");
if(!function_exists('curl_init')) {
$app = JFactory::getApplication();
$app->enqueueMessage('The Delyva shipping plugin needs the CURL library installed but it seems that it is not available on your server. Please contact your web hosting to set it up.','error');
return false;
}
//$usable_methods = $methods;
$local_usable_rates = array();
$local_messages = array();
$ret = parent::onShippingDisplay($order, $methods, $local_usable_rates, $local_messages);
if($ret === false)
return false;
//print_r("test");
//write file to check output;
//$myfile = fopen("testing.txt", "w") or die("Unable to open file!");
//fwrite($myfile,print_r($this,true));
//fclose($myfile);
//$ret = parent::onShippingDisplay($order, $methods, $usable_methods, $messages);
foreach($methods as $method)
{
if($method->shipping_type == "delyva")
{
$delyva = $method;
}
}
//get response by using curl;
$data = $this->delyvacurl($order,$delyva);
print_r($data);
//write file to check output;
$myfile = fopen("testing.txt", "w") or die("Unable to open file!");
fwrite($myfile,print_r($data,true));
fclose($myfile);
//display all delyva's shipping methods;
foreach($data as $newmethods)
{
$shipping_id = $newmethods->service_code;
$shipping_name = $newmethods->name;
$shipping_description = $newmethods->description;
$shipping_price = $newmethods->price;
$shipping_days = $newmethods->deliveryDays;
$newmethods->shipping_id = $shipping_id;
$newmethods->shipping_type = "delyva";
$newmethods->shipping_zone_namekey = "";
$newmethods->shipping_tax_id = "0";
$newmethods->shipping_price = $shipping_price;
$newmethods->shipping_currency_id = "1";
$newmethods->shipping_name = $shipping_name;
$newmethods->shipping_description = $shipping_days;
$newmethods->shipping_published = "1";
$newmethods->shipping_ordering = "1";
$newmethods->shipping_currency = "";
$newmethods->shipping_params = $delyva->shipping_params;
if ($newmethods->shipping_params->disable){
$disable=$newmethods->shipping_params->disable;
}
$newmethods->shipping_images = "";
$newmethods->shipping_access = "all";
//if($usable_methods){
array_push($usable_methods, $newmethods);
array_push($local_usable_rates, $newmethods);
//print_r($usable_methods);
//}
}
return true;
}
//postparam
private function delyvacurl($order, $delyva)
{
$url = $delyva->shipping_params->url;
$customerid = $delyva->shipping_params->customerid;
$apikey = $delyva->shipping_params->passphrase;
$sender_address = $delyva->shipping_params->config_sender_address;
$sender_postcode = $delyva->shipping_params->config_sender_postcode;
$sender_country = $delyva->shipping_params->config_sender_country;
$receiver_address = $order->shipping_address->address_street;
$receiver_postcode = $order->shipping_address->address_post_code;
$send_country = "MY";
if (substr_count($order->shipping_address->address_country[0], "Malaysia")>=1) {
$send_country = "MY";
}
if (substr_count($order->shipping_address->address_country[0], "Singapore")>=1) {
$send_country = "SG"; }
if (substr_count($order->shipping_address->address_country[0], "Taiwan")>=1) {
$send_country = "TW"; }
if (substr_count($order->shipping_address->address_country[0], "United Kingdom")>=1) {
$send_country = "GB"; }
if (substr_count($order->shipping_address->address_country[0], "Australia")>=1) {
$send_country = "AU"; }
if (substr_count($order->shipping_address->address_country[0], "Afghanistan")>=1) {
$send_country = "AF"; }
if (substr_count($order->shipping_address->address_country[0], "Argentina")>=1) {
$send_country = "AR"; }
if (substr_count($order->shipping_address->address_country[0], "Austria")>=1) {
$send_country = "AT"; }
if (substr_count($order->shipping_address->address_country[0], "Brazil")>=1) {
$send_country = "BR"; }
if (substr_count($order->shipping_address->address_country[0], "Cambodia")>=1) {
$send_country = "KH"; }
if (substr_count($order->shipping_address->address_country[0], "Canada")>=1) {
$send_country = "CA"; }
if (substr_count($order->shipping_address->address_country[0], "Colombia")>=1) {
$send_country = "CO"; }
if (substr_count($order->shipping_address->address_country[0], "Denmark")>=1) {
$send_country = "DK"; }
if (substr_count($order->shipping_address->address_country[0], "Egypt")>=1) {
$send_country = "EG"; }
if (substr_count($order->shipping_address->address_country[0], "France")>=1) {
$send_country = "FR"; }
if (substr_count($order->shipping_address->address_country[0], "Germany")>=1) {
$send_country = "DE"; }
if (substr_count($order->shipping_address->address_country[0], "Hong Kong")>=1) {
$send_country = "HK"; }
if (substr_count($order->shipping_address->address_country[0], "India")>=1) {
$send_country = "IN"; }
if (substr_count($order->shipping_address->address_country[0], "Indonesia")>=1) {
$send_country = "ID"; }
if (substr_count($order->shipping_address->address_country[0], "Iraq")>=1) {
$send_country = "IQ"; }
if (substr_count($order->shipping_address->address_country[0], "Italy")>=1) {
$send_country = "IT"; }
if (substr_count($order->shipping_address->address_country[0], "Japan")>=1) {
$send_country = "JP"; }
if (substr_count($order->shipping_address->address_country[0], "Jordan")>=1) {
$send_country = "JO"; }
if (substr_count($order->shipping_address->address_country[0], "Macau")>=1) {
$send_country = "MO"; }
if (substr_count($order->shipping_address->address_country[0], "Maldives")>=1) {
$send_country = "MV"; }
if (substr_count($order->shipping_address->address_country[0], "Mexico")>=1) {
$send_country = "MX"; }
if (substr_count($order->shipping_address->address_country[0], "Myanmar")>=1) {
$send_country = "MM"; }
if (substr_count($order->shipping_address->address_country[0], "New Zealand")>=1) {
$send_country = "NZ"; }
if (substr_count($order->shipping_address->address_country[0], "Nigeria")>=1) {
$send_country = "NG"; }
if (substr_count($order->shipping_address->address_country[0], "Pakistan")>=1) {
$send_country = "PK"; }
if (substr_count($order->shipping_address->address_country[0], "Philippines")>=1) {
$send_country = "PE"; }
if (substr_count($order->shipping_address->address_country[0], "Portugal")>=1) {
$send_country = "PT"; }
if (substr_count($order->shipping_address->address_country[0], "Saudi Arabia")>=1) {
$send_country = "SA"; }
if (substr_count($order->shipping_address->address_country[0], "Spain")>=1) {
$send_country = "ES"; }
if (substr_count($order->shipping_address->address_country[0], "Sri Lanka")>=1) {
$send_country = "LK"; }
if (substr_count($order->shipping_address->address_country[0], "Switzerland")>=1) {
$send_country = "CH"; }
if (substr_count($order->shipping_address->address_country[0], "Thailand")>=1) {
$send_country = "TH"; }
if (substr_count($order->shipping_address->address_country[0], "Turkey")>=1) {
$send_country = "TR"; }
if (substr_count($order->shipping_address->address_country[0], "United States")>=1) {
$send_country = "US"; }
if (substr_count($order->shipping_address->address_country[0], "Vietnam")>=1) {
$send_country = "VN"; }
$receiver_country = $send_country;
if ($order->weight<=0){
$order->weight = "1";
}
//$item_weight = $order->package["weight"]["value"];
$item_weight = $order->weight;
$item_type = $delyva->shipping_params->item_type;
switch($item_type)
{
case 0:
$item_type = "PARCEL";
break;
case 1:
$item_type = "DOCUMENT";
break;
case 2:
$item_type = "FOOD";
break;
case 3:
$item_type = "FLOWER";
break;
case 4:
$item_type = "CHILL";
break;
case 5:
$item_type = "FROZEN";
break;
case 6:
$item_type = "CAKE";
break;
case 7:
$item_type = "OTHER";
break;
case 8:
$item_type = "TASK";
break;
case 9:
$item_type = "BULKY";
break;
}
date_default_timezone_set("Asia/Kuala_Lumpur");
$date = new JDate('now +9 hour');
$shipment_date = date_format($date, 'Y-m-d')."T".date_format($date, 'H:i:s').".000Z";
$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, "
api.matdespatch.com/v2/service/price_quote
"); v1:https://api.matdespatch.com/api/services/check
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_USERPWD, $customerid.":".$apikey);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
\"sender_address\": \"".$sender_address."\",
\"sender_postcode\": \"".$sender_postcode."\",
\"sender_country\": \"".$sender_country."\",
\"receiver_address\": \"".$receiver_address."\",
\"receiver_postcode\": \"".$receiver_postcode."\",
\"receiver_country\": \"".$receiver_country."\",
\"item_weight\": ".$item_weight.",
\"item_type\": \"".$item_type."\",
\"shipment_date\": \"".$shipment_date."\"
}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
// "Authorization: Basic NWNhNTUzZTkzYTQ2NTIwNjVlY2NkZGIyOjUzOWQwNjkxZDgwOWUxZjk5NThiNjU4ZWYwYTUwNGJm",
//"Authorization: 5e15e47ce1925f20dcab1f13:7160aed31123d7840ff607e60fd6ad74",
));
$response = curl_exec($ch);
$response = json_decode($response);
//print_r($response);
curl_close($ch);
return $response;
}
//do not touch here...
}