13
13
use Magento \Framework \Exception \NoSuchEntityException ;
14
14
use Magento \Framework \HTTP \AsyncClient \Request ;
15
15
use 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 ;
16
19
17
- class UpsAuth
20
+ class UpsAuth extends AbstractCarrier
18
21
{
19
22
public const TEST_AUTH_URL = 'https://wwwcie.ups.com/security/v1/oauth/token ' ;
20
23
public const CACHE_KEY_PREFIX = 'ups_api_token_ ' ;
@@ -29,14 +32,24 @@ class UpsAuth
29
32
*/
30
33
private $ cache ;
31
34
35
+ /**
36
+ * @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory
37
+ */
38
+ public $ _rateErrorFactory ;
39
+
32
40
/**
33
41
* @param AsyncClientInterface|null $asyncHttpClient
34
42
* @param Cache $cacheManager
43
+ * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
35
44
*/
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
+ ) {
38
50
$ this ->asyncHttpClient = $ asyncHttpClient ?? ObjectManager::getInstance ()->get (AsyncClientInterface::class);
39
51
$ this ->cache = $ cacheManager ;
52
+ $ this ->_rateErrorFactory = $ rateErrorFactory ;
40
53
}
41
54
42
55
/**
@@ -76,7 +89,16 @@ public function getAccessToken($clientId, $clientSecret)
76
89
$ result = $ responseData ->access_token ;
77
90
$ this ->cache ->save ($ result , $ cacheKey , [], $ responseData ->expires_in ?: 10000 );
78
91
} 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 );
80
102
}
81
103
return $ result ;
82
104
} catch (\Magento \Framework \HTTP \AsyncClient \HttpException $ e ) {
@@ -85,4 +107,10 @@ public function getAccessToken($clientId, $clientSecret)
85
107
}
86
108
return $ result ;
87
109
}
110
+
111
+ // phpcs:ignore
112
+ public function collectRates (RateRequest $ rateRequest )
113
+ {
114
+ // TODO: Implement collectRates() method.
115
+ }
88
116
}
0 commit comments