joshnuss / sveltekit-stripe-subscriptions

Example of Stripe Subscriptions & PaymentElement with SvelteKit
34 stars 1 forks source link

Free trials #1

Closed nmalchy closed 11 months ago

nmalchy commented 11 months ago

The tutorial seemed to be working fine until the step where you need to get the client_secret from the latest_invoice because when I console.log the subscription object subscription.latest_invoice.payment_intent is == null

The web hooks were all 200 too.

Not sure what's up as I'm very new to the Stripe API so any ideas would be appreciated.

joshnuss commented 11 months ago

Hi @nmalchy,

I retested this repo, and it is working for me. Maybe something changed in newer versions of the API.

What version of the Stripe API are you using?

joshnuss commented 11 months ago

Ignore my last comment. I tested with the latest API, and it works.

Are you sure you set the option expand: ['latest_invoice.payment_intent'] when creating the subscription?

nmalchy commented 11 months ago

Ignore my last comment. I tested with the latest API, and it works.

Are you sure you set the option expand: ['latest_invoice.payment_intent'] when creating the subscription?

Thanks for the swift response.

I am using the latest API "2023-10-16" because typescript was yelling at me, but since that isn't the issue, the only thing I can think of that might be messing stuff up is the priceId I'm using? I just want to test a free trial and so I went into the dashboard and creating a subscription product then took that ID, and looks like it's actually creating the subscriptions via POSTs since I see them in my dashboard... could this be some race condition thing? So weird though cause we're awaiting the subscription creation..

EDIT: yes expand: ['latest_invoice.payment_intent'] was set properly

nmalchy commented 11 months ago

Could it be because my price is $0.00 it's just automatically going straight to the end of the process and so this intermediate step of putting in payment info based on the "payment intent" is already done so to speak according to Stripe since there's technically no intent given it's a free trial?

I would ideally still like the user to input payment info so that when the trial ends we can choose to charge or not

nmalchy commented 11 months ago

I was right, so if the price_id you use is $0.00 then this step breaks; I just created a new sub product with a price set to $5 and the UI rendered without errors, closing this now.

Would be really cool if you could update the DEV article or this repo to include a free-trial flow!

Thanks for writing the article!

joshnuss commented 11 months ago

For trials, no need to set the price to zero.

When creating the subscription, set on of the trial options trial_end, trial_period_days, trial_from_plan or trial_settings

The key thing is, the customer isn't paying for an invoice or being charged right now, they're submitting their payment info for a possible future charge. This is called a Setup Intent.

There are a few things you'll need to change to use setup intents instead of payment intents:

The completion logic also changes in src/routes/checkout/complete/+page.server.js:

And in the webhook handler, make sure to handle the customer.subscription.trial_will_end

Hope that clears it up!