Skip to content

Commit fa054e0

Browse files
committed
AC-13258::Migrate DHL integration from outdated DHL Express XML to new MyDHL API RESTful APIs-code review fixes
1 parent 2b8e2e7 commit fa054e0

File tree

1 file changed

+23
-94
lines changed

1 file changed

+23
-94
lines changed

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

Lines changed: 23 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,79 +1363,14 @@ protected function _addRate(SimpleXMLElement $shipmentDetails)
13631363
return $this;
13641364
}
13651365

1366-
/**
1367-
* Get product codes for DHL REST API for future use
1368-
*
1369-
* @return array
1370-
* @throws Throwable
1371-
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
1372-
*/
1373-
private function getProductCode()
1374-
{
1375-
$rawRequest = $this->_rawRequest;
1376-
$url = $this->getGatewayURL();
1377-
1378-
/** @var HttpResponseDeferredInterface[] $httpResponse */
1379-
$httpResponse = [];
1380-
1381-
$params = [
1382-
'accountNumber' => $this->getConfigData('account'),
1383-
'originCountryCode' => $rawRequest->getOrigCountryId(),
1384-
'originCityName' => $rawRequest->getOrigCity(),
1385-
'destinationCountryCode' => $rawRequest->getDestCountryId(),
1386-
'destinationCityName' => $rawRequest->getDestCity(),
1387-
'weight' => (int) $this->_getWeight($rawRequest->getWeight()),
1388-
'length' => $this->_getDimension($this->getConfigData('depth')),
1389-
'width' => $this->_getDimension($this->getConfigData('width')),
1390-
'height' => $this->_getDimension($this->getConfigData('height')),
1391-
'plannedShippingDate' => date('Y-m-d', strtotime($this->_getShipDate())),
1392-
'isCustomsDeclarable' => 'false',
1393-
'unitOfMeasurement' => 'metric'
1394-
];
1395-
1396-
$queryString = http_build_query($params);
1397-
$productParams = (object)[];
1398-
$productPayload = json_encode($productParams);
1399-
1400-
$headers = [
1401-
"Authorization" => "Basic " . $this->getDhlAccessToken(),
1402-
"Content-Type" => "application/json",
1403-
"x-version" => "2.12.0"
1404-
];
1405-
1406-
$httpResponse = $this->httpClient->request(
1407-
new Request($url . '/products?' . $queryString, Request::METHOD_GET, $headers, $productPayload)
1408-
);
1409-
$debugData['request'] = $queryString;
1410-
1411-
try {
1412-
$responseResult = $httpResponse->get();
1413-
$jsonResponse = $responseResult->getStatusCode() >= 400 ? '' : $responseResult->getBody();
1414-
$debugData['result'] = $jsonResponse;
1415-
} catch (HttpException $e) {
1416-
$debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()];
1417-
$this->_logger->critical($e);
1418-
}
1419-
$this->_debug($debugData);
1420-
// Decode JSON to array
1421-
$productCodes = [];
1422-
if (!empty($jsonResponse)) {
1423-
$data = json_decode($jsonResponse, true);
1424-
if (json_last_error() === JSON_ERROR_NONE && isset($data['products'])) {
1425-
$productCodes = array_map(fn($product) => $product['productCode'], $data['products']);
1426-
}
1427-
}
1428-
return $productCodes;
1429-
}
1430-
14311366
/**
14321367
* DHL REST API for Quote Data
14331368
*
14341369
* @return Result\ProxyDeferred
14351370
* @throws LocalizedException
14361371
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
14371372
*/
1438-
protected function _getQuotesRest()
1373+
protected function _getQuotesRest() : Result\ProxyDeferred
14391374
{
14401375
$rawRequest = $this->_rawRequest;
14411376
$url = $this->getGatewayURL();
@@ -1501,15 +1436,8 @@ protected function _getQuotesRest()
15011436

15021437
$ratePayload = json_encode($rateParams, JSON_PRETTY_PRINT);
15031438

1504-
/** Rest API Payload */
1505-
$headers = [
1506-
"Authorization" => "Basic " . $this->getDhlAccessToken(),
1507-
"Content-Type" => "application/json",
1508-
"x-version" => "2.12.0"
1509-
];
1510-
15111439
$httpResponse = $this->httpClient->request(
1512-
new Request($url . '/rates', Request::METHOD_POST, $headers, $ratePayload)
1440+
new Request($url . '/rates', Request::METHOD_POST, $this->getRestHeaders(), $ratePayload)
15131441
);
15141442
$debugData['request'] = $ratePayload;
15151443

@@ -1542,14 +1470,28 @@ function () use ($httpResponse, $debugData) {
15421470
*
15431471
* @return string
15441472
*/
1545-
private function getDhlAccessToken()
1473+
private function getDhlAccessToken() : string
15461474
{
15471475
$username = (string) $this->getConfigData('api_key');
15481476
$password = (string) $this->getConfigData('api_secret');
15491477
$access_token = base64_encode($username . ":" . $password);
15501478
return $access_token;
15511479
}
15521480

1481+
/**
1482+
* Rest API Headers
1483+
*
1484+
* @return string[]
1485+
*/
1486+
private function getRestHeaders(): array
1487+
{
1488+
return $headers = [
1489+
"Authorization" => "Basic " . $this->getDhlAccessToken(),
1490+
"Content-Type" => "application/json",
1491+
"x-version" => "2.12.0"
1492+
];
1493+
}
1494+
15531495
/**
15541496
* Parse response from DHL REST API
15551497
*
@@ -1558,7 +1500,7 @@ private function getDhlAccessToken()
15581500
* @throws LocalizedException
15591501
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
15601502
*/
1561-
protected function _parseRestResponse($rateResponse)
1503+
protected function _parseRestResponse($rateResponse): Result
15621504
{
15631505
$responseError = __('The response is in wrong format.');
15641506
if ($rateResponse !== null && strlen($rateResponse) > 0) {
@@ -1610,7 +1552,7 @@ protected function _parseRestResponse($rateResponse)
16101552
* @throws LocalizedException
16111553
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
16121554
*/
1613-
protected function _addRestRate($product, $exchangeRates)
1555+
protected function _addRestRate($product, $exchangeRates): self
16141556
{
16151557
if (isset($product['productName'])
16161558
&& isset($product['productCode'])
@@ -2183,7 +2125,7 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
21832125
* @SuppressWarnings(PHPMD.NPathComplexity)
21842126
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
21852127
*/
2186-
protected function _doShipmentRequestRest()
2128+
protected function _doShipmentRequestRest(): DataObject
21872129
{
21882130
$rawRequest = $this->_request;
21892131
$url = $this->getGatewayURL().'/shipments';
@@ -2348,13 +2290,6 @@ protected function _doShipmentRequestRest()
23482290
], $dutiable)
23492291
];
23502292

2351-
/** Rest API Payload */
2352-
$headers = [
2353-
"Authorization" => "Basic " . $this->getDhlAccessToken(),
2354-
"Content-Type" => "application/json",
2355-
"x-version" => "2.12.0"
2356-
];
2357-
23582293
$shippingPayload = json_encode($shippingParams);
23592294

23602295
$debugData = ['request' => $this->filterDebugData($shippingPayload)];
@@ -2363,7 +2298,7 @@ protected function _doShipmentRequestRest()
23632298
new Request(
23642299
$url,
23652300
Request::METHOD_POST,
2366-
$headers,
2301+
$this->getRestHeaders(),
23672302
$shippingPayload
23682303
)
23692304
);
@@ -2398,7 +2333,7 @@ protected function _doShipmentRequestRest()
23982333
* @param string|string[] $trackings
23992334
* @return \Magento\Shipping\Model\Tracking\Result|null
24002335
*/
2401-
public function getTracking($trackings)
2336+
public function getTracking($trackings): ?\Magento\Shipping\Model\Tracking\Result
24022337
{
24032338
if (!is_array($trackings)) {
24042339
$trackings = [$trackings];
@@ -2601,19 +2536,13 @@ protected function _getRestTracking($trackings)
26012536
$trackingPayload = (object)[];
26022537
$trackingPayload = json_encode($trackingPayload);
26032538

2604-
$headers = [
2605-
"Authorization" => "Basic " . $this->getDhlAccessToken(),
2606-
"Content-Type" => "application/json",
2607-
"x-version" => "2.12.0"
2608-
];
2609-
26102539
$debugData = ['request' => $this->filterDebugData($trackingPayload)];
26112540
try {
26122541
$response = $this->httpClient->request(
26132542
new Request(
26142543
$url.$queryString,
26152544
Request::METHOD_GET,
2616-
$headers,
2545+
$this->getRestHeaders(),
26172546
$trackingPayload
26182547
)
26192548
);

0 commit comments

Comments
 (0)