Use Oauth2 for UPS tracking

This commit is contained in:
Skylar Ittner 2025-04-23 14:57:31 -06:00
parent 766f132451
commit 24310ee8e2
4 changed files with 285 additions and 103 deletions

View File

@ -0,0 +1,185 @@
<?php
class Tracking_UPS {
/**
*
* @param string $code
* @return \TrackingInfo
* @throws TrackingException
*/
public static function track(string $code, string $carrier = ""): TrackingInfo {
$barcode = new TrackingBarcode($code);
try {
$trackrequest = [
"Security" => [
"UsernameToken" => [
"Username" => env("ups_user_account"),
"Password" => env("ups_password")
],
"UPSServiceAccessToken" => [
"AccessLicenseNumber" => env("ups_access_key")
]
],
"TrackRequest" => [
"Request" => [
"RequestAction" => "Track",
"RequestOption" => "activity"
],
"InquiryNumber" => $code
]
];
$headers = [
'Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept',
'Access-Control-Allow-Methods: POST',
'Access-Control-Allow-Origin: *',
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
//curl_setopt($ch, CURLOPT_URL, "https://wwwcie.ups.com/rest/Track"); // TEST
curl_setopt($ch, CURLOPT_URL, "https://onlinetools.ups.com/rest/Track"); // PROD
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($trackrequest));
$response = curl_exec($ch);
//header("Content-Type: application/json");
//echo json_encode(json_decode($response), JSON_PRETTY_PRINT);
//exit();
// CHECK TO SEE IF WE GOT AN ERROR
// IF SO, FORMAT IT LIKE THIS ::28::Operation timed out afterseconds
if ((curl_errno($ch)) && (curl_errno($ch) != 0)) {
throw new TrackingException(curl_error($ch));
}
$result = json_decode($response, true);
if (!empty($result["response"])) {
// Should have trackResponse instead...
if (!empty($result["response"]["errors"][0]["message"])) {
throw new TrackingException($result["response"]["errors"][0]["message"]);
}
throw new TrackingException("The UPS tracking system is having problems. Try again later.");
}
if (!empty($result["Fault"])) {
// Should have trackResponse instead...
if (!empty($result["Fault"]["detail"]["Errors"]["ErrorDetail"]["PrimaryErrorCode"]["Description"])) {
throw new TrackingException($result["Fault"]["detail"]["Errors"]["ErrorDetail"]["PrimaryErrorCode"]["Description"]);
}
throw new TrackingException("The UPS tracking system is having problems. Try again later.");
}
if (!empty($result["TrackResponse"]["Shipment"]) && !empty($result["TrackResponse"]["Shipment"])) {
$trackinfo = $result["TrackResponse"]["Shipment"];
} 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();
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 = [];
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;
}
}
}
$info->setCarrier("ups");
$info->setService(new Service($trackinfo["Service"]["Code"], $trackinfo["Service"]["Description"]));
$info->setCarrierAttributionText(CarrierAssets::getAttribution(Carriers::getCarrierCode($info->getCarrier())));
$info->setCarrierLogo(CarrierAssets::getLogo(Carriers::getCarrierCode($info->getCarrier())));
if (count($trackinfo["Package"]["Activity"]) > 0) {
// If there's only one entry, it might not be an array
if (isset($trackinfo["Package"]["Activity"][0])) {
$current = $trackinfo["Package"]["Activity"][0];
} else {
$current = $trackinfo["Package"]["Activity"];
}
$current_status = new TrackingEntry(
TrackingStatus::UPSEventTypeToStatus($current["Status"]["Type"]),
$current["Status"]["Description"] ?? "Unknown",
DateTime::createFromFormat('Ymd His', "$current[Date] $current[Time]")->format("c")
);
$current_location = new Location();
$current_location->city = $current["ActivityLocation"]["Address"]["City"] ?? "";
$current_location->state = $current["ActivityLocation"]["Address"]["StateProvinceCode"] ?? "";
$current_location->zip = $current["ActivityLocation"]["Address"]["PostalCode"] ?? "";
$current_location->country = $current["ActivityLocation"]["Address"]["CountryCode"] ?? "";
$current_status->setLocation($current_location);
$info->setCurrentStatus($current_status);
}
foreach ($trackinfo["ShipmentAddress"] as $address) {
switch ($address["Type"]["Code"]) {
case "02": // ShipTo Address
$to = new Location();
$to->city = $address["Address"]["City"] ?? "";
$to->state = $address["Address"]["StateProvinceCode"] ?? "";
$to->zip = $address["Address"]["PostalCode"] ?? "";
$to->country = $address["Address"]["CountryCode"] ?? "";
$info->setTo($to);
break;
}
}
//
// $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
if (!isset($trackinfo["Package"]["Activity"][0])) {
$trackinfo["Package"]["Activity"] = [
$trackinfo["Package"]["Activity"]
];
}
foreach ($trackinfo["Package"]["Activity"] as $history) {
$location = new Location();
$location->city = $history["ActivityLocation"]["Address"]["City"] ?? "";
$location->state = $history["ActivityLocation"]["Address"]["StateProvinceCode"] ?? "";
$location->zip = $history["ActivityLocation"]["Address"]["PostalCode"] ?? "";
$location->country = $history["ActivityLocation"]["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 = "";
}
$info->appendHistoryEntry(new TrackingEntry(
TrackingStatus::UPSEventTypeToStatus($history["Status"]["Type"]),
$history["Status"]["Description"] ?? "Unknown",
$datetimestring,
$location
));
}
return $info;
}
}

