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/theyoungdesigners_com/modules/freightUps/ |
Upload File : |
<?php
namespace modules\freightUps;
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;
const CARRIER_ID = 'se-613701';
public function __construct() {
parent::__construct(__DIR__);
$this->__ConstructInput();
$this->__ConstructOutput();
$this->identifier = 'ups';
}
// class must be listed in a 'depends' setting to be activated
public function Activate() {
hook_add('freight.calculate', [$this, '__get_calculate']); // calculate the freight cost for the given service(s)
hook_add('tracking.validate', [$this, '__tracking_validate']); // do we recognize this tracking number? (is it in the correct format, not "does it exist")
// to do: generate a link to the tracking results
}
// https://shipengine.github.io/shipengine-openapi/#operation/compare_bulk_rates
// https://www.shipengine.com/docs/rates/
public function __get_calculate($params) {
if (empty($params->originZip)) {
return $params->status = 'error: no origin zip';
}
if (!property_exists($params, 'services')) {
$params->services = [];
}
$oz = $param->weight / 1000 * 35.274;
if ($oz < 1) $oz = 1;
$oz = ceil($oz);
[$from_state, $from_ctry] = \Misc::ZipToState($params->originZip);
[$to_state, $to_ctry] = \Misc::ZipToState($params->targetZip);
$json = [
'carrier_id' => static::CARRIER_ID,
'from_postal_code' => $params->originZip,
'from_state_province' => $from_state,
'from_country_code' => $from_ctry,
'to_postal_code' => $params->targetZip,
'to_state_province' => $to_state,
'to_country_code' => $to_ctry,
'weight' => [
'value' => $oz,
'unit' => 'ounce',
],
'dimensions' => [
'unit' => 'inch',
'length' => 0,
'width' => 0,
'height' => 0,
],
'confirmation' => 'none',
'address_residential_indicator' => 'unknown',
'ship_date' => gmdate('Y-m-d\TH:00:00.000\Z', $params->shipdate),
];
$curl = new \CurlObject;
$curl->Url('https://api.shipengine.com/v1/rates/estimate')
->postdataJson($json)
->RequestHeader('API-Key', $this->settings['apikey'])
->Execute();
$body = $curl->Body();
$result = json_decode($body, TRUE);
foreach($result as $service) {
$rate_item = $service['service_code'];
$svc_id = substr(md5($rate_item . $this->identifier), 0, 8);
$params->services[] = [
'source' => $this->identifier,
'key' => $svc_id,
'value' => $rate_item,
'caption' => $service['service_type'],
'cost' => $service['shipping_amount']['amount'],
'shipdate' => $params->shipdate,
'estimated' => strtotime(str_replace('T', ' ', $service['estimated_delivery_date'])),
];
// error_log(json_encode($service, JSON_UNESCAPED_SLASHES));
}
}
// https://www.codeproject.com/Articles/21224/Calculating-the-UPS-Tracking-Number-Check-Digit
public function parseTrackingNumber($tracking_number) {
$tracking_number = strtoupper($tracking_number);
if (substr($tracking_number, 0, 2) == '1Z') {
$result = new \StdClass;
$result->carrier = $this->identifier;
$result->tracking = $tracking_number;
$result->account = substr($tracking_number, 2, 6);
$result->service = substr($tracking_number, 8, 2); // 01 = next day, 02 = second day air, 03 = ground
$result->invoice = substr($tracking_number, 10, 5);
$result->package = substr($tracking_number, 15, 2); // package number
$result->check = substr($tracking_number, 17, 1); // check Digit
$tracking = substr($tracking_number, 2, 15); // checksum based on this
$new = '';
for($i = 0; $i < strlen($tracking); $i++) {
$ch = $tracking[$i];
if ($ch >= 'A' && $ch <= 'Z') {
$ch = ord($ch) - 63;
$ch = $ch % 10;
}
$new .= $ch;
}
$tracking = $new;
$total = 0;
for($i = 0; $i < strlen($tracking); $i++) {
if ($i & 1) { // odd
$total += 2 * $tracking[$i];
} else { // even
$total += $tracking[$i];
}
}
$checkdigit = $total % 10;
if ($checkdigit > 0) $checkdigit = 10 - $checkdigit;
$result->valid = $result->check == $checkdigit;
return $result;
}
return NULL;
}
public function __tracking_validate(&$result, $tracking_number) {
if (!is_object($result)) {
$tracking_number = str_replace(' ', '', $tracking_number);
if (preg_match('~\b1Z[A-Z0-9]{16}\b~', $tracking_number)) {
$result = $this->parseTrackingNumber($tracking_number);
}
}
}
}