#bryanschreiner_best-practices
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1219379786955096145
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
First question, is it recommended to go this route (add a card, set as default, create subscription after) and 2) can you help me understand why my subscriptions are not starting and why the invoice is left to open? I'm trying to mirror the same functionality that the checkout session has
I can send whatever code is helpful
Hello, can you send me the ID of a subscription that you tried this with? That sounds like it should work on its surface
Also these guides may be helpful in demonstrating how this process generally works. I am happy to help solve this hear, just wanted to flag them in case you hadn't seen them
https://docs.stripe.com/billing/subscriptions/build-subscriptions?ui=elements
https://docs.stripe.com/payments/accept-a-payment-deferred?platform=web&type=subscription
sub_1OvmS9FGTemLEdoK8kSPEacS
can you tell me about #7 confirm subscription from the client side?
that seems like something i wouldn't expect
customerId: workspace.customerId,
paymentBehavior: 'default_incomplete',
trialPeriod: trialPeriod || 0,
items: [
{
price: priceId,
quantity,
},
],
expand: ['latest_invoice.payment_intent'],```
This is what i'm doing to create the subscription, and I'm not confirming it or passing anything back to the front end after this
For sub_1OvmS9FGTemLEdoK8kSPEacS, the subscription is set to be automatically charged but manually finalizing the invoice turns of automatic collection unless you specify auto_advance: true (๐คท) https://docs.stripe.com/api/invoices/finalize#finalize_invoice-auto_advance
Ah you are actually using something more like this https://docs.stripe.com/payments/finalize-payments-on-the-server?platform=web&type=subscription
So basically, your method is fine but you would also want to call /pay on the invoice or manually confirm the invoice's payment intent client side
we are doing some 3rd party tax calculation in stripe. Another team member was telling me about it but i don't understand it fully. I think we charge the amount and start the subscription wiht the checkout session, but it doesn't finalize for an hour
Just to double check, have you talked with Avalara about what needs to happen when creating subscriptions directly yourself to make sure the tax gets applied correctly?
i have not, no
You should be able to do this any which way but they may have restrictions on which of our integration paths they can do this on
ok i'll check with them
i was planning on just trying it out and seeing if it worked and then reaching out if I had issues.
What is /pay?
invoice/pay endpoint?
Yes, sorry meant to link it but am bouncing between a couple of threads.
no worries at all, i appreciate the help!
do you have that documentation?
๐ Stepping in for my teammate
Here is our API reference doc on the /pay endpoint: https://docs.stripe.com/api/invoices/pay
I'm confused on this. I see in stripe my payment has requires_confirmation but i'm not sure why. Does that mean the customer has to do something? I wasn't expecting a customer to have to do anything after submitting a credit card
pi_3OvnIlFGTemLEdoK13IWn1cp
Out of curiosity, how will customers provide payment details when this is live? The reason I ask is I see the payment method used for this customer's subscription was added via the Dashboard directly (vs. collecting it using the PaymentElement and latest_invoice.payment_intent.client_secret).
Oh i'm just testing this in a development environment, they will add them through the app UI with the payment element
creating a setup intent to add the payment method, and then it'll get saved as the default payment method before calling the create subscription endpoint on the server
Hm, hold on a second. You shouldn't need to create a SetupIntent at all, unless there's a long period of time between when you expect a customer to provide their payment details and when their subscription is created
oh let me know if that's the case, I thought I needed to create a setup intent if I wanted to display the payment element
Ah, I see! No, no need to create a SetupIntent in addition to the Subscription. When you create the Subscription with default_incomplete, you can expand latest_invoice.payment_intent and use the PaymentIntent's client secret to render the PaymentElement
In more detail, my flow is 1) request a setup intent to add a card 2) client side confirms card details and submits it via the server 3) client sends id to the backend to set this payment method as the default for the customer 4) create a subscription
Should the flow be to create a subscription or retrieve it every time I load the checkout page?
See this: https://docs.stripe.com/billing/subscriptions/build-subscriptions?ui=elements
The flow should be to create a customer, create a Subscription for that customer, use the client secret from the first invoice's PaymentIntent to render the PaymentElement.
This won't set the PaymentMethod as the customer's invoice_settings.default_payment_method. However, you can use payment_settings.save_default_payment_method to attach the new PM to the Subscription: https://docs.stripe.com/api/subscriptions/create#create_subscription-payment_settings-save_default_payment_method
Alternatively, you can make a separate call to update the Customer after completing the first payment on the subscription
Will this be compatible with Avalara and how we keep our invoices open for an hour before finalizing them so that we can charge tax if applicable?
my checkout flow has a "monthly" or "yearly" toggle which is actually different priceIds in stripe, so I'd rather not create a subscription before collecting payment details
If you create a Subscription with default_incomplete, the first invoice will remain open for 23 hours or until paid, whichever comes first: https://docs.stripe.com/billing/subscriptions/overview#payment-window
If you'd rather collect payment method details before you create a Subscription, I recommend following this flow instead: https://docs.stripe.com/payments/accept-a-payment-deferred?platform=web&type=subscription
You'll present the PaymentElement first without having to create the Subscription or a SetupIntent
I have the second option working, but the problem earlier in the thread that I was running up against was that it was just leaving the invoice open and wasn't starting the subscription. I'm trying to figure out how to start the subscription but also allow Avalara to update the invoice if needed
I haven't yet tried calling /pay on the invoice
Gotcha, okay. If you have the deferred intent flow working, I recommend testing it once more so you have a PaymentMethod that was created via the Element (vs. the Dashboard)
i'm not sure I follow, I'm not using the payment method as part of the subscription create call
customerId: workspace.customerId,
paymentBehavior: 'default_incomplete',
trialPeriod: 0,
items: [
{
price: priceId,
quantity,
},
],
expand: ['latest_invoice.payment_intent'],
})```
And before this I am just setting the payment method as the default on the stripe customer
I'll recreate it again but i'm not sure why that would work
Oh, I was a little confused when you said you're using the deferred intent flow since the PI you shared is tied to an invoice > Subscription > Customer, where the PM on that customer was not created via Elements
that is correct just for testing, but in my app it would be created via elements.
I'm not following why that would affect a subscription getting created with the code I sent you above
You'll still need to confirm the payment client side: https://docs.stripe.com/payments/accept-a-payment-deferred?platform=web&type=subscription#confirm-the-subscription
what does that look like to the end user? Can I mimick that server side?
I have a lot of front end calls and I'd rather keep things server side to eliminate bugs around users refreshing or losing connection, etc
The other person in this chat recommended invoice/pay or auto_advance. I'll admit I'm still relatively new to stripe so I'm looking for best practices
Let's take a step back. This flow allows you to present a PaymentElement before creating a Subscription: https://docs.stripe.com/payments/accept-a-payment-deferred?platform=web&type=subscription
That said, when your customer submits the payment form, a few things should happen: the Subscription is created server side, your server passes the client secret to your frontend, and the payment is confirmed frontend
Ok. I can wire it up to work like that. I wasn't expecting it to come front end and require the client to confirm the payment but I can do that
If you'd rather finalize payments server side, you can follow this guide instead: https://docs.stripe.com/payments/finalize-payments-on-the-server?platform=web&type=subscription
You'd still present the PaymentElement before creating the Subscription (or any other Intent). When your customer submits the payment form, a PaymentMethod object will be created. You can then create the Subscription and confirm the PaymentIntent server side
if i finalize the payments server side, i'm assuming that would break the integration with avalara, is that correct?
what would break exactly?
i don't want to finalize the invoice before taxes are calculated. we leave it open for an hour
You'll need to get a little creative with this, as the first invoice for Subscriptions will auto finalize if the collection method is charge_automatically.
I'm trying to mimick what the checkout session does, any tips there?
You could create a subscription with trial_end a few seconds into the future. The first invoice will thus be a $0 invoice and once the trial is over, the first non-zero invoice will be created with a one-hour draft period
what does the checkout session do behind the scenes?
Alternatively, you could create a subscription using a subscription schedule: https://docs.stripe.com/billing/subscriptions/subscription-schedules/use-cases#start-subscription-future
The subscription will be active but the first invoice will be in draft.
With Checkout, a subscription isn't created until a customer pays/completes the Checkout Session
so is that a possibility to do the same thing?