move to HttpClient
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
if (!defined('_PS_VERSION_')) {
|
||||
exit;
|
||||
}
|
||||
use Symfony\Component\HttpClient\HttpClient;
|
||||
|
||||
|
||||
class Usps_Api_Bridge extends Module
|
||||
{
|
||||
@@ -354,46 +356,59 @@ class Usps_Api_Bridge extends Module
|
||||
$clientSecret = Configuration::get('USPS_BRIDGE_CLIENT_SECRET');
|
||||
$isLive = (bool)Configuration::get('USPS_BRIDGE_LIVE_MODE');
|
||||
|
||||
// URLs based on documentation (Verification pending next step)
|
||||
// CORRECT URLs based on the OpenAPI Spec provided:
|
||||
// Prod: https://apis.usps.com/oauth2/v3
|
||||
// Test: https://apis-tem.usps.com/oauth2/v3
|
||||
$url = $isLive
|
||||
? 'https://api.usps.com/oauth2/v3/token'
|
||||
: 'https://api-cat.usps.com/oauth2/v3/token';
|
||||
? 'https://apis.usps.com/oauth2/v3/token'
|
||||
: 'https://apis-tem.usps.com/oauth2/v3/token';
|
||||
|
||||
$this->log("Requesting New Token from: " . $url);
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
|
||||
'client_id' => $clientId,
|
||||
'client_secret' => $clientSecret,
|
||||
'grant_type' => 'client_credentials'
|
||||
]));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
// Create Symfony Client
|
||||
$client = HttpClient::create([
|
||||
'timeout' => 10,
|
||||
'verify_peer' => true, // Set to true in strict production environments
|
||||
'verify_host' => false,
|
||||
]);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
try {
|
||||
$response = $client->request('POST', $url, [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
],
|
||||
// 'json' key automatically encodes the array to JSON and sets Content-Type
|
||||
'json' => [
|
||||
'client_id' => $clientId,
|
||||
'client_secret' => $clientSecret,
|
||||
'grant_type' => 'client_credentials',
|
||||
// 'scope' => 'prices international-prices' // Specifying scope helps avoid ambiguity
|
||||
],
|
||||
]);
|
||||
|
||||
if (curl_errno($ch)) {
|
||||
$this->log("CURL Error: " . curl_error($ch));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get status code
|
||||
$statusCode = $response->getStatusCode();
|
||||
|
||||
$data = json_decode($response, true);
|
||||
// Convert response to array (pass false to prevent throwing exceptions on 4xx/5xx)
|
||||
$data = $response->toArray(false);
|
||||
|
||||
if ($httpCode == 200 && isset($data['access_token'])) {
|
||||
$expiresIn = isset($data['expires_in']) ? (int)$data['expires_in'] : 3599;
|
||||
if ($statusCode == 200 && isset($data['access_token'])) {
|
||||
$expiresIn = isset($data['expires_in']) ? (int)$data['expires_in'] : 3599;
|
||||
|
||||
Configuration::updateValue('USPS_BRIDGE_ACCESS_TOKEN', $data['access_token']);
|
||||
Configuration::updateValue('USPS_BRIDGE_TOKEN_EXPIRY', time() + $expiresIn);
|
||||
Configuration::updateValue('USPS_BRIDGE_ACCESS_TOKEN', $data['access_token']);
|
||||
Configuration::updateValue('USPS_BRIDGE_TOKEN_EXPIRY', time() + $expiresIn);
|
||||
|
||||
$this->log("Token refreshed successfully.");
|
||||
return $data['access_token'];
|
||||
$this->log("Token refreshed successfully.");
|
||||
return $data['access_token'];
|
||||
}
|
||||
|
||||
// Log detailed error from USPS
|
||||
$this->log("Token Request Failed [HTTP $statusCode]: " . json_encode($data));
|
||||
} catch (\Exception $e) {
|
||||
$this->log("Symfony HTTP Client Error: " . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->log("Token Request Failed: " . print_r($response, true));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user