diff --git a/lib/Tracking_UPS.lib.old.php b/lib/Tracking_UPS.lib.old.php new file mode 100644 index 0000000..5e52f9d --- /dev/null +++ b/lib/Tracking_UPS.lib.old.php @@ -0,0 +1,185 @@ + [ + "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; + } + +} diff --git a/lib/Tracking_UPS.lib.php b/lib/Tracking_UPS.lib.php index 5e52f9d..4ea289f 100644 --- a/lib/Tracking_UPS.lib.php +++ b/lib/Tracking_UPS.lib.php @@ -11,72 +11,14 @@ class Tracking_UPS { 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)); - } + $response = UPSAPIs::getAPIRequest("track/v1/details/$code?locale=en_US&returnSignature=false&returnMilestones=false&returnPOD=false"); $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"]; + if (!empty($result["trackResponse"]["shipment"]) && count($result["trackResponse"]["shipment"]) > 0) { + $trackinfo = $result["trackResponse"]["shipment"][0]; } else { throw new TrackingException("No tracking details found."); } @@ -88,58 +30,58 @@ class Tracking_UPS { } $info = new TrackingInfo(); - if (!empty($trackinfo["Package"]["TrackingNumber"])) { - $info->setCode($trackinfo["Package"]["TrackingNumber"]); - } else if (is_array($trackinfo["Package"])) { + 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; + 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->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 (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]; + if (isset($trackinfo["package"]["activity"][0])) { + $current = $trackinfo["package"]["activity"][0]; } else { - $current = $trackinfo["Package"]["Activity"]; + $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") + 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_location->city = $current["location"]["address"]["city"] ?? ""; + $current_location->state = $current["location"]["address"]["stateProvince"] ?? ""; + $current_location->zip = $current["location"]["address"]["postalCode"] ?? ""; + $current_location->country = $current["location"]["address"]["countryCode"] ?? ""; $current_status->setLocation($current_location); $info->setCurrentStatus($current_status); } - foreach ($trackinfo["ShipmentAddress"] as $address) { - switch ($address["Type"]["Code"]) { + foreach ($trackinfo["packageAddress"] as $address) { + switch ($address["type"]) { 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"] ?? ""; + $to->city = $address["address"]["city"] ?? ""; + $to->state = $address["address"]["stateProvince"] ?? ""; + $to->zip = $address["address"]["postalCode"] ?? ""; + $to->country = $address["address"]["countryCode"] ?? ""; $info->setTo($to); break; } @@ -153,27 +95,27 @@ class Tracking_UPS { // // $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"] + if (!isset($trackinfo["package"]["activity"][0])) { + $trackinfo["package"]["activity"] = [ + $trackinfo["package"]["activity"] ]; } - foreach ($trackinfo["Package"]["Activity"] as $history) { + 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"); + $location->city = $history["location"]["address"]["city"] ?? ""; + $location->state = $history["location"]["address"]["stateProvince"] ?? ""; + $location->zip = $history["location"]["address"]["postalCode"] ?? ""; + $location->country = $history["location"]["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", + TrackingStatus::UPSEventTypeToStatus($history["status"]["type"]), + $history["status"]["description"] ?? "Unknown", $datetimestring, $location )); diff --git a/lib/UPSAPIs.lib.php b/lib/UPSAPIs.lib.php new file mode 100644 index 0000000..350eafb --- /dev/null +++ b/lib/UPSAPIs.lib.php @@ -0,0 +1,55 @@ +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; + } +} diff --git a/lib/USPSAPIs.lib.php b/lib/USPSAPIs.lib.php index 3987f21..1a4c9e3 100644 --- a/lib/USPSAPIs.lib.php +++ b/lib/USPSAPIs.lib.php @@ -33,7 +33,7 @@ class USPSAPIs { public static function getAPIRequest($endpoint) { $headers = [ - "Authorization: Bearer " . USPSAPIs::getBearerToken(true), + "Authorization: Bearer " . USPSAPIs::getBearerToken(), 'Content-Type: application/json' ];