Warning: chmod(): Operation not permitted in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [0] in function chmod in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [1] in function include_once in /var/www/hopeinstoughton_www/wp-settings.php on line 560 [2] in function require_once in /var/www/hopeinstoughton_www/wp-config.php on line 85 [3] in function require_once in /var/www/hopeinstoughton_www/wp-load.php on line 50 [4] in function require_once in /var/www/hopeinstoughton_www/wp-blog-header.php on line 13 [5] in function require in /var/www/hopeinstoughton_www/index.php on line 17
| Server IP : 94.177.8.99 / Your IP : 216.73.217.165 Web Server : Apache/2.4.58 (Ubuntu) System : Linux aries 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : www-data ( 33) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/kjvdictionary_store/modules/freightAuspost/ |
Upload File : |
<?php
namespace modules\freightAuspost;
class main extends \moduleMain {
use \modules\input\traits {
\modules\input\traits::__construct as __ConstructInput;
}
use \modules\output\traits {
\modules\output\traits::__construct as __ConstructOutput;
}
private $identifier;
private $apikey;
public function __construct() {
parent::__construct(__DIR__);
$this->__ConstructInput();
$this->__ConstructOutput();
$this->identifier = 'auspost';
}
public function Activate() {
// hook_add('tracking.validate', [$this, '__tracking_validate']); // do we recognize this tracking number? (is it in the correct format, not "does it exist")
hook_add('freight.calculate', [$this, '__get_calculate']); // calculate the freight cost for the given service(s)
}
protected function getApiKey() {
$url = 'https://auspost.com.au/parcels-mail/calculate-postage-delivery-times/scripts/app.js';
$curl = new \CurlObject;
$body = $curl->Url($url)
->Execute()
->Body();
if (preg_match('~postcodeApi\:\{key:"(.*?)",limit~', $body, $match)) {
return $match[1];
}
return false; // no key found
}
public function __get_calculate($params) {
if (empty($params->originZip)) {
return $params->status = 'error: no origin zip';
}
if (!property_exists($params, 'services')) {
$params->services = [];
}
$g = ceil($params->weight / 10) * 10; // this is in grams
if ($g < 125) {
$product = 'AUS_LETTER_REGULAR_LARGE_125';
$name = 'Large letter (125g)';
} elseif ($g < 250) {
$product = 'AUS_LETTER_REGULAR_LARGE_250';
$name = 'Large letter (250g)';
} elseif ($g < 500) {
$product = 'AUS_LETTER_REGULAR_LARGE_500';
$name = 'Large letter (500g)'; // names are in the "contentDetails" structure
} else {
$queryParams = [
"from_postcode" => $params->originZip,
"to_postcode" => $params->targetZip,
"length" => 22,
"width" => 16,
"height" => 5,
"weight" => $g / 1000,
"service_code" => 'AUS_PARCEL_REGULAR',
];
return $this->parcel_calculation($params, $queryParams);
}
// calculate letter rates
$queryParams = [
'category' => 'DOMESTIC',
'from' => $params->originZip,
'to' => $params->targetZip
];
$curl = new \CurlObject;
$body = $curl->Url('https://digitalapi.auspost.com.au/postage/v4/catalogue/service.json?' . http_build_query($queryParams))
->RequestHeader('AUTH-KEY', $this->apikey)
->Execute()
->Body();
$body = json_decode($body, TRUE);
foreach($body['items'] as $item) {
if ($item['code'] == 'REGULAR') {
foreach($item['items'] as $subitem) {
if ($subitem['type'] == 'ProductGroup') {
$service = $this->_getProductDetails($subitem['items'], $product);
if (!is_null($service)) {
$svc_id = substr(md5($product . $this->identifier), 0, 8);
$params->services[] = [
'source' => $this->identifier,
'key' => $svc_id,
'value' => $product,
'caption' => $name, // no tracking
'cost' => (float) $service['items'][0]['price'],
'shipdate' => $params->shipdate,
// 'estimated' => strtotime(str_replace('T', ' ', $service['estimated_delivery_date'])),
];
}
return;
}
}
}
}
/*
["postage_result"]=>
array(4) {
["service"]=>
string(11) "Parcel Post"
["delivery_time"]=>
string(28) "Delivered in 4 business days"
["total_cost"]=>
string(5) "15.25"
["costs"]=>
array(1) {
["cost"]=>
array(2) {
["item"]=>
string(11) "Parcel Post"
["cost"]=>
string(5) "15.25"
}
}
}
}
*/
}
private function _getProductDetails($items, $product) {
foreach($items as $item) {
if ($item['code'] == $product) {
return $item;
}
}
return NULL;
}
private function parcel_calculation($params, $queryParams) {
$curl = new \CurlObject;
$body = $curl->Url('https://digitalapi.auspost.com.au/postage/parcel/domestic/calculate?' . http_build_query($queryParams))
->RequestHeader('AUTH-KEY', $this->apikey)
->Execute()
->Body();
$body = json_decode($body, TRUE);
if (isset($body['postage_result'])) {
$service = $body['postage_result'];
$product = $queryParams['service_code'];
$svc_id = substr(md5($product . $this->identifier), 0, 8);
$params->services[] = [
'source' => $this->identifier,
'key' => $svc_id,
'value' => $product,
'caption' => $service['service'],
'cost' => (float) $service['total_cost'],
'shipdate' => $params->shipdate,
// 'estimated' => strtotime(str_replace('T', ' ', $service['estimated_delivery_date'])),
];
}
}
final function test() {
$param = new \StdClass;
$param->originZip = '4020';
$param->targetZip = '3131';
$param->weight = 200;
$param->shipdate = time() + 86400;
header('Content-type: text/plain');
$this->apikey = $this->getApiKey();
$this->__get_calculate($param);
var_dump($param);
exit;
}
}
/*
https://digitalapi.auspost.com.au/postage/v4/catalogue/service.json?category=DOMESTIC&from=4034&to=3131
// AUTH-KEY 62b9613ddab3f8cdaf89c47c0234729e
{
"items": [
{
"id": "a0f30bd3-18a3-4b38-8ee1-04d96cc50efb",
"type": "Service",
"code": "REGULAR",
"items": [
{
"id": "e1ea162c-3b00-4e01-9157-9b2a140cbbf3",
"type": "Feature",
"code": "AUS_SERVICE_OPTION_TRACKING",
"items": [
{
"type": "Price",
"price": 0
}
],
"availability": "NOT_AVAILABLE"
},
{
"id": "be1c050e-3bf7-455b-95be-e8915d988c56",
"type": "DeliveryEstimate",
"code": "REGION",
"minimum": 4,
"maximum": 5,
"estimate": "4-5 business days",
"unit": "Day"
},
{
"id": "a88e5f7a-6fe3-4066-a448-9cefbd5f5bda",
"type": "Callout",
"code": "AUS_LETTER_REGULAR_RTS_CALLOUT",
"items": [
{
"id": "03963700-f2fc-44c1-b4b1-5cbfddbd27c1",
"type": "Reference",
"code": "AUS_LETTER_REGULAR_RTS_CALLOUT"
}
],
"location": "Trailing"
},
{
"id": "f9477f65-faf1-44f1-ae6e-ec425accaa66",
"type": "ProductGroup",
"items": [
{
"id": "7ccee141-69ec-4f24-8108-2687ceae5518",
"type": "Product",
"code": "AUS_LETTER_REGULAR_SMALL",
"items": [
{
"type": "Price",
"code": "0",
"price": 1.7
}
],
"weight": 250,
"length": 130,
"width": 240,
"height": 5
},
{
"id": "e99428c6-d3cf-407f-89ba-f7ebe7268b5f",
"type": "Product",
"code": "AUS_LETTER_REGULAR_LARGE_125",
"items": [
{
"type": "Price",
"code": "0",
"price": 3.4
}
],
"weight": 125,
"length": 260,
"width": 360,
"height": 20
},
{
"id": "ac84d515-8f08-4f74-b694-621dc881b2b9",
"type": "Product",
"code": "AUS_LETTER_REGULAR_LARGE_250",
"items": [
{
"type": "Price",
"code": "0",
"price": 5.1
}
],
"weight": 250,
"length": 260,
"width": 360,
"height": 20
},
{
"id": "b281ca06-1c9e-4049-ab48-655c4bc342ec",
"type": "Product",
"code": "AUS_LETTER_REGULAR_LARGE_500",
"items": [
{
"type": "Price",
"code": "0",
"price": 8.5
}
],
"weight": 500,
"length": 260,
"width": 360,
"height": 20
}
]
},
{
"id": "2c05e24c-422c-46ed-b7c7-8a66bd3b49d3",
"type": "ProductGroup",
"items": [
{
"id": "f4180c7d-a1d8-4b8c-be32-4242ad8db799",
"type": "Feature",
"code": "AUS_SERVICE_OPTION_DELIVERY_CONFIRMATION",
"items": [
{
"type": "Price",
"price": 2.95
}
],
"availability": "EXTRA"
},
{
"id": "0e725cea-c8fd-4a0e-a241-e4a252cd4b1b",
"type": "Feature",
"code": "AUS_SERVICE_OPTION_PERSON_TO_PERSON",
"items": [
{
"type": "Price",
"price": 6.3
}
],
"availability": "EXTRA"
},
{
"id": "50714991-5672-4f31-9895-7eae11a62e65",
"type": "Feature",
"code": "AUS_SERVICE_OPTION_EXTRA_COVER",
"attributes": [
{
"name": "EXTRA_COVER_RATE_STEP",
"value": "100"
},
{
"name": "EXTRA_COVER_RATE",
"value": "2"
},
{
"name": "EXTRA_COVER_MINIMUM_COVER",
"value": "2"
},
{
"name": "EXTRA_COVER_MAXIMUM_COVER",
"value": "5000"
}
],
"availability": "FORMULA"
},
{
"id": "ac4b7386-58e5-4793-9b3b-4babfe3a9181",
"code": "AUS_PARCEL_REGULAR_PREPAID_GROUP_HIDDEN",
"name": "Prepaid satchels",
"icon": "prepaid-satchels.svg",
"description": "Our prepaid satchels combine the packaging and postage for an all-inclusive price."
},
]
}
*/