diff --git a/app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/paypal.js b/app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/paypal.js index 9804ee8489625..253f3530701bc 100644 --- a/app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/paypal.js +++ b/app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/paypal.js @@ -7,6 +7,7 @@ define([ 'jquery', 'underscore', + 'mage/utils/wrapper', 'Magento_Checkout/js/view/payment/default', 'Magento_Braintree/js/view/payment/adapter', 'Magento_Checkout/js/model/quote', @@ -18,6 +19,7 @@ define([ ], function ( $, _, + wrapper, Component, Braintree, quote, @@ -218,8 +220,9 @@ define([ /** * Re-init PayPal Auth Flow + * @param {Function} callback - Optional callback */ - reInitPayPal: function () { + reInitPayPal: function (callback) { if (Braintree.checkout) { Braintree.checkout.teardown(function () { Braintree.checkout = null; @@ -228,6 +231,18 @@ define([ this.disableButton(); this.clientConfig.paypal.amount = this.grandTotalAmount; + this.clientConfig.paypal.shippingAddressOverride = this.getShippingAddress(); + + if (callback) { + this.clientConfig.onReady = wrapper.wrap( + this.clientConfig.onReady, + function (original, checkout) { + this.clientConfig.onReady = original; + original(checkout); + callback(); + }.bind(this) + ); + } Braintree.setConfig(this.clientConfig); Braintree.setup(); @@ -404,7 +419,11 @@ define([ * Triggers when customer click "Continue to PayPal" button */ payWithPayPal: function () { - if (additionalValidators.validate()) { + this.reInitPayPal(function () { + if (!additionalValidators.validate()) { + return; + } + try { Braintree.checkout.paypal.initAuthFlow(); } catch (e) { @@ -412,7 +431,7 @@ define([ message: $t('Payment ' + this.getTitle() + ' can\'t be initialized.') }); } - } + }.bind(this)); }, /** diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Braintree/frontend/js/view/payment/method-renderer/paypal.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Braintree/frontend/js/view/payment/method-renderer/paypal.test.js index a9987f5e01ba8..bcfaecd277754 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Braintree/frontend/js/view/payment/method-renderer/paypal.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Braintree/frontend/js/view/payment/method-renderer/paypal.test.js @@ -27,7 +27,24 @@ define([ }) }, 'Magento_Braintree/js/view/payment/adapter': { + config: {}, + + /** Stub */ + onReady: function () {}, + + /** Stub */ + setConfig: function (config) { + this.config = config; + }, + + /** Stub */ + setup: function () { + this.config.onReady(this.checkout); + }, + checkout: { + /** Stub */ + teardown: function () {}, paypal: { /** Stub */ initAuthFlow: function () {}