PayPal Checkout JavaScript: changing currency without redirecting or refreshing page

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 07:11:40

问题


Our website has a checkout flow that would break if redirecting to PayPal for approval. Also we have different currencies so the PayPal SDK will not work for our purposes, since you need to reload to change the currency.

Can we build our own checkout integration opening the URL received from the API side in a new window, or is our only option to use their SDK?


回答1:


You need to reload the JS SDK to change the currency, but you don't need to refresh the page. Here's a short vanilla JS function and example use:

function loadAsync(url, callback) {
  var s = document.createElement('script');
  s.setAttribute('src', url); s.onload = callback;
  document.head.insertBefore(s, document.head.firstElementChild);
}

loadAsync('https://www.paypal.com/sdk/js?client-id=sb&currency=USD', function() {
  paypal.Buttons({

    // Set up the transaction
    createOrder: function(data, actions) {
        return actions.order.create({
            purchase_units: [{
                amount: {
                    value: '0.01'
                }
            }]
        });
    },

    // Finalize the transaction
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
            // Show a success message to the buyer
            alert('Transaction completed by ' + details.payer.name.given_name);
        });
    }

  }).render('body');
});

Alternatively, the paypal-js package was recently released and does much the same thing


Since you mentioned having a server-side API integration, you'll want to use the front-end code of https://developer.paypal.com/demo/checkout/#/pattern/server and pair it with two routes on your server, one for 'Set Up Transaction' and one for 'Capture Transaction', documented at https://developer.paypal.com/docs/checkout/reference/server-integration/



来源:https://stackoverflow.com/questions/63483639/paypal-checkout-javascript-changing-currency-without-redirecting-or-refreshing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!