使用 paypal 节点 sdk 将自定义字段添加到计费协议

Add custom field to billing agreement using paypal node 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');
        }
    });
})

如果我在执行协议时尝试将它添加到数据参数中,它永远不会在任何地方返回。因此,目前如果用户要开始或停止订阅,我不知道是什么用户做的。

我不确定您使用的订阅 API 和计费协议的版本,但 SDK 不支持最新版本,并且不支持 custom 参数。

当用户通过您的 site/application 创建用户时,您必须在创建时将 Subscription/billing 协议 ID 与用户相关联。这个与用户相关的 Subscription/billing 协议 ID 应该保存在您自己的数据库中。然后,可以查找该 ID 上的任何后续事件并与用户匹配(尽管你真的想将用户视为在文件中具有活动的 Subscription/BA ID 作为对象)