neel
neel

Reputation: 29

How can i get the subscription id in stripe for canceling the subscription in node.js?

Any ideas Guys? How can i cancel the subscription in stripe?

That's my code :

app.post('/cancel-subscription', async(req, res) => {

    const {subscriptionId} = req.body 
    const deleted = await stripe.subscriptions.del(
        stripe.customers.id
      );
    res.send(deleted);
});

Upvotes: 1

Views: 163

Answers (1)

chillin
chillin

Reputation: 4486

I'm not familiar with Stripe's API, but their documentation for cancelling a subscription (https://stripe.com/docs/api/subscriptions/cancel?lang=node) gives an example like:

const deleted = await stripe.subscriptions.del(
  'sub_AgOem0qdyCowxn'
);

So, in your case, try something like:

app.post('/cancel-subscription', async(req, res) => {
    const { subscriptionId } = req.body;
    const deleted = await stripe.subscriptions.del(subscriptionId);
    res.send("Cancelled the subscription"); // Just an example, include whatever response necessary.
});

However:

  1. This assumes that the subscription ID is present within the request's body (under subscriptionId property).
  2. Double check if it's safe/recommended to send the contents of deleted in its entirety back to the client (in case it contains anything sensitive). You might also need to use res.json instead of res.send (depending on the data type).

Upvotes: 1

Related Questions