Skip to content

Commit aa22959

Browse files
committed
AC-13258::Migrate DHL integration from outdated DHL Express XML to new MyDHL API RESTful APIs-QA fixes
1 parent 34be096 commit aa22959

File tree

4 files changed

+80
-31
lines changed

4 files changed

+80
-31
lines changed

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

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ class Carrier extends AbstractDhl implements CarrierInterface
108108
private const SERVICE_PREFIX_SHIPVAL = 'SHIP';
109109
private const SERVICE_PREFIX_TRACKING = 'TRCK';
110110

111+
/** DHL REST API */
112+
113+
public const WEIGHT_UNIT_LB = 'LB';
114+
public const WEIGHT_UNIT_KG = 'KG';
115+
public const WEIGHT_UNIT_IMPERIAL = 'imperial';
116+
public const WEIGHT_UNIT_METRIC = 'metric';
117+
public const API_RESPONSE_STATUS_SUCCESS = 'Success';
118+
public const DHL_TYPE_XML = 'DHL_XML';
119+
public const DHL_TYPE_REST = 'DHL_REST';
120+
public const DHL_REST_API_VERSION = '2.12.0';
121+
111122
/**
112123
* Rate request data
113124
*
@@ -418,9 +429,9 @@ public function collectRates(RateRequest $request)
418429
//Loading quotes
419430
//Saving $result to use proper result with the callback
420431
$result = null;
421-
if ($this->getConfigData('type') == 'DHL_XML') {
432+
if ($this->getConfigData('type') == self::DHL_TYPE_XML) {
422433
$this->_result = $result = $this->_getQuotes();
423-
} elseif ($this->getConfigData('type') == 'DHL_REST') {
434+
} elseif ($this->getConfigData('type') == self::DHL_TYPE_REST) {
424435
$this->_result = $result = $this->_getQuotesRest();
425436
}
426437
//After quotes are loaded parsing the response.
@@ -1374,6 +1385,7 @@ protected function _getQuotesRest()
13741385
{
13751386
$rawRequest = $this->_rawRequest;
13761387
$url = $this->getGatewayURL();
1388+
$packageWeightUnit = $this->_getRestPackageWeightUnit();
13771389

13781390
/** Dutiable */
13791391
$dutiable = ["isCustomsDeclarable" => false];
@@ -1414,7 +1426,7 @@ protected function _getQuotesRest()
14141426
]
14151427
],
14161428
"plannedShippingDateAndTime" => date('Y-m-d\TH:i:s\Z', strtotime($this->_getShipDate())),
1417-
"unitOfMeasurement" => "metric",
1429+
"unitOfMeasurement" => $packageWeightUnit,
14181430
"getAdditionalInformation" => [
14191431
[
14201432
"typeCode" => "allValueAddedServices",
@@ -1488,7 +1500,7 @@ private function getRestHeaders(): array
14881500
return [
14891501
"Authorization" => "Basic " . $this->getDhlAccessToken(),
14901502
"Content-Type" => "application/json",
1491-
"x-version" => "2.12.0"
1503+
"x-version" => self::DHL_REST_API_VERSION
14921504
];
14931505
}
14941506

@@ -1696,7 +1708,7 @@ protected function _doShipmentRequest(DataObject $request)
16961708
$this->_prepareShipmentRequest($request);
16971709
$this->_mapRequestToShipment($request);
16981710
$this->setRequest($request);
1699-
if ($this->getConfigData('type') == 'DHL_REST') {
1711+
if ($this->getConfigData('type') == self::DHL_TYPE_REST) {
17001712
return $this->_doShipmentRequestRest();
17011713
}
17021714
return $this->_doRequest();
@@ -2196,6 +2208,8 @@ protected function _doShipmentRequestRest(): DataObject
21962208
$i++;
21972209
}
21982210

2211+
$packageWeightUnit = $this->_getRestPackageWeightUnit();
2212+
21992213
/** Dutiable */
22002214
$dutiable = ["isCustomsDeclarable" => false];
22012215
if ($this->isDutiable(
@@ -2286,7 +2300,7 @@ protected function _doShipmentRequestRest(): DataObject
22862300
"packages" => $packages,
22872301
"description" => "Shipment",
22882302
"incoterm" => "DAP",
2289-
"unitOfMeasurement" => "metric"
2303+
"unitOfMeasurement" => $packageWeightUnit
22902304
], $dutiable)
22912305
];
22922306

