1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2012 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
7
7
namespace Magento \Dhl \Model ;
@@ -417,12 +417,11 @@ public function collectRates(RateRequest $request)
417
417
$ this ->setRequest ($ requestDhl );
418
418
//Loading quotes
419
419
//Saving $result to use proper result with the callback
420
+ $ result = null ;
420
421
if ($ this ->getConfigData ('type ' ) == 'DHL_XML ' ) {
421
422
$ this ->_result = $ result = $ this ->_getQuotes ();
422
423
} 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 ();
426
425
}
427
426
//After quotes are loaded parsing the response.
428
427
return $ this ->proxyDeferredFactory ->create (
@@ -490,34 +489,27 @@ protected function _addParams(DataObject $requestObject)
490
489
* @return $this
491
490
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
492
491
* @SuppressWarnings(PHPMD.NPathComplexity)
492
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
493
493
*/
494
494
public function setRequest (DataObject $ request )
495
495
{
496
496
$ this ->_request = $ request ;
497
497
$ this ->setStore ($ request ->getStoreId ());
498
498
499
499
$ requestObject = new DataObject ();
500
-
501
500
$ requestObject ->setIsGenerateLabelReturn ($ request ->getIsGenerateLabelReturn ());
502
-
503
501
$ requestObject ->setStoreId ($ request ->getStoreId ());
504
-
505
502
if ($ request ->getLimitMethod ()) {
506
503
$ requestObject ->setService ($ request ->getLimitMethod ());
507
504
}
508
-
509
505
$ requestObject = $ this ->_addParams ($ requestObject );
510
506
511
507
/** setting destination city name in case of guest customer specific to DHL REST API */
512
508
$ region = $ this ->_regionFactory ->create ()->loadByCode (
513
509
$ request ->getDestRegionCode (),
514
510
$ request ->getDestCountryId ()
515
511
);
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 () : '' ;
521
513
522
514
if ($ request ->getDestPostcode ()) {
523
515
$ requestObject ->setDestPostal ($ request ->getDestPostcode ());
@@ -595,7 +587,6 @@ public function setRequest(DataObject $request)
595
587
}
596
588
597
589
$ requestObject ->setBaseSubtotalInclTax ($ request ->getBaseSubtotalInclTax ());
598
-
599
590
$ this ->setRawRequest ($ requestObject );
600
591
601
592
return $ this ;
@@ -1373,10 +1364,11 @@ protected function _addRate(SimpleXMLElement $shipmentDetails)
1373
1364
}
1374
1365
1375
1366
/**
1376
- * Get product codes for DHL REST API
1367
+ * Get product codes for DHL REST API for future use
1377
1368
*
1378
1369
* @return array
1379
1370
* @throws Throwable
1371
+ * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
1380
1372
*/
1381
1373
private function getProductCode ()
1382
1374
{
@@ -1426,37 +1418,31 @@ private function getProductCode()
1426
1418
}
1427
1419
$ this ->_debug ($ debugData );
1428
1420
// 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
+ }
1432
1428
return $ productCodes ;
1433
1429
}
1434
1430
1435
1431
/**
1436
1432
* DHL REST API for Quote Data
1437
1433
*
1438
- * @param array $productCodes
1439
1434
* @return Result\ProxyDeferred
1440
1435
* @throws LocalizedException
1436
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1441
1437
*/
1442
- protected function _getQuotesRest (array $ productCodes )
1438
+ protected function _getQuotesRest ()
1443
1439
{
1444
1440
$ rawRequest = $ this ->_rawRequest ;
1445
1441
$ url = $ this ->getGatewayURL ();
1446
1442
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
-
1456
1443
/** Dutiable */
1457
1444
$ dutiable = ["isCustomsDeclarable " => false ];
1458
1445
if ($ this ->isDutiable ($ rawRequest ->getOrigCountryId (), $ rawRequest ->getDestCountryId ())) {
1459
- $ isCustomDeclarable = true ;
1460
1446
$ declaredValue = (int ) $ rawRequest ->getValue ();
1461
1447
$ baseCurrencyCode = $ this ->_storeManager
1462
1448
->getWebsite ($ this ->_request ->getWebsiteId ())
@@ -1492,7 +1478,6 @@ protected function _getQuotesRest(array $productCodes)
1492
1478
"number " => $ this ->getConfigData ('account ' )
1493
1479
]
1494
1480
],
1495
- "productsAndServices " => $ productCodesParams ,
1496
1481
"plannedShippingDateAndTime " => date ('Y-m-d\TH:i:s\Z ' , strtotime ($ this ->_getShipDate ())),
1497
1482
"unitOfMeasurement " => "metric " ,
1498
1483
"getAdditionalInformation " => [
@@ -1526,8 +1511,8 @@ protected function _getQuotesRest(array $productCodes)
1526
1511
$ httpResponse = $ this ->httpClient ->request (
1527
1512
new Request ($ url . '/rates ' , Request::METHOD_POST , $ headers , $ ratePayload )
1528
1513
);
1529
-
1530
1514
$ debugData ['request ' ] = $ ratePayload ;
1515
+
1531
1516
return $ this ->proxyDeferredFactory ->create (
1532
1517
[
1533
1518
'deferred ' => new CallbackDeferred (
@@ -1545,7 +1530,6 @@ function () use ($httpResponse, $debugData) {
1545
1530
}
1546
1531
$ debugData ['result ' ] = $ jsonResponse ;
1547
1532
$ this ->_debug ($ debugData );
1548
-
1549
1533
return $ this ->_parseRestResponse ($ jsonResponse );
1550
1534
}
1551
1535
)
@@ -1572,6 +1556,7 @@ private function getDhlAccessToken()
1572
1556
* @param string $rateResponse
1573
1557
* @return Result
1574
1558
* @throws LocalizedException
1559
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
1575
1560
*/
1576
1561
protected function _parseRestResponse ($ rateResponse )
1577
1562
{
@@ -1623,6 +1608,7 @@ protected function _parseRestResponse($rateResponse)
1623
1608
* @param array $exchangeRates
1624
1609
* @return $this
1625
1610
* @throws LocalizedException
1611
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
1626
1612
*/
1627
1613
protected function _addRestRate ($ product , $ exchangeRates )
1628
1614
{
@@ -2193,13 +2179,17 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
2193
2179
* @return DataObject
2194
2180
* @throws LocalizedException
2195
2181
* @throws Throwable
2182
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
2183
+ * @SuppressWarnings(PHPMD.NPathComplexity)
2184
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2196
2185
*/
2197
2186
protected function _doShipmentRequestRest ()
2198
2187
{
2199
2188
$ rawRequest = $ this ->_request ;
2200
2189
$ url = $ this ->getGatewayURL ().'/shipments ' ;
2201
2190
2202
2191
/** shipper */
2192
+ $ shipperAddress = $ receiverAddress = [];
2203
2193
$ shipAddress = $ rawRequest ->getShipperAddressStreet1 () . ' ' . $ rawRequest ->getShipperAddressStreet2 ();
2204
2194
$ shipAddress = $ this ->string ->split ($ shipAddress , 45 , false , true );
2205
2195
if (is_array ($ shipAddress )) {
@@ -2270,7 +2260,6 @@ protected function _doShipmentRequestRest()
2270
2260
$ rawRequest ->getShipperAddressCountryCode (),
2271
2261
$ rawRequest ->getRecipientAddressCountryCode ()
2272
2262
)) {
2273
- $ isCustomDeclarable = true ;
2274
2263
$ declaredValue = sprintf ("%.2F " , $ rawRequest ->getOrderShipment ()->getOrder ()->getSubtotal ());
2275
2264
$ baseCurrencyCode = $ this ->_storeManager ->getWebsite ($ rawRequest ->getWebsiteId ())->getBaseCurrencyCode ();
2276
2265
/** Export Declaration details */
@@ -2281,11 +2270,11 @@ protected function _doShipmentRequestRest()
2281
2270
$ nodeExportItem = [];
2282
2271
$ nodeExportItem ['number ' ] = $ itemNo ;
2283
2272
$ 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 ' ];
2286
2275
$ 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 ' ];
2289
2278
$ nodeExportItem ['manufacturerCountry ' ] = $ rawRequest ->getShipperAddressCountryCode ();
2290
2279
$ nodeExportItems [$ i ] = $ nodeExportItem ;
2291
2280
$ i ++;
@@ -2296,9 +2285,11 @@ protected function _doShipmentRequestRest()
2296
2285
"declaredValue " => $ declaredValue ,
2297
2286
"declaredValueCurrency " => $ baseCurrencyCode ,
2298
2287
"exportDeclaration " => [
2299
- "lineItems " => [
2300
- $ nodeExportItems
2301
- ]
2288
+ "lineItems " => $ nodeExportItems ,
2289
+ "invoice " => [
2290
+ "number " => $ rawRequest ->getOrderShipment ()->getOrder ()->getIncrementId (),
2291
+ "date " => date ('Y-m-d ' )
2292
+ ]
2302
2293
]
2303
2294
];
2304
2295
}
@@ -2310,7 +2301,7 @@ protected function _doShipmentRequestRest()
2310
2301
"pickup " => [
2311
2302
"isRequested " => false
2312
2303
],
2313
- "productCode " => " N " ,
2304
+ "productCode " => $ rawRequest -> getShippingMethod () ,
2314
2305
"accounts " => [
2315
2306
[
2316
2307
"typeCode " => "shipper " ,
@@ -2365,6 +2356,7 @@ protected function _doShipmentRequestRest()
2365
2356
];
2366
2357
2367
2358
$ shippingPayload = json_encode ($ shippingParams );
2359
+
2368
2360
$ debugData = ['request ' => $ this ->filterDebugData ($ shippingPayload )];
2369
2361
try {
2370
2362
$ response = $ this ->httpClient ->request (
@@ -2413,7 +2405,7 @@ public function getTracking($trackings)
2413
2405
}
2414
2406
2415
2407
if ($ this ->getConfigData ('type ' ) == 'DHL_REST ' ) {
2416
- $ this ->_getRestTracking ($ trackings );
2408
+ $ this ->_getRestTracking (implode ( ' , ' , $ trackings) );
2417
2409
} else {
2418
2410
$ this ->_getXMLTracking ($ trackings );
2419
2411
}
@@ -2600,7 +2592,7 @@ protected function _getRestTracking($trackings)
2600
2592
$ url = $ this ->getGatewayURL ().'/tracking? ' ;
2601
2593
2602
2594
$ trackingParams = [
2603
- 'shipmentTrackingNumber ' => $ trackings[ 0 ] ,
2595
+ 'shipmentTrackingNumber ' => $ trackings ,
2604
2596
'language ' => 'en ' ,
2605
2597
'limit ' => 10
2606
2598
];
@@ -2639,6 +2631,8 @@ protected function _getRestTracking($trackings)
2639
2631
* @param string[] $trackings
2640
2632
* @param string $response
2641
2633
* @return void
2634
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
2635
+ * @SuppressWarnings(PHPMD.NPathComplexity)
2642
2636
*/
2643
2637
protected function _parseRestTrackingResponse ($ trackings , $ response )
2644
2638
{
@@ -2648,10 +2642,10 @@ protected function _parseRestTrackingResponse($trackings, $response)
2648
2642
if (!empty (trim ($ response ))) {
2649
2643
$ trackingData = json_decode ($ response );
2650
2644
if (!empty ($ trackingData )
2651
- && (isset ($ trackingData ->shipments [ 0 ]-> status )
2652
- && $ trackingData ->shipments [ 0 ]-> status !== 'Success ' )
2645
+ && (isset ($ trackingData ->status )
2646
+ && $ trackingData ->status !== 'Success ' )
2653
2647
) {
2654
- $ this ->_errors ['error ' ] = __ ('Error %1 ' , $ trackingData ->shipments [ 0 ]-> status );
2648
+ $ this ->_errors ['error ' ] = __ ('Error %1 ' , $ trackingData ->message );
2655
2649
} elseif (!empty ($ trackingData ) && $ trackingData ->shipments [0 ]->status == 'Success ' ) {
2656
2650
foreach ($ trackingData ->shipments as $ shipment ) {
2657
2651
$ awbinfoData = [];
0 commit comments