#paulo-lacerda_code

1 messages ยท Page 1 of 1 (latest)

lapis topazBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1438549667595030580

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

wanton horizon
#

Hi ๐Ÿ‘‹ with the API this has to be performed as two separate requests. One to create the Subscription Schedule from the Subscription, then a second to update the newly created Subscription Schedule.

main cipher
#

const schedule = await stripe.subscriptionSchedules.create({
from_subscription: SUBSCRIPTION_ID,
});

const update = await stripe.subscriptionSchedules.update(schedule.id, {
  phases: [
    {
      start_date: Math.floor(Date.now() / 1000) + 6 * 24 * 60 * 60,
      billing_cycle_anchor: 'phase_start',
      items: [{ price: originalPriceId }],
    },
  ],
});

something like this?

wanton horizon
#

What are you trying to accomplish exactly? Asking because you won't be able to change the start_date of the current phase. You'll likely want to add a new phase that starts right away, but it may be easier to just update the Subscription if that's what you're after.

main cipher
#

we are building a feature to allow the users to change their billing date, but we require them to have at least one invoice per month, so in some cases we want to schedule a billing date anchor change for next month

#

in the subscription update we can just set to change the anchor to now

wanton horizon
#

Ah, gotcha, I just saw the Date.now and assumed, I should have read further and saw that you were adding to it ๐Ÿ˜…

So yup, two requests, but the second one will need to pass back in everything that is currently set for the Subscription Schedule's current phase, and you should provide the details to create a new phase.

So phases will have two entries in its array.

main cipher
#

so I should copy the original phase and add an end date to it?

wanton horizon
#

Yup

main cipher
#

ok, I'll try that

main cipher
#

const schedule = await stripe.subscriptionSchedules.create({
from_subscription: SUBSCRIPTION_ID,
});

const updatedPhases = schedule.phases; // Copy existing phases

const futureDate = Math.floor(Date.now() / 1000) + 6 * 24 * 60 * 60;
if (updatedPhases.length > 0) {
  const lastPhase = updatedPhases[updatedPhases.length - 1];
  updatedPhases.push({
    ...lastPhase,
    start_date: futureDate,
    billing_cycle_anchor: 'phase_start',
  });
  lastPhase.end_date = futureDate; // Set end_date for the last phase
}

const updatedSchedule = await stripe.subscriptionSchedules.update(schedule.id, {
  phases: updatedPhases,
});

I'm trying to do something like this, but I'm getting type errors

wanton horizon
#

What are the errors?

main cipher
#

Type 'import("stripe").Stripe.SubscriptionSchedule.Phase[]' is not assignable to type 'import("stripe").Stripe.SubscriptionScheduleUpdateParams.Phase[]'.
Type 'import("stripe").Stripe.SubscriptionSchedule.Phase' is not assignable to type 'import("stripe").Stripe.SubscriptionScheduleUpdateParams.Phase'.
Types of property 'add_invoice_items' are incompatible.
Type 'import("stripe").Stripe.SubscriptionSchedule.Phase.AddInvoiceItem[]' is not assignable to type 'import("stripe").Stripe.SubscriptionScheduleUpdateParams.Phase.AddInvoiceItem[]'.
Type 'import("stripe").Stripe.SubscriptionSchedule.Phase.AddInvoiceItem' is not assignable to type 'import("stripe").Stripe.SubscriptionScheduleUpdateParams.Phase.AddInvoiceItem'.
Types of property 'price' are incompatible.
Type 'string | Price | DeletedPrice' is not assignable to type 'string | undefined'.
Type 'Price' is not assignable to type 'string'

the SubscriptionSchedule and SubscriptionScheduleUpdateParams phase objects are a bit different, so I'm really not sure what should I copy from the existing schedule

wanton horizon
#

Yeah, Subscription Schedules are unfortunately pretty painful to work with. You can't just dump the entire existing phase, you'll have to map its values one-by-one. It may be easier to start with the least information possible for the request to succeed (start_date, end_date, items) and see what drops that is important to you. Then you can adjust the code to maintain those values.

main cipher
#

that makes sense, thanks

wanton horizon
#

Any time!