@@ -1361,20 +1361,11 @@ public function getAllowedMethods()
1361
1361
*/
1362
1362
protected function _formShipmentRequest (DataObject $ request )
1363
1363
{
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 ' ];
1377
1367
}
1368
+ $ shipmentItems = array_merge (...$ shipmentItems );
1378
1369
1379
1370
$ xmlRequest = $ this ->_xmlElFactory ->create (
1380
1371
['data ' => '<?xml version = "1.0" ?><ShipmentConfirmRequest xml:lang="en-US"/> ' ]
@@ -1389,7 +1380,7 @@ protected function _formShipmentRequest(DataObject $request)
1389
1380
// UPS Print Return Label
1390
1381
$ returnPart ->addChild ('Code ' , '9 ' );
1391
1382
}
1392
- $ shipmentPart ->addChild ('Description ' , substr ( implode ( ' ' , $ itemsDesc ), 0 , 35 ));
1383
+ $ shipmentPart ->addChild ('Description ' , $ this -> generateShipmentDescription ( $ shipmentItems ));
1393
1384
//empirical
1394
1385
1395
1386
$ shipperPart = $ shipmentPart ->addChild ('Shipper ' );
@@ -1481,77 +1472,95 @@ protected function _formShipmentRequest(DataObject $request)
1481
1472
1482
1473
$ servicePart = $ shipmentPart ->addChild ('Service ' );
1483
1474
$ 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 );
1512
1511
}
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 );
1531
1528
}
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
+ );
1539
1538
}
1540
1539
}
1541
1540
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
+
1542
1551
$ shipmentPart ->addChild ('PaymentInformation ' )
1543
1552
->addChild ('Prepaid ' )
1544
1553
->addChild ('BillShipper ' )
1545
1554
->addChild ('AccountNumber ' , $ this ->getConfigData ('shipper_number ' ));
1546
1555
1547
- if ($ request -> getPackagingType () != $ this ->configHelper ->getCode ('container ' , 'ULE ' )
1556
+ if (! in_array ( $ this ->configHelper ->getCode ('container ' , 'ULE ' ), $ packagingTypes )
1548
1557
&& $ request ->getShipperAddressCountryCode () == self ::USA_COUNTRY_ID
1549
1558
&& ($ request ->getRecipientAddressCountryCode () == 'CA '
1550
1559
|| $ request ->getRecipientAddressCountryCode () == 'PR ' )
1551
1560
) {
1552
1561
$ invoiceLineTotalPart = $ shipmentPart ->addChild ('InvoiceLineTotal ' );
1553
1562
$ invoiceLineTotalPart ->addChild ('CurrencyCode ' , $ request ->getBaseCurrencyCode ());
1554
- $ invoiceLineTotalPart ->addChild ('MonetaryValue ' , ceil ($ packageParams -> getCustomsValue () ));
1563
+ $ invoiceLineTotalPart ->addChild ('MonetaryValue ' , ceil ($ customsTotal ));
1555
1564
}
1556
1565
1557
1566
$ labelPart = $ xmlRequest ->addChild ('LabelSpecification ' );
@@ -1561,6 +1570,25 @@ protected function _formShipmentRequest(DataObject $request)
1561
1570
return $ xmlRequest ->asXml ();
1562
1571
}
1563
1572
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
+
1564
1592
/**
1565
1593
* Send and process shipment accept request
1566
1594
*
@@ -1638,30 +1666,29 @@ public function getShipAcceptUrl()
1638
1666
/**
1639
1667
* Request quotes for given packages.
1640
1668
*
1641
- * @param DataObject[] $packages
1669
+ * @param DataObject $request
1642
1670
* @return string[] Quote IDs.
1643
1671
* @throws LocalizedException
1644
1672
* @throws RuntimeException
1645
1673
*/
1646
- private function requestQuotes (array $ packages ): array
1674
+ private function requestQuotes (DataObject $ request ): array
1647
1675
{
1648
1676
/** @var HttpResponseDeferredInterface[] $quotesRequests */
1649
- $ quotesRequests = [];
1650
1677
//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
+
1665
1692
$ ids = [];
1666
1693
//Processing quote responses
1667
1694
foreach ($ quotesRequests as $ quotesRequest ) {
@@ -1672,6 +1699,7 @@ private function requestQuotes(array $packages): array
1672
1699
try {
1673
1700
/** @var Element $response */
1674
1701
$ response = $ this ->_xmlElFactory ->create (['data ' => $ httpResponse ->getBody ()]);
1702
+ $ this ->_debug (['response_quote ' => $ response ]);
1675
1703
} catch (Throwable $ e ) {
1676
1704
throw new RuntimeException ($ e ->getMessage ());
1677
1705
}
@@ -1708,6 +1736,12 @@ private function requestShipments(array $quoteIds): array
1708
1736
$ request ->addChild ('RequestAction ' , 'ShipAccept ' );
1709
1737
$ xmlRequest ->addChild ('ShipmentDigest ' , $ quoteId );
1710
1738
1739
+ $ debugRequest = $ this ->filterDebugData ($ this ->_xmlAccessRequest ) . $ xmlRequest ->asXml ();
1740
+ $ this ->_debug (
1741
+ [
1742
+ 'request_shipment ' => $ debugRequest
1743
+ ]
1744
+ );
1711
1745
$ shippingRequests [] = $ this ->asyncHttpClient ->request (
1712
1746
new Request (
1713
1747
$ this ->getShipAcceptUrl (),
@@ -1721,27 +1755,30 @@ private function requestShipments(array $quoteIds): array
1721
1755
/** @var DataObject[] $results */
1722
1756
$ results = [];
1723
1757
foreach ($ shippingRequests as $ shippingRequest ) {
1724
- $ result = new DataObject ();
1725
1758
$ httpResponse = $ shippingRequest ->get ();
1726
1759
if ($ httpResponse ->getStatusCode () >= 400 ) {
1727
1760
throw new LocalizedException (__ ('Failed to send the package ' ));
1728
1761
}
1729
1762
try {
1730
1763
/** @var Element $response */
1731
1764
$ response = $ this ->_xmlElFactory ->create (['data ' => $ httpResponse ->getBody ()]);
1765
+ $ this ->_debug (['response_shipment ' => $ response ]);
1732
1766
} catch (Throwable $ e ) {
1733
1767
throw new RuntimeException ($ e ->getMessage ());
1734
1768
}
1735
1769
if (isset ($ response ->Error )) {
1736
1770
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 ;
1740
1777
// phpcs:ignore Magento2.Functions.DiscouragedFunction
1741
1778
$ result ->setLabelContent (base64_decode ($ shippingLabelContent ));
1742
1779
$ result ->setTrackingNumber ($ trackingNumber );
1780
+ $ results [] = $ result ;
1743
1781
}
1744
- $ results [] = $ result ;
1745
1782
}
1746
1783
1747
1784
return $ results ;
@@ -1841,25 +1878,16 @@ public function requestToShipment($request)
1841
1878
if ($ request ->getStoreId () != null ) {
1842
1879
$ this ->setStore ($ request ->getStoreId ());
1843
1880
}
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
- }
1855
1881
1856
1882
// phpcs:disable
1857
1883
try {
1858
- $ quoteIds = $ this ->requestQuotes ($ packageRequests );
1884
+ $ quoteIds = $ this ->requestQuotes ($ request );
1859
1885
$ labels = $ this ->requestShipments ($ quoteIds );
1860
1886
} catch (LocalizedException $ exception ) {
1887
+ $ this ->_logger ->critical ($ exception );
1861
1888
return new DataObject (['errors ' => [$ exception ->getMessage ()]]);
1862
1889
} catch (RuntimeException $ exception ) {
1890
+ $ this ->_logger ->critical ($ exception );
1863
1891
return new DataObject (['errors ' => __ ('Failed to send items ' )]);
1864
1892
}
1865
1893
// phpcs:enable
0 commit comments