#WpgJetsFan13-schedule-subscription

1 messages ยท Page 1 of 1 (latest)

visual summit
#

hello, can you explain in detail what you're trying to do?

it would help to understand like:

1/ what Subscription your CheckoutSession is creating e.g. $5/month

2/ what you want to change it to, using a SubscriptionSchedule? like "$2/month first two months, then $10/month after" etc

civic schooner
#

$5/month for 12 months

#

I don't want to change anything, I just want it to end after 12 months

#
unit_amount: 500,
currency: 'cad',
recurring: {
    interval: 'month',
    interval_count: 1
},
product: product.id
#

but how do I make it end after 12 months

visual summit
#

ah I see, so "create Subscription using Checkout for $5/month, then use SubSchedule to cancel it after 12 months", right?

civic schooner
#

that's what I'm trying to do

visual summit
#

ok looking

civic schooner
#

but if I create subscriptionschedule from existing subscription id, then I can't set phases

#

and the phase items is where I can set 'iterations'

#

tried to add it like this, when I receive the customer.subscription.created webhook:

const schedule = await client.subscriptionSchedules.create({
    from_subscription: event.data.object.id
});
await client.subscriptionSchedules.update(schedule.id, {
    phases: [
        {
            items: [
                {
                    price: 'price_1LcDjeCT5ZiUAGNjrR4VLL9o',
                    quantity: 1
                }
            ],
            iterations: 12,
            start_date: Math.floor(Date.now()/1000)
        }
    ]
});

Response:
StripeInvalidRequestError: You can not modify the start date of the current phase.
#

Without start_date set, it also gave me an error saying I needed to set the start_date to anchor the subscription

#

I wonder if it's as simple as just setting an end date?

#

nvm, you can't set that. I think maybe I just have to listen to a webhook for payment and then count down in my database from 12

visual summit
#

there is a way to do this with SubSchedules, still looking

#

just spinning up a quick sample

civic schooner
#

nice

visual summit
#

ah I think I have what you need

when you update the SubSchedule, you need to pass start_date as the subscription.start_date , you're setting it to "now" which is different from the Subscription's start_date

and also, specify end_behavior: "release"

await client.subscriptionSchedules.update(schedule.id, {
    phases: [
        {
            items: [
                {
                    price: 'price_1LcDjeCT5ZiUAGNjrR4VLL9o',
                    quantity: 1
                }
            ],
            iterations: 12,
            start_date: event.data.object.start_date
            // see line above
        }
    ],
    end_behavior: "release",
    // see line above
});
#

try that, log out the SubscriptionSchedule object and look at its current_phase.end_date which should end in a year from your start date

civic schooner
#

thanks, I'll try that out.

#

I still get the same error: StripeInvalidRequestError: You can not modify the start date of the current phase.

visual summit
civic schooner
#

I'll have to make the request again hang on

#

req_9i7KXWT6MiK4Kx

#

I wonder if I just set the cancel_at to a future date 1 year from now with subscriptions.update, if that would work

hearty mica
#

๐Ÿ‘‹ Hopping in since @visual summit has to head out soon - give me a minute to catch up

civic schooner
#

actually, I think it worked, what @visual summit suggested. I just had a relic in my code that messed it up

hearty mica
#

๐Ÿ‘ good to hear!

civic schooner
#

yup, it created the timestamp for 1 year from now. nice! Thanks! So glad I convinced my boss to switch to Stripe from Square, you guys are far more helpful