@@ -108,6 +108,17 @@ class Carrier extends AbstractDhl implements CarrierInterface
108
108
private const SERVICE_PREFIX_SHIPVAL = 'SHIP ' ;
109
109
private const SERVICE_PREFIX_TRACKING = 'TRCK ' ;
110
110
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
+
111
122
/**
112
123
* Rate request data
113
124
*
@@ -418,9 +429,9 @@ public function collectRates(RateRequest $request)
418
429
//Loading quotes
419
430
//Saving $result to use proper result with the callback
420
431
$ result = null ;
421
- if ($ this ->getConfigData ('type ' ) == ' DHL_XML ' ) {
432
+ if ($ this ->getConfigData ('type ' ) == self :: DHL_TYPE_XML ) {
422
433
$ this ->_result = $ result = $ this ->_getQuotes ();
423
- } elseif ($ this ->getConfigData ('type ' ) == ' DHL_REST ' ) {
434
+ } elseif ($ this ->getConfigData ('type ' ) == self :: DHL_TYPE_REST ) {
424
435
$ this ->_result = $ result = $ this ->_getQuotesRest ();
425
436
}
426
437
//After quotes are loaded parsing the response.
@@ -1374,6 +1385,7 @@ protected function _getQuotesRest()
1374
1385
{
1375
1386
$ rawRequest = $ this ->_rawRequest ;
1376
1387
$ url = $ this ->getGatewayURL ();
1388
+ $ packageWeightUnit = $ this ->_getRestPackageWeightUnit ();
1377
1389
1378
1390
/** Dutiable */
1379
1391
$ dutiable = ["isCustomsDeclarable " => false ];
@@ -1414,7 +1426,7 @@ protected function _getQuotesRest()
1414
1426
]
1415
1427
],
1416
1428
"plannedShippingDateAndTime " => date ('Y-m-d\TH:i:s\Z ' , strtotime ($ this ->_getShipDate ())),
1417
- "unitOfMeasurement " => " metric " ,
1429
+ "unitOfMeasurement " => $ packageWeightUnit ,
1418
1430
"getAdditionalInformation " => [
1419
1431
[
1420
1432
"typeCode " => "allValueAddedServices " ,
@@ -1488,7 +1500,7 @@ private function getRestHeaders(): array
1488
1500
return [
1489
1501
"Authorization " => "Basic " . $ this ->getDhlAccessToken (),
1490
1502
"Content-Type " => "application/json " ,
1491
- "x-version " => " 2.12.0 "
1503
+ "x-version " => self :: DHL_REST_API_VERSION
1492
1504
];
1493
1505
}
1494
1506
@@ -1696,7 +1708,7 @@ protected function _doShipmentRequest(DataObject $request)
1696
1708
$ this ->_prepareShipmentRequest ($ request );
1697
1709
$ this ->_mapRequestToShipment ($ request );
1698
1710
$ this ->setRequest ($ request );
1699
- if ($ this ->getConfigData ('type ' ) == ' DHL_REST ' ) {
1711
+ if ($ this ->getConfigData ('type ' ) == self :: DHL_TYPE_REST ) {
1700
1712
return $ this ->_doShipmentRequestRest ();
1701
1713
}
1702
1714
return $ this ->_doRequest ();
@@ -2196,6 +2208,8 @@ protected function _doShipmentRequestRest(): DataObject
2196
2208
$ i ++;
2197
2209
}
2198
2210
2211
+ $ packageWeightUnit = $ this ->_getRestPackageWeightUnit ();
2212
+
2199
2213
/** Dutiable */
2200
2214
$ dutiable = ["isCustomsDeclarable " => false ];
2201
2215
if ($ this ->isDutiable (
@@ -2286,7 +2300,7 @@ protected function _doShipmentRequestRest(): DataObject
2286
2300
"packages " => $ packages ,
2287
2301
"description " => "Shipment " ,
2288
2302
"incoterm " => "DAP " ,
2289
- "unitOfMeasurement " => " metric "
2303
+ "unitOfMeasurement " => $ packageWeightUnit
2290
2304
], $ dutiable )
2291
2305
];
2292
2306
@@ -2339,8 +2353,8 @@ public function getTracking(string|array $trackings): ?\Magento\Shipping\Model\T
2339
2353
$ trackings = [$ trackings ];
2340
2354
}
2341
2355
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 );
2344
2358
} else {
2345
2359
$ this ->_getXMLTracking ($ trackings );
2346
2360
}
@@ -2518,16 +2532,16 @@ protected function _parseXmlTrackingResponse($trackings, $response)
2518
2532
}
2519
2533
2520
2534
/**
2521
- * @param string $trackings
2535
+ * @param string[] $trackings
2522
2536
* @return void
2523
2537
* @throws Throwable
2524
2538
*/
2525
- protected function _getRestTracking (string $ trackings ): void
2539
+ protected function _getRestTracking (array $ trackings ): void
2526
2540
{
2527
2541
$ url = $ this ->getGatewayURL ().'/tracking? ' ;
2528
2542
2529
2543
$ trackingParams = [
2530
- 'shipmentTrackingNumber ' => $ trackings ,
2544
+ 'shipmentTrackingNumber ' => implode ( ' , ' , $ trackings) ,
2531
2545
'language ' => 'en ' ,
2532
2546
'limit ' => 10
2533
2547
];
@@ -2548,6 +2562,21 @@ protected function _getRestTracking(string $trackings): void
2548
2562
);
2549
2563
$ responseBody = $ response ->get ()->getBody ();
2550
2564
$ 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
+ }
2551
2580
} catch (Exception $ e ) {
2552
2581
$ this ->_errors [$ e ->getCode ()] = $ e ->getMessage ();
2553
2582
$ responseBody = '' ;
@@ -2557,13 +2586,13 @@ protected function _getRestTracking(string $trackings): void
2557
2586
}
2558
2587
2559
2588
/**
2560
- * @param string $trackings
2589
+ * @param string[] $trackings
2561
2590
* @param string $response
2562
2591
* @return void
2563
2592
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2564
2593
* @SuppressWarnings(PHPMD.NPathComplexity)
2565
2594
*/
2566
- protected function _parseRestTrackingResponse (string $ trackings , string $ response ): void
2595
+ protected function _parseRestTrackingResponse (array $ trackings , string $ response ): void
2567
2596
{
2568
2597
$ errorTitle = __ ('Unable to retrieve tracking ' );
2569
2598
$ resultArr = [];
@@ -2572,20 +2601,28 @@ protected function _parseRestTrackingResponse(string $trackings, string $respons
2572
2601
$ trackingData = json_decode ($ response );
2573
2602
if (!empty ($ trackingData )
2574
2603
&& (isset ($ trackingData ->status )
2575
- && $ trackingData ->status !== ' Success ' )
2604
+ && $ trackingData ->status !== self :: API_RESPONSE_STATUS_SUCCESS )
2576
2605
) {
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 ) {
2579
2611
foreach ($ trackingData ->shipments as $ shipment ) {
2580
2612
$ awbinfoData = [];
2581
2613
$ trackNum = isset ($ shipment ->shipmentTrackingNumber )
2582
2614
? (string )$ shipment ->shipmentTrackingNumber : '' ;
2583
2615
if ($ shipment ->description ) {
2584
2616
$ awbinfoData ['service ' ] = (string )$ shipment ->description ;
2585
2617
}
2618
+ if ($ shipment ->unitOfMeasurements == self ::WEIGHT_UNIT_IMPERIAL ) {
2619
+ $ shipmentMeasurements = self ::WEIGHT_UNIT_LB ;
2620
+ } else {
2621
+ $ shipmentMeasurements = self ::WEIGHT_UNIT_KG ;
2622
+ }
2586
2623
2587
2624
$ awbinfoData ['weight ' ] = (string )$ shipment ->totalWeight . ' ' .
2588
- (string )$ shipment -> unitOfMeasurements ;
2625
+ (string )$ shipmentMeasurements ;
2589
2626
$ packageProgress = [];
2590
2627
if (isset ($ shipment ->events )) {
2591
2628
foreach ($ shipment ->events as $ shipmentEvent ) {
@@ -2811,20 +2848,32 @@ private function buildSoftwareVersion(): string
2811
2848
private function getGatewayURL (): string
2812
2849
{
2813
2850
if ($ this ->getConfigData ('sandbox_mode ' )) {
2814
- if ($ this ->getConfigData ('type ' ) == ' DHL_XML ' ) {
2851
+ if ($ this ->getConfigData ('type ' ) == self :: DHL_TYPE_XML ) {
2815
2852
return (string )$ this ->getConfigData ('sandbox_xml_url ' );
2816
2853
} else {
2817
2854
return (string )$ this ->getConfigData ('sandbox_rest_url ' );
2818
2855
}
2819
2856
} else {
2820
- if ($ this ->getConfigData ('type ' ) == ' DHL_XML ' ) {
2857
+ if ($ this ->getConfigData ('type ' ) == self :: DHL_TYPE_XML ) {
2821
2858
return (string )$ this ->getConfigData ('gateway_xml_url ' );
2822
2859
} else {
2823
2860
return (string )$ this ->getConfigData ('gateway_rest_url ' );
2824
2861
}
2825
2862
}
2826
2863
}
2827
2864
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
+
2828
2877
/**
2829
2878
* Generating Export Declaration Details
2830
2879
*
0 commit comments