cohs
cohs

Reputation: 85

Add custom field to billing agreement using paypal node sdk

Im attempting to add a custom field to a billing agreement so I know what user is starting or stopping their subscription when I recieve he IPN POST call.

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
                }
            }
        }
    });
})

When I add to the billing agreement I get malformed json error.

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');
        }
    });
})

If I try to add it in the data parameter when executing the agreement it is never returned anywhere. So currently if a user were to start or stop a subscription I wont know what user did it.

Upvotes: 2

Views: 309

Answers (1)

Preston PHX
Preston PHX

Reputation: 30377

I am not sure what version of the Subscriptions API and Billing Agreements you are using, but the SDKs do not support the latest version, and a custom parameter is not supported.

You must associate Subscription/billing agreement IDs with a user at creation time, when a user creates one via your site/application. This user-associated Subscription/billing agreement id should be persisted in your own database. Then, any later events on that id can be looked up and matched with the user (although really you want to think of users as having active Subscription/BA ids on file as an object)

Upvotes: 1

Related Questions