Skip to content

Commit 239d499

Browse files
authored
Merge pull request #9752 from magento-gl/DHL_AC-13258_25APR25
Migrate DHL integration from outdated DHL Express XML to new MyDHL API RESTful APIs-rating,shipping and tracking api implementation
2 parents 6ea6112 + 6bdba02 commit 239d499

17 files changed

+5741
-40
lines changed

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

Lines changed: 677 additions & 16 deletions
Large diffs are not rendered by default.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Dhl\Model\Config\Source;
9+
10+
use Magento\Framework\Data\OptionSourceInterface;
11+
12+
class Type implements OptionSourceInterface
13+
{
14+
/**
15+
* @inheritdoc
16+
*/
17+
public function toOptionArray()
18+
{
19+
return [
20+
['value' => 'DHL_XML', 'label' => __('DHL XML')],
21+
['value' => 'DHL_REST', 'label' => __('DHL REST')]
22+
];
23+
}
24+
}

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

Lines changed: 56 additions & 9 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

@@ -98,6 +98,11 @@ class CarrierTest extends TestCase
9898
*/
9999
private $productMetadataMock;
100100

101+
/**
102+
* @var mixed
103+
*/
104+
private mixed $responseData;
105+
101106
/**
102107
* @inheritdoc
103108
*/
@@ -173,9 +178,12 @@ public function scopeConfigGetValue($path)
173178
'carriers/dhl/intl_shipment_days' => 'Mon,Tue,Wed,Thu,Fri,Sat',
174179
'carriers/dhl/allowed_methods' => 'IE',
175180
'carriers/dhl/international_service' => 'IE',
176-
'carriers/dhl/gateway_url' => 'https://xmlpi-ea.dhl.com/XMLShippingServlet',
181+
'carriers/dhl/gateway_xml_url' => 'https://xmlpi-ea.dhl.com/XMLShippingServlet',
182+
'carriers/dhl/gateway_rest_url' => 'https://express.api.dhl.com/mydhlapi',
177183
'carriers/dhl/id' => 'some ID',
178184
'carriers/dhl/password' => 'some password',
185+
'carriers/dhl/api_key' => 'some key',
186+
'carriers/dhl/api_secret' => 'some secret key',
179187
'carriers/dhl/content_type' => 'N',
180188
'carriers/dhl/nondoc_methods' => '1,3,4,8,P,Q,E,F,H,J,M,V,Y',
181189
'carriers/dhl/showmethod' => 1,
@@ -453,15 +461,19 @@ public static function buildSoftwareVersionProvider()
453461
*
454462
* @dataProvider getGatewayURLProvider
455463
* @param $sandboxMode
464+
* @param string $type
456465
* @param $expectedURL
457466
* @throws \ReflectionException
458467
*/
459-
public function testGetGatewayURL($sandboxMode, $expectedURL)
468+
public function testGetGatewayURL($sandboxMode, $type, $expectedURL)
460469
{
461470
$scopeConfigValueMap = [
462-
['carriers/dhl/gateway_url', 'store', null, 'https://xmlpi-ea.dhl.com/XMLShippingServlet'],
463-
['carriers/dhl/sandbox_url', 'store', null, 'https://xmlpitest-ea.dhl.com/XMLShippingServlet'],
464-
['carriers/dhl/sandbox_mode', 'store', null, $sandboxMode]
471+
['carriers/dhl/sandbox_mode', 'store', null, $sandboxMode],
472+
['carriers/dhl/type', 'store', null, $type],
473+
['carriers/dhl/sandbox_xml_url', 'store', null, 'https://xmlpitest-ea.dhl.com/XMLShippingServlet'],
474+
['carriers/dhl/sandbox_rest_url', 'store', null, 'https://express.api.dhl.com/mydhlapi/test'],
475+
['carriers/dhl/gateway_xml_url', 'store', null, 'https://xmlpi-ea.dhl.com/XMLShippingServlet'],
476+
['carriers/dhl/gateway_rest_url', 'store', null, 'https://express.api.dhl.com/mydhlapi']
465477
];
466478

467479
$this->scope->method('getValue')
@@ -487,8 +499,10 @@ public function testGetGatewayURL($sandboxMode, $expectedURL)
487499
public static function getGatewayURLProvider()
488500
{
489501
return [
490-
'standard_url' => [0, 'https://xmlpi-ea.dhl.com/XMLShippingServlet'],
491-
'sandbox_url' => [1, 'https://xmlpitest-ea.dhl.com/XMLShippingServlet']
502+
'standard_xml_url' => [0, 'DHL_XML', 'https://xmlpi-ea.dhl.com/XMLShippingServlet'],
503+
'sandbox_xml_url' => [1, 'DHL_XML', 'https://xmlpitest-ea.dhl.com/XMLShippingServlet'],
504+
'standard_rest_url' => [0, 'DHL_REST', 'https://express.api.dhl.com/mydhlapi'],
505+
'sandbox_rest_url' => [1, 'DHL_REST', 'https://express.api.dhl.com/mydhlapi/test']
492506
];
493507
}
494508

@@ -670,4 +684,37 @@ private function getHttpClientFactory(): MockObject
670684

671685
return $httpClientFactory;
672686
}
687+
688+
public function testSuccessfulLabelGeneration()
689+
{
690+
$this->responseData = json_decode(file_get_contents(__DIR__ . '/_files/response_shipping_label.json'), true);
691+
$result = new DataObject();
692+
693+
// Extract label content from response and decode it
694+
$labelContent = base64_decode($this->responseData['documents'][0]['content']);
695+
696+
$result->setData('shipping_label_content', $labelContent);
697+
698+
// Assert that the label was correctly set
699+
$this->assertEquals($labelContent, $result->getData('shipping_label_content'));
700+
}
701+
702+
public function testLabelGenerationException()
703+
{
704+
$this->responseData = json_decode(file_get_contents(__DIR__ . '/_files/response_shipping_label.json'), true);
705+
$this->expectException(\Exception::class);
706+
707+
$result = new DataObject();
708+
709+
// Simulate a failure (e.g., missing document content)
710+
$invalidResponse = $this->responseData;
711+
unset($invalidResponse['documents'][0]['content']);
712+
713+
if (!isset($invalidResponse['documents'][0]['content'])) {
714+
throw new \Exception("Shipping label content is missing");
715+
}
716+
717+
$labelContent = base64_decode($invalidResponse['documents'][0]['content']);
718+
$result->setData('shipping_label_content', $labelContent);
719+
}
673720
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"shipmentTrackingNumber": "1111111111",
3+
"trackingUrl": "https://express.api.dhl.com/mydhlapi/test/shipments/1111111111/tracking",
4+
"packages": [
5+
{
6+
"referenceNumber": 1,
7+
"trackingNumber": "AB111111111111111111",
8+
"trackingUrl": "https://express.api.dhl.com/mydhlapi/test/shipments/1111111111/tracking?pieceTrackingNumber=AB111111111111111111"
9+
}
10+
],
11+
"documents": [
12+
{
13+
"imageFormat": "PDF",
14+
"content": "OutputImageContent",
15+
"typeCode": "label"
16+
}
17+
]
18+
}

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

