Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paypal SDK, how to change currency dynamically without reinjecting and reinitializing the SDK itself?

My website has products in several currencies up for sale on the same page, so a person can click the product that sold in EUR and pay in euros, or they can click the product that is sold in USD and pay in usd and so on...

The problem is that once you initialise the new PayPal SDK, you cannot change the currency that it accepts without:

  1. destroying the element
  2. changing the link to the SDK, so that it would accept a different currency
  3. manually injecting it into the page
  4. reinitialising it

As you can probably understand it is not very fast, stable or safe at the same time. Am I missing something? I know that you could send the currency as a parameter in the old Express Checkout version.

The PayPal documentation is infuriating, it is missing a lot of information and doesn't have a big community around it, so I could not find the answer to my question anywhere.

I have tried sending the currency in the payment parameters, but if it is different from the initialised currency, it throws a currency mismatch error once you try to confirm the payment.

Right now I am manually reinjecting and reinitialising the paypal SDK with the correct currency if the user clicks on the option of paying with PayPal, but it is slow and requires hardcoding sleep (although it is probably due to my lack of knowledge, there are probably better ways).

Here's the pseudocode of my current setup that is not acceptable:

initialisePaypalSDK(currency) {
    destroy old initialisation
    change link to paypal with new currency
    inject new link to page
    initialise the new sdk
    sleep until the paypal variable is defined
    showPayPalButton()
}

I expect that there is an easier and a safer way of changing the currency than this. Thanks.

like image 361
Артём Юрков Avatar asked May 15 '19 06:05

Артём Юрков


People also ask

What is the PayPal SDK?

The JavaScript SDK displays relevant, PayPal-supported payment methods on your page, giving your buyers a personalized and streamlined checkout experience. There are a couple of ways you can integrate the SDK.

How do I integrate the PayPal SDK with JavaScript?

The JavaScript SDK displays relevant, PayPal-supported payment methods on your page, giving your buyers a personalized and streamlined checkout experience. There are a couple of ways you can integrate the SDK. You can add it in a script tag. This loads the main object paypal to the global window scope of the browser.

How do I Change my currency on PayPal?

You can change your currency on Paypal by clicking the Set as Primary button on the confirmation pop-up page. How Do I Change Currency On Paypal App? On the left side of the page, click PayPal balance.


1 Answers

You can use a backend to solve this issue. First, define createOrder function like this:

const createOrder = async (data, actions) => {
  return await axios.get('/yourbackend/api/get_order_id')
}

const onApprove = (data, actions) => {
// ... code to handle the payment status
}

paypal.Buttons({createOrder, onApprove}).render('#paypal-button-container')

Then just implement REST Api for order creation (/v2/checkout/orders) on your backend. Then return id of the created payment.

like image 50
Vitaly Radchik Avatar answered Nov 03 '22 02:11

Vitaly Radchik