Skip to content

Commit

Permalink
Merge pull request #3170 from magento-borg/BugFixPR
Browse files Browse the repository at this point in the history
[2.3.0-regression] Bug fixes
  • Loading branch information
cpartica authored Sep 17, 2018
2 parents 0c9d6d1 + ad2d79d commit 22a2e2c
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 55 deletions.
4 changes: 4 additions & 0 deletions app/code/Magento/Braintree/Model/Ui/PayPal/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function __construct(Config $config, ResolverInterface $resolver)
*/
public function getConfig()
{
$requireBillingAddressAll = \Magento\Paypal\Model\Config::REQUIRE_BILLING_ADDRESS_ALL;

return [
'payment' => [
self::PAYPAL_CODE => [
Expand All @@ -60,6 +62,8 @@ public function getConfig()
'vaultCode' => self::PAYPAL_VAULT_CODE,
'skipOrderReview' => $this->config->isSkipOrderReview(),
'paymentIcon' => $this->config->getPayPalIcon(),
'isRequiredBillingAddress' =>
(int)$this->config->isRequiredBillingAddress() === $requireBillingAddressAll
]
]
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public function testGetConfig($expected)
'width' => 30, 'height' => 26, 'url' => 'https://icon.test.url'
]);

$this->config->method('isRequiredBillingAddress')
->willReturn(1);

self::assertEquals($expected, $this->configProvider->getConfig());
}

