craftcms / commerce-paypal-checkout

PayPal Checkout gateway for Craft Commerce.
https://plugins.craftcms.com/commerce-paypal-checkout
MIT License
5 stars 10 forks source link

PayPal error: Shipping city required #76

Closed joostwaaijer closed 1 year ago

joostwaaijer commented 1 year ago

Description Paypal checkout is returning the following error when trying to buy from United Arab Emirates:

{
    "name": "UNPROCESSABLE_ENTITY",
    "details": [{
        "field": "/purchase_units/@reference_id=='default'/shipping/address/admin_area_2",
        "issue": "CITY_REQUIRED",
        "description": "The specified country requires a city (address.admin_area_2)"
    }],
    "message": "The requested action could not be performed, semantically incorrect, or failed business validation.",
    "debug_id": "c41446ace65eb",
    "links": [{
        "href": "https://developer.paypal.com/docs/api/orders/v2/#error-CITY_REQUIRED",
        "rel": "information_link",
        "method": "GET"
    }]
}

The city is saved in the craft commerce systems as locality, but when checking the address forms for the United Arab Emirates, there is no possible way to set the locality, see image:

image

To Reproduce How, and what happened?

  1. Add items to cart
  2. Add billing and shipping address info from United Arab Emirates
  3. Go to checkout
  4. Receive the error

Expected behavior Orders from United Arab Emirates should be able to checkout.

Additional info

lukeholder commented 1 year ago

You are correct that the "Locality" field is not defined as a "used field" by our Google-supplied address data for the United Arab Emirates, but it looks like Paypal requires it.

To manually add it to the "used fields" data for that Country, please add the following to your project inside a module:

Event::on(
    \craft\services\Addresses::class,
    \craft\services\Addresses::EVENT_DEFINE_USED_FIELDS,
    function (\craft\events\DefineAddressFieldsEvent $event) {
        if($event->countryCode == 'AE'){
            $fields = array_merge(['locality'],$event->fields);
            $event->fields = $fields;
        }
    }
);

This will allow the field to be added to addresses for that country in the CP and the front-end.

Hope that helps.