Skip to content

Commit 3708535

Browse files
committed
AC-13258::Migrate DHL integration from outdated DHL Express XML to new MyDHL API RESTful APIs-Integration test added for Shipment API
AC-13258::Migrate DHL integration from outdated DHL Express XML to new MyDHL API RESTful APIs-Integration test added for Tracking API AC-13258::Migrate DHL integration from outdated DHL Express XML to new MyDHL API RESTful APIs-fixes for static failures AC-13258::Migrate DHL integration from outdated DHL Express XML to new MyDHL API RESTful APIs-fixes for static failures
1 parent 5a5773a commit 3708535

15 files changed

+4025
-301
lines changed

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

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2012 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Dhl\Model;
@@ -417,12 +417,11 @@ public function collectRates(RateRequest $request)
417417
$this->setRequest($requestDhl);
418418
//Loading quotes
419419
//Saving $result to use proper result with the callback
420+
$result = null;
420421
if ($this->getConfigData('type') == 'DHL_XML') {
421422
$this->_result = $result = $this->_getQuotes();
422423
} elseif ($this->getConfigData('type') == 'DHL_REST') {
423-
// get the product code as per shipment country
424-
$productCodes = $this->getProductCode();
425-
$this->_result = $result = $this->_getQuotesRest($productCodes);
424+
$this->_result = $result = $this->_getQuotesRest();
426425
}
427426
//After quotes are loaded parsing the response.
428427
return $this->proxyDeferredFactory->create(
@@ -490,34 +489,27 @@ protected function _addParams(DataObject $requestObject)
490489
* @return $this
491490
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
492491
* @SuppressWarnings(PHPMD.NPathComplexity)
492+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
493493
*/
494494
public function setRequest(DataObject $request)
495495
{
496496
$this->_request = $request;
497497
$this->setStore($request->getStoreId());
498498

499499
$requestObject = new DataObject();
500-
501500
$requestObject->setIsGenerateLabelReturn($request->getIsGenerateLabelReturn());
502-
503501
$requestObject->setStoreId($request->getStoreId());
504-
505502
if ($request->getLimitMethod()) {
506503
$requestObject->setService($request->getLimitMethod());
507504
}
508-
509505
$requestObject = $this->_addParams($requestObject);
510506

511507
/** setting destination city name in case of guest customer specific to DHL REST API */
512508
$region = $this->_regionFactory->create()->loadByCode(
513509
$request->getDestRegionCode(),
514510
$request->getDestCountryId()
515511
);
516-
if ($region->getId()) {
517-
$destCityName = $region->getName(); // Returning region name as fallback
518-
} else {
519-
$destCityName = ''; //returning blank in case of XML API
520-
}
512+
$destCityName = $region->getId() ? $region->getName() : '';
521513

522514
if ($request->getDestPostcode()) {
523515
$requestObject->setDestPostal($request->getDestPostcode());
@@ -595,7 +587,6 @@ public function setRequest(DataObject $request)
595587
}
596588

597589
$requestObject->setBaseSubtotalInclTax($request->getBaseSubtotalInclTax());
598-
599590
$this->setRawRequest($requestObject);
600591

601592
return $this;
@@ -1373,10 +1364,11 @@ protected function _addRate(SimpleXMLElement $shipmentDetails)
13731364
}
13741365

13751366
/**
1376-
* Get product codes for DHL REST API
1367+
* Get product codes for DHL REST API for future use
13771368
*
13781369
* @return array
13791370
* @throws Throwable
1371+
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
13801372
*/
13811373
private function getProductCode()
13821374
{
@@ -1426,37 +1418,31 @@ private function getProductCode()
14261418
}
14271419
$this->_debug($debugData);
14281420
// Decode JSON to array
1429-
$data = json_decode($jsonResponse, true);
1430-
$productCodes = array_map(fn($product) => $product['productCode'], $data['products']);
1431-
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+
}
14321428
return $productCodes;
14331429
}
14341430

