14
14
use Magento \Framework \Webapi \Soap \ClientFactory ;
15
15
use Magento \Framework \Xml \Security ;
16
16
use Magento \Quote \Model \Quote \Address \RateRequest ;
17
+ use Magento \Shipping \Model \Carrier \AbstractCarrier ;
17
18
use Magento \Shipping \Model \Carrier \AbstractCarrierOnline ;
18
19
use Magento \Shipping \Model \Rate \Result ;
19
20
@@ -31,21 +32,21 @@ class Carrier extends AbstractCarrierOnline implements \Magento\Shipping\Model\C
31
32
*
32
33
* @var string
33
34
*/
34
- const CODE = 'fedex ' ;
35
+ public const CODE = 'fedex ' ;
35
36
36
37
/**
37
38
* Purpose of rate request
38
39
*
39
40
* @var string
40
41
*/
41
- const RATE_REQUEST_GENERAL = 'general ' ;
42
+ public const RATE_REQUEST_GENERAL = 'general ' ;
42
43
43
44
/**
44
45
* Purpose of rate request
45
46
*
46
47
* @var string
47
48
*/
48
- const RATE_REQUEST_SMARTPOST = 'SMART_POST ' ;
49
+ public const RATE_REQUEST_SMARTPOST = 'SMART_POST ' ;
49
50
50
51
/**
51
52
* Code of the carrier
@@ -123,7 +124,7 @@ class Carrier extends AbstractCarrierOnline implements \Magento\Shipping\Model\C
123
124
protected $ _productCollectionFactory ;
124
125
125
126
/**
126
- * @inheritdoc
127
+ * @var string[]
127
128
*/
128
129
protected $ _debugReplacePrivateDataKeys = [
129
130
'Key ' , 'Password ' , 'MeterNumber ' ,
@@ -381,15 +382,16 @@ public function setRequest(RateRequest $request)
381
382
$ r ->setDestCity ($ request ->getDestCity ());
382
383
}
383
384
384
- $ weight = $ this ->getTotalNumOfBoxes ($ request ->getPackageWeight ());
385
- $ r ->setWeight ($ weight );
386
385
if ($ request ->getFreeMethodWeight () != $ request ->getPackageWeight ()) {
387
386
$ r ->setFreeMethodWeight ($ request ->getFreeMethodWeight ());
388
387
}
389
388
389
+ $ r ->setWeight ($ request ->getPackageWeight ());
390
390
$ r ->setValue ($ request ->getPackagePhysicalValue ());
391
391
$ r ->setValueWithDiscount ($ request ->getPackageValueWithDiscount ());
392
392
393
+ $ r ->setPackages ($ this ->createPackages ((float ) $ request ->getPackageWeight (), (array ) $ request ->getPackages ()));
394
+
393
395
$ r ->setMeterNumber ($ this ->getConfigData ('meter_number ' ));
394
396
$ r ->setKey ($ this ->getConfigData ('key ' ));
395
397
$ r ->setPassword ($ this ->getConfigData ('password ' ));
@@ -445,7 +447,6 @@ protected function _formRateRequest($purpose)
445
447
'DropoffType ' => $ r ->getDropoffType (),
446
448
'ShipTimestamp ' => date ('c ' ),
447
449
'PackagingType ' => $ r ->getPackaging (),
448
- 'TotalInsuredValue ' => ['Amount ' => $ r ->getValue (), 'Currency ' => $ this ->getCurrencyCode ()],
449
450
'Shipper ' => [
450
451
'Address ' => ['PostalCode ' => $ r ->getOrigPostal (), 'CountryCode ' => $ r ->getOrigCountry ()],
451
452
],
@@ -464,37 +465,36 @@ protected function _formRateRequest($purpose)
464
465
'CustomsValue ' => ['Amount ' => $ r ->getValue (), 'Currency ' => $ this ->getCurrencyCode ()],
465
466
],
466
467
'RateRequestTypes ' => 'LIST ' ,
467
- 'PackageCount ' => '1 ' ,
468
468
'PackageDetail ' => 'INDIVIDUAL_PACKAGES ' ,
469
- 'RequestedPackageLineItems ' => [
470
- '0 ' => [
471
- 'Weight ' => [
472
- 'Value ' => (double )$ r ->getWeight (),
473
- 'Units ' => $ this ->getConfigData ('unit_of_measure ' ),
474
- ],
475
- 'GroupPackageCount ' => 1 ,
476
- ],
477
- ],
478
469
],
479
470
];
480
471
472
+ foreach ($ r ->getPackages () as $ packageNum => $ package ) {
473
+ $ ratesRequest ['RequestedShipment ' ]['RequestedPackageLineItems ' ][$ packageNum ]['GroupPackageCount ' ] = 1 ;
474
+ $ ratesRequest ['RequestedShipment ' ]['RequestedPackageLineItems ' ][$ packageNum ]['Weight ' ]['Value ' ]
475
+ = (double ) $ package ['weight ' ];
476
+ $ ratesRequest ['RequestedShipment ' ]['RequestedPackageLineItems ' ][$ packageNum ]['Weight ' ]['Units ' ]
477
+ = $ this ->getConfigData ('unit_of_measure ' );
478
+ if (isset ($ package ['price ' ])) {
479
+ $ ratesRequest ['RequestedShipment ' ]['RequestedPackageLineItems ' ][$ packageNum ]['InsuredValue ' ]['Amount ' ]
480
+ = (double ) $ package ['price ' ];
481
+ $ ratesRequest ['RequestedShipment ' ]['RequestedPackageLineItems ' ][$ packageNum ]['InsuredValue ' ]['Currency ' ]
482
+ = $ this ->getCurrencyCode ();
483
+ }
484
+ }
485
+
486
+ $ ratesRequest ['RequestedShipment ' ]['PackageCount ' ] = count ($ r ->getPackages ());
487
+
481
488
if ($ r ->getDestCity ()) {
482
489
$ ratesRequest ['RequestedShipment ' ]['Recipient ' ]['Address ' ]['City ' ] = $ r ->getDestCity ();
483
490
}
484
491
485
- if ($ purpose == self ::RATE_REQUEST_GENERAL ) {
486
- $ ratesRequest ['RequestedShipment ' ]['RequestedPackageLineItems ' ][0 ]['InsuredValue ' ] = [
487
- 'Amount ' => $ r ->getValue (),
488
- 'Currency ' => $ this ->getCurrencyCode (),
492
+ if ($ purpose == self ::RATE_REQUEST_SMARTPOST ) {
493
+ $ ratesRequest ['RequestedShipment ' ]['ServiceType ' ] = self ::RATE_REQUEST_SMARTPOST ;
494
+ $ ratesRequest ['RequestedShipment ' ]['SmartPostDetail ' ] = [
495
+ 'Indicia ' => (double )$ r ->getWeight () >= 1 ? 'PARCEL_SELECT ' : 'PRESORTED_STANDARD ' ,
496
+ 'HubId ' => $ this ->getConfigData ('smartpost_hubid ' ),
489
497
];
490
- } else {
491
- if ($ purpose == self ::RATE_REQUEST_SMARTPOST ) {
492
- $ ratesRequest ['RequestedShipment ' ]['ServiceType ' ] = self ::RATE_REQUEST_SMARTPOST ;
493
- $ ratesRequest ['RequestedShipment ' ]['SmartPostDetail ' ] = [
494
- 'Indicia ' => (double )$ r ->getWeight () >= 1 ? 'PARCEL_SELECT ' : 'PRESORTED_STANDARD ' ,
495
- 'HubId ' => $ this ->getConfigData ('smartpost_hubid ' ),
496
- ];
497
- }
498
498
}
499
499
500
500
return $ ratesRequest ;
@@ -632,6 +632,40 @@ protected function _prepareRateResponse($response)
632
632
return $ result ;
633
633
}
634
634
635
+ /**
636
+ * Get final price for shipping method with handling fee per package
637
+ *
638
+ * @param float $cost
639
+ * @param string $handlingType
640
+ * @param float $handlingFee
641
+ * @return float
642
+ */
643
+ protected function _getPerpackagePrice ($ cost , $ handlingType , $ handlingFee )
644
+ {
645
+ if ($ handlingType == AbstractCarrier::HANDLING_TYPE_PERCENT ) {
646
+ return $ cost + $ cost * $ this ->_numBoxes * $ handlingFee / 100 ;
647
+ }
648
+
649
+ return $ cost + $ this ->_numBoxes * $ handlingFee ;
650
+ }
651
+
652
+ /**
653
+ * Get final price for shipping method with handling fee per order
654
+ *
655
+ * @param float $cost
656
+ * @param string $handlingType
657
+ * @param float $handlingFee
658
+ * @return float
659
+ */
660
+ protected function _getPerorderPrice ($ cost , $ handlingType , $ handlingFee )
661
+ {
662
+ if ($ handlingType == self ::HANDLING_TYPE_PERCENT ) {
663
+ return $ cost + $ cost * $ handlingFee / 100 ;
664
+ }
665
+
666
+ return $ cost + $ handlingFee ;
667
+ }
668
+
635
669
/**
636
670
* Get origin based amount form response of rate estimation
637
671
*
@@ -809,14 +843,6 @@ protected function _parseXmlResponse($response)
809
843
if (strlen (trim ($ response )) > 0 ) {
810
844
$ xml = $ this ->parseXml ($ response , \Magento \Shipping \Model \Simplexml \Element::class);
811
845
if (is_object ($ xml )) {
812
- if (is_object ($ xml ->Error ) && is_object ($ xml ->Error ->Message )) {
813
- $ errorTitle = (string )$ xml ->Error ->Message ;
814
- } elseif (is_object ($ xml ->SoftError ) && is_object ($ xml ->SoftError ->Message )) {
815
- $ errorTitle = (string )$ xml ->SoftError ->Message ;
816
- } else {
817
- $ errorTitle = 'Sorry, something went wrong. Please try again or contact us and we \'ll try to help. ' ;
818
- }
819
-
820
846
$ allowedMethods = explode (", " , $ this ->getConfigData ('allowed_methods ' ));
821
847
822
848
foreach ($ xml ->Entry as $ entry ) {
@@ -833,11 +859,7 @@ protected function _parseXmlResponse($response)
833
859
}
834
860
835
861
asort ($ priceArr );
836
- } else {
837
- $ errorTitle = 'Response is in the wrong format. ' ;
838
862
}
839
- } else {
840
- $ errorTitle = 'For some reason we can \'t retrieve tracking info right now. ' ;
841
863
}
842
864
843
865
$ result = $ this ->_rateFactory ->create ();
@@ -1212,6 +1234,7 @@ public function getResponse()
1212
1234
}
1213
1235
}
1214
1236
}
1237
+ // phpstan:ignore
1215
1238
if (empty ($ statuses )) {
1216
1239
$ statuses = __ ('Empty response ' );
1217
1240
}
@@ -1821,4 +1844,24 @@ private function getPaymentType(DataObject $request): string
1821
1844
? 'RECIPIENT '
1822
1845
: 'SENDER ' ;
1823
1846
}
1847
+
1848
+ /**
1849
+ * Creates packages for rate request.
1850
+ *
1851
+ * @param float $totalWeight
1852
+ * @param array $packages
1853
+ * @return array
1854
+ */
1855
+ private function createPackages (float $ totalWeight , array $ packages ): array
1856
+ {
1857
+ if (empty ($ packages )) {
1858
+ $ dividedWeight = $ this ->getTotalNumOfBoxes ($ totalWeight );
1859
+ for ($ i =0 ; $ i < $ this ->_numBoxes ; $ i ++) {
1860
+ $ packages [$ i ]['weight ' ] = $ dividedWeight ;
1861
+ }
1862
+ }
1863
+ $ this ->_numBoxes = count ($ packages );
1864
+
1865
+ return $ packages ;
1866
+ }
1824
1867
}
0 commit comments