improve external logging

This commit is contained in:
O K
2025-12-27 12:32:16 +02:00
parent 1ec760de1d
commit 9f5f685bf2

View File

@@ -44,6 +44,7 @@ class Usps_Api_Bridge extends Module
Configuration::deleteByName('USPS_BRIDGE_CLIENT_ID') && Configuration::deleteByName('USPS_BRIDGE_CLIENT_ID') &&
Configuration::deleteByName('USPS_BRIDGE_CLIENT_SECRET') && Configuration::deleteByName('USPS_BRIDGE_CLIENT_SECRET') &&
Configuration::deleteByName('USPS_BRIDGE_ACCESS_TOKEN'); Configuration::deleteByName('USPS_BRIDGE_ACCESS_TOKEN');
Configuration::deleteByName('USPS_BRIDGE_EXTERNAL_DEBUG_URL');
} }
public function getContent() 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_LIVE_MODE', Tools::getValue('USPS_BRIDGE_LIVE_MODE'));
Configuration::updateValue('USPS_BRIDGE_DEBUG_IPS', Tools::getValue('USPS_BRIDGE_DEBUG_IPS')); 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_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 // Clear token on save to force refresh with new credentials
Configuration::deleteByName('USPS_BRIDGE_ACCESS_TOKEN'); 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')], ['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' => [ 'submit' => [
'title' => $this->l('Save'), '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_LIVE_MODE' => Configuration::get('USPS_BRIDGE_LIVE_MODE'),
'USPS_BRIDGE_DEBUG_IPS' => Configuration::get('USPS_BRIDGE_DEBUG_IPS'), 'USPS_BRIDGE_DEBUG_IPS' => Configuration::get('USPS_BRIDGE_DEBUG_IPS'),
'USPS_BRIDGE_LOGGING' => Configuration::get('USPS_BRIDGE_LOGGING'), '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; $payload['rateIndicator'] = $flatRateIndicator;
// Flat rates technically ignore dims/category, but we keep valid enums // Flat rates technically ignore dims/category, but we keep valid enums
} }
$this->externalLog(['sendApiRequest' => [$client, $payload, $isInternational, $destAddress, $destZip]]);
// --- SEND REQUEST --- // --- SEND REQUEST ---
$response = $this->sendApiRequest($client, $payload, $isInternational, $destAddress, $destZip); $response = $this->sendApiRequest($client, $payload, $isInternational, $destAddress, $destZip);
$this->externalLog(['sendApiRequest' => ['response' => $response]]);
// Retry Logic: If Commercial fails, try Retail // Retry Logic: If Commercial fails, try Retail
if (isset($response['error']) && $payload['priceType'] === 'COMMERCIAL') { 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) private function sendApiRequest($client, $payload, $isInternational, $destAddress, $destZip)
{ {
// 1. Prepare the specific payload for the cache key // 1. Prepare the specific payload for the cache key
// We simulate the modifications we are about to do to ensure the key is accurate // We simulate the modifications we are about to do to ensure the key is accurate
$cachePayload = $payload; $cachePayload = $payload;
@@ -273,7 +283,6 @@ class Usps_Api_Bridge extends Module
$cachePayload['destinationCountryCode'] = Country::getIsoById($destAddress->id_country); $cachePayload['destinationCountryCode'] = Country::getIsoById($destAddress->id_country);
$cachePayload['originZIPCode'] = $payload['originZIPCode']; // Ensure consistency $cachePayload['originZIPCode'] = $payload['originZIPCode']; // Ensure consistency
// unset($cachePayload['destinationEntryFacilityType']); // unset($cachePayload['destinationEntryFacilityType']);
unset($cachePayload['destinationZIPCode']); unset($cachePayload['destinationZIPCode']);
$endpointType = 'INT'; $endpointType = 'INT';
} else { } else {
@@ -293,6 +302,7 @@ class Usps_Api_Bridge extends Module
return $this->apiRuntimeCache[$cacheKey]; return $this->apiRuntimeCache[$cacheKey];
} }
$this->externalLog(['sendApiRequest' => ['payload' => $payload, 'isInternational' => $isInternational, 'destAddress' => $destAddress, 'destZip' => $destZip]]);
// 4. Perform Request // 4. Perform Request
if ($isInternational) { if ($isInternational) {
@@ -300,6 +310,7 @@ class Usps_Api_Bridge extends Module
} else { } else {
$response = $client->getDomesticRate($cachePayload); $response = $client->getDomesticRate($cachePayload);
} }
$this->externalLog(['sendApiRequest' => ['response' => $response]]);
// 5. Determine if we should cache // 5. Determine if we should cache
// We DO cache API errors (like 400 Bad Request) because retrying them won't fix invalid data. // 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 = [ $map = [
// DOMESTIC // DOMESTIC
'USA_0' => 'USPS_GROUND_ADVANTAGE', // Was First-Class 'USA_0' => 'USPS_GROUND_ADVANTAGE', // Was First-Class
// 'USA_1' => 'PRIORITY_MAIL', 'USA_1' => 'PRIORITY_MAIL',
'USA_1' => 'USPS_GROUND_ADVANTAGE', 'USA_3' => 'PRIORITY_MAIL_EXPRESS',
// 'USA_3' => 'PRIORITY_MAIL_EXPRESS',
'USA_3' => 'USPS_GROUND_ADVANTAGE',
'USA_6' => 'MEDIA_MAIL', 'USA_6' => 'MEDIA_MAIL',
'USA_7' => 'LIBRARY_MAIL', 'USA_7' => 'LIBRARY_MAIL',
'USA_1058' => 'USPS_GROUND_ADVANTAGE', 'USA_1058' => 'USPS_GROUND_ADVANTAGE',
// INTERNATIONAL // INTERNATIONAL
// 'INT_1' => 'PRIORITY_MAIL_EXPRESS_INTERNATIONAL', 'INT_1' => 'PRIORITY_MAIL_EXPRESS_INTERNATIONAL',
'INT_1' => 'PRIORITY_MAIL_INTERNATIONAL',
'INT_2' => 'PRIORITY_MAIL_INTERNATIONAL', 'INT_2' => 'PRIORITY_MAIL_INTERNATIONAL',
'INT_15' => 'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE', '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' 'INT_4' => 'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE'
]; ];
@@ -534,6 +542,9 @@ class Usps_Api_Bridge extends Module
} }
public function externalLog(array $message) public function externalLog(array $message)
{ {
if (!Validate::isUrl(Configuration::get('USPS_BRIDGE_EXTERNAL_DEBUG_URL'))) {
return;
}
$client = HttpClient::create([ $client = HttpClient::create([
'timeout' => 10, 'timeout' => 10,
@@ -541,7 +552,7 @@ class Usps_Api_Bridge extends Module
]); ]);
try { try {
$response[] = $client->request('POST', 'https://panariga.com/apitest/9ZWFWsHaaITIwp1D9VrT33bQgXFOAdZ2mww9yG7oFcDimmW5zmDHdQ6vApX2laUc', [ $response[] = $client->request('POST', Configuration::get('USPS_BRIDGE_EXTERNAL_DEBUG_URL'), [
'headers' => [ 'headers' => [
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
'Accept' => 'application/json', 'Accept' => 'application/json',