add mail classes mappings override

This commit is contained in:
O K
2025-12-27 12:42:24 +02:00
parent 9f5f685bf2
commit d0eaba08d4

View File

@@ -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,10 +374,9 @@ 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
@@ -392,11 +390,37 @@ class Usps_Api_Bridge extends Module
'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_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;
}