Reset billing cycle anchor of active subscription with subscription schedule

73 views
Skip to first unread message

Lionel Debauge

unread,
Dec 2, 2021, 12:13:35 PM12/2/21
to Stripe API Discussion

Hi,

Due to our business we need sometimes to cancel “cancellation” of a subscription doing the following:

Stripe::Subscription.update(subscription.id, {cancel_at: "" })

Unfortunately this changes the billing anchor of a subscription to be set to the day of the cancellation. Our business requires us to have billing cycles that start at the beginning of each month. Looking at subscription schedules it seems possible to reset a billing cycle anchor by setting a phase that will start at the end of the current month. What we would like to do is therefore:

  1. Cancel the subscription cancellation
  2. Create a subscription schedules with 2 phases

Phase 1: starts at the current billing cycle period end (the cancelled end of the subscription) and ends at the end of the month Phase 2: starts at the end of the previous phase (beginning of month) and has a billing cycle anchor set to “phase_start”. No end date is set so that this phase lasts until further changes are made.

We tried in test mode with the following code

schedule = Stripe::SubscriptionSchedule.create({
from_subscription: ‘sub_1K2BG6EqnnYTeijJd8lo5fnU’
})

schedule = Stripe::SubscriptionSchedule.create({
from_subscription: ‘sub_1K2BG6EqnnYTeijJd8lo5fnU’
})

Stripe::SubscriptionSchedule.update(
‘sub_sched_1Js3gjEqnnYTeijJL603YRJo’,
{
phases: [
{
start_date: schedule.current_phase.end_date + 1,
items: [
{price: “price_1JtaKNEqnnYTeijJtNiP1N2X”},
],
end_date: Time.zone.today.end_of_month.end_of_day.to_i,
},
{
items: [
{price: “price_1JtaKNEqnnYTeijJtNiP1N2X”},
],
iterations: 11,
billing_cycle_anchor: ‘phase_start’,
}
]
}
)


However when I run this I get the following error which tells me that my 1st phase start date is invalid because it is not before other phases end date. I assume it's referring to the current phase end date since the phases end I start to introduce are after the 1st phase start date.

Stripe::InvalidRequestError: `start_date` must be less than each phase's `end_date` or its derived end date based on `iterations` and `plans`.

Can you confirm to me if it's possible to add phases to an active subscription with a subscription schedule ? If so, can you give me guidance to avoid this error ? 

Thanks a lot for all the help you can give me.

Best regards,

Lionel

Remi J.

unread,
Dec 2, 2021, 3:48:42 PM12/2/21
to api-d...@lists.stripe.com
Hello Lionel,

When you update a SubscriptionSchedule you have to pass all phases, you are not just appending the new ones. Your update call seems to plan the next phase that is after the current one since you pass `schedule.current_phase.end_date + 1` which would not work.

Let's assume you have a subscription anchored on the 1st of the month and you cancel on the 15th and you cancel the cancelation on the 3rd of the month, then I assume that you want the following 3 separate phases:

1/ Dec 3 - Dec 15: current phase for the subscription as it's the new billing cycle anchor even if you stopped the cancelation
2/ Dec 15 - Jan 1 : extra phase for partial period to get you back to the right billing cycle anchor
3/ Jan 1 - Feb 1: last phase on normal billing cycle with `billing_cycle_anchor: ‘phase_start'` (though that shouldn't be needed)

With that set up you can then have the Schedule release the underlying subscription to continue charging monthly on the 1st of the month.

To do this, you need to tweak your code to properly pass the missing first phase that represents the Dec 3 - Dec 15, which is the "current phase" on the Schedule once created from the existing Subscription. Passing all 3 phases should be what you're missing!

SubscriptionSchedules can be quite finicky to debug end to end as there's a lot of moving pieces with phases dates and passing the right subset of phases on updates. I'd recommend following up with our support team for your exact use-case and providing clear examples of failed requests and underlying subscription ids. You can contact them here https://support.stripe.com/contact or you can also chat with developers on our Discord server: https://stripe.com/go/developer-chat

Best,
Remi

--
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.

Lionel Debauge

unread,
Dec 14, 2021, 12:42:51 PM12/14/21
to Stripe API Discussion
Hello Remi,

It has been pretty busy in our company these days so I did not have the chance to reply earlier. With your help we have found a workaround and are working on delivering the feature. Thanks a lot for your help !

Best regards,

Lionel

To unsubscribe from this topic, visit https://groups.google.com/a/lists.stripe.com/d/topic/api-discuss/gl0oVouRHeg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to api-discuss...@lists.stripe.com.


--

Lionel Debauge

Web developer

livecolonies.com | LinkedIn | Jobs

Reply all
Reply to author
Forward
0 new messages