#nerder
1 messages · Page 1 of 1 (latest)
👋 I can help! what error are you seeing?
this is my actual code:
async downgrade(gym: Gym, subscription: Subscription, newPriceId: string) {
this.logger.log(`Downgrading subscription [${subscription.id}] to [${newPriceId}]`);
//1. Create a new subscription schedule from the current subscription
const schedule = await this.stripe.subscriptionSchedules.create(
{
from_subscription: subscription.id,
},
{
stripeAccount: gym.accountId.value,
},
);
//2. Update the subscription schedule to change the price at the end of the current period for the old subscription
await this.stripe.subscriptionSchedules.update(
schedule.id,
{
end_behavior: 'release',
proration_behavior: 'none',
phases: [
{
start_date: subscription.currentPeriodEnd,
items: [
{
price: newPriceId,
},
],
},
],
},
{
stripeAccount: gym.accountId.value,
},
);
//3. Cancel the old subscription at the end of the period
await this.stripe.subscriptions.update(
subscription.id,
{
cancel_at_period_end: true,
},
{ stripeAccount: gym.accountId.value },
);
}
the error is this: Error: You can not modify the start date of the current phase
but if I try to add start_date to the first call to create i couldn't either
Yeah there's a couple of things that are catching my eye:
- In step 2 you're not also passing in the current phases. If you want the change to kick in at the END of the current period you need to have two phases - one for the current phase, and one for the change in price
- I'm not understanding why you want to cancel the subscription at the end of the current period if you're using a subscription schedule to update it to a diferent price at the end of the period
ok starting from your second point, is because probably i misunderstood how this works
Yeah I'd definitely recommend reading through this section - https://stripe.com/docs/billing/subscriptions/subscription-schedules#updating
the subscription will stay the same, only the item will change
In particular that last example for "To add additional phases to a subscription schedule, pass in the current phase, and then define your new phases:"
and yup, even if you just want the item to change you'd need it to be a separate phase
ok, but what do you mean with current phase?
I don't get it from the example
basically, what I need to do is to recreate the current phase?
You'll want to look at the output you get when you create the Subscription Schedule from the existing subscription
You should be getting back at least one phase in the phases hash - if you want the subscription to stay the same for it's current period you'll want to re-pass those phases in when you update it
ah ok!
await this.stripe.subscriptionSchedules.update(
schedule.id,
{
end_behavior: 'release',
proration_behavior: 'none',
phases: [
{
start_date: subscription.currentPeriodStart,
end_date: subscription.currentPeriodEnd,
items: [
{
price: subscription.itemId,
},
],
},
{
start_date: subscription.currentPeriodEnd,
items: [
{
price: newPriceId,
},
],
},
],
},
{
stripeAccount: gym.accountId.value,
},
);
this is one way
but i can do it simply spreading in the phases from the schedule i created before
yup!
for some reason typescript complains if i try to do it in this way
await this.stripe.subscriptionSchedules.update(
schedule.id,
{
end_behavior: 'release',
proration_behavior: 'none',
phases: [
...schedule.phases
{
start_date: subscription.currentPeriodEnd,
items: [
{
price: newPriceId,
},
],
},
],
},
{
stripeAccount: gym.accountId.value,
},
);
What does typescript say?