如何在 Stripe 中获取附加到客户的订阅

How to get subscriptions attached to a customer in Stripe

我在 Stripe 上有一个用户,我想取消他的订阅。我在文档中找到了谈论客户对象 (here), and the subscription object (here) 的部分,但是我怎样才能获得附加到 stripe 用户的订阅 ID?我知道我需要使用这个:

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

但是为此,我需要附加到客户的订阅 ID

好的,没关系。我一定是瞎了,这在文档中是正确的。你可以这样做:

router.get('/cancel-premium', async (req, res, next) => {
    var user = await stripe.customers.retrieve(customerID);
    console.log(user.subscriptions);
    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);
});