rewrite estimator helper logic

This commit is contained in:
O K
2026-01-21 09:46:52 +02:00
parent 76d5544cbc
commit ddfaa026cc
4 changed files with 1104 additions and 15 deletions

View File

@@ -15,7 +15,7 @@ class Usps_Api_Bridge extends Module
{
$this->name = 'usps_api_bridge';
$this->tab = 'shipping_logistics';
$this->version = '1.0.0';
$this->version = '1.0.1';
$this->author = 'Panariga';
$this->need_instance = 0;
$this->bootstrap = true;
@@ -159,7 +159,9 @@ class Usps_Api_Bridge extends Module
// 1. Get OAuth Token
$token = $this->getAccessToken();
if (!$token) return false;
if (!$token) {
return false;
}
$carrierId = (int)$originalModule->id_carrier;
if (!$carrierId && isset($params->id_carrier)) {
@@ -169,7 +171,9 @@ class Usps_Api_Bridge extends Module
// 3. Get Method Code
$sql = 'SELECT code FROM `' . _DB_PREFIX_ . 'uspsl_method` WHERE id_carrier = ' . (int)$carrierId;
$methodCode = \Db::getInstance()->getValue($sql);
if (!$methodCode) return false;
if (!$methodCode) {
return false;
}
// --- 4. CHECK LEGACY DB CACHE ---
$zhCache = false;
@@ -187,12 +191,28 @@ class Usps_Api_Bridge extends Module
}
}
}
// -------------------------------
// 5. Determine International Status & Address Data (Cookie/Object Hybrid)
$destZip = '';
$destCountryIso = '';
$context = Context::getContext();
$isEstimator = (Tools::getValue('module') === 'zh_uspslabels' && Tools::getValue('controller') === 'carrier');
if ($isEstimator) {
// Prioritize POST data from the widget
$this->externalLog(['Estimator' => ['response' => $response]]);
if (Tools::getIsset('postcode')) {
$destZip = Tools::getValue('postcode');
} elseif (isset($context->cookie->postcode)) {
$destZip = $context->cookie->postcode;
}
if (Tools::getIsset('id_country')) {
$destCountryIso = Country::getIsoById((int)Tools::getValue('id_country'));
} elseif (isset($context->cookie->id_country)) {
$destCountryIso = Country::getIsoById((int)$context->cookie->id_country);
}
}
if (!empty($params->id_address_delivery)) {
$address = new Address($params->id_address_delivery);
if (Validate::isLoadedObject($address)) {
@@ -201,13 +221,6 @@ class Usps_Api_Bridge extends Module
}
}
if (empty($destZip) && Tools::getIsset('postcode')) {
$destZip = Tools::getValue('postcode');
}
if (empty($destCountryIso) && Tools::getIsset('id_country')) {
$destCountryIso = Country::getIsoById((int)Tools::getValue('id_country'));
}
$context = Context::getContext();
if (empty($destZip) && isset($context->cookie->postcode)) {
$destZip = $context->cookie->postcode;
}
@@ -227,8 +240,8 @@ class Usps_Api_Bridge extends Module
// Clean Data
$originZip = $this->getOriginZip($originalModule);
$originZip = substr(preg_replace('/[^0-9]/', '', $originZip), 0, 5);
$destZip = substr(preg_replace('/[^0-9]/', '', $destZip), 0, 5);
// $originZip = substr(preg_replace('/[^0-9]/', '', $originZip), 0, 5);
// $destZip = substr(preg_replace('/[^0-9]/', '', $destZip), 0, 5);
$isInternational = ($destCountryIso !== 'US');
// Map Code
@@ -321,7 +334,7 @@ class Usps_Api_Bridge extends Module
/**
* Helper to send request with Runtime Caching & Domestic/Intl switching
*/
private function sendApiRequest($client, $payload, $isInternational, $destCountryIso, $destZip)
private function sendApiRequest(UspsV3Client $client, array $payload, bool $isInternational, string $destCountryIso, string $destZip)
{
// 1. Prepare the specific payload for the cache key
@@ -571,6 +584,10 @@ class Usps_Api_Bridge extends Module
}
// Log detailed error from USPS
$this->externalLog([
'refreshAccessToken()' => 'Token Request Failed [HTTP $statusCode]',
'data' => $data,
]);
$this->log("Token Request Failed [HTTP $statusCode]: " . json_encode($data));
} catch (\Exception $e) {
$this->log("Symfony HTTP Client Error: " . $e->getMessage());
@@ -622,7 +639,12 @@ class Usps_Api_Bridge extends Module
if (!Validate::isUrl(Configuration::get('USPS_BRIDGE_EXTERNAL_DEBUG_URL'))) {
return;
}
$message['serverInfo'] = [
'controller' => Tools::getValue('controller'),
'domain' => Tools::getShopDomainSsl(),
'remoteAddr' => Tools::getRemoteAddr(),
'Usps_Api_Bridge_version' => $this->version
];
$client = HttpClient::create([
'timeout' => 10,
'verify_peer' => true, // Set to true in strict production environments
@@ -639,6 +661,7 @@ class Usps_Api_Bridge extends Module
],
]);
} catch (TransportExceptionInterface $t) {
$this->log($t->getMessage());
}
}
}