8
8
9
9
use Magento \Catalog \Model \Product \Type ;
10
10
use Magento \Framework \Module \Dir ;
11
+ use Magento \Sales \Exception \DocumentValidationException ;
11
12
use Magento \Sales \Model \Order \Shipment ;
12
13
use Magento \Quote \Model \Quote \Address \RateRequest ;
13
14
use Magento \Quote \Model \Quote \Address \RateResult \Error ;
14
15
use Magento \Shipping \Model \Carrier \AbstractCarrier ;
15
16
use Magento \Shipping \Model \Rate \Result ;
16
17
use Magento \Framework \Xml \Security ;
18
+ use Magento \Dhl \Model \Validator \XmlValidator ;
17
19
18
20
/**
19
21
* DHL International (API v1.4)
@@ -197,6 +199,13 @@ class Carrier extends \Magento\Dhl\Model\AbstractDhl implements \Magento\Shippin
197
199
'SiteID ' , 'Password '
198
200
];
199
201
202
+ /**
203
+ * Xml response validator
204
+ *
205
+ * @var \Magento\Dhl\Model\Validator\XmlValidator
206
+ */
207
+ private $ xmlValidator ;
208
+
200
209
/**
201
210
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
202
211
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
@@ -223,6 +232,7 @@ class Carrier extends \Magento\Dhl\Model\AbstractDhl implements \Magento\Shippin
223
232
* @param \Magento\Framework\Stdlib\DateTime $dateTime
224
233
* @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
225
234
* @param array $data
235
+ * @param \Magento\Dhl\Model\Validator\XmlValidatorFactory $xmlValidatorFactory
226
236
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
227
237
*/
228
238
public function __construct (
@@ -250,7 +260,8 @@ public function __construct(
250
260
\Magento \Framework \Filesystem \Directory \ReadFactory $ readFactory ,
251
261
\Magento \Framework \Stdlib \DateTime $ dateTime ,
252
262
\Magento \Framework \HTTP \ZendClientFactory $ httpClientFactory ,
253
- array $ data = []
263
+ array $ data = [],
264
+ \Magento \Dhl \Model \Validator \XmlValidator $ xmlValidator = null
254
265
) {
255
266
$ this ->readFactory = $ readFactory ;
256
267
$ this ->_carrierHelper = $ carrierHelper ;
@@ -282,6 +293,8 @@ public function __construct(
282
293
if ($ this ->getConfigData ('content_type ' ) == self ::DHL_CONTENT_TYPE_DOC ) {
283
294
$ this ->_freeMethod = 'free_method_doc ' ;
284
295
}
296
+ $ this ->xmlValidator = $ xmlValidator
297
+ ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (XmlValidator::class);
285
298
}
286
299
287
300
/**
@@ -1042,63 +1055,30 @@ protected function _setQuotesRequestXmlDate(\SimpleXMLElement $requestXml, $date
1042
1055
* @param string $response
1043
1056
* @return bool|\Magento\Framework\DataObject|Result|Error
1044
1057
* @throws \Magento\Framework\Exception\LocalizedException
1045
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
1046
- * @SuppressWarnings(PHPMD.NPathComplexity)
1058
+ * @since 2.0.0
1047
1059
*/
1048
1060
protected function _parseResponse ($ response )
1049
1061
{
1050
1062
$ responseError = __ ('The response is in wrong format. ' );
1051
-
1052
- if (strlen (trim ($ response )) > 0 ) {
1053
- if (strpos (trim ($ response ), '<?xml ' ) === 0 ) {
1054
- $ xml = $ this ->parseXml ($ response , \Magento \Shipping \Model \Simplexml \Element::class);
1055
- if (is_object ($ xml )) {
1056
- if (in_array ($ xml ->getName (), ['ErrorResponse ' , 'ShipmentValidateErrorResponse ' ])
1057
- || isset ($ xml ->GetQuoteResponse ->Note ->Condition )
1058
- ) {
1059
- $ code = null ;
1060
- $ data = null ;
1061
- if (isset ($ xml ->Response ->Status ->Condition )) {
1062
- $ nodeCondition = $ xml ->Response ->Status ->Condition ;
1063
- } else {
1064
- $ nodeCondition = $ xml ->GetQuoteResponse ->Note ->Condition ;
1065
- }
1066
-
1067
- if ($ this ->_isShippingLabelFlag ) {
1068
- foreach ($ nodeCondition as $ condition ) {
1069
- $ code = isset ($ condition ->ConditionCode ) ? (string )$ condition ->ConditionCode : 0 ;
1070
- $ data = isset ($ condition ->ConditionData ) ? (string )$ condition ->ConditionData : '' ;
1071
- if (!empty ($ code ) && !empty ($ data )) {
1072
- break ;
1073
- }
1074
- }
1075
- throw new \Magento \Framework \Exception \LocalizedException (
1076
- __ ('Error #%1 : %2 ' , trim ($ code ), trim ($ data ))
1077
- );
1078
- }
1079
-
1080
- $ code = isset ($ nodeCondition ->ConditionCode ) ? (string )$ nodeCondition ->ConditionCode : 0 ;
1081
- $ data = isset ($ nodeCondition ->ConditionData ) ? (string )$ nodeCondition ->ConditionData : '' ;
1082
- $ this ->_errors [$ code ] = __ ('Error #%1 : %2 ' , trim ($ code ), trim ($ data ));
1083
- } else {
1084
- if (isset ($ xml ->GetQuoteResponse ->BkgDetails ->QtdShp )) {
1085
- foreach ($ xml ->GetQuoteResponse ->BkgDetails ->QtdShp as $ quotedShipment ) {
1086
- $ this ->_addRate ($ quotedShipment );
1087
- }
1088
- } elseif (isset ($ xml ->AirwayBillNumber )) {
1089
- return $ this ->_prepareShippingLabelContent ($ xml );
1090
- } else {
1091
- $ this ->_errors [] = $ responseError ;
1092
- }
1093
- }
1063
+ try {
1064
+ $ this ->xmlValidator ->validate ($ response );
1065
+ $ xml = simplexml_load_string ($ response );
1066
+ if (isset ($ xml ->GetQuoteResponse ->BkgDetails ->QtdShp )) {
1067
+ foreach ($ xml ->GetQuoteResponse ->BkgDetails ->QtdShp as $ quotedShipment ) {
1068
+ $ this ->_addRate ($ quotedShipment );
1094
1069
}
1070
+ } elseif (isset ($ xml ->AirwayBillNumber )) {
1071
+ return $ this ->_prepareShippingLabelContent ($ xml );
1095
1072
} else {
1096
1073
$ this ->_errors [] = $ responseError ;
1097
1074
}
1098
- } else {
1099
- $ this ->_errors [] = $ responseError ;
1075
+ } catch (DocumentValidationException $ e ) {
1076
+ if ($ e ->getCode () > 0 ) {
1077
+ $ this ->_errors [$ e ->getCode ()] = $ e ->getMessage ();
1078
+ } else {
1079
+ $ this ->_errors [] = $ e ->getMessage ();
1080
+ }
1100
1081
}
1101
-
1102
1082
/* @var $result Result */
1103
1083
$ result = $ this ->_rateFactory ->create ();
1104
1084
if ($ this ->_rates ) {
@@ -1547,14 +1527,15 @@ protected function _doRequest()
1547
1527
$ xml ->addChild ('LabelImageFormat ' , 'PDF ' , '' );
1548
1528
1549
1529
$ request = $ xml ->asXML ();
1550
- if (!$ request && !mb_detect_encoding ($ request ) == 'UTF-8 ' ) {
1530
+ if (!$ request && !( mb_detect_encoding ($ request ) == 'UTF-8 ' ) ) {
1551
1531
$ request = utf8_encode ($ request );
1552
1532
}
1553
1533
1554
1534
$ responseBody = $ this ->_getCachedQuotes ($ request );
1555
1535
if ($ responseBody === null ) {
1556
1536
$ debugData = ['request ' => $ request ];
1557
1537
try {
1538
+ /** @var \Magento\Framework\HTTP\ZendClient $client */
1558
1539
$ client = $ this ->_httpClientFactory ->create ();
1559
1540
$ client ->setUri ((string )$ this ->getConfigData ('gateway_url ' ));
1560
1541
$ client ->setConfig (['maxredirects ' => 0 , 'timeout ' => 30 ]);
@@ -1664,7 +1645,7 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
1664
1645
$ packageType = 'CP ' ;
1665
1646
}
1666
1647
$ nodeShipmentDetails ->addChild ('PackageType ' , $ packageType );
1667
- $ nodeShipmentDetails ->addChild ('Weight ' , $ rawRequest ->getPackageWeight ());
1648
+ $ nodeShipmentDetails ->addChild ('Weight ' , sprintf ( ' %.3f ' , $ rawRequest ->getPackageWeight () ));
1668
1649
$ nodeShipmentDetails ->addChild ('DimensionUnit ' , substr ($ this ->_getDimensionUnit (), 0 , 1 ));
1669
1650
$ nodeShipmentDetails ->addChild ('WeightUnit ' , substr ($ this ->_getWeightUnit (), 0 , 1 ));
1670
1651
$ nodeShipmentDetails ->addChild ('GlobalProductCode ' , $ rawRequest ->getShippingMethod ());
@@ -1748,7 +1729,8 @@ protected function _getXMLTracking($trackings)
1748
1729
if ($ responseBody === null ) {
1749
1730
$ debugData = ['request ' => $ request ];
1750
1731
try {
1751
- $ client = new \Magento \Framework \HTTP \ZendClient ();
1732
+ /** @var \Magento\Framework\HTTP\ZendClient $client */
1733
+ $ client = $ this ->_httpClientFactory ->create ();
1752
1734
$ client ->setUri ((string )$ this ->getConfigData ('gateway_url ' ));
1753
1735
$ client ->setConfig (['maxredirects ' => 0 , 'timeout ' => 30 ]);
1754
1736
$ client ->setRawData ($ request );
0 commit comments