17
17
18
18
/**
19
19
* Wrapper that performs Paypal Express and Checkout communication
20
- * Use current Paypal Express method instance
20
+ *
21
21
* @SuppressWarnings(PHPMD.TooManyFields)
22
22
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
23
23
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -26,6 +26,7 @@ class Checkout
26
26
{
27
27
/**
28
28
* Cache ID prefix for "pal" lookup
29
+ *
29
30
* @var string
30
31
*/
31
32
const PAL_CACHE_ID = 'paypal_express_checkout_pal ' ;
@@ -367,6 +368,7 @@ public function __construct(
367
368
368
369
/**
369
370
* Checkout with PayPal image URL getter
371
+ *
370
372
* Spares API calls of getting "pal" variable, by putting it into cache per store view
371
373
*
372
374
* @return string
@@ -599,8 +601,8 @@ public function canSkipOrderReviewStep()
599
601
600
602
/**
601
603
* Update quote when returned from PayPal
602
- * rewrite billing address by paypal
603
- * save old billing address for new customer
604
+ *
605
+ * Rewrite billing address by paypal, save old billing address for new customer, and
604
606
* export shipping address in case address absence
605
607
*
606
608
* @param string $token
@@ -616,14 +618,15 @@ public function returnFromPaypal($token)
616
618
617
619
$ this ->ignoreAddressValidation ();
618
620
621
+ // check if we came from the Express Checkout button
622
+ $ isButton = (bool )$ quote ->getPayment ()->getAdditionalInformation (self ::PAYMENT_INFO_BUTTON );
623
+
619
624
// import shipping address
620
625
$ exportedShippingAddress = $ this ->_getApi ()->getExportedShippingAddress ();
621
626
if (!$ quote ->getIsVirtual ()) {
622
627
$ shippingAddress = $ quote ->getShippingAddress ();
623
628
if ($ shippingAddress ) {
624
- if ($ exportedShippingAddress
625
- && $ quote ->getPayment ()->getAdditionalInformation (self ::PAYMENT_INFO_BUTTON ) == 1
626
- ) {
629
+ if ($ exportedShippingAddress && $ isButton ) {
627
630
$ this ->_setExportedAddressData ($ shippingAddress , $ exportedShippingAddress );
628
631
// PayPal doesn't provide detailed shipping info: prefix, middlename, lastname, suffix
629
632
$ shippingAddress ->setPrefix (null );
@@ -651,24 +654,29 @@ public function returnFromPaypal($token)
651
654
}
652
655
653
656
// import billing address
654
- $ portBillingFromShipping = $ quote ->getPayment ()->getAdditionalInformation (self ::PAYMENT_INFO_BUTTON ) == 1
655
- && $ this ->_config ->getValue (
656
- 'requireBillingAddress '
657
- ) != \Magento \Paypal \Model \Config::REQUIRE_BILLING_ADDRESS_ALL
658
- && !$ quote ->isVirtual ();
659
- if ($ portBillingFromShipping ) {
657
+ $ requireBillingAddress = (int )$ this ->_config ->getValue (
658
+ 'requireBillingAddress '
659
+ ) === \Magento \Paypal \Model \Config::REQUIRE_BILLING_ADDRESS_ALL ;
660
+
661
+ if ($ isButton && !$ requireBillingAddress && !$ quote ->isVirtual ()) {
660
662
$ billingAddress = clone $ shippingAddress ;
661
663
$ billingAddress ->unsAddressId ()->unsAddressType ()->setCustomerAddressId (null );
662
664
$ data = $ billingAddress ->getData ();
663
665
$ data ['save_in_address_book ' ] = 0 ;
664
666
$ quote ->getBillingAddress ()->addData ($ data );
665
667
$ quote ->getShippingAddress ()->setSameAsBilling (1 );
666
668
} else {
667
- $ billingAddress = $ quote ->getBillingAddress ();
669
+ $ billingAddress = $ quote ->getBillingAddress ()-> setCustomerAddressId ( null ) ;
668
670
}
669
671
$ exportedBillingAddress = $ this ->_getApi ()->getExportedBillingAddress ();
670
672
671
- $ this ->_setExportedAddressData ($ billingAddress , $ exportedBillingAddress );
673
+ // Since country is required field for billing and shipping address,
674
+ // we consider the address information to be empty if country is empty.
675
+ $ isEmptyAddress = ($ billingAddress ->getCountryId () === null );
676
+
677
+ if ($ requireBillingAddress || $ isEmptyAddress ) {
678
+ $ this ->_setExportedAddressData ($ billingAddress , $ exportedBillingAddress );
679
+ }
672
680
$ billingAddress ->setCustomerNote ($ exportedBillingAddress ->getData ('note ' ));
673
681
$ quote ->setBillingAddress ($ billingAddress );
674
682
$ quote ->setCheckoutMethod ($ this ->getCheckoutMethod ());
@@ -904,17 +912,6 @@ public function getCheckoutMethod()
904
912
*/
905
913
protected function _setExportedAddressData ($ address , $ exportedAddress )
906
914
{
907
- // Exported data is more priority if we came from Express Checkout button
908
- $ isButton = (bool )$ this ->_quote ->getPayment ()->getAdditionalInformation (self ::PAYMENT_INFO_BUTTON );
909
-
910
- // Since country is required field for billing and shipping address,
911
- // we consider the address information to be empty if country is empty.
912
- $ isEmptyAddress = ($ address ->getCountryId () === null );
913
-
914
- if (!$ isButton && !$ isEmptyAddress ) {
915
- return ;
916
- }
917
-
918
915
foreach ($ exportedAddress ->getExportedKeys () as $ key ) {
919
916
$ data = $ exportedAddress ->getData ($ key );
920
917
if (!empty ($ data )) {
@@ -951,6 +948,8 @@ protected function _setBillingAgreementRequest()
951
948
}
952
949
953
950
/**
951
+ * Get api
952
+ *
954
953
* @return \Magento\Paypal\Model\Api\Nvp
955
954
*/
956
955
protected function _getApi ()
@@ -963,8 +962,9 @@ protected function _getApi()
963
962
964
963
/**
965
964
* Attempt to collect address shipping rates and return them for further usage in instant update API
966
- * Returns empty array if it was impossible to obtain any shipping rate
967
- * If there are shipping rates obtained, the method must return one of them as default.
965
+ *
966
+ * Returns empty array if it was impossible to obtain any shipping rate and
967
+ * if there are shipping rates obtained, the method must return one of them as default.
968
968
*
969
969
* @param Address $address
970
970
* @param bool $mayReturnEmpty
@@ -1048,8 +1048,8 @@ protected function _prepareShippingOptions(Address $address, $mayReturnEmpty = f
1048
1048
* Compare two shipping options based on their amounts
1049
1049
*
1050
1050
* This function is used as a callback comparison function in shipping options sorting process
1051
- * @see self::_prepareShippingOptions()
1052
1051
*
1052
+ * @see self::_prepareShippingOptions()
1053
1053
* @param \Magento\Framework\DataObject $option1
1054
1054
* @param \Magento\Framework\DataObject $option2
1055
1055
* @return int
@@ -1064,6 +1064,7 @@ protected static function cmpShippingOptions(DataObject $option1, DataObject $op
1064
1064
1065
1065
/**
1066
1066
* Try to find whether the code provided by PayPal corresponds to any of possible shipping rates
1067
+ *
1067
1068
* This method was created only because PayPal has issues with returning the selected code.
1068
1069
* If in future the issue is fixed, we don't need to attempt to match it. It would be enough to set the method code
1069
1070
* before collecting shipping rates
@@ -1089,6 +1090,7 @@ protected function _matchShippingMethodCode(Address $address, $selectedCode)
1089
1090
1090
1091
/**
1091
1092
* Create payment redirect url
1093
+ *
1092
1094
* @param bool|null $button
1093
1095
* @param string $token
1094
1096
* @return void
@@ -1112,6 +1114,7 @@ public function getCustomerSession()
1112
1114
1113
1115
/**
1114
1116
* Set shipping options to api
1117
+ *
1115
1118
* @param \Magento\Paypal\Model\Cart $cart
1116
1119
* @param \Magento\Quote\Model\Quote\Address|null $address
1117
1120
* @return void
0 commit comments