paypal - PayPal 智能支付按钮:设置发票 ID 以避免重复交易
问题描述
有时,由于意外点击了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
其他有用的购买单位参数包括:
invoice_id
未用于先前完成的交易的唯一标识,用于识别用于会计目的的订单并防止重复付款。
您选择的
custom_id
值供您的系统或自己参考。此值对买方不编入索引或可见。
这就是我的代码的样子:
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');
}
推荐阅读
- javascript - 如何从 CKEditor 5 捕获外部事件?
- android - 如何插入 FFI 功能的完成百分比?
- javascript - 如何添加linkedin个人资料徽章以响应应用程序
- reactjs - Cypress open-ct 和 run-ct 破坏了我的 CPU
- dart - 为什么在检查可空性的 if 语句的范围内仍然可以将可空值视为可空值?
- java - Java编程字符大小
- python - Django 错误:字段 'id' 需要一个数字,但得到了 'list'
- reactjs - useEffect 后变量值重置
- django - 如果我尝试将用户添加到他/她已经在其中的组中,django 是否会有任何问题
- python - 计算在 python 中冷却池所需的时间