@@ -1363,79 +1363,14 @@ protected function _addRate(SimpleXMLElement $shipmentDetails)
1363
1363
return $ this ;
1364
1364
}
1365
1365
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
-
1431
1366
/**
1432
1367
* DHL REST API for Quote Data
1433
1368
*
1434
1369
* @return Result\ProxyDeferred
1435
1370
* @throws LocalizedException
1436
1371
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1437
1372
*/
1438
- protected function _getQuotesRest ()
1373
+ protected function _getQuotesRest () : Result \ ProxyDeferred
1439
1374
{
1440
1375
$ rawRequest = $ this ->_rawRequest ;
1441
1376
$ url = $ this ->getGatewayURL ();
@@ -1501,15 +1436,8 @@ protected function _getQuotesRest()
1501
1436
1502
1437
$ ratePayload = json_encode ($ rateParams , JSON_PRETTY_PRINT );
1503
1438
1504
- /** Rest API Payload */
1505
- $ headers = [
1506
- "Authorization " => "Basic " . $ this ->getDhlAccessToken (),
1507
- "Content-Type " => "application/json " ,
1508
- "x-version " => "2.12.0 "
1509
- ];
1510
-
1511
1439
$ 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 )
1513
1441
);
1514
1442
$ debugData ['request ' ] = $ ratePayload ;
1515
1443
@@ -1542,14 +1470,28 @@ function () use ($httpResponse, $debugData) {
1542
1470
*
1543
1471
* @return string
1544
1472
*/
1545
- private function getDhlAccessToken ()
1473
+ private function getDhlAccessToken () : string
1546
1474
{
1547
1475
$ username = (string ) $ this ->getConfigData ('api_key ' );
1548
1476
$ password = (string ) $ this ->getConfigData ('api_secret ' );
1549
1477
$ access_token = base64_encode ($ username . ": " . $ password );
1550
1478
return $ access_token ;
1551
1479
}
1552
1480
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
+
1553
1495
/**
1554
1496
* Parse response from DHL REST API
1555
1497
*
@@ -1558,7 +1500,7 @@ private function getDhlAccessToken()
1558
1500
* @throws LocalizedException
1559
1501
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1560
1502
*/
1561
- protected function _parseRestResponse ($ rateResponse )
1503
+ protected function _parseRestResponse ($ rateResponse ): Result
1562
1504
{
1563
1505
$ responseError = __ ('The response is in wrong format. ' );
1564
1506
if ($ rateResponse !== null && strlen ($ rateResponse ) > 0 ) {
@@ -1610,7 +1552,7 @@ protected function _parseRestResponse($rateResponse)
1610
1552
* @throws LocalizedException
1611
1553
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1612
1554
*/
1613
- protected function _addRestRate ($ product , $ exchangeRates )
1555
+ protected function _addRestRate ($ product , $ exchangeRates ): self
1614
1556
{
1615
1557
if (isset ($ product ['productName ' ])
1616
1558
&& isset ($ product ['productCode ' ])
@@ -2183,7 +2125,7 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
2183
2125
* @SuppressWarnings(PHPMD.NPathComplexity)
2184
2126
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2185
2127
*/
2186
- protected function _doShipmentRequestRest ()
2128
+ protected function _doShipmentRequestRest (): DataObject
2187
2129
{
2188
2130
$ rawRequest = $ this ->_request ;
2189
2131
$ url = $ this ->getGatewayURL ().'/shipments ' ;
@@ -2348,13 +2290,6 @@ protected function _doShipmentRequestRest()
2348
2290
], $ dutiable )
2349
2291
];
2350
2292
2351
- /** Rest API Payload */
2352
- $ headers = [
2353
- "Authorization " => "Basic " . $ this ->getDhlAccessToken (),
2354
- "Content-Type " => "application/json " ,
2355
- "x-version " => "2.12.0 "
2356
- ];
2357
-
2358
2293
$ shippingPayload = json_encode ($ shippingParams );
2359
2294
2360
2295
$ debugData = ['request ' => $ this ->filterDebugData ($ shippingPayload )];
@@ -2363,7 +2298,7 @@ protected function _doShipmentRequestRest()
2363
2298
new Request (
2364
2299
$ url ,
2365
2300
Request::METHOD_POST ,
2366
- $ headers ,
2301
+ $ this -> getRestHeaders () ,
2367
2302
$ shippingPayload
2368
2303
)
2369
2304
);
@@ -2398,7 +2333,7 @@ protected function _doShipmentRequestRest()
2398
2333
* @param string|string[] $trackings
2399
2334
* @return \Magento\Shipping\Model\Tracking\Result|null
2400
2335
*/
2401
- public function getTracking ($ trackings )
2336
+ public function getTracking ($ trackings ): ? \ Magento \ Shipping \ Model \ Tracking \ Result
2402
2337
{
2403
2338
if (!is_array ($ trackings )) {
2404
2339
$ trackings = [$ trackings ];
@@ -2601,19 +2536,13 @@ protected function _getRestTracking($trackings)
2601
2536
$ trackingPayload = (object )[];
2602
2537
$ trackingPayload = json_encode ($ trackingPayload );
2603
2538
2604
- $ headers = [
2605
- "Authorization " => "Basic " . $ this ->getDhlAccessToken (),
2606
- "Content-Type " => "application/json " ,
2607
- "x-version " => "2.12.0 "
2608
- ];
2609
-
2610
2539
$ debugData = ['request ' => $ this ->filterDebugData ($ trackingPayload )];
2611
2540
try {
2612
2541
$ response = $ this ->httpClient ->request (
2613
2542
new Request (
2614
2543
$ url .$ queryString ,
2615
2544
Request::METHOD_GET ,
2616
- $ headers ,
2545
+ $ this -> getRestHeaders () ,
2617
2546
$ trackingPayload
2618
2547
)
2619
2548
);
0 commit comments