PayPal REST API 的未来付款功能允许商家存储客户的支付信息,以便在未来的交易中使用,而无需客户每次都要重新输入支付细节。这种功能通常用于订阅服务、定期付款或客户可能进行多次购买的场景。
PayPal的未来付款主要有两种实现方式:
原因:
解决方案:
// 示例代码:创建计费协议
paypal.billingAgreement.create({
description: "Premium Subscription",
payer: {
payment_method: "paypal"
},
plan: {
type: "MERCHANT_INITIATED_BILLING",
merchant_preferences: {
return_url: "https://example.com/return",
cancel_url: "https://example.com/cancel",
auto_bill_amount: "YES",
initial_fail_amount_action: "CONTINUE"
}
}
}, function(error, billingAgreement){
if(error){
console.error(error);
} else {
// 重定向到PayPal授权页面
res.redirect(billingAgreement.links[0].href);
}
});
原因:
解决方案:
// 示例代码:执行参考交易
paypal.payment.create({
intent: "sale",
payer: {
payment_method: "paypal",
funding_instruments: [{
billing_agreement_id: "BA-123456789" // 之前创建的计费协议ID
}]
},
transactions: [{
amount: {
total: "10.00",
currency: "USD"
},
description: "Monthly Subscription"
}]
}, function(error, payment){
if(error){
console.error(error);
// 应检查错误类型并采取相应措施
// 如通知客户更新支付信息等
} else {
console.log("Payment completed:", payment.id);
}
});
原因:
解决方案:
// 示例代码:取消计费协议
paypal.billingAgreement.cancel("BA-123456789", {
note: "Cancelled by merchant"
}, function(error, response){
if(error){
console.error(error);
} else {
console.log("Billing agreement cancelled");
}
});