#jesse-with-subscription-elements
1 messages ยท Page 1 of 1 (latest)
Hi there! Does this get you oriented in the right direction?
Docs about using Elements to build a subscription integration: https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements
yeah, I followed this doc as my starting point. everything working until about step 5. I found in the docs that adding a trial_end in the subscription.create call causes latest_invoice.payment_intent to be null
here is what I have in my create call
customer: customerId,
items: [
{
price: selectedPrice?.id,
},
],
trial_end: trialEndDataTimestamp,
payment_behavior: 'default_incomplete',
payment_settings: { save_default_payment_method: 'on_subscription' },
expand: ['pending_setup_intent'],
});```
from this, I am then saving the new sub Id into my db for future reference and then returning the client secret to the client code and creating the payment element
but for what ever reason, the invoice.paid and invoice.payment_succeeded events are being sent out before I have credit card info saved off and is technically starting a trial without that info
So, when a trial is created for the first billing cycle, a zero-dollar invoice should be created, so those events firing does not surprise me. The payment information is created on step 6, so all of this seems totally normal
correct, but is there a way to delay those events until after payment info is collected? is there something on the Stripe.Invoice object that I can detect in that initial creation to set the local db copy of the info to incomplete or something?
is there a way to delay those events until after payment info is collected?
Not as far as I know
is there something on the Stripe.Invoice object that I can detect in that initial creation to set the local db copy of the info to incomplete or something?
I don't understand what you mean by this. What do you want the database to be able to surface about the Invoice? Why?
sorry. for my implantation Im storing off the expiresAt from the subscription object so I can reduce the number of calls I make to the stripe api
Hello ๐
give me a moment to catch up here ๐
hey!
can you give me a short summary of what you're stuck on?
yeah
im trying to convert a subscription checkout flow to use elements, what I am currently stuck on is having a subscription start before a user gives their billing info since I am including a 30 day trial with the sub
So you're starting a subscription server-side with a 30 days trial and are having trouble collecting billing info?
trouble insuring I have collected their billing info before starting the trial
iam able to get the payment element started and collect info
but if refresh the page after ive selected the sub plan I want and before i put in my billing info, my trial has stated and I dont know if its a best practice thing on my end or if there is something im missing
sorry if this is kinda confusing, you stare at one section of code for too many days in a row with only alittle progress and things get mushed together.
All good, It is a little trickier for sure ๐ as the trial creates $0 invoice and that'd cause latest_invoice.payment_intent to be null
yeah, i saw that in the docs and switched to checking the payment intent to get the client secret
let me do a quick write up on what I have right now, maybe that would help?
Sure, go for it
If you really just want to make sure that the payment method is collected before the subscription starts, you can use the SetupIntent API to save the payment method for future usage, save it as customer's default pm
https://stripe.com/docs/payments/save-and-reuse
And then start/create a subscription server-side
ok so I was also looking to using SetupIntent intent, but stuck on the last step of starting a sub
so what I tried was
- create the customer, save the customerId to my db
- create the Setupintent like this
customer: customerId,
usage: 'off_session',
});``
then return the client_secret to my client and create the payment elements
then in my submit handler for the element I have this
//`Elements` instance that was used to create the Payment Element
elements,
confirmParams: {
return_url: `${window.location.href}`,
});```
and thats where my understanding of what todo starts to trail off
you mean after you successfully setup the payment mthod?
yeah
after that you create a subscription ๐
const subscription = await stripe.subscriptions.create({
customer: customerId,
items: [
{
price: selectedPrice?.id,
},
],
trial_end: trialEndDataTimestamp,
payment_behavior: 'default_incomplete',
payment_settings: { save_default_payment_method: 'on_subscription' },
});
does that automatically pull the info?
You can also pass in a default_payment_method that you just setup
https://stripe.com/docs/api/subscriptions/create#create_subscription-default_payment_method
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
from the setup?
if you don't pass default_payment_method , it will pull the payment method info from customer.invoice_settings.default_payment_method
so would default_payment_method be the setupintent Id ?
no the payment_method id that you can retrieve from the successful SetupIntent
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
so I can have my return url on the confirmSetup call point to a api end point where I can do all this setup and create the sub