@@ -2339,8 +2353,8 @@ public function getTracking(string|array $trackings): ?\Magento\Shipping\Model\T
23392353
$trackings = [$trackings];
23402354
}
23412355

2342-
if ($this->getConfigData('type') == 'DHL_REST') {
2343-
$this->_getRestTracking(implode(',', $trackings));
2356+
if ($this->getConfigData('type') == self::DHL_TYPE_REST) {
2357+
$this->_getRestTracking($trackings);
23442358
} else {
23452359
$this->_getXMLTracking($trackings);
23462360
}
@@ -2518,16 +2532,16 @@ protected function _parseXmlTrackingResponse($trackings, $response)
25182532
}
25192533

25202534
/**
2521-
* @param string $trackings
2535+
* @param string[] $trackings
25222536
* @return void
25232537
* @throws Throwable
25242538
*/
2525-
protected function _getRestTracking(string $trackings): void
2539+
protected function _getRestTracking(array $trackings): void
25262540
{
25272541
$url = $this->getGatewayURL().'/tracking?';
25282542

25292543
$trackingParams = [
2530-
'shipmentTrackingNumber' => $trackings,
2544+
'shipmentTrackingNumber' => implode(',', $trackings),
25312545
'language' => 'en',
25322546
'limit' => 10
25332547
];
@@ -2548,6 +2562,21 @@ protected function _getRestTracking(string $trackings): void
25482562
);
25492563
$responseBody = $response->get()->getBody();
25502564
$debugData['result'] = $this->filterDebugData($responseBody);
2565+
2566+
// Check if the response contains valid JSON data
2567+
$decoded = json_decode($responseBody, true);
2568+
if (isset($decoded['additionalDetails']) && is_array($decoded['additionalDetails'])) {
2569+
$validTrackings = [];
2570+
foreach ($decoded['additionalDetails'] as $detail) {
2571+
if (preg_match('/Shipments Found for shipmentTrackingNumber (\d+)/', $detail, $matches)) {
2572+
$validTrackings[] = $matches[1];
2573+
}
2574+
}
2575+
if (!empty($validTrackings)) {
2576+
$this->_getRestTracking($validTrackings);
2577+
return;
2578+
}
2579+
}
25512580
} catch (Exception $e) {
25522581
$this->_errors[$e->getCode()] = $e->getMessage();
25532582
$responseBody = '';
@@ -2557,13 +2586,13 @@ protected function _getRestTracking(string $trackings): void
25572586
}
25582587

