From d0eaba08d4552b296c480ea18ebc5077aec33d45 Mon Sep 17 00:00:00 2001 From: O K Date: Sat, 27 Dec 2025 12:42:24 +0200 Subject: [PATCH] add mail classes mappings override --- usps_api_bridge.php | 60 +++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/usps_api_bridge.php b/usps_api_bridge.php index 9a9b32a..8ffef29 100644 --- a/usps_api_bridge.php +++ b/usps_api_bridge.php @@ -170,9 +170,7 @@ class Usps_Api_Bridge extends Module if (!$methodCode) return false; - // 3. Map Old Code to New API Enum - $newApiClass = $this->mapServiceCodeToApiClass($methodCode); - if (!$newApiClass) return false; + // 4. Pack Products $packedBoxes = $originalModule->getHelper()->getCarrierHelper()->packProducts($products, $params->id); @@ -193,7 +191,8 @@ class Usps_Api_Bridge extends Module $originZip = substr(preg_replace('/[^0-9]/', '', $originZip), 0, 5); $destZip = substr(preg_replace('/[^0-9]/', '', $destAddress->postcode), 0, 5); $isInternational = ($destAddress->id_country != Country::getByIso('US')); - + $newApiClass = $this->mapServiceCodeToApiClass($methodCode, $isInternational); + if (!$newApiClass) return false; // 7. Loop through boxes foreach ($packedBoxes as $packedBox) { @@ -375,28 +374,53 @@ class Usps_Api_Bridge extends Module /** * MAPPING LOGIC: Old Module Codes -> New API Enums */ - private function mapServiceCodeToApiClass($oldCode) + private function mapServiceCodeToApiClass($oldCode, $isInternational) { - // Mappings based on your provided file classes/Model/Method.php - // and the New API Spec Enums + // 1. Define the Standard Map $map = [ // DOMESTIC - 'USA_0' => 'USPS_GROUND_ADVANTAGE', // Was First-Class - 'USA_1' => 'PRIORITY_MAIL', - 'USA_3' => 'PRIORITY_MAIL_EXPRESS', - 'USA_6' => 'MEDIA_MAIL', - 'USA_7' => 'LIBRARY_MAIL', + 'USA_0' => 'USPS_GROUND_ADVANTAGE', // Was First-Class + 'USA_1' => 'PRIORITY_MAIL', + 'USA_3' => 'PRIORITY_MAIL_EXPRESS', + 'USA_6' => 'MEDIA_MAIL', + 'USA_7' => 'LIBRARY_MAIL', 'USA_1058' => 'USPS_GROUND_ADVANTAGE', // INTERNATIONAL - 'INT_1' => 'PRIORITY_MAIL_EXPRESS_INTERNATIONAL', - 'INT_2' => 'PRIORITY_MAIL_INTERNATIONAL', - 'INT_15' => 'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE', - // 'INT_4' => 'GLOBAL_EXPRESS_GUARANTEED' - retired - 'INT_4' => 'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE' - + 'INT_1' => 'PRIORITY_MAIL_EXPRESS_INTERNATIONAL', + 'INT_2' => 'PRIORITY_MAIL_INTERNATIONAL', + 'INT_15' => 'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE', + 'INT_4' => 'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE', // GXG is suspended/retired, fallback to First Class ]; + if (!isset($map[$oldCode])) { + return false; + } + + $apiClass = $map[$oldCode]; + + // 2. International Override Logic + // If the destination is International, but the mapped class is Domestic, swap it. + if ($isInternational) { + switch ($apiClass) { + case 'PRIORITY_MAIL': + return 'PRIORITY_MAIL_INTERNATIONAL'; + + case 'PRIORITY_MAIL_EXPRESS': + return 'PRIORITY_MAIL_EXPRESS_INTERNATIONAL'; + + // Ground Advantage, Media, and Library do not exist internationally. + // The closest equivalent is First-Class Package International. + case 'USPS_GROUND_ADVANTAGE': + case 'MEDIA_MAIL': + case 'LIBRARY_MAIL': + return 'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE'; + } + } + + return $apiClass; + } + return isset($map[$oldCode]) ? $map[$oldCode] : false; }