【问题标题】:Add custom field to billing agreement using paypal node sdk使用 paypal 节点 sdk 将自定义字段添加到计费协议
【发布时间】:2020-07-21 15:25:40
【问题描述】:

我正在尝试将自定义字段添加到计费协议中,以便在我收到他的 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');
        }
    });
})

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

【问题讨论】:

    标签: javascript node.js paypal paypal-ipn


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 2015-02-08
      • 2015-05-04
      • 2015-11-01
      • 2022-01-14
      • 2021-09-27
      • 2014-12-08
      • 2021-09-13
      • 2020-10-06
      相关资源
      最近更新 更多