您可以将Paypal Node SDK 用于您的用例,而不是依赖可嵌入的 Paypal 订阅按钮。 SDK 将为您提供与 NodeJs 更好的集成。
基本上有两个步骤可以做到这一点:
1.) 定义计费方案对象
计费计划对象定义订阅计划,包括周期数、支付频率、任何设置费用等。
var billingPlanAttribs = {
name: 'Food of the World Club Membership: Standard',
description: 'Monthly plan for getting the t-shirt of the month.',
type: 'fixed',
payment_definitions: [{
name: 'Standard Plan',
type: 'REGULAR',
frequency_interval: '1',
frequency: 'MONTH',
cycles: '11',
amount: {
currency: 'USD',
value: '19.99'
}
}],
merchant_preferences: {
setup_fee: {
currency: 'USD',
value: '1'
},
cancel_url: 'http://localhost:3000/cancel',
return_url: 'http://localhost:3000/processagreement',
max_fail_attempts: '0',
auto_bill_amount: 'YES',
initial_fail_amount_action: 'CONTINUE'
}
};
当然,您需要将 cancel_url 和 return_url 更改为您的实际 Firebase 函数端点(或 localhost,如果您出于开发目的在 localhost 中运行您的函数)
2.) 创建和激活计费计划,因此一旦您创建或定义了计费 - 您将需要创建该对象并激活计费计划,如下所示:
paypal.billingPlan.create(billingPlanAttribs, function (error, billingPlan){
var billingPlanUpdateAttributes;
if (error){
console.error(JSON.stringify(error));
throw error;
} else {
// Create billing plan patch object
billingPlanUpdateAttributes = [{
op: 'replace',
path: '/',
value: {
state: 'ACTIVE'
}
}];
// Activate the plan by changing status to active
paypal.billingPlan.update(billingPlan.id, billingPlanUpdateAttributes, function(error, response){
if (error){
console.error(JSON.stringify(error));
throw error;
} else {
console.log('Billing plan created under ID: ' + billingPlan.id);
}
});
}
});
同样,所有这些都记录在 Paypal's Developer Section 中。
Here's also a link to their github example using NodeJs(与 Firebase 函数相同的底层后端)