Add EasyPost tracking API, handle carrier logo/attribution better
This commit is contained in:
parent
9d98e34cd6
commit
4b82dc323d
@ -50,4 +50,5 @@ $SETTINGS = [
|
||||
"ups_user_account" => "",
|
||||
"ups_password" => "",
|
||||
"shippo_key" => "",
|
||||
"easypost_key" => "",
|
||||
];
|
49
lib/CarrierAssets.lib.php
Normal file
49
lib/CarrierAssets.lib.php
Normal file
File diff suppressed because one or more lines are too long
@ -125,6 +125,10 @@ class Carriers {
|
||||
"code" => "ups",
|
||||
"name" => "UPS"
|
||||
],
|
||||
"upsdap" => [
|
||||
"code" => "ups",
|
||||
"name" => "UPS"
|
||||
],
|
||||
"ups_mi_datamatrix" => [
|
||||
"code" => "usps",
|
||||
"name" => "USPS"
|
||||
|
@ -30,7 +30,8 @@ class Tracking {
|
||||
case "ups":
|
||||
return Tracking_UPS::track($barcode->getSanitized());
|
||||
default:
|
||||
return Tracking_Shippo::track($barcode->getSanitized());
|
||||
return Tracking_EasyPost::track($barcode->getSanitized());
|
||||
//return Tracking_Shippo::track($barcode->getSanitized());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,14 +22,19 @@ class TrackingStatus {
|
||||
case "TRANSIT":
|
||||
case "IN_TRANSIT":
|
||||
case "ACCEPTED":
|
||||
case "OUT_FOR_DELIVERY":
|
||||
return TrackingStatus::TRACKING_STATUS_TRANSIT;
|
||||
case "DELIVERED":
|
||||
case "AVAILABLE_FOR_PICKUP":
|
||||
return TrackingStatus::TRACKING_STATUS_DELIVERED;
|
||||
case "RETURNED":
|
||||
case "RETURN_TO_SENDER":
|
||||
return TrackingStatus::TRACKING_STATUS_RETURNED;
|
||||
case "FAILURE":
|
||||
case "DELIVERY ATTEMPT":
|
||||
case "ALERT":
|
||||
case "ERROR":
|
||||
case "CANCELLED":
|
||||
return TrackingStatus::TRACKING_STATUS_FAILURE;
|
||||
default:
|
||||
return TrackingStatus::TRACKING_STATUS_UNKNOWN;
|
||||
|
101
lib/Tracking_EasyPost.lib.php
Normal file
101
lib/Tracking_EasyPost.lib.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
class Tracking_EasyPost {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $code
|
||||
* @return \TrackingInfo
|
||||
* @throws TrackingException
|
||||
*/
|
||||
public static function track(string $code, string $carrier = ""): TrackingInfo {
|
||||
$barcode = new TrackingBarcode($code);
|
||||
|
||||
\EasyPost\EasyPost::setApiKey(env("easypost_key"));
|
||||
|
||||
try {
|
||||
if (empty($carrier)) {
|
||||
$tracker = \EasyPost\Tracker::create(array(
|
||||
"tracking_code" => $code
|
||||
));
|
||||
} else {
|
||||
$trackinginfo["carrier"] = $carrier;
|
||||
$tracker = \EasyPost\Tracker::create(array(
|
||||
"tracking_code" => $code,
|
||||
"carrier" => $carrier
|
||||
));
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
throw new TrackingException("There was a server problem. This code cannot be tracked right now.");
|
||||
}
|
||||
|
||||
$info = new TrackingInfo();
|
||||
|
||||
$info->setCode($tracker->tracking_code);
|
||||
$info->setCarrier(strtolower($tracker->carrier));
|
||||
$info->setCarrierAttributionText(CarrierAssets::getAttribution(Carriers::getCarrierCode($info->getCarrier())));
|
||||
$info->setCarrierLogo(CarrierAssets::getLogo(Carriers::getCarrierCode($info->getCarrier())));
|
||||
|
||||
if (!empty($tracker->tracking_details) && count($tracker->tracking_details) > 0) {
|
||||
|
||||
$events_sorted = $tracker->tracking_details;
|
||||
|
||||
// start https://stackoverflow.com/a/2910637
|
||||
function date_compare($a, $b) {
|
||||
$t1 = strtotime($a->datetime);
|
||||
$t2 = strtotime($b->datetime);
|
||||
return $t2 - $t1;
|
||||
}
|
||||
usort($events_sorted, 'date_compare');
|
||||
// end https://stackoverflow.com/a/2910637
|
||||
|
||||
foreach ($events_sorted as $history) {
|
||||
$location = new Location();
|
||||
$location->city = $history->tracking_location["city"];
|
||||
$location->state = $history->tracking_location["state"];
|
||||
$location->zip = $history->tracking_location["zip"];
|
||||
$location->country = $history->tracking_location["country"];
|
||||
$info->appendHistoryEntry(new TrackingEntry(TrackingStatus::stringToStatus($history->status), $history->message, $history->datetime, $location));
|
||||
}
|
||||
|
||||
$current_status = new TrackingEntry(
|
||||
TrackingStatus::stringToStatus(
|
||||
$events_sorted[0]->status),
|
||||
$events_sorted[0]->message,
|
||||
$events_sorted[0]->datetime
|
||||
);
|
||||
if (!empty($events_sorted[0]->tracking_location)) {
|
||||
$current_location = new Location();
|
||||
$current_location->city = $events_sorted[0]->tracking_location->city;
|
||||
$current_location->state = $events_sorted[0]->tracking_location->state;
|
||||
$current_location->zip = $events_sorted[0]->tracking_location->zip;
|
||||
$current_location->country = $events_sorted[0]->tracking_location->country;
|
||||
$current_status->setLocation($current_location);
|
||||
}
|
||||
$info->setCurrentStatus($current_status);
|
||||
}
|
||||
|
||||
if (!empty($tracker->carrier_detail->origin_tracking_location)) {
|
||||
$from = new Location();
|
||||
$from->city = $tracker->carrier_detail->origin_tracking_location->city ?? "";
|
||||
$from->state = $tracker->carrier_detail->origin_tracking_location->state ?? "";
|
||||
$from->zip = $tracker->carrier_detail->origin_tracking_location->zip ?? "";
|
||||
$from->country = $tracker->carrier_detail->origin_tracking_location->country ?? "";
|
||||
|
||||
$info->setFrom($from);
|
||||
}
|
||||
|
||||
if (!empty($tracker->carrier_detail->destination_tracking_location)) {
|
||||
$to = new Location();
|
||||
$to->city = $tracker->carrier_detail->destination_tracking_location->city ?? "";
|
||||
$to->state = $tracker->carrier_detail->destination_tracking_location->state ?? "";
|
||||
$to->zip = $tracker->carrier_detail->destination_tracking_location->zip ?? "";
|
||||
$to->country = $tracker->carrier_detail->destination_tracking_location->country ?? "";
|
||||
|
||||
$info->setTo($to);
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
}
|
@ -14,9 +14,8 @@ class Tracking_HelenaExpress {
|
||||
|
||||
$barcode = new TrackingBarcode($code);
|
||||
|
||||
|
||||
try {
|
||||
$status = json_decode(file_get_contents("https://helena.express/tracker/api.php?code=" . $barcode->getCode()));
|
||||
$status = json_decode(file_get_contents("https://helena.express/apis/track/?code=" . $barcode->getCode()));
|
||||
} catch (Exception $ex) {
|
||||
throw new TrackingException("There was a server problem. This code cannot be tracked right now.");
|
||||
}
|
||||
@ -30,6 +29,8 @@ class Tracking_HelenaExpress {
|
||||
$info->setCode($barcode->getCode());
|
||||
$info->setCarrier($status->info->carrier);
|
||||
$info->setService(new Service("", "Local Courier"));
|
||||
$info->setCarrierAttributionText(CarrierAssets::getAttribution(Carriers::getCarrierCode($info->getCarrier())));
|
||||
$info->setCarrierLogo(CarrierAssets::getLogo(Carriers::getCarrierCode($info->getCarrier())));
|
||||
|
||||
if (!empty($status->events)) {
|
||||
$current_status = new TrackingEntry(
|
||||
|
@ -31,6 +31,8 @@ class Tracking_Shippo {
|
||||
$info->setCode($barcode->getCode());
|
||||
$info->setCarrier($barcode->getCarrier());
|
||||
$info->setService(new Service($status->servicelevel->token, $status->servicelevel->name));
|
||||
$info->setCarrierAttributionText(CarrierAssets::getAttribution(Carriers::getCarrierCode($info->getCarrier())));
|
||||
$info->setCarrierLogo(CarrierAssets::getLogo(Carriers::getCarrierCode($info->getCarrier())));
|
||||
|
||||
if (!empty($status->tracking_status)) {
|
||||
$current_status = new TrackingEntry(
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user