1717
1818/**
1919 * Wrapper that performs Paypal Express and Checkout communication
20- * Use current Paypal Express method instance
20+ *
2121 * @SuppressWarnings(PHPMD.TooManyFields)
2222 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2323 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -26,6 +26,7 @@ class Checkout
2626{
2727 /**
2828 * Cache ID prefix for "pal" lookup
29+ *
2930 * @var string
3031 */
3132 const PAL_CACHE_ID = 'paypal_express_checkout_pal ' ;
@@ -367,6 +368,7 @@ public function __construct(
367368
368369 /**
369370 * Checkout with PayPal image URL getter
371+ *
370372 * Spares API calls of getting "pal" variable, by putting it into cache per store view
371373 *
372374 * @return string
@@ -599,8 +601,8 @@ public function canSkipOrderReviewStep()
599601
600602 /**
601603 * 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
604606 * export shipping address in case address absence
605607 *
606608 * @param string $token
@@ -616,14 +618,15 @@ public function returnFromPaypal($token)
616618
617619 $ this ->ignoreAddressValidation ();
618620
621+ // check if we came from the Express Checkout button
622+ $ isButton = (bool )$ quote ->getPayment ()->getAdditionalInformation (self ::PAYMENT_INFO_BUTTON );
623+
619624 // import shipping address
620625 $ exportedShippingAddress = $ this ->_getApi ()->getExportedShippingAddress ();
621626 if (!$ quote ->getIsVirtual ()) {
622627 $ shippingAddress = $ quote ->getShippingAddress ();
623628 if ($ shippingAddress ) {
624- if ($ exportedShippingAddress
625- && $ quote ->getPayment ()->getAdditionalInformation (self ::PAYMENT_INFO_BUTTON ) == 1
626- ) {
629+ if ($ exportedShippingAddress && $ isButton ) {
627630 $ this ->_setExportedAddressData ($ shippingAddress , $ exportedShippingAddress );
628631 // PayPal doesn't provide detailed shipping info: prefix, middlename, lastname, suffix
629632 $ shippingAddress ->setPrefix (null );
@@ -651,24 +654,29 @@ public function returnFromPaypal($token)
651654 }
652655
653656 // 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 ()) {
660662 $ billingAddress = clone $ shippingAddress ;
661663 $ billingAddress ->unsAddressId ()->unsAddressType ()->setCustomerAddressId (null );
662664 $ data = $ billingAddress ->getData ();
663665 $ data ['save_in_address_book ' ] = 0 ;
664666 $ quote ->getBillingAddress ()->addData ($ data );
665667 $ quote ->getShippingAddress ()->setSameAsBilling (1 );
666668 } else {
667- $ billingAddress = $ quote ->getBillingAddress ();
669+ $ billingAddress = $ quote ->getBillingAddress ()-> setCustomerAddressId ( null ) ;
668670 }
669671 $ exportedBillingAddress = $ this ->_getApi ()->getExportedBillingAddress ();
670672
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+ }
672680 $ billingAddress ->setCustomerNote ($ exportedBillingAddress ->getData ('note ' ));
673681 $ quote ->setBillingAddress ($ billingAddress );
674682 $ quote ->setCheckoutMethod ($ this ->getCheckoutMethod ());
@@ -904,17 +912,6 @@ public function getCheckoutMethod()
904912 */
905913 protected function _setExportedAddressData ($ address , $ exportedAddress )
906914 {
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-
918915 foreach ($ exportedAddress ->getExportedKeys () as $ key ) {
919916 $ data = $ exportedAddress ->getData ($ key );
920917 if (!empty ($ data )) {
@@ -951,6 +948,8 @@ protected function _setBillingAgreementRequest()
951948 }
952949
953950 /**
951+ * Get api
952+ *
954953 * @return \Magento\Paypal\Model\Api\Nvp
955954 */
956955 protected function _getApi ()
@@ -963,8 +962,9 @@ protected function _getApi()
963962
964963 /**
965964 * 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.
968968 *
969969 * @param Address $address
970970 * @param bool $mayReturnEmpty
@@ -1048,8 +1048,8 @@ protected function _prepareShippingOptions(Address $address, $mayReturnEmpty = f
10481048 * Compare two shipping options based on their amounts
10491049 *
10501050 * This function is used as a callback comparison function in shipping options sorting process
1051- * @see self::_prepareShippingOptions()
10521051 *
1052+ * @see self::_prepareShippingOptions()
10531053 * @param \Magento\Framework\DataObject $option1
10541054 * @param \Magento\Framework\DataObject $option2
10551055 * @return int
@@ -1064,6 +1064,7 @@ protected static function cmpShippingOptions(DataObject $option1, DataObject $op
10641064
10651065 /**
10661066 * Try to find whether the code provided by PayPal corresponds to any of possible shipping rates
1067+ *
10671068 * This method was created only because PayPal has issues with returning the selected code.
10681069 * 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
10691070 * before collecting shipping rates
@@ -1089,6 +1090,7 @@ protected function _matchShippingMethodCode(Address $address, $selectedCode)
10891090
10901091 /**
10911092 * Create payment redirect url
1093+ *
10921094 * @param bool|null $button
10931095 * @param string $token
10941096 * @return void
@@ -1112,6 +1114,7 @@ public function getCustomerSession()
11121114
11131115 /**
11141116 * Set shipping options to api
1117+ *
11151118 * @param \Magento\Paypal\Model\Cart $cart
11161119 * @param \Magento\Quote\Model\Quote\Address|null $address
11171120 * @return void
0 commit comments