25592588
/**
2560-
* @param string $trackings
2589+
* @param string[] $trackings
25612590
* @param string $response
25622591
* @return void
25632592
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
25642593
* @SuppressWarnings(PHPMD.NPathComplexity)
25652594
*/
2566-
protected function _parseRestTrackingResponse(string $trackings, string $response): void
2595+
protected function _parseRestTrackingResponse(array $trackings, string $response): void
25672596
{
25682597
$errorTitle = __('Unable to retrieve tracking');
25692598
$resultArr = [];
@@ -2572,20 +2601,28 @@ protected function _parseRestTrackingResponse(string $trackings, string $respons
25722601
$trackingData = json_decode($response);
25732602
if (!empty($trackingData)
25742603
&& (isset($trackingData->status)
2575-
&& $trackingData->status !== 'Success')
2604+
&& $trackingData->status !== self::API_RESPONSE_STATUS_SUCCESS)
25762605
) {
2577-
$this->_errors['error'] = __('Error %1', $trackingData->message);
2578-
} elseif (!empty($trackingData) && $trackingData->shipments[0]->status == 'Success') {
2606+
foreach ($trackings as $tracking) {
2607+
$this->_errors[$tracking] = __('Unable to retrieve tracking');
2608+
}
2609+
} elseif (!empty($trackingData)
2610+
&& $trackingData->shipments[0]->status == self::API_RESPONSE_STATUS_SUCCESS) {
25792611
foreach ($trackingData->shipments as $shipment) {
25802612
$awbinfoData = [];
25812613
$trackNum = isset($shipment->shipmentTrackingNumber)
25822614
? (string)$shipment->shipmentTrackingNumber : '';
25832615
if ($shipment->description) {
25842616
$awbinfoData['service'] = (string)$shipment->description;
25852617
}
2618+
if ($shipment->unitOfMeasurements == self::WEIGHT_UNIT_IMPERIAL) {
2619+
$shipmentMeasurements = self::WEIGHT_UNIT_LB;
2620+
} else {
2621+
$shipmentMeasurements = self::WEIGHT_UNIT_KG;
2622+
}
25862623

25872624
$awbinfoData['weight'] = (string)$shipment->totalWeight . ' ' .
2588-
(string)$shipment->unitOfMeasurements;
2625+
(string)$shipmentMeasurements;
25892626
$packageProgress = [];
25902627
if (isset($shipment->events)) {
25912628
foreach ($shipment->events as $shipmentEvent) {
@@ -2811,20 +2848,32 @@ private function buildSoftwareVersion(): string
28112848
private function getGatewayURL(): string
28122849
{
28132850
if ($this->getConfigData('sandbox_mode')) {
2814-
if ($this->getConfigData('type') == 'DHL_XML') {
2851+
if ($this->getConfigData('type') == self::DHL_TYPE_XML) {
28152852
return (string)$this->getConfigData('sandbox_xml_url');
28162853
} else {
28172854
return (string)$this->getConfigData('sandbox_rest_url');
28182855
}
28192856
} else {
2820-
if ($this->getConfigData('type') == 'DHL_XML') {
2857+
if ($this->getConfigData('type') == self::DHL_TYPE_XML) {
28212858
return (string)$this->getConfigData('gateway_xml_url');
28222859
} else {
28232860
return (string)$this->getConfigData('gateway_rest_url');
28242861
}
28252862
}
28262863
}
28272864

2865+
/**
2866+
* Get the weight Unit for Rest API
2867+
*
2868+
* @return string
2869+
* @throws LocalizedException
2870+
*/
2871+
private function _getRestPackageWeightUnit(): string
2872+
{
2873+
$packageWeightUnit = substr($this->_getWeightUnit(), 0, 1);
2874+
return $packageWeightUnit === 'L' ? self::WEIGHT_UNIT_IMPERIAL : self::WEIGHT_UNIT_METRIC;
2875+
}
2876+
28282877
/**
28292878
* Generating Export Declaration Details
28302879
*

dev/tests/integration/testsuite/Magento/Dhl/Model/CarrierTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -862,12 +862,12 @@ public function testRequestRestToShip(
862862
'packages' => [
863863
'package' => [
864864
'params' => [
865-
'width' => '5',
866-
'length' => '5',
867-
'height' => '5',
868-
'dimension_units' => 'CENTIMETER',
869-
'weight_units' => 'KILOGRAM',
870-
'weight' => '100',
865+
'width' => '2',
866+
'length' => '2',
867+
'height' => '2',
868+
'dimension_units' => 'IN',
869+
'weight_units' => 'L',
870+
'weight' => '220',
871871
'customs_value' => '100.00',
872872
'container' => Carrier::DHL_CONTENT_TYPE_NON_DOC,
873873
],
@@ -1029,21 +1029,21 @@ public static function trackingRestDataProvider() : array
10291029
'tracking' => 2725476530,
10301030
'service' => 'Shipment',
10311031
'progressdetail' => [],
1032-
'weight' => '222 imperial',
1032+
'weight' => '222 LB',
10331033
];
10341034
$expectedTrackingDataB = [
10351035
'carrier' => 'dhl',
10361036
'carrier_title' => 'DHL',
10371037
'tracking' => 5539315121,
10381038
'service' => 'Shipment',
10391039
'progressdetail' => [],
1040-
'weight' => '222 imperial',
1040+
'weight' => '222 LB',
10411041
];
10421042
$expectedTrackingDataC = [
10431043
'carrier' => 'dhl',
10441044
'carrier_title' => 'DHL',
1045-
'tracking' => 'error',
1046-
'error_message' => __('Error %1', 'No data found'),
1045+
'tracking' => '1234567892',
1046+
'error_message' => __('Unable to retrieve tracking'),
10471047
];
10481048

10491049
return [

dev/tests/integration/testsuite/Magento/Dhl/_files/domestic_shipment_request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
],
5959
"description": "Shipment",
6060
"incoterm": "DAP",
61-
"unitOfMeasurement": "metric",
61+
"unitOfMeasurement": "imperial",
6262
"isCustomsDeclarable": false
6363
}
6464
}

dev/tests/integration/testsuite/Magento/Dhl/_files/shipment_request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
],
5959
"description": "Shipment",
6060
"incoterm": "DAP",
61-
"unitOfMeasurement": "metric",
61+
"unitOfMeasurement": "imperial",
6262
"isCustomsDeclarable": true,
6363
"declaredValue": "10.00",
6464
"declaredValueCurrency": "USD",

0 commit comments

Comments
 (0)