#kikko088_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/1229533321277739050
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
Hi there!
Just to be clear, are you currently sending Checkout Session URLs via email to users? If so, I don't recommend doing this
There are a few other ways you can do this better. Instead of creating the setup mode Session when the trial is about to end, I recommend sending your customers an email that lets them know their trial is about to end and has them log in to provide their payment details
Once they're logged into your site, you can create the Session at that point
ok this make sense, so I could send them an email with a link to my app, let them login (if not already loged), have a button to update the cc info, create the session redirect them to the checkout an proceed using the webhooks to complete the process...can I still use:
await stripe.checkout.sessions.create({
payment_method_types: ["card"],
mode: "setup",
customer: object.customer as string,
success_url: `${config.common.frontEndUrl}/admin/payment-method-updated?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${config.common.frontEndUrl}/admin/declined`,
})
to create the checkout url? this should only update the cc info, is there a way to create a pyament link for the subscription? I could find this in the docs but maybe I didn't searched enough....
ps. this is an amazing service, you replied in less then 1 minute, many thanks
The code above can still be used to create the setup mode Checkout Session for that particular customer.
What do you mean by payment link exactly?
the session above will just create a checkout to add the payment method, is not doing the real payment (this is created before the trial is ended)
Right, correct. If you collect payment information before the trial ends, you can set the newly-added PaymentMethod as the customer's default PaymentMethod for invoices. Then, when the subscription cycles, we'll automatically attempt payment with the default PaymentMethod without the customer having to come back on session.
ok great, last question, the session above doesn't set the payment method as default, to do so I need to listen for the checkout.session.completed event and then do:
const { payment_method } = await stripe.setupIntents.retrieve(setupIntentId);
await stripe.customers.update(customerId, {
invoice_settings: { default_payment_method: payment_method },
});
is it possible to set the payment as default method when I create the checkout session? I feel like this is an extra step that might be done in a better way
You likely want to change this slightly:
const { payment_method } = await stripe.setupIntents.retrieve(setupIntentId);
stripe.setupIntents.retrieve is returning the SetupIntent object, not the ID of the PaymentMethod created when that SetupIntent was confirmed
But no, there's no other way to do this when working with Checkout. Once the PM has been added to the customer, you'll need to manually update that customer's invoice_settings.default_payment_method value.
yeah actually I have also this utility to get the id:
export const getPaymentMethodId = (
paymentMethod: string | Stripe.PaymentMethod,
) => (typeof paymentMethod === "string" ? paymentMethod : paymentMethod.id);
An alternative is to use the customer portal to have your customer add a PaymentMethod and set a default themselves (or set it for them once they've added a PM)
yeah this is the last of my option just for branding reason (but it might be the best one)....in case I'll use this, I think I'll still need to authenticate the user and then create the portal link right?
Correct, yes. So you'd still want to send them an email that includes a link to your app, where they'd need to authenticate before you create a portal link