#ziwen-subs-schedules
1 messages · Page 1 of 1 (latest)
I don't fully grasp the question to be candid.
then finish the payment on the backend with the ID
How exactly are you completing the payment on the backend? can you elaborate?
have you looked at the defer-intent flow? It allows you to collect payment method info without requiring a client-secret associated with the elements
https://stripe.com/docs/payments/accept-a-payment-deferred?platform=web&type=subscription
In hindsight, it seems like we do use PaymentIntents, but retrieve it (from a finalized invoice) instead of creating it. I guess my question is self resolved. Feel free to resolve the question, but I'll also post my method below to answer your question.
Here's what we do:
I generate a draft invoice on page load. Once the user submits their payment details on Stripe elements, I created a payment method id as follows instead of a setupIntent in the guide
const { error, paymentMethod } = await stripe.createPaymentMethod({ elements })
On the backend, I attach the payment method Id to the customer
const pm = await stripe.paymentMethods.attach(paymentId, { customer: customerId })
at which point I finalize the invoice. It seems like we get the payment intent from the invoice afterward, and confirm it with the payment methoid Id.
const paymentIntent = invoice.payment_intent as StripeClass.PaymentIntent await stripe.paymentIntents.confirm(paymentIntent.id, { payment_method: paymentId }) await stripe.customers.update(customerId, { invoice_settings: { default_payment_method: paymentId } })
Yup the flow makes sense.
These are just one-time payment invoices correct? Not related to a subscription?
Actually, these are related to a subscription, which I created with a subscription schedule
Would that be a problem?
Additionally, if I did create a subscription (of 1 year) but the subscription is not paid for until 3 days later, will the subscription end date details on stripe also be pushed back 3 days later?
You said
I generate a draft invoice on page load
Did you mean you create a subscription that generates a draft invoice automatically?
yes sorry ^
Additionally, if I did create a subscription (of 1 year) but the subscription is not paid for until 3 days later, will the subscription end date details on stripe also be pushed back 3 days later?
Not really, the start date of the subscription is the usual billing cycle unless you update it by adding in a trial or some other way
I see, then would it be possible to update the subscription schedule start date (on the backend) after the user submits their payment details but before the invoice is finalized?
Basically there can be a delay between when the subscription schedule is created and when the user submits their payment info. I'm wondering how it is possible to make sure the user gets their full length subscription time and if other devs have tackled this issue (and how they resolve it)
I mean the invoice doesnt' get generated until you create a subscription right?
Are you creating a subscription on the page load or a subscription schedule?
Yes, so I call a backend api on page load, which creates the subscription schedule:
(on page load) => axios.post(...) => ( creates subscription schedule on backend or fetches if created already)
// Create draft subscription for web plan const subscriptionSchedule = await stripe.subscriptionSchedules.create({ customer: stripeId, phases: [ { items: [{ price: subscriptionPriceId, quantity: 1 }], iterations: 1, default_tax_rates: [taxRateId] } ], start_date: Math.floor(Date.now() / 1000), expand: [ 'subscription', 'subscription.latest_invoice', 'subscription.latest_invoice.discounts' ] }) const subscription = subscriptionSchedule.subscription as StripeClass.Subscription const invoice = subscription.latest_invoice as StripeClass.Invoice
Gotcha. So if the subscription is starting in the future then you should be able to update the start date
So are you suggesting initializing the start date to somewhere in the far future, then changing to Date.now() once the payment is submitted?
What are the downsides to voiding an invoice besides clutter? I was also thinking of potentially doing that (voiding the initial invoice and subscription and then recreating a copy when payment info is submitted)
No downsides I can think of.
If all you want is to store the payment method prior to creating a subscription and control the timing of when subscription gets created then you can instead
1/ Use SetupIntents API to collect payment method information
https://stripe.com/docs/payments/save-and-reuse
2/ Create a subscription whenever you want (or when your customer clicks on "subscribe" button)
I will talk with my team and consider it.
Right now, the benefit of creating the draft invoice initially is for stripe to do the discounts/tax calculations, then we can render the invoice details on the same page that they submit their card info.
Thanks for the subscription schedule advice. I think I'm going to do the following:
- Set subscription schedule start date to X (date in far future) and set end behavior to cancel
- When a user submits payment before X, update the start date to current time
- When a user submits a payment after X (which means subscription schedule status should be cancelled), then void the old invoice and remake everything
👍