Lines changed: 15 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">
@@ -17,6 +17,10 @@
1717
<field id="title" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
1818
<label>Title</label>
1919
</field>
20+
<field id="type" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" canRestore="1">
21+
<label>DHL Type</label>
22+
<source_model>Magento\Dhl\Model\Config\Source\Type</source_model>
23+
</field>
2024
<field id="id" translate="label" type="obscure" sortOrder="50" showInDefault="1" showInWebsite="1">
2125
<label>Access ID</label>
2226
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
@@ -25,9 +29,18 @@
2529
<label>Password</label>
2630
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
2731
</field>
32+
<field id="api_key" translate="label" type="obscure" sortOrder="55" showInDefault="1" showInWebsite="1">
33+
<label>API KEY</label>
34+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
35+
</field>
36+
<field id="api_secret" translate="label" type="obscure" sortOrder="65" showInDefault="1" showInWebsite="1">
37+
<label>API Secret</label>
38+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
39+
</field>
2840
<field id="account" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" canRestore="1">
2941
<label>Account Number</label>
3042
</field>
43+
3144
<field id="content_type" translate="label comment" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" canRestore="1">
3245
<label>Content Type (Non Domestic)</label>
3346
<comment>Whether to use Documents or NonDocuments service for non domestic shipments. (Shipments within the EU are classed as domestic)</comment>

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

Lines changed: 9 additions & 4 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">
@@ -20,14 +20,19 @@
2020
<account />
2121
<active>0</active>
2222
<title>DHL</title>
23+
<type>DHL_XML</type>
2324
<sallowspecific>0</sallowspecific>
2425
<nondoc_methods>1,3,4,8,P,Q,E,F,H,J,M,N,V,Y</nondoc_methods>
2526
<doc_methods>2,5,6,7,9,B,C,D,U,K,L,G,W,I,N,O,R,S,T,X</doc_methods>
2627
<free_method>G</free_method>
27-
<gateway_url>https://xmlpi-ea.dhl.com/XMLShippingServlet</gateway_url>
28-
<sandbox_url>https://xmlpitest-ea.dhl.com/XMLShippingServlet</sandbox_url>
28+
<gateway_xml_url>https://xmlpi-ea.dhl.com/XMLShippingServlet</gateway_xml_url>
29+
<sandbox_xml_url>https://xmlpitest-ea.dhl.com/XMLShippingServlet</sandbox_xml_url>
30+
<gateway_rest_url>https://express.api.dhl.com/mydhlapi</gateway_rest_url>
31+
<sandbox_rest_url>https://express.api.dhl.com/mydhlapi/test</sandbox_rest_url>
2932
<id backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
3033
<password backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
34+
<api_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
35+
<api_secret backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
3136
<content_type>N</content_type>
3237
<specificerrmsg>This shipping method is currently unavailable. If you would like to ship using this shipping method, please contact us.</specificerrmsg>
3338
<divide_order_weight>1</divide_order_weight>

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
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">
99
<type name="Magento\Config\Model\Config\TypePool">
1010
<arguments>
1111
<argument name="sensitive" xsi:type="array">
1212
<item name="carriers/dhl/account" xsi:type="string">1</item>
13-
<item name="carriers/dhl/gateway_url" xsi:type="string">1</item>
13+
<item name="carriers/dhl/gateway_xml_url" xsi:type="string">1</item>
14+
<item name="carriers/dhl/gateway_rest_url" xsi:type="string">1</item>
1415
<item name="carriers/dhl/id" xsi:type="string">1</item>
1516
<item name="carriers/dhl/password" xsi:type="string">1</item>
17+
<item name="carriers/dhl/api_key" xsi:type="string">1</item>
18+
<item name="carriers/dhl/api_secret" xsi:type="string">1</item>
1619
</argument>
1720
<argument name="environment" xsi:type="array">
1821
<item name="carriers/dhl/debug" xsi:type="string">1</item>
19-
<item name="carriers/dhl/gateway_url" xsi:type="string">1</item>
22+
<item name="carriers/dhl/gateway_xml_url" xsi:type="string">1</item>
23+
<item name="carriers/dhl/gateway_rest_url" xsi:type="string">1</item>
2024
</argument>
2125
</arguments>
2226
</type>

0 commit comments

Comments
 (0)