719-286-0751 [email protected]

Magento 2 – Fix Braintree PayPal Express Incorrect Shipping Address

Using Magento 2.1.x with the Braintree PayPal Express feature can cause a glitch with the user’s shipping address. The result of the bug is:

  • User navigates to PayPal Express from the Cart Page
  • User selects a shipping address associated to their PayPal Account
  • Upon returning to Magento, the State Code (ie, “CO”, “CA”, “GA”) is incorrect on the address.

Most customers won’t catch this (they’ll assume you correctly received the address from PayPal Express), so it can cause quite a bit of customer service hassle.

So, how does the bug work? Let’s look at what causes it:

  1. User navigates to cart page and estimates shipping for the wrong state (or user’s browser autofills the estimated shipping state field)
  2. User then clicks “Pay with PayPal Express”
  3. User goes to PayPal selects a shipping address with a different state
  4. When they return, the Braintree QuoteUpdater class does not correctly update the shipping address information to override the incorrect state they estimated for.

So, what’s the fix? The fix is centered around fixing this method:

Magento_Braintree/Model/Paypal/Helper/QuoteUpdater::updateAddressData

We need to update the function to look like this:


/**
     * @mod Ensure the correct region is getting set upon a return from PayPal
     * Sets address data from exported address
     *
     * @param Address $address
     * @param array $addressData
     * @return void
     */
    private function updateAddressData(Address $address, array $addressData)
    {
        $extendedAddress = isset($addressData['extendedAddress'])
            ? $addressData['extendedAddress']
            : null;

        $address->setStreet([$addressData['streetAddress'], $extendedAddress]);
        $address->setCity($addressData['locality']);
        $address->setRegionCode($addressData['region']);
        $address->setCountryId($addressData['countryCodeAlpha2']);
        $address->setPostcode($addressData['postalCode']);

        if (isset($addressData['region']) && $address->getCountryId()) {
            $regionModel = $this->regionFactory->create()->loadByCode(
                $addressData['region'],
                $address->getCountryId()
            );

            if ($regionModel->getId()) {
                $address->setRegionId($regionModel->getId());
            } else {
                $address->setRegion($address->getRegionCode());
            }
        }
    }

You’ll notice our override will ensure the region_id field gets correctly set (which is what was missing).

To quickly apply this fix, please follow the below instructions which use our git repository to install the fix as an extension:


mkdir -p app/code/Cadence/BraintreePaypalFix 
git clone https://github.com/cadencelabs/m2-braintree-paypalexpress-fix app/code/Cadence/BraintreePaypalFix
bin/magento setup:upgrade 

That’s it! You should see the fix applied.

Alan Barber is the Lead Web Developer at Cadence Labs and a Magento Certified developer.

Need help with Magento 2?

We’ve had a lot of experience fixing problems with Magento 2. If you need help, head over to the Cadence Labs contact page, or email us at [email protected]. We offer affordable rates for our Magento development services. 

Submit a Comment

Your email address will not be published. Required fields are marked *

Install our webapp on your iPhone! Tap and then Add to homescreen.
Share This