#hugo_checkout-customer

1 messages ยท Page 1 of 1 (latest)

plucky horizonBOT
#

๐Ÿ‘‹ 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/1339633381268390038

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

strange plume
#

Hello there

strange badger
#

Hello

strange plume
#

I believe it is created on the creation of the Checkout Session, but I'm actually not positive and really the best thing to do is to test this out here.

strange badger
#

So in the doc, the following statement is a wrong suggestion:

 switch (eventType) {
    case 'checkout.session.completed':
      // Payment is successful and the subscription is created.
      // You should provision the subscription and save the customer ID to your database.
      break;
...

Because if the checkout.session.completed is not fired for some reason (card declined, bank error etc...), it will create X Stripe customers for X attempts ? Until one succed and then the id is recorded in my system.

So the net result would be:

  • I have the "succeeded" Stripe customer id
  • But in Stripe I have plenty of customers that was the same guy

Or did I get it wrong ?

strange plume
#

In order for a Subscription to be attempted to be charged there would have to be a Customer created.

#

So that statement isn't wrong, but the recommendation is that you ignore the Customer until a successful payment occurs.

#

If you want to just listen for customer.created and track this separately then that works too.

strange badger
strange plume
#

If you consider them "zombie" Stripe customers then yes that is true.

#

So like I said you can record that Customer regardless of the Checkout Session being completed if you so desire.

#

And then re-use it.

strange badger
#

I'm might miss something, but following this, when I'll search for a customer "example@gmail.com"

I'll have:

  • 4 customers for which no sessions was completed (for let's say 4 payment fails)
  • 1 "real" customer for which the checkout session was completed
strange badger
strange plume
#

There isn't really a best practice here -- it is completely up to you.

#

But the easiest way to do this is to create the Customer before redirecting to Checkout

#

And associate that Customer to your user in your own database.

plucky horizonBOT
strange badger
#

So I need to check if I already have a customer, if not manually create the customer, and pass it's id when I create the checkout session ?

strange plume
#

Yep

strange badger
#

Ok, but how do I know about the email (or other needed options ?) as I use the Stripe UI ?

serene mica
#

Hi ๐Ÿ‘‹

I'm stepping in as my colleague needs to go.

strange badger
#

And is the CC details and all other details (address etc) will be updated automatically then ?

serene mica
#

If your customers don't have an existing email address or saved payment methods, these will be collected during the Checkout process and saved to the Stripe customer record. You can listen for the checkout.session.completed webhook event to trigger retrieving these details from the Customer object if you need/want to check them after the customer completes checkout.

strange badger
#

I don't want to do something special with these details, I just want to be sure they'll be attached to the customer I create manually.

Also, as the first question: but how do I know about the email (or other needed options ?) as I use the Stripe UI ?

// We are in the server endpoint to create the checkout session
if (!stripeCustomerId) {
        const customer = await stripe.customers.create({
            email: ???,
        });
        stripeCustomerId = customer.id;
    }

#

Or I just create the customer without any details ?

const customer = await stripe.customers.create(); ?

serene mica
#

What do you mean when you say "as I use the Stripe UI"?

#

The email address isn't a required parameter to create a Stripe Customer ID - API doc. You would just need to store the ID in your own database that links this Customer ID with your customer records.

Stripe will update the Customer record with the email they provide in the Checkout Session.

strange badger
#
  1. When the user clicks on my Pricing button Subscribe. I hit my server POST /api/stripe/create-checkout-session.

  2. In this endpoint, I create the checkout session using the Stripe SDK:

export async function createCheckoutSession(
    userId: string,
    successUrl: string,
    cancelUrl: string,
    priceId: string,
    isDevOrPreview: boolean,
    referalId?: string
) {
    const stripe = new Stripe(isDevOrPreview ? SECRET_STRIPE_TEST : SECRET_STRIPE);
    let stripeCustomerId = await getStripeCustomerId(userId);

    if (!stripeCustomerId) {
        const customer = await stripe.customers.create();
        stripeCustomerId = customer.id;
        await setStripeCustomerId(userId, stripeCustomerId);
    }

    const options: StripeType.Checkout.SessionCreateParams = {
        customer: stripeCustomerId,
        mode: "subscription",
        line_items: [{ price: priceId, quantity: 1 }],
        success_url: successUrl,
        cancel_url: cancelUrl,
        metadata: {
            userId: userId,
        },
        subscription_data: {
            metadata: {
                userId: userId,
            },
        },
    };

    if (referalId) options.client_reference_id = referalId; // Rewardfull

    const session = await stripe.checkout.sessions.create(options);

    return session.url;
}

So I have no access to what the user fill as it's before the checkout session url is created.

strange badger
serene mica
#

Correct

strange badger
#

Great, thanks for your time.

As a note, you should update the doc, it's so unclear first sight ๐Ÿ™‚

serene mica
#

I'm happy we could shed some ๐Ÿ’ก on this

strange badger
#

Last question:

There is no issue creating the customer before the first checkout session creation (as I'll retrieve the id from my system for later attempts) in the long run ?

A user might have clicked on "Subscribe" in my pricing 2 years ago, and actually buying today. This would use the (blank / no details) customer created 2 years ago

serene mica
#

As long as you store enough data in your system to know which of your customers map to the Stripe Customer objects, it should be fine.

strange badger
#

Ok, thanks!

serene mica
#

Happy to help ๐Ÿ™‚