Skip to content

Commit cdf14f6

Browse files
committed
Merge remote-tracking branch 'origin/MC-33267' into 2.4.1-develop-pr25
2 parents 636b246 + d5080d8 commit cdf14f6

File tree

6 files changed

+338
-104
lines changed

6 files changed

+338
-104
lines changed

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

Lines changed: 131 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,20 +1361,11 @@ public function getAllowedMethods()
13611361
*/
13621362
protected function _formShipmentRequest(DataObject $request)
13631363
{
1364-
$packageParams = $request->getPackageParams();
1365-
$height = $packageParams->getHeight();
1366-
$width = $packageParams->getWidth();
1367-
$length = $packageParams->getLength();
1368-
$weightUnits = $packageParams->getWeightUnits() == \Zend_Measure_Weight::POUND ? 'LBS' : 'KGS';
1369-
$dimensionsUnits = $packageParams->getDimensionUnits() == \Zend_Measure_Length::INCH ? 'IN' : 'CM';
1370-
1371-
$itemsDesc = [];
1372-
$itemsShipment = $request->getPackageItems();
1373-
foreach ($itemsShipment as $itemShipment) {
1374-
$item = new DataObject();
1375-
$item->setData($itemShipment);
1376-
$itemsDesc[] = $item->getName();
1364+
$packages = $request->getPackages();
1365+
foreach ($packages as $package) {
1366+
$shipmentItems[] = $package['items'];
13771367
}
1368+
$shipmentItems = array_merge(...$shipmentItems);
13781369

13791370
$xmlRequest = $this->_xmlElFactory->create(
13801371
['data' => '<?xml version = "1.0" ?><ShipmentConfirmRequest xml:lang="en-US"/>']
@@ -1389,7 +1380,7 @@ protected function _formShipmentRequest(DataObject $request)
13891380
// UPS Print Return Label
13901381
$returnPart->addChild('Code', '9');
13911382
}
1392-
$shipmentPart->addChild('Description', substr(implode(' ', $itemsDesc), 0, 35));
1383+
$shipmentPart->addChild('Description', $this->generateShipmentDescription($shipmentItems));
13931384
//empirical
13941385

13951386
$shipperPart = $shipmentPart->addChild('Shipper');
@@ -1481,77 +1472,95 @@ protected function _formShipmentRequest(DataObject $request)
14811472

14821473
$servicePart = $shipmentPart->addChild('Service');
14831474
$servicePart->addChild('Code', $request->getShippingMethod());
1484-
$packagePart = $shipmentPart->addChild('Package');
1485-
$packagePart->addChild('Description', substr(implode(' ', $itemsDesc), 0, 35));
1486-
//empirical
1487-
$packagePart->addChild('PackagingType')->addChild('Code', $request->getPackagingType());
1488-
$packageWeight = $packagePart->addChild('PackageWeight');
1489-
$packageWeight->addChild('Weight', $request->getPackageWeight());
1490-
$packageWeight->addChild('UnitOfMeasurement')->addChild('Code', $weightUnits);
1491-
1492-
// set dimensions
1493-
if ($length || $width || $height) {
1494-
$packageDimensions = $packagePart->addChild('Dimensions');
1495-
$packageDimensions->addChild('UnitOfMeasurement')->addChild('Code', $dimensionsUnits);
1496-
$packageDimensions->addChild('Length', $length);
1497-
$packageDimensions->addChild('Width', $width);
1498-
$packageDimensions->addChild('Height', $height);
1499-
}
1500-
1501-
// ups support reference number only for domestic service
1502-
if ($this->_isUSCountry($request->getRecipientAddressCountryCode())
1503-
&& $this->_isUSCountry($request->getShipperAddressCountryCode())
1504-
) {
1505-
if ($request->getReferenceData()) {
1506-
$referenceData = $request->getReferenceData() . $request->getPackageId();
1507-
} else {
1508-
$referenceData = 'Order #' .
1509-
$request->getOrderShipment()->getOrder()->getIncrementId() .
1510-
' P' .
1511-
$request->getPackageId();
1475+
1476+
$packagePart = [];
1477+
$customsTotal = 0;
1478+
$packagingTypes = [];
1479+
$deliveryConfirmationLevel = $this->_getDeliveryConfirmationLevel(
1480+
$request->getRecipientAddressCountryCode()
1481+
);
1482+
foreach ($packages as $packageId => $package) {
1483+
$packageItems = $package['items'];
1484+
$packageParams = new DataObject($package['params']);
1485+
$packagingType = $package['params']['container'];
1486+
$packagingTypes[] = $packagingType;
1487+
$height = $packageParams->getHeight();
1488+
$width = $packageParams->getWidth();
1489+
$length = $packageParams->getLength();
1490+
$weight = $packageParams->getWeight();
1491+
$weightUnits = $packageParams->getWeightUnits() == \Zend_Measure_Weight::POUND ? 'LBS' : 'KGS';
1492+
$dimensionsUnits = $packageParams->getDimensionUnits() == \Zend_Measure_Length::INCH ? 'IN' : 'CM';
1493+
$deliveryConfirmation = $packageParams->getDeliveryConfirmation();
1494+
$customsTotal += $packageParams->getCustomsValue();
1495+
1496+
$packagePart[$packageId] = $shipmentPart->addChild('Package');
1497+
$packagePart[$packageId]->addChild('Description', $this->generateShipmentDescription($packageItems));
1498+
//empirical
1499+
$packagePart[$packageId]->addChild('PackagingType')->addChild('Code', $packagingType);
1500+
$packageWeight = $packagePart[$packageId]->addChild('PackageWeight');
1501+
$packageWeight->addChild('Weight', $weight);
1502+
$packageWeight->addChild('UnitOfMeasurement')->addChild('Code', $weightUnits);
1503+
1504+
// set dimensions
1505+
if ($length || $width || $height) {
1506+
$packageDimensions = $packagePart[$packageId]->addChild('Dimensions');
1507+
$packageDimensions->addChild('UnitOfMeasurement')->addChild('Code', $dimensionsUnits);
1508+
$packageDimensions->addChild('Length', $length);
1509+
$packageDimensions->addChild('Width', $width);
1510+
$packageDimensions->addChild('Height', $height);
15121511
}
1513-
$referencePart = $packagePart->addChild('ReferenceNumber');
1514-
$referencePart->addChild('Code', '02');
1515-
$referencePart->addChild('Value', $referenceData);
1516-
}
1517-
1518-
$deliveryConfirmation = $packageParams->getDeliveryConfirmation();
1519-
if ($deliveryConfirmation) {
1520-
/** @var $serviceOptionsNode Element */
1521-
$serviceOptionsNode = null;
1522-
switch ($this->_getDeliveryConfirmationLevel($request->getRecipientAddressCountryCode())) {
1523-
case self::DELIVERY_CONFIRMATION_PACKAGE:
1524-
$serviceOptionsNode = $packagePart->addChild('PackageServiceOptions');
1525-
break;
1526-
case self::DELIVERY_CONFIRMATION_SHIPMENT:
1527-
$serviceOptionsNode = $shipmentPart->addChild('ShipmentServiceOptions');
1528-
break;
1529-
default:
1530-
break;
1512+
1513+
// ups support reference number only for domestic service
1514+
if ($this->_isUSCountry($request->getRecipientAddressCountryCode())
1515+
&& $this->_isUSCountry($request->getShipperAddressCountryCode())
1516+
) {
1517+
if ($request->getReferenceData()) {
1518+
$referenceData = $request->getReferenceData() . $packageId;
1519+
} else {
1520+
$referenceData = 'Order #' .
1521+
$request->getOrderShipment()->getOrder()->getIncrementId() .
1522+
' P' .
1523+
$packageId;
1524+
}
1525+
$referencePart = $packagePart[$packageId]->addChild('ReferenceNumber');
1526+
$referencePart->addChild('Code', '02');
1527+
$referencePart->addChild('Value', $referenceData);
15311528
}
1532-
if ($serviceOptionsNode !== null) {
1533-
$serviceOptionsNode->addChild(
1534-
'DeliveryConfirmation'
1535-
)->addChild(
1536-
'DCISType',
1537-
$packageParams->getDeliveryConfirmation()
1538-
);
1529+
1530+
if ($deliveryConfirmation && $deliveryConfirmationLevel === self::DELIVERY_CONFIRMATION_PACKAGE) {
1531+
$serviceOptionsNode = $packagePart[$packageId]->addChild('PackageServiceOptions');
1532+
$serviceOptionsNode->addChild(
1533+
'DeliveryConfirmation'
1534+
)->addChild(
1535+
'DCISType',
1536+
$deliveryConfirmation
1537+
);
15391538
}
15401539
}
15411540

1541+
if (isset($deliveryConfirmation) && $deliveryConfirmationLevel === self::DELIVERY_CONFIRMATION_SHIPMENT) {
1542+
$serviceOptionsNode = $shipmentPart->addChild('ShipmentServiceOptions');
1543+
$serviceOptionsNode->addChild(
1544+
'DeliveryConfirmation'
1545+
)->addChild(
1546+
'DCISType',
1547+
$deliveryConfirmation
1548+
);
1549+
}
1550+
15421551
$shipmentPart->addChild('PaymentInformation')
15431552
->addChild('Prepaid')
15441553
->addChild('BillShipper')
15451554
->addChild('AccountNumber', $this->getConfigData('shipper_number'));
15461555

1547-
if ($request->getPackagingType() != $this->configHelper->getCode('container', 'ULE')
1556+
if (!in_array($this->configHelper->getCode('container', 'ULE'), $packagingTypes)
15481557
&& $request->getShipperAddressCountryCode() == self::USA_COUNTRY_ID
15491558
&& ($request->getRecipientAddressCountryCode() == 'CA'
15501559
|| $request->getRecipientAddressCountryCode() == 'PR')
15511560
) {
15521561
$invoiceLineTotalPart = $shipmentPart->addChild('InvoiceLineTotal');
15531562
$invoiceLineTotalPart->addChild('CurrencyCode', $request->getBaseCurrencyCode());
1554-
$invoiceLineTotalPart->addChild('MonetaryValue', ceil($packageParams->getCustomsValue()));
1563+
$invoiceLineTotalPart->addChild('MonetaryValue', ceil($customsTotal));
15551564
}
15561565

15571566
$labelPart = $xmlRequest->addChild('LabelSpecification');
@@ -1561,6 +1570,25 @@ protected function _formShipmentRequest(DataObject $request)
15611570
return $xmlRequest->asXml();
15621571
}
15631572

1573+
/**
1574+
* Generates shipment description.
1575+
*
1576+
* @param array $items
1577+
* @return string
1578+
*/
1579+
private function generateShipmentDescription(array $items): string
1580+
{
1581+
$itemsDesc = [];
1582+
$itemsShipment = $items;
1583+
foreach ($itemsShipment as $itemShipment) {
1584+
$item = new \Magento\Framework\DataObject();
1585+
$item->setData($itemShipment);
1586+
$itemsDesc[] = $item->getName();
1587+
}
1588+
1589+
return substr(implode(' ', $itemsDesc), 0, 35);
1590+
}
1591+
15641592
/**
15651593
* Send and process shipment accept request
15661594
*
@@ -1638,30 +1666,29 @@ public function getShipAcceptUrl()
16381666
/**
16391667
* Request quotes for given packages.
16401668
*
1641-
* @param DataObject[] $packages
1669+
* @param DataObject $request
16421670
* @return string[] Quote IDs.
16431671
* @throws LocalizedException
16441672
* @throws RuntimeException
16451673
*/
1646-
private function requestQuotes(array $packages): array
1674+
private function requestQuotes(DataObject $request): array
16471675
{
16481676
/** @var HttpResponseDeferredInterface[] $quotesRequests */
1649-
$quotesRequests = [];
16501677
//Getting quotes
1651-
foreach ($packages as $package) {
1652-
$this->_prepareShipmentRequest($package);
1653-
$rawXmlRequest = $this->_formShipmentRequest($package);
1654-
$this->setXMLAccessRequest();
1655-
$xmlRequest = $this->_xmlAccessRequest . $rawXmlRequest;
1656-
$quotesRequests[] = $this->asyncHttpClient->request(
1657-
new Request(
1658-
$this->getShipConfirmUrl(),
1659-
Request::METHOD_POST,
1660-
['Content-Type' => 'application/xml'],
1661-
$xmlRequest
1662-
)
1663-
);
1664-
}
1678+
$this->_prepareShipmentRequest($request);
1679+
$rawXmlRequest = $this->_formShipmentRequest($request);
1680+
$this->setXMLAccessRequest();
1681+
$xmlRequest = $this->_xmlAccessRequest . $rawXmlRequest;
1682+
$this->_debug(['request_quote' => $this->filterDebugData($this->_xmlAccessRequest) . $rawXmlRequest]);
1683+
$quotesRequests[] = $this->asyncHttpClient->request(
1684+
new Request(
1685+
$this->getShipConfirmUrl(),
1686+
Request::METHOD_POST,
1687+
['Content-Type' => 'application/xml'],
1688+
$xmlRequest
1689+
)
1690+
);
1691+
16651692
$ids = [];
16661693
//Processing quote responses
16671694
foreach ($quotesRequests as $quotesRequest) {
@@ -1672,6 +1699,7 @@ private function requestQuotes(array $packages): array
16721699
try {
16731700
/** @var Element $response */
16741701
$response = $this->_xmlElFactory->create(['data' => $httpResponse->getBody()]);
1702+
$this->_debug(['response_quote' => $response]);
16751703
} catch (Throwable $e) {
16761704
throw new RuntimeException($e->getMessage());
16771705
}
@@ -1708,6 +1736,12 @@ private function requestShipments(array $quoteIds): array
17081736
$request->addChild('RequestAction', 'ShipAccept');
17091737
$xmlRequest->addChild('ShipmentDigest', $quoteId);
17101738

1739+
$debugRequest = $this->filterDebugData($this->_xmlAccessRequest) . $xmlRequest->asXml();
1740+
$this->_debug(
1741+
[
1742+
'request_shipment' => $debugRequest
1743+
]
1744+
);
17111745
$shippingRequests[] = $this->asyncHttpClient->request(
17121746
new Request(
17131747
$this->getShipAcceptUrl(),
@@ -1721,27 +1755,30 @@ private function requestShipments(array $quoteIds): array
17211755
/** @var DataObject[] $results */
17221756
$results = [];
17231757
foreach ($shippingRequests as $shippingRequest) {
1724-
$result = new DataObject();
17251758
$httpResponse = $shippingRequest->get();
17261759
if ($httpResponse->getStatusCode() >= 400) {
17271760
throw new LocalizedException(__('Failed to send the package'));
17281761
}
17291762
try {
17301763
/** @var Element $response */
17311764
$response = $this->_xmlElFactory->create(['data' => $httpResponse->getBody()]);
1765+
$this->_debug(['response_shipment' => $response]);
17321766
} catch (Throwable $e) {
17331767
throw new RuntimeException($e->getMessage());
17341768
}
17351769
if (isset($response->Error)) {
17361770
throw new RuntimeException((string)$response->Error->ErrorDescription);
1737-
} else {
1738-
$shippingLabelContent = (string)$response->ShipmentResults->PackageResults->LabelImage->GraphicImage;
1739-
$trackingNumber = (string)$response->ShipmentResults->PackageResults->TrackingNumber;
1771+
}
1772+
1773+
foreach ($response->ShipmentResults->PackageResults as $packageResult) {
1774+
$result = new DataObject();
1775+
$shippingLabelContent = (string)$packageResult->LabelImage->GraphicImage;
1776+
$trackingNumber = (string)$packageResult->TrackingNumber;
17401777
// phpcs:ignore Magento2.Functions.DiscouragedFunction
17411778
$result->setLabelContent(base64_decode($shippingLabelContent));
17421779
$result->setTrackingNumber($trackingNumber);
1780+
$results[] = $result;
17431781
}
1744-
$results[] = $result;
17451782
}
17461783

17471784
return $results;
@@ -1841,25 +1878,16 @@ public function requestToShipment($request)
18411878
if ($request->getStoreId() != null) {
18421879
$this->setStore($request->getStoreId());
18431880
}
1844-
/** @var Shipment[] $packageRequests */
1845-
$packageRequests = [];
1846-
//Preparing packages info.
1847-
foreach ($packages as $packageId => $package) {
1848-
$request->setPackageId($packageId);
1849-
$request->setPackagingType($package['params']['container']);
1850-
$request->setPackageWeight($package['params']['weight']);
1851-
$request->setPackageParams(new DataObject($package['params']));
1852-
$request->setPackageItems($package['items']);
1853-
$packageRequests[] = clone $request;
1854-
}
18551881

18561882
// phpcs:disable
18571883
try {
1858-
$quoteIds = $this->requestQuotes($packageRequests);
1884+
$quoteIds = $this->requestQuotes($request);
18591885
$labels = $this->requestShipments($quoteIds);
18601886
} catch (LocalizedException $exception) {
1887+
$this->_logger->critical($exception);
18611888
return new DataObject(['errors' => [$exception->getMessage()]]);
18621889
} catch (RuntimeException $exception) {
1890+
$this->_logger->critical($exception);
18631891
return new DataObject(['errors' => __('Failed to send items')]);
18641892
}
18651893
// phpcs:enable

dev/tests/integration/framework/Magento/TestFramework/HTTP/AsyncClientInterfaceMock.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class AsyncClientInterfaceMock implements AsyncClientInterface
3333
*/
3434
private $lastRequest;
3535

36+
/**
37+
* @var Request[]
38+
*/
39+
private $requests = [];
40+
3641
/**
3742
* AsyncClientInterfaceMock constructor.
3843
* @param GuzzleAsyncClient $client
@@ -63,12 +68,34 @@ public function getLastRequest(): ?Request
6368
return $this->lastRequest;
6469
}
6570

71+
/**
72+
* Returns all requests made.
73+
*
74+
* @return Request[]|null
75+
*/
76+
public function getRequests(): array
77+
{
78+
return $this->requests;
79+
}
80+
81+
/**
82+
* Clear requests.
83+
*
84+
* @return void
85+
*/
86+
public function clearRequests()
87+
{
88+
$this->requests = [];
89+
$this->lastRequest = null;
90+
}
91+
6692
/**
6793
* @inheritDoc
6894
*/
6995
public function request(Request $request): HttpResponseDeferredInterface
7096
{
7197
$this->lastRequest = $request;
98+
$this->requests[] = $request;
7299
if ($mockResponse = array_shift($this->mockResponses)) {
73100
return new MockDeferredResponse($mockResponse);
74101
}

0 commit comments

Comments
 (0)