javascript - 使用 paypal 节点 sdk 将自定义字段添加到计费协议
问题描述
我试图在计费协议中添加一个自定义字段,以便在我收到他的 IPN POST 呼叫时知道哪个用户正在开始或停止他们的订阅。
const billingAgreementAttributes = {
"name": "TEST Web App",
"description": "TEST WEB APP.",
"start_date": null,
"plan": {
"id": ""
},
"custom": "string"
"payer": {
"payment_method": "paypal",
},
};
api.post('/create', authCheck, (req, res) => {
const body = req.body
const type = body.type // default basic
const user = body.user
let isoDate = new Date();
isoDate.setSeconds(isoDate.getSeconds() + 120);
isoDate.toISOString().slice(0, 19) + 'Z';
let agreement = billingAgreementAttributes
agreement.plan.id = type ? basic : unlimited
agreement.start_date = isoDate
// Use activated billing plan to create agreement
paypal.billingAgreement.create(agreement, function (error, billingAgreement) {
if (error) {
console.error(JSON.stringify(error));
res.json(error)
} else {
console.log("Create Billing Agreement Response");
//console.log(billingAgreement);
for (var index = 0; index < billingAgreement.links.length; index++) {
if (billingAgreement.links[index].rel === 'approval_url') {
var approval_url = billingAgreement.links[index].href;
console.log("For approving subscription via Paypal, first redirect user to");
console.log(approval_url);
res.json({ approval_url })
// See billing_agreements/execute.js to see example for executing agreement
// after you have payment token
}
}
}
});
})
当我添加到计费协议时,我收到格式错误的 json 错误。
api.post('/execute', authCheck, (req, res) => {
const token = req.body
console.log(token)
paypal.billingAgreement.execute(token.token, {"custom": "foobar"}, function (error, billingAgreement) {
if (error) {
console.error(JSON.stringify(error));
res.json(error)
} else {
res.json(billingAgreement)
console.log(JSON.stringify(billingAgreement));
console.log('Billing Agreement Created Successfully');
}
});
})
如果我在执行协议时尝试将它添加到 data 参数中,它永远不会返回任何地方。因此,目前如果用户开始或停止订阅,我不知道是哪个用户完成的。
解决方案
我不确定您使用的是哪个版本的订阅 API 和计费协议,但 SDK 不支持最新版本,并且custom
不支持某个参数。
当用户通过您的站点/应用程序创建订阅/计费协议 ID 时,您必须在创建时将其与用户相关联。此与用户相关的订阅/计费协议 ID 应保存在您自己的数据库中。然后,可以查找该 id 上的任何后续事件并将其与用户匹配(尽管您确实希望将用户视为在文件中具有活动订阅/BA id 作为对象)
推荐阅读
- java - 获取 IgniteCheckedException:在单个节点上启用持久性时,默认 Ignite 实例已启动异常
- javascript - 如何使 v-btn 在单击时不启动路由但仍根据路由显示活动状态?
- javascript - 如何使用 javascript 或 jquery 将参数值设置为“onClick”函数
- tableau-api - 箱形图错误 - 箱形部分未突出显示且数据不太合理 - Tableau
- docker - 为什么我的 alpine linux docker 应用程序不允许直接连接到套接字?
- sparql - 如何在另一个 sparql 查询中进行 sparql 查询?
- apache-flink - Flink Optimizer 如何决定并行度?
- android - Firebase MLKit 条形码扫描仪:添加支持的格式
- omnet++ - handleMessage(): Unknown protocol: id = 47, name = udp -- 在网络初始化期间的模块 (inet::MessageDispatcher)
- javascript - 如何检查 html 元素是否包含我在 JavaScript 中指定的每个类