API - Change currency when shipping country changes

gavwvin
Contributor
Contributor

Using the Javascript SDK I'm using a button to initiate the payment process. 

For various reasons we want the user to pay in the currency that matches the delivery country (e.g., UK orders pay in GBP, US orders pay in USD etc). Because they can change the country in the paypal flow, I am trying to patch the order to change the currency when they change the shipping country.

I am using onShippingChange to make a call to a page on our server to calculate the new shipping amount, and new currency for the new location, then using orders.patch to update the currency and shipping amount in the paypal flow:

paypal.Buttons({
        // Set up the transaction
        createOrder: function(data, actions) {
...
        },
        //update shipping
        onShippingChange: function(data, actions){
          // get country
          var country = data.shipping_address.country_code;
          var params = { country: country, id: currentOrder.id };
          callPageOnOurServerToGetNewShippingAndCurrency(params).then(function(response){
            var newAmount = response.paypal.purchase_units[0].amount; //this contains the full breakdown of new amounts, with new currency code
            actions.order.patch([
              {
                op: 'replace',
                path: '/purchase_units/@reference_id==\'default\'/amount',
                value: newAmount
              }
            ]);
          }).catch(function(response){
...
          });
        },
        // Finalize the transaction
        onApprove: function(data, actions) {
...
        }
    }).render(containerId);

I get the following error:

"Error: Status code: 422 {
"name": "UNPROCESSABLE_ENTITY",
"details": [
{
"location": "body",
"issue": "CANNOT_MIX_CURRENCIES",
"description": "CANNOT_MIX_CURRENCIES"
}
],
"message": "The requested action could not be performed, semantically incorrect, or failed business validation.",
"debug_id": "f932417d2a8dd",
"links": [
{
"href": "https://developer.paypal.com/docs/api/orders/v2/#error-CANNOT_MIX_CURRENCIES",
"rel": "information_link",
"method": "GET"
}
]
}"



Here is the value passed to createOrder:


{
"application_context": {
"brand_name": "My Business Name",
"locale": "en-GB",
"landing_page": "NO_PREFERENCE",
"shipping_preference": "GET_FROM_FILE",
"user_action": "PAY_NOW"
},
"purchase_units": [
{
"invoice_id": 14848,
"amount": {
"currency_code": "USD",
"value": 286.34,
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": 249.84
},
"shipping": {
"currency_code": "USD",
"value": 36.5
}
}
}
}
]
}


And the value passed to patch (just the amount part):


{
"currency_code": "GBP",
"value": 211.91,
"breakdown": {
"item_total": {
"currency_code": "GBP",
"value": 211.91
},
"shipping": {
"currency_code": "GBP",
"value": 0
}
}
}

All the currencies in the newAmount value match, there are no mixed currencies.

What am I doing wrong? Is this possible?

Login to Me Too
1 REPLY 1

Kavyar
Moderator
Moderator

Good day @gavwvin,

 

Thank you for posting to the PayPal community.

 

CANNOT_MIX_CURRENCIES: This error occurs when the currency code used is not consistent across all API calls for a specific order.

 

To resolve this issue, it is important to ensure that the same currency code is passed in the "Update/Patch/Create/Capture" API calls.

 

If you are still experiencing issues, please create an MTS ticket via the following URL - https://www.paypal-support.com/s/?language=en_US . Please ensure that you provide detailed information and error details when submitting the ticket.

 

Sincerely,

Kavya

PayPal MTS

 

If this post or any other was helpful, please enrich the community by giving kudos or accepting it as a solution.

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.