8
8
9
9
namespace Magento \Ups \Model ;
10
10
11
+ use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Quote \Model \Quote \Address \RateResult \Error ;
12
13
use Magento \Quote \Model \Quote \Address \RateRequest ;
13
14
use Magento \Shipping \Model \Carrier \AbstractCarrierOnline ;
16
17
use Magento \Shipping \Model \Simplexml \Element ;
17
18
use Magento \Ups \Helper \Config ;
18
19
use Magento \Framework \Xml \Security ;
20
+ use Magento \Framework \HTTP \ClientFactory ;
19
21
20
22
/**
21
23
* UPS shipping implementation
@@ -129,6 +131,11 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface
129
131
'UserId ' , 'Password '
130
132
];
131
133
134
+ /**
135
+ * @var ClientFactory
136
+ */
137
+ private $ httpClientFactory ;
138
+
132
139
/**
133
140
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
134
141
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
@@ -148,6 +155,7 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface
148
155
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
149
156
* @param Config $configHelper
150
157
* @param array $data
158
+ * @param ClientFactory $httpClientFactory
151
159
*
152
160
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
153
161
*/
@@ -169,7 +177,8 @@ public function __construct(
169
177
\Magento \CatalogInventory \Api \StockRegistryInterface $ stockRegistry ,
170
178
\Magento \Framework \Locale \FormatInterface $ localeFormat ,
171
179
Config $ configHelper ,
172
- array $ data = []
180
+ array $ data = [],
181
+ ClientFactory $ httpClientFactory = null
173
182
) {
174
183
$ this ->_localeFormat = $ localeFormat ;
175
184
$ this ->configHelper = $ configHelper ;
@@ -191,6 +200,7 @@ public function __construct(
191
200
$ stockRegistry ,
192
201
$ data
193
202
);
203
+ $ this ->httpClientFactory = $ httpClientFactory ?: ObjectManager::getInstance ()->get (ClientFactory::class);
194
204
}
195
205
196
206
/**
@@ -734,22 +744,17 @@ protected function _getXmlQuotes()
734
744
if ($ xmlResponse === null ) {
735
745
$ debugData ['request ' ] = $ xmlParams ;
736
746
try {
737
- $ ch = curl_init ();
738
- curl_setopt ($ ch , CURLOPT_URL , $ url );
739
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
740
- curl_setopt ($ ch , CURLOPT_HEADER , 0 );
741
- curl_setopt ($ ch , CURLOPT_POST , 1 );
742
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ xmlRequest );
743
- curl_setopt ($ ch , CURLOPT_TIMEOUT , 30 );
744
- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , (bool )$ this ->getConfigFlag ('mode_xml ' ));
745
- $ xmlResponse = curl_exec ($ ch );
746
- if ($ xmlResponse !== false ) {
747
- $ debugData ['result ' ] = $ xmlResponse ;
748
- $ this ->_setCachedQuotes ($ xmlRequest , $ xmlResponse );
749
- } else {
750
- $ debugData ['result ' ] = ['error ' => curl_error ($ ch )];
751
- }
752
- curl_close ($ ch );
747
+ $ client = $ this ->httpClientFactory ->create ();
748
+ $ client ->setOptions (
749
+ [
750
+ CURLOPT_SSL_VERIFYPEER => (bool )$ this ->getConfigFlag ('mode_xml ' ),
751
+ CURLOPT_HEADER => 0 ,
752
+ ]
753
+ );
754
+ $ client ->post ($ url , $ xmlRequest );
755
+ $ xmlResponse = $ client ->getBody ();
756
+ $ debugData ['result ' ] = $ xmlResponse ;
757
+ $ this ->_setCachedQuotes ($ xmlRequest , $ xmlResponse );
753
758
} catch (\Exception $ e ) {
754
759
$ debugData ['result ' ] = ['error ' => $ e ->getMessage (), 'code ' => $ e ->getCode ()];
755
760
$ xmlResponse = '' ;
@@ -1005,20 +1010,11 @@ protected function _getXmlTracking($trackings)
1005
1010
$ debugData = ['request ' => $ xmlRequest ];
1006
1011
1007
1012
try {
1008
- $ ch = curl_init ();
1009
- curl_setopt ($ ch , CURLOPT_URL , $ url );
1010
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
1011
- curl_setopt ($ ch , CURLOPT_HEADER , 0 );
1012
- curl_setopt ($ ch , CURLOPT_POST , 1 );
1013
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ xmlRequest );
1014
- curl_setopt ($ ch , CURLOPT_TIMEOUT , 30 );
1015
- $ xmlResponse = curl_exec ($ ch );
1016
- if ($ xmlResponse !== false ) {
1017
- $ debugData ['result ' ] = $ xmlResponse ;
1018
- } else {
1019
- $ debugData ['result ' ] = ['error ' => curl_error ($ ch )];
1020
- }
1021
- curl_close ($ ch );
1013
+ $ client = $ this ->httpClientFactory ->create ();
1014
+ $ client ->setOption (CURLOPT_HEADER , 0 );
1015
+ $ client ->post ($ url , $ xmlRequest );
1016
+ $ xmlResponse = $ client ->getBody ();
1017
+ $ debugData ['result ' ] = $ xmlResponse ;
1022
1018
} catch (\Exception $ e ) {
1023
1019
$ debugData ['result ' ] = ['error ' => $ e ->getMessage (), 'code ' => $ e ->getCode ()];
1024
1020
$ xmlResponse = '' ;
@@ -1441,15 +1437,15 @@ protected function _sendShipmentAcceptRequest(Element $shipmentConfirmResponse)
1441
1437
$ debugData = ['request ' => $ xmlRequest ->asXML ()];
1442
1438
1443
1439
try {
1444
- $ ch = curl_init ( $ this ->getShipAcceptUrl () );
1445
- curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , 1 );
1446
- curl_setopt ( $ ch , CURLOPT_HEADER , 0 );
1447
- curl_setopt ( $ ch , CURLOPT_POST , 1 );
1448
- curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ this -> _xmlAccessRequest . $ xmlRequest -> asXML ());
1449
- curl_setopt ( $ ch , CURLOPT_TIMEOUT , 30 );
1450
- curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , ( bool ) $ this -> getConfigFlag ( ' mode_xml ' ) );
1451
- $ xmlResponse = curl_exec ( $ ch );
1452
-
1440
+ $ client = $ this ->httpClientFactory -> create ( );
1441
+ $ client -> setOptions (
1442
+ [
1443
+ CURLOPT_SSL_VERIFYPEER => ( bool ) $ this -> getConfigFlag ( ' mode_xml ' ),
1444
+ CURLOPT_HEADER => 0 ,
1445
+ ]
1446
+ );
1447
+ $ client -> post ( $ this -> getShipAcceptUrl (), $ this -> _xmlAccessRequest . $ xmlRequest -> asXML () );
1448
+ $ xmlResponse = $ client -> getBody ();
1453
1449
$ debugData ['result ' ] = $ xmlResponse ;
1454
1450
$ this ->_setCachedQuotes ($ xmlRequest , $ xmlResponse );
1455
1451
} catch (\Exception $ e ) {
@@ -1510,23 +1506,22 @@ protected function _doShipmentRequest(\Magento\Framework\DataObject $request)
1510
1506
$ xmlResponse = $ this ->_getCachedQuotes ($ xmlRequest );
1511
1507
1512
1508
if ($ xmlResponse === null ) {
1513
- $ url = $ this ->getShipConfirmUrl ();
1514
-
1515
1509
$ debugData = ['request ' => $ xmlRequest ];
1516
- $ ch = curl_init ();
1517
- curl_setopt ($ ch , CURLOPT_URL , $ url );
1518
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
1519
- curl_setopt ($ ch , CURLOPT_HEADER , 0 );
1520
- curl_setopt ($ ch , CURLOPT_POST , 1 );
1521
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ xmlRequest );
1522
- curl_setopt ($ ch , CURLOPT_TIMEOUT , 30 );
1523
- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , (bool )$ this ->getConfigFlag ('mode_xml ' ));
1524
- $ xmlResponse = curl_exec ($ ch );
1525
- if ($ xmlResponse === false ) {
1526
- throw new \Exception (curl_error ($ ch ));
1527
- } else {
1510
+ $ url = $ this ->getShipConfirmUrl ();
1511
+ try {
1512
+ $ client = $ this ->httpClientFactory ->create ();
1513
+ $ client ->setOptions (
1514
+ [
1515
+ CURLOPT_SSL_VERIFYPEER => (bool )$ this ->getConfigFlag ('mode_xml ' ),
1516
+ CURLOPT_HEADER => 0 ,
1517
+ ]
1518
+ );
1519
+ $ client ->post ($ url , $ xmlRequest );
1520
+ $ xmlResponse = $ client ->getBody ();
1528
1521
$ debugData ['result ' ] = $ xmlResponse ;
1529
1522
$ this ->_setCachedQuotes ($ xmlRequest , $ xmlResponse );
1523
+ } catch (\Exception $ e ) {
1524
+ $ debugData ['result ' ] = ['code ' => $ e ->getCode (), 'error ' => $ e ->getMessage ()];
1530
1525
}
1531
1526
}
1532
1527
0 commit comments