Alex Ironside
Alex Ironside

Reputation: 5039

How to get subscriptions attached to a customer in Stripe

I have a user on Stripe, and I want to cancel his subscription. I found the part in the docs talking about the customer object (here), and the subscription object (here), but how can I get the subscription id attached to the stripe user? I know I need to use this:

router.get('/cancel-premium', async (req, res, next) => {
    const subscription = await stripe.subscriptions.retrieve('sub_49ty4767H20z6a');
    stripe.subscriptions.del('sub_49ty4767H20z6a', {at_period_end: true});
});

but to do this, I need the subscription id attached to the customer

Upvotes: 1

Views: 2601

Answers (1)

Alex Ironside
Alex Ironside

Reputation: 5039

Ok, never mind. I must be blind, it was right in the docs. You can do it like this:

router.get('/cancel-premium', async (req, res, next) => {
    var user = await stripe.customers.retrieve(customerID);

    var sub = user.subscriptions.data[0].id;

    const subscription = await stripe.subscriptions.retrieve(sub);

    stripe.subscriptions.del(sub, {at_period_end: false});

    user = await stripe.customers.retrieve(req.user.stripeId);

    console.log(user.subscriptions);
});

Upvotes: 2

Related Questions