2020use Magento \Shipping \Model \Carrier \AbstractCarrier ;
2121use Magento \Shipping \Model \Carrier \AbstractCarrierOnline ;
2222use Magento \Shipping \Model \Rate \Result ;
23+ use Magento \Framework \App \CacheInterface ;
2324
2425/**
2526 * Fedex shipping implementation
@@ -174,6 +175,11 @@ class Carrier extends AbstractCarrierOnline implements \Magento\Shipping\Model\C
174175 */
175176 private $ decoderInterface ;
176177
178+ /**
179+ * @var CacheInterface
180+ */
181+ private $ cache ;
182+
177183 /**
178184 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
179185 * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
@@ -194,6 +200,7 @@ class Carrier extends AbstractCarrierOnline implements \Magento\Shipping\Model\C
194200 * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
195201 * @param \Magento\Framework\HTTP\Client\CurlFactory $curlFactory
196202 * @param \Magento\Framework\Url\DecoderInterface $decoderInterface
203+ * @param CacheInterface $cache
197204 * @param array $data
198205 * @param Json|null $serializer
199206 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -218,6 +225,7 @@ public function __construct(
218225 \Magento \Catalog \Model \ResourceModel \Product \CollectionFactory $ productCollectionFactory ,
219226 CurlFactory $ curlFactory ,
220227 DecoderInterface $ decoderInterface ,
228+ CacheInterface $ cache ,
221229 array $ data = [],
222230 ?Json $ serializer = null
223231 ) {
@@ -244,6 +252,7 @@ public function __construct(
244252 $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
245253 $ this ->curlFactory = $ curlFactory ;
246254 $ this ->decoderInterface = $ decoderInterface ;
255+ $ this ->cache = $ cache ;
247256 }
248257
249258 /**
@@ -966,7 +975,19 @@ private function retrieveAccessToken(?string $apiKey, ?string $secretKey): strin
966975 $ this ->_debug (__ ('Authentication keys are missing. ' ));
967976 return null ;
968977 }
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+ }
970991 $ requestArray = [
971992 'grant_type ' => self ::AUTHENTICATION_GRANT_TYPE ,
972993 'client_id ' => $ apiKey ,
@@ -980,8 +1001,14 @@ private function retrieveAccessToken(?string $apiKey, ?string $secretKey): strin
9801001 if (!empty ($ response ['errors ' ])) {
9811002 $ debugData = ['request_type ' => 'Access Token Request ' , 'result ' => $ response ];
9821003 $ this ->_debug ($ debugData );
983- } elseif (!empty ($ response ['access_token ' ])) {
1004+ } elseif (!empty ($ response ['access_token ' ]) && isset ( $ response [ ' expires_in ' ]) ) {
9841005 $ 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 ' ]);
9851012 }
9861013
9871014 return $ accessToken ;
0 commit comments