英文:
Stripe subscription Id coming back undefined when querying by customer ID
问题
我的 Stripe 客户 ID 不再能从 Stripe 中查询到相应的订阅 ID,查询结果返回 undefined
,因此我的客户无法取消订阅。
我想要遵循 Stripe 文档,但我无法获取订阅 ID 并且我在 Stack 上找到了一些示例,建议使用格式 customer.subscriptions.data[0]
,但它并没有按预期工作(GitHub Autopilot 也建议使用此格式)。
我能够通过 Webhook 创建新客户,所以我的连接和 stripeSecret
正确工作,但我该如何修改我的代码来使用客户 ID 取消订阅:
控制器函数
module.exports.cancelSubscription = async (req, res) => {
const user = await User.findById(req.user.id);
if (!user) {
req.flash('error', '无法找到该用户');
return res.redirect('/users');
}
const customer = await stripe.customers.retrieve(user.stripeId);
// 使用客户 ID 获取订阅 ID
const subscriptionId = customer.subscriptions.data[0];
// 取消订阅
stripe.subscriptions.del(subscriptionId, { at_period_end: false });
// 在我的数据库中更新用户订阅状态
user.subscription.active = "已取消";
await user.save();
req.flash('success', '订阅已取消');
res.redirect(`/users/${user._id}`);
}
谢谢!
英文:
My stripe customer ID is no longer querying the corresponding subscription ID from stripe and the results come back undefined
so my customers can't cancel subscriptions.
I'd like to follow the stripe docs but I can't get the subscription ID to return and I've found a couple examples on stack that recommend the format as customer.subscriptions.data[0]
but it's not working as intended (github autopilot also recommends this format).
I'm able to create new customers via the webhook so my connection and stripeSecret
are working correctly but how do I fix my code to cancel a subscription using the customer ID:
Controller function
module.exports.cancelSubscription = async (req, res) => {
const user = await User.findById(req.user.id);
if (!user) {
req.flash('error', 'Cannot find that user');
return res.redirect('/users');
}
const customer = await stripe.customers.retrieve(user.stripeId);
// use customer id to get subscription id
const subscriptionId = customer.subscriptions.data[0];
// cancel subscription
stripe.subscriptions.del(subscriptionId, { at_period_end: false });
// update user subscription status in my database
user.subscription.active = "cancelled";
await user.save();
req.flash('success', 'Subscription cancelled');
res.redirect(`/users/${user._id}`);
}
Thank you!
答案1
得分: 0
subscriptions
参数在客户对象中默认不包括,可以通过在expand
参数中包含它来扩展它。例如,
const customer = await stripe.customers.retrieve(cus_xxx, {
expand: ['subscriptions'],
});
const subscriptions = customer.subscriptions.data;
或者,您也可以使用List Subscriptions API,并将customer
设置为检索客户的订阅。
在检索到订阅后,您可以使用Cancel Subscription API逐个取消订阅。
英文:
subscriptions
parameter in Customer object is not included by default and it's expandable by including it in expand
parameter. For example,
const customer = await stripe.customers.retrieve(cus_xxx, {
expand: ['subscriptions'],
});
const subscriptions = customer.subscriptions.data;
Alternatively, you can also use List Subscriptions API with customer
set to retrieve customer's subscriptions.
After the subscriptions are retrieved, you can then delete the subscription individually with Cancel Subscription API.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论