我想在当前期间结束时将客户降级。当发生这种情况。我知道将试用期设置为当前期末并将proration_behavior
设置为none
,最后设置一个Webhook来监听subscription.deleted
然后创建新订阅的方法1 2。我正在尝试寻找一种不使用webhooks的方法,以便Stripe顺其自然地处理开关。
我认为使用subscription schedules可以做到这一点。
我谨记以下几点,但我还没有尝试过,我想先征求意见:
// 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
},
],
});
以上内容将创建一个计划(如果该计划不存在),然后更新该计划以降级该计划。我不确定第二阶段结束是否会真正起作用,我可能需要增加一天来确定。还是只是不做任何设置,让时间表发生,但我想知道在这种情况下,如果第一阶段结束时将删除第一阶段,否则,下一次客户切换计划时,它可能会省略新阶段在阵列中计划。我可以通过以下操作解决此问题:
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,
},
],```