data.netsyms.net/lib/Tracking_UPS.lib.php

135 lines
6.1 KiB
PHP
Raw Normal View History

2021-05-21 18:50:46 -06:00
<?php
class Tracking_UPS {
/**
*
* @param string $code
* @return \TrackingInfo
* @throws TrackingException
*/
public static function track(string $code, string $carrier = ""): TrackingInfo {
$barcode = new TrackingBarcode($code);
2025-04-23 14:57:31 -06:00
try {
$response = UPSAPIs::getAPIRequest("track/v1/details/$code?locale=en_US&returnSignature=false&returnMilestones=false&returnPOD=false");
2021-05-21 18:50:46 -06:00
$result = json_decode($response, true);
2025-04-23 14:57:31 -06:00
if (!empty($result["trackResponse"]["shipment"]) && count($result["trackResponse"]["shipment"]) > 0) {
$trackinfo = $result["trackResponse"]["shipment"][0];
2021-05-21 18:50:46 -06:00
} else {
throw new TrackingException("No tracking details found.");
}
//exitWithJson($trackinfo);
} catch (TrackingException $ex) {
throw $ex;
} catch (Exception $ex) {
throw new TrackingException("There was a server problem. This code cannot be tracked right now.");
}
$info = new TrackingInfo();
2025-04-23 14:57:31 -06:00
if (!empty($trackinfo["package"]["trackingNumber"])) {
$info->setCode($trackinfo["package"]["trackingNumber"]);
} else if (is_array($trackinfo["package"])) {
// More than one linked package in one shipment, get rid of the extra and make the
// schema match what we expect
$package = [];
2025-04-23 14:57:31 -06:00
for ($i = 0; $i < count($trackinfo["package"]); $i++) {
if ($trackinfo["package"][$i]["trackingNumber"] == $code) {
$info->setCode($trackinfo["package"][$i]["trackingNumber"]);
$package = $trackinfo["package"][$i];
$trackinfo["package"] = $package;
break;
}
}
}
2021-05-21 18:50:46 -06:00
$info->setCarrier("ups");
2025-04-29 18:58:02 -06:00
$info->setService(new Service($trackinfo["package"]["service"]["code"], $trackinfo["package"]["service"]["description"]));
$info->setCarrierAttributionText(CarrierAssets::getAttribution(Carriers::getCarrierCode($info->getCarrier())));
$info->setCarrierLogo(CarrierAssets::getLogo(Carriers::getCarrierCode($info->getCarrier())));
2021-05-21 18:50:46 -06:00
2025-04-23 14:57:31 -06:00
if (count($trackinfo["package"]["activity"]) > 0) {
// If there's only one entry, it might not be an array
2025-04-23 14:57:31 -06:00
if (isset($trackinfo["package"]["activity"][0])) {
$current = $trackinfo["package"]["activity"][0];
} else {
2025-04-23 14:57:31 -06:00
$current = $trackinfo["package"]["activity"];
}
2021-05-21 18:50:46 -06:00
$current_status = new TrackingEntry(
2025-04-23 14:57:31 -06:00
TrackingStatus::UPSEventTypeToStatus($current["status"]["type"]),
2025-04-29 18:58:02 -06:00
trim($current["status"]["description"] ?? "Unknown"),
2025-04-23 14:57:31 -06:00
DateTime::createFromFormat('Ymd His', "$current[date] $current[time]")->format("c")
2021-05-21 18:50:46 -06:00
);
$current_location = new Location();
2025-04-23 14:57:31 -06:00
$current_location->city = $current["location"]["address"]["city"] ?? "";
$current_location->state = $current["location"]["address"]["stateProvince"] ?? "";
$current_location->zip = $current["location"]["address"]["postalCode"] ?? "";
$current_location->country = $current["location"]["address"]["countryCode"] ?? "";
2021-05-21 18:50:46 -06:00
$current_status->setLocation($current_location);
$info->setCurrentStatus($current_status);
}
2025-04-29 18:58:02 -06:00
foreach ($trackinfo["package"]["packageAddress"] as $address) {
2025-04-23 14:57:31 -06:00
switch ($address["type"]) {
2025-04-29 18:58:02 -06:00
case "DESTINATION": // ShipTo Address
2021-05-21 18:50:46 -06:00
$to = new Location();
2025-04-23 14:57:31 -06:00
$to->city = $address["address"]["city"] ?? "";
$to->state = $address["address"]["stateProvince"] ?? "";
$to->zip = $address["address"]["postalCode"] ?? "";
$to->country = $address["address"]["countryCode"] ?? "";
2021-05-21 18:50:46 -06:00
$info->setTo($to);
break;
2025-04-29 18:58:02 -06:00
case "ORIGIN":
$from = new Location();
$from->city = $address["address"]["city"] ?? "";
$from->state = $address["address"]["stateProvince"] ?? "";
$from->zip = $address["address"]["postalCode"] ?? "";
$from->country = $address["address"]["countryCode"] ?? "";
$info->setFrom($from);
2021-05-21 18:50:46 -06:00
}
}
//
// $from = new Location();
// $from->city = (string) $trackinfo->OriginCity ?? "";
// $from->state = (string) $trackinfo->OriginState ?? "";
// $from->zip = (string) $trackinfo->OriginZip ?? "";
// $from->country = (string) $trackinfo->OriginCountryCode ?? "";
//
// $info->setFrom($from);
// Only one entry, so put it in itself so the loop works
2025-04-23 14:57:31 -06:00
if (!isset($trackinfo["package"]["activity"][0])) {
$trackinfo["package"]["activity"] = [
$trackinfo["package"]["activity"]
];
}
2025-04-23 14:57:31 -06:00
foreach ($trackinfo["package"]["activity"] as $history) {
2021-05-21 18:50:46 -06:00
$location = new Location();
2025-04-23 14:57:31 -06:00
$location->city = $history["location"]["address"]["city"] ?? "";
$location->state = $history["location"]["address"]["stateProvince"] ?? "";
$location->zip = $history["location"]["address"]["postalCode"] ?? "";
$location->country = $history["location"]["address"]["countryCode"] ?? "";
if (!empty($history["date"]) && !empty($history["time"])) {
$datetimestring = DateTime::createFromFormat('Ymd His', "$history[date] $history[time]")->format("c");
} else if (!empty($history["date"])) {
$datetimestring = DateTime::createFromFormat('Ymd His', "$history[date]")->format("c");
} else {
$datetimestring = "";
}
2021-05-21 18:50:46 -06:00
$info->appendHistoryEntry(new TrackingEntry(
2025-04-23 14:57:31 -06:00
TrackingStatus::UPSEventTypeToStatus($history["status"]["type"]),
2025-04-29 18:58:02 -06:00
trim($history["status"]["description"] ?? "Unknown"),
$datetimestring,
2021-05-21 18:50:46 -06:00
$location
));
}
return $info;
}
}