首页 > 解决方案 > 使用 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 参数中,它永远不会返回任何地方。因此,目前如果用户开始或停止订阅,我不知道是哪个用户完成的。

标签: javascriptnode.jspaypalpaypal-ipn

解决方案


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

当用户通过您的站点/应用程序创建订阅/计费协议 ID 时,您必须在创建时将其与用户相关联。此与用户相关的订阅/计费协议 ID 应保存在您自己的数据库中。然后,可以查找该 id 上的任何后续事件并将其与用户匹配(尽管您确实希望将用户视为在文件中具有活动订阅/BA id 作为对象)


推荐阅读