#kirito

1 messages ยท Page 1 of 1 (latest)

hollow merlinBOT
clever siren
#

Hello

#

You want the update to occur one year from the time of update?

#

Or at the end of a yearly cycle?

#

And do the new/old Prices have the same interval? (like are they both annual?)

rich void
#

at the end of a yearly cycle, what I want exactly is a yearly subscription, but the payment should be monthly, that mean if a user want to upgrade or downgrade the subscription (change the prices) it should happen after 1 year cycle.

#

Yes they have the same interval (annual).

clever siren
#

Hmm I'm a bit confused. You want the payment to be monthly?

#

Then that wouldn't be an annual interval

#

That would be a monthly interval

rich void
#

yes the payment to be monthly and the user need to pay the current subscription for the duration of at least 1 year, if he want to change the effect should effect only after 1 year cycle.

clever siren
#

Okay I see

#

Have you seen that documentation yet or looked into that API?

rich void
#

yes, I tried it first but if I use from_subscription field I cant change the plans

clever siren
#

Hmm what do you mean you can't change plans?

#

You have to update the Subscription schedule after you create it (using from_subscription)

#

When you update it you set 2 phases

#

On the second phase is when you would have the plans change

rich void
#

I cant use phases with from_subscription

night steppe
#

Hello! I'm taking over and catching up...

#

Can you provide more details? What do you mean when you say you can't use phases with from_subscription?

rich void
#

Hello

#

ok I tried to use from_subscription with pashes contain items but it give me an error of unknown params

#

could You show me how to do it with from_subscription and Subscription Schedules API

#

could You show me how to do it with from_subscription and Subscription Schedules API

#

here is the code I used const subscription = await stripe.subscriptionSchedules.create({ start_date: oneYearFromNow, from_subscription: subscriptionId, end_behavior: "none", phases: [ { items: { id: oldSubscription.items.data[0].id, price: planId, }, }, ], metadata: { ...newMetadata, kisaco_subscription_code: code, }, })

night steppe
rich void
#

req_vSPPnEOgg40XWf

night steppe
rich void
#

Ok, I will try

#

What you ask me to do is something similar to this : ``` const subscription = await stripe.subscriptionSchedules.create({
from_subscription: subscriptionId,
})

const subscription2 = await stripe.subscriptionSchedules.update(subscription.id, {
start_date: oneYearFromNow,
end_behavior: "none",
phases: [
{
items:
{
id: oldSubscription.items.data[0].id,
price: planId,
},
},
],
metadata: {
...newMetadata,
kisaco_subscription_code: code,
},
})``` ?

night steppe
#

Yeah, more or less. I recommend renaming const subscription to const subscriptionSchedule, and I think you need to adjust the phases to account for the current phase, but that's the basic approach.

rich void
#

how could I do adjust the phases?

#

or what do you mean exactly by that ?

night steppe
#

When updating a subscription schedule, you need to pass in all current and future phases that you want to keep.

#

After you create the Subscription Schedule from the Subscription have a look at it; there will already be a phase there based off of the Subscription.

#

If you want to keep that for some period of time and then switch to something different in the future you need to specify two phases when you update the Subscription Schedule: the current phase and the next phase in the future.

rich void
#

ok so how about something like this : ``` const subscriptionSchedule = await stripe.subscriptionSchedules.create({
from_subscription: subscriptionId,
})

const updatedSubscriptionSchedule = await stripe.subscriptionSchedules.update(subscriptionSchedule.id, {
phases: [
{
items:
{
id: oldSubscription.items.data[0].id,
price: oldSubscription.items.data[0].id,
},
end_date: oneYearFromNow,
},
{
items:
{
id: oldSubscription.items.data[0].id,
price: planId,
},
start_date: oneYearFromNow,

  },
],
metadata: {
  ...newMetadata,
  kisaco_subscription_code: code,
},

})````

night steppe
#

I can't validate code snippets for you. I recommend running this code in test mode to see if it does what you expect.

rich void
#

No I got an error

night steppe
#

What does the error say?

rich void
#

StripeInvalidRequestError: You cannot migrate a subscription that is already attached to a schedule: sub_sched_1MJhtxFeR0PUZyO7KqHGJKa5.

night steppe
#

Okay, so the Subscription you're trying to create a Subscription Schedule for already has a Subscription Schedule.

#

You need to modify the existing one instead of trying to create a new one.

#

You can't have multiple Subscription Schedules for the same Subscription.

rich void
#

I understand, how about storing the schedules as a metadata and each time I want to update the subscription I check whether it exist or not.

night steppe
#

Yep, you can do that.

rich void
#

Thank you, I will try it right now and see if there is any problem

#

I got the following error : StripeInvalidRequestError: Invalid array

#

param: 'phases[0][items]',
detail: undefined,

night steppe
#

items needs to be an array, not an object.

#

It needs to be an array of objects, in fact.

rich void
#

OK and in this object should I also add the id params?

#

like these ``` [{
id: oldSubscription.items.data[0].id,
price: oldSubscription.items.data[0].id,
}],````

#

because I still get this error : param: 'phases[0][items][0][id]',
detail: undefined,

night steppe
#

That likely means oldSubscription.items.data[0].id is undefined. Can you log out that value to make sure it's what you expect?

rich void
#

si_N3psyuTrc5VBlF

#

I think the error is because it is the id of the subscription item and not the price.

#

But I'm not sure why it show undefined

night steppe
#

Can you give me the request ID showing that error?

rich void
#

req_Qw1VmjRW95Pwub

night steppe
#

Not sure about the undefined part, but if you remove the id parameter that should help you move forward.

rich void
#

yes it did, but now the error is : ๐Ÿ’ฅ StripeInvalidRequestError: The subscription schedule update is missing at least one phase with a start_date to anchor end dates to.

night steppe
#

Okay. Sounds like you need to add a start_date.

rich void
#

but when I update I got another error : ๐Ÿ’ฅ StripeInvalidRequestError: You can not modify the start date of the current phase.

#

which kind of confusing.

night steppe
#

What are you setting start_date to?

rich void
#

"now"

night steppe
#

You can't have the current phase start now. It already started in the past.

#

Because you're keeping the current phase, correct?

#

What start_date did you specify for the second phase?

rich void
#

the same as the end_data of the first one

#

phases: [
{
items: [{ price: oldSubscription.items.data[0].price.id }],
end_date: oneYearFromNow,
},
{
items: [{ price: planId }],
start_date: oneYearFromNow,

  },
],
night steppe
#

Can you share the request ID for that one?

rich void
#

req_NOOlWf8UmPX6AG

night steppe
#

Okay, so if you add start_date to the first phase and set it to the start date of the current phase what happens?

rich void
#

what do you mean by the the start date of the current phase?

night steppe
#

If you look at the Subscription Schedule you can see the current phase's start_date.

#

Use that value when updating.

rich void
#

Yes, it worked I'm now trying with the duration of 5min to see if the subscription will really update after that

#

Thank you so much for your help

night steppe
#

No problem! ๐Ÿ™‚