#nerder

1 messages · Page 1 of 1 (latest)

gleaming valveBOT
quaint barn
#

👋 I can help! what error are you seeing?

faint path
#

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

quaint barn
#

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
faint path
#

ok starting from your second point, is because probably i misunderstood how this works

quaint barn
faint path
#

the subscription will stay the same, only the item will change

quaint barn
#

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

faint path
#

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?

quaint barn
#

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

faint path
#

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

quaint barn
#

yup!

faint path
#

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,
      },
    );
quaint barn
#

What does typescript say?