#Timmy-Checkout-Sessions

1 messages · Page 1 of 1 (latest)

tight heron
#

Hi! Checkout sessions are active for 24 hours before they automatically expire if they aren't actioned. However, the point here is that you should store Checkout Sessions in your database alongside with your customer info to authenticate your customer and provision access to the Billing Portal.

wooden hearth
#

So I need the session info to allow the customer to access the Customer Portal

#

What if the user wants to access the Customer Portal 24h after he subscribed to my service?

tight heron
#

In the quickstart guide the Checkout Session ID is just used to access the Customer ID that you are providing access to the Customer Portal

#

The Session itself isn't relevant other than to access the corresponding Customer ID

#

However this is usually done via storing Customer data in your database and however you want to authenticate the customer.

#

It is really up to you... using the Checkout Session to grab the Customer ID is just an example of a route you could go

wooden hearth
#

Interesting, what route should I go though? I guess I could just store the Stripe Customer Id in my user table right?

tight heron
#

Yeah usually people store that data in their own database, and then you retrieve the data from your database when you authenticate the customer that is visiting your site

wooden hearth
#

Using only the Stripe customer id doesn't cause any security threads right?

tight heron
#

No that ID can only be used with your key

wooden hearth
#

Okay okay

#

Stupid question

#

but where/when is a stripe customer created?

tight heron
#

So it is up to you for whether you want to create the Customer ahead of time, or if it is a Customer that is returning to purchase again you will want to pass their Customer ID.

wooden hearth
#

So I should store the Stripe Customer Id to my database once the user initates a checkout right?

tight heron
#

I would store it if they successfully complete a Checkout Session

wooden hearth
#

So once a certain event gets triggered?

tight heron
#

Yeah. When Checkout.Session.Completed hits your webhook endpoint

#

That is the recommended route

wooden hearth
#

Awesome thanks

#

okay so

#
  1. Customer Sings up -> logs in
  2. Customer initiates checkout process
  3. Stripe creates a customer
  4. Payment went through (Customer actually subscribed)
  5. I store the Stripe Customer Id to my db
tight heron
#

Yeah. Only clarification is that Stripe only creates the Customer if the Session is successful.

#

So 3/4 essentially happen in tandem

#

But yeah that flow is correct

wooden hearth
#

Sorry but what exactly is a successful session?

#

If the user pays?

tight heron
#

Yes if the Session is successfully completed. This doesn't necessary mean payment as you could start a trial subscription for instance

wooden hearth
#

I see

#

Okay so once Checkout.Session.Completed gets triggered I basically store the Stripe customer id in my db and

#

set a db value to true (for example) so the user actually has access to what he paid for

tight heron
#

Yep

wooden hearth
#

I store the Stripe Customer Id so the user can later access the Customer portal for example

tight heron
#

You want to track the Subscription status to provision your product

#

Yep

wooden hearth
#

So if the user cancels the subscriptions an event will trigger and call my webhook

tight heron
#

Yep

wooden hearth
#

Awesome thanks, I understand Stripe much better now 😄

#

Is there a way for me to save this conversation and somehow archive it?

tight heron
#

The thread will get archived and you can come back to it. I'm not actually sure how long it is retained tbh

wooden hearth
#

Great thanks

#

off-topic but is your name based on the dude or the ship?

tight heron
#

No just a random name lol

wooden hearth
#

Okay was just curious haha

#

Anyway

#

thanks a lot for your help 👍

tight heron
#

👍

wooden hearth
#

Hey sorry to disturb you, I have one more question 😅

#

so I am trying to write the Stripe Customer Id to my db once the Checkout.Session.Completed even triggers

#

How do I know to which User (in my db) the Stripe customer id belongs to

#

Do I need to save the Session Id of the checkout process and then find a user that has that session id once Checkout.Session.Completed gets triggered

#

and then assign the Stripe user id to that db user?

#

To summarize: How do I map the info I get in the Checkout.Session.Completed event to a specific user in my database

raw quarry
wooden hearth
#

Thanks for getting back to me ^^

#

So the new stripe user gets created once I open a new checkout session without providing a customer right?

#

I don’t create the user manually right now

#

Based on the documentation I am supposed to associate the stripe user with my user once Checkout.Session.Completed gets triggered

#

So should I just associate my stripe user with my user once the person creates a new checkout session?

median isle
wooden hearth
#

Yes that is what’s currently happening

#

Now I just need to assign that stripe user is to my user (in my db)

#

Which I am supposed to do using the Checkout.Session.Completed event

median isle
#

Yes, you can get the customer from customer property of the session object.

wooden hearth
#

But that event only knows the stripe user id

median isle
wooden hearth
#

Yeah sorry I mean the customer id

#

So how am I supposed to map the stripe customer id to a user in my db in the Checkout.Session.Completed event?

median isle
#

How do you store the customer object in your DB?

wooden hearth
#

I am not 100% sure what you mean but:

The user creates an account
Signs in
Pays using stripe

Every user in my db has a property called ‘StripeCustomerId’

#

Also I just saw that the checkout session object has a property called ‘client_reference_id’

#

Should I use that? (do I have access to that in the Checkout.Session.Completed event?

#

Also please excuse my bad writing I am on my phone right now 😅

median isle
#

No worries, I'd suggest you to use the customer.id, which is a unique key, to map the Stripe customer to your DB.

wooden hearth
#

I have the stripe customer id but I don’t know which of my user it belongs to

#

The event doesn’t give me any information

median isle
#

Do you have an event ID that I can take a look?

wooden hearth
#

Not right now sorry

#

I am not at my pc

median isle
#

No worries, I'll keep this thread open so feel free to come back and either my colleague or I will continue helping you