From 689ab37072b85783f81f3e24e3809db65d98bdb4 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Thu, 13 Feb 2025 23:55:15 -0700 Subject: [PATCH] Fix some bugs with the USPS tracking --- lib/Tracking_USPS.lib.php | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/Tracking_USPS.lib.php b/lib/Tracking_USPS.lib.php index 454d1c4..548be60 100644 --- a/lib/Tracking_USPS.lib.php +++ b/lib/Tracking_USPS.lib.php @@ -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"]))); }