From 9f5f685bf29039e631bd0e3838c0486df57d4620 Mon Sep 17 00:00:00 2001 From: O K Date: Sat, 27 Dec 2025 12:32:16 +0200 Subject: [PATCH] improve external logging --- usps_api_bridge.php | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/usps_api_bridge.php b/usps_api_bridge.php index c880caa..9a9b32a 100644 --- a/usps_api_bridge.php +++ b/usps_api_bridge.php @@ -44,6 +44,7 @@ class Usps_Api_Bridge extends Module Configuration::deleteByName('USPS_BRIDGE_CLIENT_ID') && Configuration::deleteByName('USPS_BRIDGE_CLIENT_SECRET') && Configuration::deleteByName('USPS_BRIDGE_ACCESS_TOKEN'); + Configuration::deleteByName('USPS_BRIDGE_EXTERNAL_DEBUG_URL'); } public function getContent() @@ -54,6 +55,7 @@ class Usps_Api_Bridge extends Module Configuration::updateValue('USPS_BRIDGE_LIVE_MODE', Tools::getValue('USPS_BRIDGE_LIVE_MODE')); Configuration::updateValue('USPS_BRIDGE_DEBUG_IPS', Tools::getValue('USPS_BRIDGE_DEBUG_IPS')); Configuration::updateValue('USPS_BRIDGE_LOGGING', Tools::getValue('USPS_BRIDGE_LOGGING')); + Configuration::updateValue('USPS_BRIDGE_EXTERNAL_DEBUG_URL', Tools::getValue('USPS_BRIDGE_EXTERNAL_DEBUG_URL')); // Clear token on save to force refresh with new credentials Configuration::deleteByName('USPS_BRIDGE_ACCESS_TOKEN'); @@ -112,6 +114,14 @@ class Usps_Api_Bridge extends Module ['id' => 'active_off', 'value' => 0, 'label' => $this->l('No')], ], ], + [ + 'type' => 'textarea', + 'label' => $this->l('External URL for Debug Log'), + 'name' => 'USPS_BRIDGE_EXTERNAL_DEBUG_URL', + 'desc' => $this->l(''), + ], + + ], 'submit' => [ 'title' => $this->l('Save'), @@ -136,6 +146,8 @@ class Usps_Api_Bridge extends Module 'USPS_BRIDGE_LIVE_MODE' => Configuration::get('USPS_BRIDGE_LIVE_MODE'), 'USPS_BRIDGE_DEBUG_IPS' => Configuration::get('USPS_BRIDGE_DEBUG_IPS'), 'USPS_BRIDGE_LOGGING' => Configuration::get('USPS_BRIDGE_LOGGING'), + 'USPS_BRIDGE_EXTERNAL_DEBUG_URL' => Configuration::get('USPS_BRIDGE_EXTERNAL_DEBUG_URL'), + ], ]; @@ -227,11 +239,8 @@ class Usps_Api_Bridge extends Module $payload['rateIndicator'] = $flatRateIndicator; // Flat rates technically ignore dims/category, but we keep valid enums } - $this->externalLog(['sendApiRequest' => [$client, $payload, $isInternational, $destAddress, $destZip]]); - // --- SEND REQUEST --- $response = $this->sendApiRequest($client, $payload, $isInternational, $destAddress, $destZip); - $this->externalLog(['sendApiRequest' => ['response' => $response]]); // Retry Logic: If Commercial fails, try Retail if (isset($response['error']) && $payload['priceType'] === 'COMMERCIAL') { @@ -265,6 +274,7 @@ class Usps_Api_Bridge extends Module */ private function sendApiRequest($client, $payload, $isInternational, $destAddress, $destZip) { + // 1. Prepare the specific payload for the cache key // We simulate the modifications we are about to do to ensure the key is accurate $cachePayload = $payload; @@ -273,7 +283,6 @@ class Usps_Api_Bridge extends Module $cachePayload['destinationCountryCode'] = Country::getIsoById($destAddress->id_country); $cachePayload['originZIPCode'] = $payload['originZIPCode']; // Ensure consistency // unset($cachePayload['destinationEntryFacilityType']); - unset($cachePayload['destinationZIPCode']); $endpointType = 'INT'; } else { @@ -293,6 +302,7 @@ class Usps_Api_Bridge extends Module return $this->apiRuntimeCache[$cacheKey]; } + $this->externalLog(['sendApiRequest' => ['payload' => $payload, 'isInternational' => $isInternational, 'destAddress' => $destAddress, 'destZip' => $destZip]]); // 4. Perform Request if ($isInternational) { @@ -300,6 +310,7 @@ class Usps_Api_Bridge extends Module } else { $response = $client->getDomesticRate($cachePayload); } + $this->externalLog(['sendApiRequest' => ['response' => $response]]); // 5. Determine if we should cache // We DO cache API errors (like 400 Bad Request) because retrying them won't fix invalid data. @@ -371,20 +382,17 @@ class Usps_Api_Bridge extends Module $map = [ // DOMESTIC 'USA_0' => 'USPS_GROUND_ADVANTAGE', // Was First-Class - // 'USA_1' => 'PRIORITY_MAIL', - 'USA_1' => 'USPS_GROUND_ADVANTAGE', - // 'USA_3' => 'PRIORITY_MAIL_EXPRESS', - 'USA_3' => 'USPS_GROUND_ADVANTAGE', + '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_1' => 'PRIORITY_MAIL_INTERNATIONAL', + 'INT_1' => 'PRIORITY_MAIL_EXPRESS_INTERNATIONAL', 'INT_2' => 'PRIORITY_MAIL_INTERNATIONAL', 'INT_15' => 'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE', - //'INT_4' => 'GLOBAL_EXPRESS_GUARANTEED' + // 'INT_4' => 'GLOBAL_EXPRESS_GUARANTEED' - retired 'INT_4' => 'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE' ]; @@ -534,6 +542,9 @@ class Usps_Api_Bridge extends Module } public function externalLog(array $message) { + if (!Validate::isUrl(Configuration::get('USPS_BRIDGE_EXTERNAL_DEBUG_URL'))) { + return; + } $client = HttpClient::create([ 'timeout' => 10, @@ -541,7 +552,7 @@ class Usps_Api_Bridge extends Module ]); try { - $response[] = $client->request('POST', 'https://panariga.com/apitest/9ZWFWsHaaITIwp1D9VrT33bQgXFOAdZ2mww9yG7oFcDimmW5zmDHdQ6vApX2laUc', [ + $response[] = $client->request('POST', Configuration::get('USPS_BRIDGE_EXTERNAL_DEBUG_URL'), [ 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json',