Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 1ae2acf

Browse files
committed
MAGETWO-71831: Unable to create a shipping label for a shipment with USPS
- Add formatting decimal values according USPS API
1 parent f757b46 commit 1ae2acf

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,10 +1795,10 @@ protected function _formIntlShipmentRequest(\Magento\Framework\DataObject $reque
17951795
}
17961796
$individualItemWeight = $itemWeight / $ceiledQty;
17971797
$itemDetail->addChild('Quantity', $ceiledQty);
1798-
$itemDetail->addChild('Value', $item->getCustomsValue() * $item->getQty());
1798+
$itemDetail->addChild('Value', sprintf('%.2F', $item->getCustomsValue() * $item->getQty()));
17991799
list($individualPoundsWeight, $individualOuncesWeight) = $this->_convertPoundOunces($individualItemWeight);
18001800
$itemDetail->addChild('NetPounds', $individualPoundsWeight);
1801-
$itemDetail->addChild('NetOunces', $individualOuncesWeight);
1801+
$itemDetail->addChild('NetOunces', sprintf('%.2F', $individualOuncesWeight));
18021802
$itemDetail->addChild('HSTariffNumber', 0);
18031803
$itemDetail->addChild('CountryOfOrigin', $countryOfManufacture);
18041804

@@ -1814,7 +1814,7 @@ protected function _formIntlShipmentRequest(\Magento\Framework\DataObject $reque
18141814
}
18151815

18161816
$xml->addChild('GrossPounds', $packagePoundsWeight);
1817-
$xml->addChild('GrossOunces', $packageOuncesWeight);
1817+
$xml->addChild('GrossOunces', sprintf('%.2F', $packageOuncesWeight));
18181818
if ($packageParams->getContentType() == 'OTHER' && $packageParams->getContentTypeOther() != null) {
18191819
$xml->addChild('ContentType', $packageParams->getContentType());
18201820
$xml->addChild('ContentTypeOther ', $packageParams->getContentTypeOther());

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Usps\Test\Unit\Model;
77

8+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
89
use Magento\Framework\HTTP\ZendClient;
910
use Magento\Framework\HTTP\ZendClientFactory;
1011
use Magento\Quote\Model\Quote\Address\RateRequest;
@@ -162,6 +163,22 @@ function ($data) {
162163
]
163164
);
164165

166+
$productCollection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
167+
->disableOriginalConstructor()
168+
->getMock();
169+
$productCollection->method('addStoreFilter')
170+
->willReturnSelf();
171+
$productCollection->method('addFieldToFilter')
172+
->willReturnSelf();
173+
$productCollection->method('addAttributeToSelect')
174+
->willReturn([]);
175+
176+
$productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
177+
->disableOriginalConstructor()
178+
->getMock();
179+
$productCollectionFactory->method('create')
180+
->willReturn($productCollection);
181+
165182
$arguments = [
166183
'scopeConfig' => $this->scope,
167184
'xmlSecurity' => new \Magento\Framework\Xml\Security(),
@@ -172,6 +189,7 @@ function ($data) {
172189
'data' => $data,
173190
'rateErrorFactory' => $this->errorFactory,
174191
'carrierHelper' => $carrierHelper,
192+
'productCollectionFactory' => $productCollectionFactory,
175193
];
176194

177195
$this->dataHelper = $this->getMockBuilder(DataHelper::class)
@@ -265,6 +283,32 @@ public function testReturnOfShipment()
265283
$this->assertNotEmpty($this->carrier->returnOfShipment($request)->getInfo()[0]['tracking_number']);
266284
}
267285

286+
public function testFormattingFloatValuesForIntlShipmentRequest()
287+
{
288+
$this->httpResponse->method('getBody')
289+
->willReturn(
290+
file_get_contents(__DIR__ . '/_files/success_usps_response_return_shipment.xml')
291+
);
292+
$request = $this->objectManager->getObject(
293+
\Magento\Shipping\Model\Shipment\ReturnShipment::class,
294+
require __DIR__ . '/_files/return_shipment_request_data.php'
295+
);
296+
297+
$request->setRecipientAddressCountryCode('UK');
298+
$formattedValuesRegex = '(<Value>5.00<\/Value>).*';
299+
$formattedValuesRegex .= '(<NetOunces>0.00<\/NetOunces>).*';
300+
$formattedValuesRegex .= '(<GrossOunces>0.00<\/GrossOunces>)';
301+
302+
$this->httpClient->expects($this->exactly(2))
303+
->method('setParameterGet')
304+
->withConsecutive(
305+
['API', 'ExpressMailIntl'],
306+
['XML', $this->matchesRegularExpression('/' . $formattedValuesRegex . '/')]
307+
);
308+
309+
$this->carrier->returnOfShipment($request);
310+
}
311+
268312
/**
269313
* Emulates the config's `getValue` method.
270314
*

0 commit comments

Comments
 (0)