use*_*075 5 subscription stripe-payments
我想在当前期间结束时降级客户。当这种情况发生时。我知道的方法是将试用期设置proration_behavior为当前期末并设置为none,最后设置一个webhook来收听subscription.deleted然后创建新订阅1 2。我试图找到一种不使用 webhooks 的方法,以便 Stripe 处理他们身边的开关。
我认为这可以通过订阅计划以某种方式实现。
我有以下想法,但我还没有尝试过,我想先征求意见:
// create schedule if it doesn't exist
let schedule: Stripe.SubscriptionSchedule;
if (!stripeSubscription.schedule) {
schedule = await this.stripe.subscriptionSchedules.create({
from_subscription: stripeSubscription.id,
});
} else {
schedule = await this.stripe.subscriptionSchedules.retrieve(stripeSubscription.id);
}
await this.stripe.subscriptionSchedules.update(schedule.id, {
end_behavior: 'release',
phases: [
{
plans: [{plan: stripeSubscription.items.data[0].plan.id}],
end_date: stripeSubscription.current_period_end,
},
{
proration_behavior: 'none',
collection_method: 'charge_automatically',
plans: [{plan: replacementPlanId}],
end_date: stripeSubscription.current_period_end
},
],
});
Run Code Online (Sandbox Code Playgroud)
如果它不存在,上面将创建一个计划,然后更新该计划以降级计划。我不确定第二阶段结束是否真的有效,我可能需要增加一天才能确定。或者只是不设置任何内容并让计划发生,但我想知道在这种情况下是否会在第一阶段结束时将其删除,否则下次客户再次切换计划时,它可能会省略包括新的阵列中的计划。我可以通过执行以下操作来解决这个问题:
phases: [
...schedule.phases.filter(phase => phase.end_date > Date.now() / 1000).map((phase, index, array) => {
if (array && array.length -1 === index && !phase.end_date) {
phase.end_date = stripeSubscription.current_period_end;
}
return {
plans: phase.plans,
end_date: phase.end_date,
};
}),
{
proration_behavior: 'none',
collection_method: 'charge_automatically',
plans: [{plan: replacementPlanId}],
end_date: stripeSubscription.current_period_end,
},
],```
Run Code Online (Sandbox Code Playgroud)