#jesse-with-subscription-elements

1 messages ยท Page 1 of 1 (latest)

fierce hornet
wheat bronze
#

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

fierce hornet
#

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

wheat bronze
#

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?

fierce hornet
#

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?

wheat bronze
#

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

peak yarrow
#

Hello ๐Ÿ‘‹
give me a moment to catch up here ๐Ÿ™‚

wheat bronze
#

hey!

peak yarrow
#

can you give me a short summary of what you're stuck on?

wheat bronze
#

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

peak yarrow
#

So you're starting a subscription server-side with a 30 days trial and are having trouble collecting billing info?

wheat bronze
#

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.

peak yarrow
#

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

wheat bronze
#

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?

peak yarrow
#

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

wheat bronze
#

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

#
  1. create the customer, save the customerId to my db
#
  1. 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

peak yarrow
#

you mean after you successfully setup the payment mthod?

wheat bronze
#

yeah

peak yarrow
#

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' },
      });
wheat bronze
#

does that automatically pull the info?

peak yarrow
wheat bronze
#

from the setup?

peak yarrow
#

if you don't pass default_payment_method , it will pull the payment method info from customer.invoice_settings.default_payment_method

wheat bronze
#

so would default_payment_method be the setupintent Id ?

peak yarrow
#

no the payment_method id that you can retrieve from the successful SetupIntent

wheat bronze
#

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