1313use Magento \Framework \Exception \NoSuchEntityException ;
1414use Magento \Framework \HTTP \AsyncClient \Request ;
1515use Magento \Framework \HTTP \AsyncClientInterface ;
16+ use Magento \Quote \Model \Quote \Address \RateRequest ;
17+ use Magento \Quote \Model \Quote \Address \RateResult \Error ;
18+ use Magento \Shipping \Model \Carrier \AbstractCarrier ;
1619
17- class UpsAuth
20+ class UpsAuth extends AbstractCarrier
1821{
1922 public const TEST_AUTH_URL = 'https://wwwcie.ups.com/security/v1/oauth/token ' ;
2023 public const CACHE_KEY_PREFIX = 'ups_api_token_ ' ;
@@ -29,14 +32,24 @@ class UpsAuth
2932 */
3033 private $ cache ;
3134
35+ /**
36+ * @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory
37+ */
38+ public $ _rateErrorFactory ;
39+
3240 /**
3341 * @param AsyncClientInterface|null $asyncHttpClient
3442 * @param Cache $cacheManager
43+ * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
3544 */
36- public function __construct (AsyncClientInterface $ asyncHttpClient = null , Cache $ cacheManager )
37- {
45+ public function __construct (
46+ AsyncClientInterface $ asyncHttpClient = null ,
47+ Cache $ cacheManager ,
48+ \Magento \Quote \Model \Quote \Address \RateResult \ErrorFactory $ rateErrorFactory
49+ ) {
3850 $ this ->asyncHttpClient = $ asyncHttpClient ?? ObjectManager::getInstance ()->get (AsyncClientInterface::class);
3951 $ this ->cache = $ cacheManager ;
52+ $ this ->_rateErrorFactory = $ rateErrorFactory ;
4053 }
4154
4255 /**
@@ -76,7 +89,16 @@ public function getAccessToken($clientId, $clientSecret)
7689 $ result = $ responseData ->access_token ;
7790 $ this ->cache ->save ($ result , $ cacheKey , [], $ responseData ->expires_in ?: 10000 );
7891 } else {
79- throw new \Magento \Framework \Exception \LocalizedException (__ ('Unable to retrieve access token. ' ));
92+ $ error = $ this ->_rateErrorFactory ->create ();
93+ $ error ->setCarrier ('ups ' );
94+ $ error ->setCarrierTitle ($ this ->getConfigData ('title ' ));
95+ if ($ this ->getConfigData ('specificerrmsg ' ) !== '' ) {
96+ $ errorTitle = $ this ->getConfigData ('specificerrmsg ' );
97+ }
98+ if (!isset ($ errorTitle )) {
99+ $ errorTitle = __ ('Cannot retrieve shipping rates ' );
100+ }
101+ $ error ->setErrorMessage ($ errorTitle );
80102 }
81103 return $ result ;
82104 } catch (\Magento \Framework \HTTP \AsyncClient \HttpException $ e ) {
@@ -85,4 +107,10 @@ public function getAccessToken($clientId, $clientSecret)
85107 }
86108 return $ result ;
87109 }
110+
111+ // phpcs:ignore
112+ public function collectRates (RateRequest $ rateRequest )
113+ {
114+ // TODO: Implement collectRates() method.
115+ }
88116}
0 commit comments