Fix some bugs with the USPS tracking

This commit is contained in:
Skylar Ittner 2025-02-13 23:55:15 -07:00
parent 51653a66ad
commit 689ab37072

View File

@ -32,11 +32,20 @@ class Tracking_USPS {
$json = json_decode($resp, true);
if (!empty($json["error"])) {
if (!empty($json["error"]["errors"]) && $json["error"]["errors"][0]["code"] == "150001") {
// Tracking number not found
throw new TrackingException(str_replace("12: ", "", $json["error"]["errors"][0]["title"]));
}
if (!empty($json["error"]["message"])) {
if (!empty($json["error"]["errors"])) {
$msg = $json["error"]["errors"][0]["title"];
$msg = preg_replace("/\s?-\s?$/", "", $msg); // Remove weird dash at end of message
switch ($json["error"]["errors"][0]["code"]) {
case "150001":
// Tracking number not found
throw new TrackingException(str_replace("12: ", "", $msg));
case "150002":
// Incorrect tracking number
throw new TrackingException(str_replace("7: ", "", $msg));
}
} else if (!empty($json["error"]["errors"]) && !empty($json["error"]["errors"][0]["code"])) {
throw new TrackingException("The USPS tracking system is having problems: \"" . trim($json["error"]["message"]) . "\" (" . $json["error"]["errors"][0]["code"] . ")");
} else if (!empty($json["error"]["message"])) {
throw new TrackingException("The USPS tracking system is having problems: \"" . trim($json["error"]["message"]) . "\"");
}
throw new TrackingException("The USPS tracking system is having problems. Try again later.");
@ -104,9 +113,9 @@ class Tracking_USPS {
$info->setCurrentStatus($current_status);
$from = new Location();
$from->city = (string) $trackinfo["originCity"] ?? "";
$from->state = (string) $trackinfo["originState"] ?? "";
$from->zip = (string) $trackinfo["originZIP"] ?? "";
$from->city = (string) (isset($trackinfo["originCity"]) ? $trackinfo["originCity"] : "");
$from->state = (string) (isset($trackinfo["originState"]) ? $trackinfo["originState"] : "");
$from->zip = (string) (isset($trackinfo["originZIP"]) ? $trackinfo["originZIP"] : "");
$from->country = (string) (isset($trackinfo["originCountry"]) ? $trackinfo["originCountry"] : "");
$info->setFrom($from);
@ -134,10 +143,20 @@ class Tracking_USPS {
$location->state = self::STATELESS_CITIES[$location->city];
}
}
$datetime = $history["eventTimestamp"];
if (empty($history["eventTimestamp"])) {
if ($i > 0 && !empty($trackinfo["trackingEvents"][$i - 1]["eventTimestamp"])) {
$datetime = $trackinfo["trackingEvents"][$i - 1]["eventTimestamp"];
} else if ($i < count($trackinfo["trackingEvents"]) - 1 && !empty($trackinfo["trackingEvents"][$i + 1]["eventTimestamp"])) {
$datetime = $trackinfo["trackingEvents"][$i + 1]["eventTimestamp"];
} else {
$datetime = "1970-01-01";
}
}
$info->appendHistoryEntry(new TrackingEntry(
TrackingStatus::USPSEventCodeToStatus((string) $history["eventCode"]),
$history["eventType"] . (TrackingStatus::USPSEventCodeToStatus((string) $history["eventCode"]) == TrackingStatus::TRACKING_STATUS_UNKNOWN ? " " . (string) $history["eventCode"] : ""),
$history["eventTimestamp"],
$datetime,
$location,
TrackingStatus::isUSPSEventCodeContainerScan((string) $history["eventCode"])));
}