You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何获取Stripe客户关联的订阅ID以取消订阅

How to Get a Customer's Subscription ID to Cancel Their Stripe Subscription

No worries, let's walk through how to pull the subscription ID linked to your customer so you can run that cancellation code properly.

First off, you'll need the Customer ID (it looks like cus_xxxxxx) for the user you want to cancel—since you mentioned they're your Stripe user, you should already have this stored somewhere in your database or can grab it from your Stripe Dashboard.

From there, you have two straightforward ways to get their subscription ID:

Option 1: Retrieve the Customer Object and Extract Subscriptions

When you fetch a Customer via the API, it includes a subscriptions property that lists all active (and some inactive) subscriptions tied to that customer. You can pull the ID directly from this list:

router.get('/cancel-premium', async (req, res, next) => {
  try {
    // Replace with your actual customer ID
    const customerId = 'cus_123456789';

    // Fetch the customer object
    const customer = await stripe.customers.retrieve(customerId);

    // Grab the first subscription ID (adjust if the customer has multiple subscriptions)
    // If they have multiple, you might filter by plan ID or status here
    const subscriptionId = customer.subscriptions.data[0].id;

    // Cancel the subscription at the end of the current period
    await stripe.subscriptions.del(subscriptionId, { at_period_end: true });

    res.status(200).send('Subscription scheduled to cancel at the end of the billing period');
  } catch (error) {
    // Handle any errors (e.g., invalid customer ID, no subscriptions found)
    next(error);
  }
});

Option 2: List Subscriptions Filtered by Customer

You can also use Stripe's subscriptions.list endpoint to directly fetch all subscriptions for a specific customer, which is useful if you want to filter by status (like only active subscriptions):

router.get('/cancel-premium', async (req, res, next) => {
  try {
    const customerId = 'cus_123456789';

    // List active subscriptions for the customer
    const subscriptions = await stripe.subscriptions.list({
      customer: customerId,
      status: 'active', // Optional: only get active subscriptions
      limit: 1 // Optional: limit to 1 result if you only need the first one
    });

    if (subscriptions.data.length === 0) {
      return res.status(404).send('No active subscriptions found for this customer');
    }

    const subscriptionId = subscriptions.data[0].id;
    await stripe.subscriptions.del(subscriptionId, { at_period_end: true });

    res.status(200).send('Subscription scheduled to cancel at the end of the billing period');
  } catch (error) {
    next(error);
  }
});

A Quick Note for Multiple Subscriptions

If your customer has more than one active subscription, you'll want to add logic to pick the right one—for example, checking the plan.id property to match the premium plan you're targeting, instead of just grabbing the first item in the list.

内容的提问来源于stack exchange,提问作者Alex Ironside

火山引擎 最新活动