Skip to content

Commit d1233a7

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-98030' into 2.1.18-develop-pr67
2 parents c652d7d + 1a380ea commit d1233a7

File tree

4 files changed

+159
-81
lines changed

4 files changed

+159
-81
lines changed

app/code/Magento/Ups/Model/Carrier.php

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\Ups\Model;
1010

11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Quote\Model\Quote\Address\RateResult\Error;
1213
use Magento\Quote\Model\Quote\Address\RateRequest;
1314
use Magento\Shipping\Model\Carrier\AbstractCarrierOnline;
@@ -16,6 +17,7 @@
1617
use Magento\Shipping\Model\Simplexml\Element;
1718
use Magento\Ups\Helper\Config;
1819
use Magento\Framework\Xml\Security;
20+
use Magento\Framework\HTTP\ClientFactory;
1921

2022
/**
2123
* UPS shipping implementation
@@ -129,6 +131,11 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface
129131
'UserId', 'Password'
130132
];
131133

134+
/**
135+
* @var ClientFactory
136+
*/
137+
private $httpClientFactory;
138+
132139
/**
133140
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
134141
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
@@ -148,6 +155,7 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface
148155
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
149156
* @param Config $configHelper
150157
* @param array $data
158+
* @param ClientFactory $httpClientFactory
151159
*
152160
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
153161
*/
@@ -169,7 +177,8 @@ public function __construct(
169177
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
170178
\Magento\Framework\Locale\FormatInterface $localeFormat,
171179
Config $configHelper,
172-
array $data = []
180+
array $data = [],
181+
ClientFactory $httpClientFactory = null
173182
) {
174183
$this->_localeFormat = $localeFormat;
175184
$this->configHelper = $configHelper;
@@ -191,6 +200,7 @@ public function __construct(
191200
$stockRegistry,
192201
$data
193202
);
203+
$this->httpClientFactory = $httpClientFactory ?: ObjectManager::getInstance()->get(ClientFactory::class);
194204
}
195205

196206
/**
@@ -734,22 +744,17 @@ protected function _getXmlQuotes()
734744
if ($xmlResponse === null) {
735745
$debugData['request'] = $xmlParams;
736746
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);
753758
} catch (\Exception $e) {
754759
$debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()];
755760
$xmlResponse = '';
@@ -1005,20 +1010,11 @@ protected function _getXmlTracking($trackings)
10051010
$debugData = ['request' => $xmlRequest];
10061011

10071012
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;
10221018
} catch (\Exception $e) {
10231019
$debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()];
10241020
$xmlResponse = '';
@@ -1441,15 +1437,15 @@ protected function _sendShipmentAcceptRequest(Element $shipmentConfirmResponse)
14411437
$debugData = ['request' => $xmlRequest->asXML()];
14421438

14431439
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();
14531449
$debugData['result'] = $xmlResponse;
14541450
$this->_setCachedQuotes($xmlRequest, $xmlResponse);
14551451
} catch (\Exception $e) {
@@ -1510,23 +1506,22 @@ protected function _doShipmentRequest(\Magento\Framework\DataObject $request)
15101506
$xmlResponse = $this->_getCachedQuotes($xmlRequest);
15111507

15121508
if ($xmlResponse === null) {
1513-
$url = $this->getShipConfirmUrl();
1514-
15151509
$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();
15281521
$debugData['result'] = $xmlResponse;
15291522
$this->_setCachedQuotes($xmlRequest, $xmlResponse);
1523+
} catch (\Exception $e) {
1524+
$debugData['result'] = ['code' => $e->getCode(), 'error' => $e->getMessage()];
15301525
}
15311526
}
15321527

0 commit comments

Comments
 (0)