14351431
/**
14361432
* DHL REST API for Quote Data
14371433
*
1438-
* @param array $productCodes
14391434
* @return Result\ProxyDeferred
14401435
* @throws LocalizedException
1436+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
14411437
*/
1442-
protected function _getQuotesRest(array $productCodes)
1438+
protected function _getQuotesRest()
14431439
{
14441440
$rawRequest = $this->_rawRequest;
14451441
$url = $this->getGatewayURL();
14461442

1447-
/** Product Codes */
1448-
if (!empty($productCodes)) {
1449-
$productCodesParams = array_map(function ($code) {
1450-
return ["productCode" => $code];
1451-
}, $productCodes);
1452-
} else {
1453-
$productCodesParams = [["productCode" => "N"]];
1454-
}
1455-
14561443
/** Dutiable */
14571444
$dutiable = ["isCustomsDeclarable" => false];
14581445
if ($this->isDutiable($rawRequest->getOrigCountryId(), $rawRequest->getDestCountryId())) {
1459-
$isCustomDeclarable = true;
14601446
$declaredValue = (int) $rawRequest->getValue();
14611447
$baseCurrencyCode = $this->_storeManager
14621448
->getWebsite($this->_request->getWebsiteId())
@@ -1492,7 +1478,6 @@ protected function _getQuotesRest(array $productCodes)
14921478
"number" => $this->getConfigData('account')
14931479
]
14941480
],
1495-
"productsAndServices" => $productCodesParams,
14961481
"plannedShippingDateAndTime" => date('Y-m-d\TH:i:s\Z', strtotime($this->_getShipDate())),
14971482
"unitOfMeasurement" => "metric",
14981483
"getAdditionalInformation" => [
@@ -1526,8 +1511,8 @@ protected function _getQuotesRest(array $productCodes)
15261511
$httpResponse = $this->httpClient->request(
15271512
new Request($url . '/rates', Request::METHOD_POST, $headers, $ratePayload)
15281513
);
1529-
15301514
$debugData['request'] = $ratePayload;
1515+
15311516
return $this->proxyDeferredFactory->create(
15321517
[
15331518
'deferred' => new CallbackDeferred(
@@ -1545,7 +1530,6 @@ function () use ($httpResponse, $debugData) {
15451530
}
15461531
$debugData['result'] = $jsonResponse;
15471532
$this->_debug($debugData);
1548-
15491533
return $this->_parseRestResponse($jsonResponse);
15501534
}
15511535
)
@@ -1572,6 +1556,7 @@ private function getDhlAccessToken()
15721556
* @param string $rateResponse
15731557
* @return Result
15741558
* @throws LocalizedException
1559+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
15751560
*/
15761561
protected function _parseRestResponse($rateResponse)
15771562
{
@@ -1623,6 +1608,7 @@ protected function _parseRestResponse($rateResponse)
16231608
* @param array $exchangeRates
16241609
* @return $this
16251610
* @throws LocalizedException
1611+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
16261612
*/
16271613
protected function _addRestRate($product, $exchangeRates)
16281614
{
@@ -2193,13 +2179,17 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
21932179
* @return DataObject
21942180
* @throws LocalizedException
21952181
* @throws Throwable
2182+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2183+
* @SuppressWarnings(PHPMD.NPathComplexity)
2184+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
21962185
*/
21972186
protected function _doShipmentRequestRest()
21982187
{
21992188
$rawRequest = $this->_request;
22002189
$url = $this->getGatewayURL().'/shipments';
22012190

22022191
/** shipper */
2192+
$shipperAddress = $receiverAddress = [];
22032193
$shipAddress = $rawRequest->getShipperAddressStreet1() . ' ' . $rawRequest->getShipperAddressStreet2();
22042194
$shipAddress = $this->string->split($shipAddress, 45, false, true);
22052195
if (is_array($shipAddress)) {
@@ -2270,7 +2260,6 @@ protected function _doShipmentRequestRest()
22702260
$rawRequest->getShipperAddressCountryCode(),
22712261
$rawRequest->getRecipientAddressCountryCode()
22722262
)) {
2273-
$isCustomDeclarable = true;
22742263
$declaredValue = sprintf("%.2F", $rawRequest->getOrderShipment()->getOrder()->getSubtotal());
22752264
$baseCurrencyCode = $this->_storeManager->getWebsite($rawRequest->getWebsiteId())->getBaseCurrencyCode();
22762265
/** Export Declaration details */
@@ -2281,11 +2270,11 @@ protected function _doShipmentRequestRest()
22812270
$nodeExportItem = [];
22822271
$nodeExportItem['number'] = $itemNo;
22832272
$nodeExportItem['description'] = $itemData['name'];
2284-
$nodeExportItem['price'] = $itemData['price'];
2285-
$nodeExportItem['quantity']['value'] = $itemData['qty'];
2273+
$nodeExportItem['price'] = (int)$itemData['price'];
2274+
$nodeExportItem['quantity']['value'] = (int)$itemData['qty'];
22862275
$nodeExportItem['quantity']['unitOfMeasurement'] = 'PCS';
2287-
$nodeExportItem['weight']['netValue'] = $itemData['weight'];
2288-
$nodeExportItem['weight']['grossValue'] = $itemData['weight'];
2276+
$nodeExportItem['weight']['netValue'] = (int)$itemData['weight'];
2277+
$nodeExportItem['weight']['grossValue'] = (int)$itemData['weight'];
22892278
$nodeExportItem['manufacturerCountry'] = $rawRequest->getShipperAddressCountryCode();
22902279
$nodeExportItems[$i] = $nodeExportItem;
22912280
$i++;
@@ -2296,9 +2285,11 @@ protected function _doShipmentRequestRest()
22962285
"declaredValue" => $declaredValue,
22972286
"declaredValueCurrency" => $baseCurrencyCode,
22982287
"exportDeclaration" => [
2299-
"lineItems" => [
2300-
$nodeExportItems
2301-
]
2288+
"lineItems" => $nodeExportItems,
2289+
"invoice" => [
2290+
"number" => $rawRequest->getOrderShipment()->getOrder()->getIncrementId(),
2291+
"date" => date('Y-m-d')
2292+
]
23022293
]
23032294
];
23042295
}
@@ -2310,7 +2301,7 @@ protected function _doShipmentRequestRest()
23102301
"pickup" => [
23112302
"isRequested" => false
23122303
],
2313-
"productCode" => "N",
2304+
"productCode" => $rawRequest->getShippingMethod(),
23142305
"accounts" => [
23152306
[
23162307
"typeCode" => "shipper",
@@ -2365,6 +2356,7 @@ protected function _doShipmentRequestRest()
23652356
];
23662357

23672358
$shippingPayload = json_encode($shippingParams);
2359+
23682360
$debugData = ['request' => $this->filterDebugData($shippingPayload)];
23692361
try {
23702362
$response = $this->httpClient->request(
@@ -2413,7 +2405,7 @@ public function getTracking($trackings)
24132405
}
24142406

24152407
if ($this->getConfigData('type') == 'DHL_REST') {
2416-
$this->_getRestTracking($trackings);
2408+
$this->_getRestTracking(implode(',', $trackings));
24172409
} else {
24182410
$this->_getXMLTracking($trackings);
24192411
}
@@ -2600,7 +2592,7 @@ protected function _getRestTracking($trackings)
26002592
$url = $this->getGatewayURL().'/tracking?';
26012593

26022594
$trackingParams = [
2603-
'shipmentTrackingNumber' => $trackings[0],
2595+
'shipmentTrackingNumber' => $trackings,
26042596
'language' => 'en',
26052597
'limit' => 10
26062598
];
@@ -2639,6 +2631,8 @@ protected function _getRestTracking($trackings)
26392631
* @param string[] $trackings
26402632
* @param string $response
26412633
* @return void
2634+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2635+
* @SuppressWarnings(PHPMD.NPathComplexity)
26422636
*/
26432637
protected function _parseRestTrackingResponse($trackings, $response)
26442638
{
@@ -2648,10 +2642,10 @@ protected function _parseRestTrackingResponse($trackings, $response)
26482642
if (!empty(trim($response))) {
26492643
$trackingData = json_decode($response);
26502644
if (!empty($trackingData)
2651-
&& (isset($trackingData->shipments[0]->status)
2652-
&& $trackingData->shipments[0]->status !== 'Success')
2645+
&& (isset($trackingData->status)
2646+
&& $trackingData->status !== 'Success')
26532647
) {
2654-
$this->_errors['error'] = __('Error %1', $trackingData->shipments[0]->status);
2648+
$this->_errors['error'] = __('Error %1', $trackingData->message);
26552649
} elseif (!empty($trackingData) && $trackingData->shipments[0]->status == 'Success') {
26562650
foreach ($trackingData->shipments as $shipment) {
26572651
$awbinfoData = [];

app/code/Magento/Dhl/Test/Unit/Model/CarrierTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

app/code/Magento/Dhl/etc/adminhtml/system.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2014 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">

app/code/Magento/Dhl/etc/config.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2014 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">

app/code/Magento/Dhl/etc/di.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2014 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

0 commit comments

Comments
 (0)