20
20
use Magento \Shipping \Model \Carrier \AbstractCarrier ;
21
21
use Magento \Shipping \Model \Carrier \AbstractCarrierOnline ;
22
22
use Magento \Shipping \Model \Rate \Result ;
23
+ use Magento \Framework \App \CacheInterface ;
23
24
24
25
/**
25
26
* Fedex shipping implementation
@@ -174,6 +175,11 @@ class Carrier extends AbstractCarrierOnline implements \Magento\Shipping\Model\C
174
175
*/
175
176
private $ decoderInterface ;
176
177
178
+ /**
179
+ * @var CacheInterface
180
+ */
181
+ private $ cache ;
182
+
177
183
/**
178
184
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
179
185
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
@@ -194,6 +200,7 @@ class Carrier extends AbstractCarrierOnline implements \Magento\Shipping\Model\C
194
200
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
195
201
* @param \Magento\Framework\HTTP\Client\CurlFactory $curlFactory
196
202
* @param \Magento\Framework\Url\DecoderInterface $decoderInterface
203
+ * @param CacheInterface $cache
197
204
* @param array $data
198
205
* @param Json|null $serializer
199
206
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -218,6 +225,7 @@ public function __construct(
218
225
\Magento \Catalog \Model \ResourceModel \Product \CollectionFactory $ productCollectionFactory ,
219
226
CurlFactory $ curlFactory ,
220
227
DecoderInterface $ decoderInterface ,
228
+ CacheInterface $ cache ,
221
229
array $ data = [],
222
230
?Json $ serializer = null
223
231
) {
@@ -244,6 +252,7 @@ public function __construct(
244
252
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
245
253
$ this ->curlFactory = $ curlFactory ;
246
254
$ this ->decoderInterface = $ decoderInterface ;
255
+ $ this ->cache = $ cache ;
247
256
}
248
257
249
258
/**
@@ -966,7 +975,19 @@ private function retrieveAccessToken(?string $apiKey, ?string $secretKey): strin
966
975
$ this ->_debug (__ ('Authentication keys are missing. ' ));
967
976
return null ;
968
977
}
969
-
978
+ $ cacheKey = 'fedex_access_token_ ' . md5 ($ apiKey . $ secretKey );
979
+ $ cacheType = 'fedex_api ' ;
980
+ $ cachedData = $ this ->cache ->load ($ cacheKey );
981
+ if ($ cachedData ) {
982
+ $ cachedData = json_decode ($ cachedData , true );
983
+ $ currentTime = time ();
984
+ if (isset ($ cachedData ['access_token ' ]) &&
985
+ isset ($ cachedData ['expires_at ' ])
986
+ && $ currentTime < $ cachedData ['expires_at ' ]
987
+ ) {
988
+ return $ cachedData ['access_token ' ];
989
+ }
990
+ }
970
991
$ requestArray = [
971
992
'grant_type ' => self ::AUTHENTICATION_GRANT_TYPE ,
972
993
'client_id ' => $ apiKey ,
@@ -980,8 +1001,14 @@ private function retrieveAccessToken(?string $apiKey, ?string $secretKey): strin
980
1001
if (!empty ($ response ['errors ' ])) {
981
1002
$ debugData = ['request_type ' => 'Access Token Request ' , 'result ' => $ response ];
982
1003
$ this ->_debug ($ debugData );
983
- } elseif (!empty ($ response ['access_token ' ])) {
1004
+ } elseif (!empty ($ response ['access_token ' ]) && isset ( $ response [ ' expires_in ' ]) ) {
984
1005
$ accessToken = $ response ['access_token ' ];
1006
+ $ expiresAt = time () + (int )$ response ['expires_in ' ];
1007
+ $ cacheData = [
1008
+ 'access_token ' => $ accessToken ,
1009
+ 'expires_at ' => $ expiresAt
1010
+ ];
1011
+ $ this ->cache ->save (json_encode ($ cacheData ), $ cacheKey , [$ cacheType ], (int )$ response ['expires_in ' ]);
985
1012
}
986
1013
987
1014
return $ accessToken ;
0 commit comments