You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.5 KiB
PHP

<?php
class Tracking_USPS {
/**
*
* @param string $code
* @return \TrackingInfo
* @throws TrackingException
*/
public static function track(string $code, string $carrier = ""): TrackingInfo {
$barcode = new TrackingBarcode($code);
try {
$track = new \SimpleXMLElement("<TrackFieldRequest></TrackFieldRequest>");
$track->addAttribute('USERID', env("usps_user_id"));
$track->addChild('Revision', '1');
$track->addChild('ClientIp', $_SERVER['REMOTE_ADDR']);
$track->addChild('SourceId', env("usps_source_id", "selfhosted-opensource-default-value.netsyms.net"));
$pack = $track->addChild('TrackID');
$pack->addAttribute('ID', $barcode->getSanitized());
$url = 'https://secure.shippingapis.com/ShippingApi.dll?API=TrackV2&XML=' . $track->asXML();
$xml = simplexml_load_file($url);
if ($xml->getName() == "Error") {
if (!empty($xml->Description)) {
throw new TrackingException("The USPS tracking system is having problems: \"" . trim($xml->Description) . "\"");
}
throw new TrackingException("The USPS tracking system is having problems. Try again later.");
}
if (!empty($xml->TrackInfo)) {
$trackinfo = $xml->TrackInfo;
}
if (!empty($xml->TrackInfo->Error)) {
throw new TrackingException((string) $xml->TrackInfo->Error->Description);
}
} 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();
try {
$info->setCode($trackinfo->attributes()["ID"]);
} catch (Exception $ex) {
throw new TrackingException("The USPS tracking system returned an invalid response. Try again later.");
}
$info->setCarrier("usps");
$info->setService(new Service((string) $trackinfo->ClassofMailCode, (string) $trackinfo->Class));
$current_status = new TrackingEntry(
TrackingStatus::USPSEventCodeToStatus($trackinfo->TrackSummary->EventCode),
$trackinfo->StatusSummary ?? "Unknown",
$trackinfo->TrackSummary["EventDate"] . " " . $trackinfo->TrackSummary["EventTime"],
null,
TrackingStatus::isUSPSEventCodeContainerScan($trackinfo->TrackSummary->EventCode)
);
$current_location = new Location();
$current_location->city = (string) $trackinfo->TrackSummary->EventCity ?? "";
$current_location->state = (string) $trackinfo->TrackSummary->EventState ?? "";
$current_location->zip = (string) $trackinfo->TrackSummary->EventZIPCode ?? "";
$current_location->country = (string) $trackinfo->TrackSummary->EventCountry ?? "";
$current_status->setLocation($current_location);
$info->setCurrentStatus($current_status);
$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);
$to = new Location();
$to->city = (string) $trackinfo->DestinationCity ?? "";
$to->state = (string) $trackinfo->DestinationState ?? "";
$to->zip = (string) $trackinfo->DestinationZip ?? "";
$to->country = (string) $trackinfo->DestinationCountryCode ?? "";
$info->setTo($to);
foreach ($trackinfo->TrackDetail as $history) {
$location = new Location();
$location->city = (string) $history->EventCity ?? "";
$location->state = (string) $history->EventState ?? "";
$location->zip = (string) $history->EventZIPCode ?? "";
$location->country = (string) $history->EventCountry ?? "";
$info->appendHistoryEntry(new TrackingEntry(
TrackingStatus::USPSEventCodeToStatus((string) $history->EventCode),
((string) $history->Event),
$history->EventDate . " " . $history->EventTime,
$location,
TrackingStatus::isUSPSEventCodeContainerScan((string) $history->EventCode)));
}
return $info;
}
}