Skip to content

Commit ba9b12e

Browse files
author
Joan He
committed
MC-6295: Update ShipmentValidationRequest
1 parent c6c3a25 commit ba9b12e

File tree

5 files changed

+282
-233
lines changed

5 files changed

+282
-233
lines changed

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

Lines changed: 74 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Dhl\Model;
88

99
use Magento\Catalog\Model\Product\Type;
10+
use Magento\Framework\App\ProductMetadataInterface;
1011
use Magento\Framework\Module\Dir;
1112
use Magento\Sales\Exception\DocumentValidationException;
1213
use Magento\Sales\Model\Order\Shipment;
@@ -213,6 +214,11 @@ class Carrier extends \Magento\Dhl\Model\AbstractDhl implements \Magento\Shippin
213214
*/
214215
private $xmlValidator;
215216

217+
/**
218+
* @var ProductMetadataInterface
219+
*/
220+
private $productMetadata;
221+
216222
/**
217223
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
218224
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
@@ -239,7 +245,8 @@ class Carrier extends \Magento\Dhl\Model\AbstractDhl implements \Magento\Shippin
239245
* @param \Magento\Framework\Stdlib\DateTime $dateTime
240246
* @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
241247
* @param array $data
242-
* @param \Magento\Dhl\Model\Validator\XmlValidator $xmlValidator
248+
* @param \Magento\Dhl\Model\Validator\XmlValidator|null $xmlValidator
249+
* @param ProductMetadataInterface|null $productMetadata
243250
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
244251
*/
245252
public function __construct(
@@ -268,7 +275,8 @@ public function __construct(
268275
\Magento\Framework\Stdlib\DateTime $dateTime,
269276
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory,
270277
array $data = [],
271-
\Magento\Dhl\Model\Validator\XmlValidator $xmlValidator = null
278+
\Magento\Dhl\Model\Validator\XmlValidator $xmlValidator = null,
279+
ProductMetadataInterface $productMetadata = null
272280
) {
273281
$this->readFactory = $readFactory;
274282
$this->_carrierHelper = $carrierHelper;
@@ -302,6 +310,8 @@ public function __construct(
302310
}
303311
$this->xmlValidator = $xmlValidator
304312
?: \Magento\Framework\App\ObjectManager::getInstance()->get(XmlValidator::class);
313+
$this->productMetadata = $productMetadata
314+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(ProductMetadataInterface::class);
305315
}
306316

307317
/**
@@ -1393,44 +1403,41 @@ protected function _doRequest()
13931403
{
13941404
$rawRequest = $this->_request;
13951405

1396-
$originRegion = $this->getCountryParams(
1397-
$this->_scopeConfig->getValue(
1398-
Shipment::XML_PATH_STORE_COUNTRY_ID,
1399-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
1400-
$this->getStore()
1401-
)
1402-
)->getRegion();
1403-
1404-
if (!$originRegion) {
1405-
throw new \Magento\Framework\Exception\LocalizedException(__('Wrong Region'));
1406-
}
1407-
1408-
if ($originRegion == 'AM') {
1409-
$originRegion = '';
1410-
}
1411-
14121406
$xmlStr = '<?xml version="1.0" encoding="UTF-8"?>' .
1413-
'<req:ShipmentValidateRequest' .
1414-
$originRegion .
1407+
'<req:ShipmentRequest' .
14151408
' xmlns:req="http://www.dhl.com"' .
14161409
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' .
1417-
' xsi:schemaLocation="http://www.dhl.com ship-val-req' .
1418-
($originRegion ? '_' .
1419-
$originRegion : '') .
1420-
'.xsd" />';
1410+
' xsi:schemaLocation="http://www.dhl.com ship-val-global-req-6.2.xsd"' .
1411+
' schemaVersion="6.2" />';
14211412
$xml = $this->_xmlElFactory->create(['data' => $xmlStr]);
14221413

14231414
$nodeRequest = $xml->addChild('Request', '', '');
14241415
$nodeServiceHeader = $nodeRequest->addChild('ServiceHeader');
1416+
$nodeServiceHeader->addChild('MessageTime', $this->buildMessageTimestamp());
1417+
// MessageReference must be 28 to 32 chars.
1418+
$nodeServiceHeader->addChild(
1419+
'MessageReference',
1420+
$this->buildMessageReference(self::SERVICE_PREFIX_SHIPVAL)
1421+
);
14251422
$nodeServiceHeader->addChild('SiteID', (string)$this->getConfigData('id'));
14261423
$nodeServiceHeader->addChild('Password', (string)$this->getConfigData('password'));
14271424

1428-
if (!$originRegion) {
1429-
$xml->addChild('RequestedPickupTime', 'N', '');
1430-
}
1431-
if ($originRegion !== 'AP') {
1432-
$xml->addChild('NewShipper', 'N', '');
1425+
$nodeMetaData = $nodeRequest->addChild('MetaData');
1426+
$nodeMetaData->addChild('SoftwareName', $this->productMetadata->getName());
1427+
$nodeMetaData->addChild('SoftwareVersion', $this->productMetadata->getVersion());
1428+
1429+
$originRegion = $this->getCountryParams(
1430+
$this->_scopeConfig->getValue(
1431+
Shipment::XML_PATH_STORE_COUNTRY_ID,
1432+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
1433+
$this->getStore()
1434+
)
1435+
)->getRegion();
1436+
if ($originRegion) {
1437+
$xml->addChild('RegionCode', $originRegion, '');
14331438
}
1439+
$xml->addChild('RequestedPickupTime', 'N', '');
1440+
$xml->addChild('NewShipper', 'N', '');
14341441
$xml->addChild('LanguageCode', 'EN', '');
14351442
$xml->addChild('PiecesEnabled', 'Y', '');
14361443

@@ -1472,8 +1479,9 @@ protected function _doRequest()
14721479
}
14731480

14741481
$nodeConsignee->addChild('City', $rawRequest->getRecipientAddressCity());
1475-
if ($originRegion !== 'AP') {
1476-
$nodeConsignee->addChild('Division', $rawRequest->getRecipientAddressStateOrProvinceCode());
1482+
$recipientAddressStateOrProvinceCode = $rawRequest->getRecipientAddressStateOrProvinceCode();
1483+
if ($recipientAddressStateOrProvinceCode) {
1484+
$nodeConsignee->addChild('Division', $recipientAddressStateOrProvinceCode);
14771485
}
14781486
$nodeConsignee->addChild('PostalCode', $rawRequest->getRecipientAddressPostalCode());
14791487
$nodeConsignee->addChild('CountryCode', $rawRequest->getRecipientAddressCountryCode());
@@ -1517,15 +1525,13 @@ protected function _doRequest()
15171525
$nodeReference->addChild('ReferenceType', 'St');
15181526

15191527
/** Shipment Details */
1520-
$this->_shipmentDetails($xml, $rawRequest, $originRegion);
1528+
$this->_shipmentDetails($xml, $rawRequest);
15211529

15221530
/** Shipper */
15231531
$nodeShipper = $xml->addChild('Shipper', '', '');
15241532
$nodeShipper->addChild('ShipperID', (string)$this->getConfigData('account'));
15251533
$nodeShipper->addChild('CompanyName', $rawRequest->getShipperContactCompanyName());
1526-
if ($originRegion !== 'AP') {
1527-
$nodeShipper->addChild('RegisteredAccount', (string)$this->getConfigData('account'));
1528-
}
1534+
$nodeShipper->addChild('RegisteredAccount', (string)$this->getConfigData('account'));
15291535

15301536
$address = $rawRequest->getShipperAddressStreet1() . ' ' . $rawRequest->getShipperAddressStreet2();
15311537
$address = $this->string->split($address, 35, false, true);
@@ -1538,8 +1544,9 @@ protected function _doRequest()
15381544
}
15391545

