有时,用户会因为意外点击 paypal 智能支付按钮中的支付按钮而多次扣费。在 paypal 仪表板 > 付款偏好中,我已经启用了选项“是,阻止每个发票 ID 多次付款”。
现在我想在我的智能按钮脚本代码中设置发票ID,如下所示:
function initPayPalButton() {
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'pay',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{"amount":{"currency_code":"USD","value":total_amount}}],
application_context: {
shipping_preference: 'NO_SHIPPING'
}
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
window.location = 'success_url';
});
},
onError: function(err) {
console.log(err);
alert('Something went wrong. Please refresh the page and try again.');
}
}).render('#button_container_id');
}
initPayPalButton();
最佳答案
使用系统中的唯一标识符添加invoice_id
,以避免重复付款。
参见:https://developer.paypal.com/docs/multiparty/checkout/standard/integrate/#link-addpaymentbuttons
Other useful purchase unit parameters include:
A unique
invoice_id
that hasn't been used for a previously-completed transaction to identify the order for accounting purposes and to prevent duplicate payments.
A
custom_id
value of your choice for your system or own reference. This value is not indexed or visible to the buyer.
这就是我的代码:
paypal.Buttons({
style: {
shape: 'rect',
color: 'silver',
label: 'buynow'
},
onError: function (err) {
// FIXME: show the user an error message
},
onClick: function(data) {
// FIXME: do something cool when the button is clicked
},
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
description: 'My awesome product',
amount: {
currency_code: "USD",
value: 9.99,
breakdown: {
item_total: {
currency_code: 'USD',
value: 9.99
},
discount: {
currency_code: "USD",
value: 1.25
}
}
},
invoice_id: 'UNIQUE-ID',
custom_id: 'ANOTHER-SYSTEM-ID'
}],
application_context: {
shipping_preference: "NO_SHIPPING"
}
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// FIXME: finalise the order and show the success message
console.log("order id: "+details.id);
});
}
}).render('#paypal-button-container');
}
关于PayPal智能支付按钮: setting an invoice id to avoid duplicate transactions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66936423/