View File

@ -11,72 +11,14 @@ class Tracking_UPS {
public static function track(string $code, string $carrier = ""): TrackingInfo { public static function track(string $code, string $carrier = ""): TrackingInfo {
$barcode = new TrackingBarcode($code); $barcode = new TrackingBarcode($code);
try { try {
$trackrequest = [ $response = UPSAPIs::getAPIRequest("track/v1/details/$code?locale=en_US&returnSignature=false&returnMilestones=false&returnPOD=false");
"Security" => [
"UsernameToken" => [
"Username" => env("ups_user_account"),
"Password" => env("ups_password")
],
"UPSServiceAccessToken" => [
"AccessLicenseNumber" => env("ups_access_key")
]
],
"TrackRequest" => [
"Request" => [
"RequestAction" => "Track",
"RequestOption" => "activity"
],
"InquiryNumber" => $code
]
];
$headers = [
'Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept',
'Access-Control-Allow-Methods: POST',
'Access-Control-Allow-Origin: *',
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
//curl_setopt($ch, CURLOPT_URL, "https://wwwcie.ups.com/rest/Track"); // TEST
curl_setopt($ch, CURLOPT_URL, "https://onlinetools.ups.com/rest/Track"); // PROD
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($trackrequest));
$response = curl_exec($ch);
//header("Content-Type: application/json");
//echo json_encode(json_decode($response), JSON_PRETTY_PRINT);
//exit();
// CHECK TO SEE IF WE GOT AN ERROR
// IF SO, FORMAT IT LIKE THIS ::28::Operation timed out afterseconds
if ((curl_errno($ch)) && (curl_errno($ch) != 0)) {
throw new TrackingException(curl_error($ch));
}
$result = json_decode($response, true); $result = json_decode($response, true);
if (!empty($result["response"])) { if (!empty($result["trackResponse"]["shipment"]) && count($result["trackResponse"]["shipment"]) > 0) {
// Should have trackResponse instead... $trackinfo = $result["trackResponse"]["shipment"][0];
if (!empty($result["response"]["errors"][0]["message"])) {
throw new TrackingException($result["response"]["errors"][0]["message"]);
}
throw new TrackingException("The UPS tracking system is having problems. Try again later.");
}
if (!empty($result["Fault"])) {
// Should have trackResponse instead...
if (!empty($result["Fault"]["detail"]["Errors"]["ErrorDetail"]["PrimaryErrorCode"]["Description"])) {
throw new TrackingException($result["Fault"]["detail"]["Errors"]["ErrorDetail"]["PrimaryErrorCode"]["Description"]);
}
throw new TrackingException("The UPS tracking system is having problems. Try again later.");
}
if (!empty($result["TrackResponse"]["Shipment"]) && !empty($result["TrackResponse"]["Shipment"])) {
$trackinfo = $result["TrackResponse"]["Shipment"];
} else { } else {
throw new TrackingException("No tracking details found."); throw new TrackingException("No tracking details found.");
} }
@ -88,58 +30,58 @@ class Tracking_UPS {
} }
$info = new TrackingInfo(); $info = new TrackingInfo();
if (!empty($trackinfo["Package"]["TrackingNumber"])) { if (!empty($trackinfo["package"]["trackingNumber"])) {
$info->setCode($trackinfo["Package"]["TrackingNumber"]); $info->setCode($trackinfo["package"]["trackingNumber"]);
} else if (is_array($trackinfo["Package"])) { } else if (is_array($trackinfo["package"])) {
// More than one linked package in one shipment, get rid of the extra and make the // More than one linked package in one shipment, get rid of the extra and make the
// schema match what we expect // schema match what we expect
$package = []; $package = [];
for ($i = 0; $i < count($trackinfo["Package"]); $i++) { for ($i = 0; $i < count($trackinfo["package"]); $i++) {
if ($trackinfo["Package"][$i]["TrackingNumber"] == $code) { if ($trackinfo["package"][$i]["trackingNumber"] == $code) {
$info->setCode($trackinfo["Package"][$i]["TrackingNumber"]); $info->setCode($trackinfo["package"][$i]["trackingNumber"]);
$package = $trackinfo["Package"][$i]; $package = $trackinfo["package"][$i];
$trackinfo["Package"] = $package; $trackinfo["package"] = $package;
break; break;
} }
} }
} }
$info->setCarrier("ups"); $info->setCarrier("ups");
$info->setService(new Service($trackinfo["Service"]["Code"], $trackinfo["Service"]["Description"])); $info->setService(new Service($trackinfo["service"]["code"], $trackinfo["service"]["description"]));
$info->setCarrierAttributionText(CarrierAssets::getAttribution(Carriers::getCarrierCode($info->getCarrier()))); $info->setCarrierAttributionText(CarrierAssets::getAttribution(Carriers::getCarrierCode($info->getCarrier())));
$info->setCarrierLogo(CarrierAssets::getLogo(Carriers::getCarrierCode($info->getCarrier()))); $info->setCarrierLogo(CarrierAssets::getLogo(Carriers::getCarrierCode($info->getCarrier())));
if (count($trackinfo["Package"]["Activity"]) > 0) { if (count($trackinfo["package"]["activity"]) > 0) {
// If there's only one entry, it might not be an array // If there's only one entry, it might not be an array
if (isset($trackinfo["Package"]["Activity"][0])) { if (isset($trackinfo["package"]["activity"][0])) {
$current = $trackinfo["Package"]["Activity"][0]; $current = $trackinfo["package"]["activity"][0];
} else { } else {
$current = $trackinfo["Package"]["Activity"]; $current = $trackinfo["package"]["activity"];
} }
$current_status = new TrackingEntry( $current_status = new TrackingEntry(
TrackingStatus::UPSEventTypeToStatus($current["Status"]["Type"]), TrackingStatus::UPSEventTypeToStatus($current["status"]["type"]),
$current["Status"]["Description"] ?? "Unknown", $current["status"]["description"] ?? "Unknown",
DateTime::createFromFormat('Ymd His', "$current[Date] $current[Time]")->format("c") DateTime::createFromFormat('Ymd His', "$current[date] $current[time]")->format("c")
); );
$current_location = new Location(); $current_location = new Location();
$current_location->city = $current["ActivityLocation"]["Address"]["City"] ?? ""; $current_location->city = $current["location"]["address"]["city"] ?? "";
$current_location->state = $current["ActivityLocation"]["Address"]["StateProvinceCode"] ?? ""; $current_location->state = $current["location"]["address"]["stateProvince"] ?? "";
$current_location->zip = $current["ActivityLocation"]["Address"]["PostalCode"] ?? ""; $current_location->zip = $current["location"]["address"]["postalCode"] ?? "";
$current_location->country = $current["ActivityLocation"]["Address"]["CountryCode"] ?? ""; $current_location->country = $current["location"]["address"]["countryCode"] ?? "";
$current_status->setLocation($current_location); $current_status->setLocation($current_location);
$info->setCurrentStatus($current_status); $info->setCurrentStatus($current_status);
} }
foreach ($trackinfo["ShipmentAddress"] as $address) { foreach ($trackinfo["packageAddress"] as $address) {
switch ($address["Type"]["Code"]) { switch ($address["type"]) {
case "02": // ShipTo Address case "02": // ShipTo Address
$to = new Location(); $to = new Location();
$to->city = $address["Address"]["City"] ?? ""; $to->city = $address["address"]["city"] ?? "";
$to->state = $address["Address"]["StateProvinceCode"] ?? ""; $to->state = $address["address"]["stateProvince"] ?? "";
$to->zip = $address["Address"]["PostalCode"] ?? ""; $to->zip = $address["address"]["postalCode"] ?? "";
$to->country = $address["Address"]["CountryCode"] ?? ""; $to->country = $address["address"]["countryCode"] ?? "";
$info->setTo($to); $info->setTo($to);
break; break;
} }
@ -153,27 +95,27 @@ class Tracking_UPS {
// //
// $info->setFrom($from); // $info->setFrom($from);
// Only one entry, so put it in itself so the loop works // Only one entry, so put it in itself so the loop works
if (!isset($trackinfo["Package"]["Activity"][0])) { if (!isset($trackinfo["package"]["activity"][0])) {
$trackinfo["Package"]["Activity"] = [ $trackinfo["package"]["activity"] = [
$trackinfo["Package"]["Activity"] $trackinfo["package"]["activity"]
]; ];
} }
foreach ($trackinfo["Package"]["Activity"] as $history) { foreach ($trackinfo["package"]["activity"] as $history) {
$location = new Location(); $location = new Location();
$location->city = $history["ActivityLocation"]["Address"]["City"] ?? ""; $location->city = $history["location"]["address"]["city"] ?? "";
$location->state = $history["ActivityLocation"]["Address"]["StateProvinceCode"] ?? ""; $location->state = $history["location"]["address"]["stateProvince"] ?? "";
$location->zip = $history["ActivityLocation"]["Address"]["PostalCode"] ?? ""; $location->zip = $history["location"]["address"]["postalCode"] ?? "";
$location->country = $history["ActivityLocation"]["Address"]["CountryCode"] ?? ""; $location->country = $history["location"]["address"]["countryCode"] ?? "";
if (!empty($history["Date"]) && !empty($history["Time"])) { if (!empty($history["date"]) && !empty($history["time"])) {
$datetimestring = DateTime::createFromFormat('Ymd His', "$history[Date] $history[Time]")->format("c"); $datetimestring = DateTime::createFromFormat('Ymd His', "$history[date] $history[time]")->format("c");
} else if (!empty($history["Date"])) { } else if (!empty($history["date"])) {
$datetimestring = DateTime::createFromFormat('Ymd His', "$history[Date]")->format("c"); $datetimestring = DateTime::createFromFormat('Ymd His', "$history[date]")->format("c");
} else { } else {
$datetimestring = ""; $datetimestring = "";
} }
$info->appendHistoryEntry(new TrackingEntry( $info->appendHistoryEntry(new TrackingEntry(
TrackingStatus::UPSEventTypeToStatus($history["Status"]["Type"]), TrackingStatus::UPSEventTypeToStatus($history["status"]["type"]),
$history["Status"]["Description"] ?? "Unknown", $history["status"]["description"] ?? "Unknown",
$datetimestring, $datetimestring,
$location $location
)); ));

55
lib/UPSAPIs.lib.php Normal file
View File

@ -0,0 +1,55 @@
<?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHP.php to edit this template
*/
class UPSAPIs {
const BASEURL = "https://wwwcie.ups.com";
//const BASEURL = "https://onlinetools.ups.com";
public static function getBearerToken(bool $force = false): string {
global $memcache;
$clientid = env("ups_client_id");
$clientsecret = env("ups_client_secret");
if (!$force && $memcache->get("logistics.tracking.ups_bearer_token") != false) {
return $memcache->get("logistics.tracking.ups_bearer_token");
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, static::BASEURL . "/security/v1/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"client_id\": \"$clientid\", \"client_secret\": \"$clientsecret\", \"grant_type\": \"client_credentials\"}");
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
$memcache->set("logistics.tracking.ups_bearer_token", $data["access_token"], ($data["expires_in"] * 1) - 120);
return $data["access_token"];
}
public static function getAPIRequest($endpoint) {
$headers = [
"Authorization: Bearer " . UPSAPIs::getBearerToken(true),
"transId: " . uniqid(),
"transactionSrc: Netsyms"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
curl_setopt($ch, CURLOPT_URL, static::BASEURL . "/$endpoint");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
$response = curl_exec($ch);
return $response;
}
}

View File

@ -33,7 +33,7 @@ class USPSAPIs {
public static function getAPIRequest($endpoint) { public static function getAPIRequest($endpoint) {
$headers = [ $headers = [
"Authorization: Bearer " . USPSAPIs::getBearerToken(true), "Authorization: Bearer " . USPSAPIs::getBearerToken(),
'Content-Type: application/json' 'Content-Type: application/json'
]; ];