15401546
$nodeShipper->addChild('City', $rawRequest->getShipperAddressCity());
1541-
if ($originRegion !== 'AP') {
1542-
$nodeShipper->addChild('Division', $rawRequest->getShipperAddressStateOrProvinceCode());
1547+
$shipperAddressStateOrProvinceCode = $rawRequest->getShipperAddressStateOrProvinceCode();
1548+
if ($shipperAddressStateOrProvinceCode) {
1549+
$nodeShipper->addChild('Division', $shipperAddressStateOrProvinceCode);
15431550
}
15441551
$nodeShipper->addChild('PostalCode', $rawRequest->getShipperAddressPostalCode());
15451552
$nodeShipper->addChild('CountryCode', $rawRequest->getShipperAddressCountryCode());
@@ -1597,13 +1604,6 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
15971604
$nodeShipmentDetails = $xml->addChild('ShipmentDetails', '', '');
15981605
$nodeShipmentDetails->addChild('NumberOfPieces', count($rawRequest->getPackages()));
15991606

1600-
if ($originRegion) {
1601-
$nodeShipmentDetails->addChild(
1602-
'CurrencyCode',
1603-
$this->_storeManager->getWebsite($this->_request->getWebsiteId())->getBaseCurrencyCode()
1604-
);
1605-
}
1606-
16071607
$nodePieces = $nodeShipmentDetails->addChild('Pieces', '', '');
16081608

16091609
/*
@@ -1622,18 +1622,12 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
16221622
}
16231623
$nodePiece->addChild('PieceID', ++$i);
16241624
$nodePiece->addChild('PackageType', $packageType);
1625-
$nodePiece->addChild('Weight', sprintf('%.1f', $package['params']['weight']));
1625+
$nodePiece->addChild('Weight', sprintf('%.3f', $package['params']['weight']));
16261626
$params = $package['params'];
16271627
if ($params['width'] && $params['length'] && $params['height']) {
1628-
if (!$originRegion) {
1629-
$nodePiece->addChild('Width', round($params['width']));
1630-
$nodePiece->addChild('Height', round($params['height']));
1631-
$nodePiece->addChild('Depth', round($params['length']));
1632-
} else {
1633-
$nodePiece->addChild('Depth', round($params['length']));
1634-
$nodePiece->addChild('Width', round($params['width']));
1635-
$nodePiece->addChild('Height', round($params['height']));
1636-
}
1628+
$nodePiece->addChild('Width', round($params['width']));
1629+
$nodePiece->addChild('Height', round($params['height']));
1630+
$nodePiece->addChild('Depth', round($params['length']));
16371631
}
16381632
$content = [];
16391633
foreach ($package['items'] as $item) {
@@ -1642,51 +1636,33 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
16421636
$nodePiece->addChild('PieceContents', substr(implode(',', $content), 0, 34));
16431637
}
16441638

1645-
if (!$originRegion) {
1646-
$nodeShipmentDetails->addChild('Weight', sprintf('%.1f', $rawRequest->getPackageWeight()));
1647-
$nodeShipmentDetails->addChild('WeightUnit', substr($this->_getWeightUnit(), 0, 1));
1648-
$nodeShipmentDetails->addChild('GlobalProductCode', $rawRequest->getShippingMethod());
1649-
$nodeShipmentDetails->addChild('LocalProductCode', $rawRequest->getShippingMethod());
1650-
$nodeShipmentDetails->addChild('Date', $this->_coreDate->date('Y-m-d'));
1651-
$nodeShipmentDetails->addChild('Contents', 'DHL Parcel');
1652-
/**
1653-
* The DoorTo Element defines the type of delivery service that applies to the shipment.
1654-
* The valid values are DD (Door to Door), DA (Door to Airport) , AA and DC (Door to
1655-
* Door non-compliant)
1656-
*/
1657-
$nodeShipmentDetails->addChild('DoorTo', 'DD');
1658-
$nodeShipmentDetails->addChild('DimensionUnit', substr($this->_getDimensionUnit(), 0, 1));
1659-
if ($package['params']['container'] == self::DHL_CONTENT_TYPE_NON_DOC) {
1660-
$packageType = 'CP';
1661-
}
1662-
$nodeShipmentDetails->addChild('PackageType', $packageType);
1663-
if ($this->isDutiable($rawRequest->getOrigCountryId(), $rawRequest->getDestCountryId())) {
1664-
$nodeShipmentDetails->addChild('IsDutiable', 'Y');
1665-
}
1666-
$nodeShipmentDetails->addChild(
1667-
'CurrencyCode',
1668-
$this->_storeManager->getWebsite($this->_request->getWebsiteId())->getBaseCurrencyCode()
1669-
);
1670-
} else {
1671-
if ($package['params']['container'] == self::DHL_CONTENT_TYPE_NON_DOC) {
1672-
$packageType = 'CP';
1673-
}
1674-
$nodeShipmentDetails->addChild('PackageType', $packageType);
1675-
$nodeShipmentDetails->addChild('Weight', sprintf('%.3f', $rawRequest->getPackageWeight()));
1676-
$nodeShipmentDetails->addChild('DimensionUnit', substr($this->_getDimensionUnit(), 0, 1));
1677-
$nodeShipmentDetails->addChild('WeightUnit', substr($this->_getWeightUnit(), 0, 1));
1678-
$nodeShipmentDetails->addChild('GlobalProductCode', $rawRequest->getShippingMethod());
1679-
$nodeShipmentDetails->addChild('LocalProductCode', $rawRequest->getShippingMethod());
1680-
1681-
/**
1682-
* The DoorTo Element defines the type of delivery service that applies to the shipment.
1683-
* The valid values are DD (Door to Door), DA (Door to Airport) , AA and DC (Door to
1684-
* Door non-compliant)
1685-
*/
1686-
$nodeShipmentDetails->addChild('DoorTo', 'DD');
1687-
$nodeShipmentDetails->addChild('Date', $this->_coreDate->date('Y-m-d'));
1688-
$nodeShipmentDetails->addChild('Contents', 'DHL Parcel TEST');
1639+
$nodeShipmentDetails->addChild('Weight', sprintf('%.3f', $rawRequest->getPackageWeight()));
1640+
$nodeShipmentDetails->addChild('WeightUnit', substr($this->_getWeightUnit(), 0, 1));
1641+
$nodeShipmentDetails->addChild('GlobalProductCode', $rawRequest->getShippingMethod());
1642+
$nodeShipmentDetails->addChild('LocalProductCode', $rawRequest->getShippingMethod());
1643+
$nodeShipmentDetails->addChild(
1644+
'Date',
1645+
$this->_coreDate->date('Y-m-d', strtotime('now + 1day'))
1646+
);
1647+
$nodeShipmentDetails->addChild('Contents', 'DHL Parcel');
1648+
/**
1649+
* The DoorTo Element defines the type of delivery service that applies to the shipment.
1650+
* The valid values are DD (Door to Door), DA (Door to Airport) , AA and DC (Door to
1651+
* Door non-compliant)
1652+
*/
1653+
$nodeShipmentDetails->addChild('DoorTo', 'DD');
1654+
$nodeShipmentDetails->addChild('DimensionUnit', substr($this->_getDimensionUnit(), 0, 1));
1655+
if ($package['params']['container'] == self::DHL_CONTENT_TYPE_NON_DOC) {
1656+
$packageType = 'CP';
16891657
}
1658+
$nodeShipmentDetails->addChild('PackageType', $packageType);
1659+
if ($this->isDutiable($rawRequest->getOrigCountryId(), $rawRequest->getDestCountryId())) {
1660+
$nodeShipmentDetails->addChild('IsDutiable', 'Y');
1661+
}
1662+
$nodeShipmentDetails->addChild(
1663+
'CurrencyCode',
1664+
$this->_storeManager->getWebsite($this->_request->getWebsiteId())->getBaseCurrencyCode()
1665+
);
16901666
}
16911667

16921668
/**

0 commit comments

Comments
 (0)