Expand All @@ -101,7 +104,8 @@ public function getConfigDataProvider()
'skipOrderReview' => false,
'paymentIcon' => [
'width' => 30, 'height' => 26, 'url' => 'https://icon.test.url'
]
],
'isRequiredBillingAddress' => true
]
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ define([
beforePlaceOrder: function (data) {
this.setPaymentMethodNonce(data.nonce);

if (quote.billingAddress() === null && typeof data.details.billingAddress !== 'undefined') {
if ((this.isRequiredBillingAddress() || quote.billingAddress() === null) &&
typeof data.details.billingAddress !== 'undefined'
) {
this.setBillingAddress(data.details, data.details.billingAddress);
}

Expand Down Expand Up @@ -264,6 +266,14 @@ define([
return window.checkoutConfig.payment[this.getCode()].isAllowShippingAddressOverride;
},

/**
* Is billing address required from PayPal side
* @returns {Boolean}
*/
isRequiredBillingAddress: function () {
return window.checkoutConfig.payment[this.getCode()].isRequiredBillingAddress;
},

/**
* Get configuration for PayPal
* @returns {Object}
Expand Down
59 changes: 31 additions & 28 deletions app/code/Magento/Paypal/Model/Express/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* Wrapper that performs Paypal Express and Checkout communication
* Use current Paypal Express method instance
*
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand All @@ -26,6 +26,7 @@ class Checkout
{
/**
* Cache ID prefix for "pal" lookup
*
* @var string
*/
const PAL_CACHE_ID = 'paypal_express_checkout_pal';
Expand Down Expand Up @@ -367,6 +368,7 @@ public function __construct(

/**
* Checkout with PayPal image URL getter
*
* Spares API calls of getting "pal" variable, by putting it into cache per store view
*
* @return string
Expand Down Expand Up @@ -599,8 +601,8 @@ public function canSkipOrderReviewStep()

/**
* Update quote when returned from PayPal
* rewrite billing address by paypal
* save old billing address for new customer
*
* Rewrite billing address by paypal, save old billing address for new customer, and
* export shipping address in case address absence
*
* @param string $token
Expand All @@ -616,14 +618,15 @@ public function returnFromPaypal($token)

$this->ignoreAddressValidation();

// check if we came from the Express Checkout button
$isButton = (bool)$quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_BUTTON);

// import shipping address
$exportedShippingAddress = $this->_getApi()->getExportedShippingAddress();
if (!$quote->getIsVirtual()) {
$shippingAddress = $quote->getShippingAddress();
if ($shippingAddress) {
if ($exportedShippingAddress
&& $quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_BUTTON) == 1
) {
if ($exportedShippingAddress && $isButton) {
$this->_setExportedAddressData($shippingAddress, $exportedShippingAddress);
// PayPal doesn't provide detailed shipping info: prefix, middlename, lastname, suffix
$shippingAddress->setPrefix(null);
Expand Down Expand Up @@ -651,24 +654,29 @@ public function returnFromPaypal($token)
}

// import billing address
$portBillingFromShipping = $quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_BUTTON) == 1
&& $this->_config->getValue(
'requireBillingAddress'
) != \Magento\Paypal\Model\Config::REQUIRE_BILLING_ADDRESS_ALL
&& !$quote->isVirtual();
if ($portBillingFromShipping) {
$requireBillingAddress = (int)$this->_config->getValue(
'requireBillingAddress'
) === \Magento\Paypal\Model\Config::REQUIRE_BILLING_ADDRESS_ALL;

if ($isButton && !$requireBillingAddress && !$quote->isVirtual()) {
$billingAddress = clone $shippingAddress;
$billingAddress->unsAddressId()->unsAddressType()->setCustomerAddressId(null);
$data = $billingAddress->getData();
$data['save_in_address_book'] = 0;
$quote->getBillingAddress()->addData($data);
$quote->getShippingAddress()->setSameAsBilling(1);
} else {
$billingAddress = $quote->getBillingAddress();
$billingAddress = $quote->getBillingAddress()->setCustomerAddressId(null);
}
$exportedBillingAddress = $this->_getApi()->getExportedBillingAddress();

$this->_setExportedAddressData($billingAddress, $exportedBillingAddress);
// Since country is required field for billing and shipping address,
// we consider the address information to be empty if country is empty.
$isEmptyAddress = ($billingAddress->getCountryId() === null);

if ($requireBillingAddress || $isEmptyAddress) {
$this->_setExportedAddressData($billingAddress, $exportedBillingAddress);
}
$billingAddress->setCustomerNote($exportedBillingAddress->getData('note'));
$quote->setBillingAddress($billingAddress);
$quote->setCheckoutMethod($this->getCheckoutMethod());
Expand Down Expand Up @@ -904,17 +912,6 @@ public function getCheckoutMethod()
*/
protected function _setExportedAddressData($address, $exportedAddress)
{
// Exported data is more priority if we came from Express Checkout button
$isButton = (bool)$this->_quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_BUTTON);

// Since country is required field for billing and shipping address,
// we consider the address information to be empty if country is empty.
$isEmptyAddress = ($address->getCountryId() === null);

if (!$isButton && !$isEmptyAddress) {
return;
}

foreach ($exportedAddress->getExportedKeys() as $key) {
$data = $exportedAddress->getData($key);
if (!empty($data)) {
Expand Down Expand Up @@ -951,6 +948,8 @@ protected function _setBillingAgreementRequest()
}

/**
* Get api
*
* @return \Magento\Paypal\Model\Api\Nvp
*/
protected function _getApi()
Expand All @@ -963,8 +962,9 @@ protected function _getApi()

/**
* Attempt to collect address shipping rates and return them for further usage in instant update API
* Returns empty array if it was impossible to obtain any shipping rate
* If there are shipping rates obtained, the method must return one of them as default.
*
* Returns empty array if it was impossible to obtain any shipping rate and
* if there are shipping rates obtained, the method must return one of them as default.
*
* @param Address $address
* @param bool $mayReturnEmpty
Expand Down Expand Up @@ -1048,8 +1048,8 @@ protected function _prepareShippingOptions(Address $address, $mayReturnEmpty = f
* Compare two shipping options based on their amounts
*
* This function is used as a callback comparison function in shipping options sorting process
* @see self::_prepareShippingOptions()
*
* @see self::_prepareShippingOptions()
* @param \Magento\Framework\DataObject $option1
* @param \Magento\Framework\DataObject $option2
* @return int
Expand All @@ -1064,6 +1064,7 @@ protected static function cmpShippingOptions(DataObject $option1, DataObject $op

/**
* Try to find whether the code provided by PayPal corresponds to any of possible shipping rates
*
* This method was created only because PayPal has issues with returning the selected code.
* 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
* before collecting shipping rates
Expand All @@ -1089,6 +1090,7 @@ protected function _matchShippingMethodCode(Address $address, $selectedCode)

/**
* Create payment redirect url
*
* @param bool|null $button
* @param string $token
* @return void
Expand All @@ -1112,6 +1114,7 @@ public function getCustomerSession()

/**
* Set shipping options to api
*
* @param \Magento\Paypal\Model\Cart $cart
* @param \Magento\Quote\Model\Quote\Address|null $address
* @return void
Expand Down
Loading

0 comments on commit 22a2e2c

Please sign in to comment.