问题
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¤cy=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