You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In stripe.checkout.sessions.create, client_reference_id serves several important purposes:
Session Identification:
constsession=awaitstripe.checkout.sessions.create({client_reference_id: 'order_123',// Your internal referenceline_items: [...],mode: 'payment'});
Idempotency Control:
Prevents duplicate checkout sessions for the same order
If you try to create another session with the same client_reference_id, it will return the existing session
Webhook Event Linking:
// In your webhook handlerapp.post('/webhook',(req,res)=>{constevent=req.body;if(event.type==='checkout.session.completed'){constsession=event.data.object;constclientReferenceId=session.client_reference_id;// Use clientReferenceId to update your order status}});
Session Retrieval:
// Find sessions by client_reference_idconstsessions=awaitstripe.checkout.sessions.list({client_reference_id: 'order_123'});
Order Tracking:
// Create session with order detailsconstsession=awaitstripe.checkout.sessions.create({client_reference_id: `order_${orderId}`,metadata: {order_id: orderId},line_items: [...],mode: 'payment'});// Later retrieve order detailsconstorder=awaitfindOrderById(session.client_reference_id);
Important Notes:
It's optional but highly recommended
Should be unique per order/transaction
Doesn't affect customer creation or subscription limits
Useful for your internal record keeping
Helps with payment reconciliation
Maximum length is 255 characters
Best Practices:
// Good exampleconstsession=awaitstripe.checkout.sessions.create({client_reference_id: `order_${orderId}_${timestamp}`,customer_email: customerEmail,line_items: [...],mode: 'payment',success_url: `${YOUR_DOMAIN}/success?session_id={CHECKOUT_SESSION_ID}`,cancel_url: `${YOUR_DOMAIN}/cancel`});// Include in error handlingtry{constsession=awaitstripe.checkout.sessions.create({client_reference_id: orderId,// ... other options});}catch(error){console.log(`Failed to create session for order ${orderId}`);throwerror;}
The text was updated successfully, but these errors were encountered:
In my testing the client_reference_id doesn't prevent other sessions to created. I can have multiple sessions with the same client_reference_id if I want to. What I do is to manually close all other sessions before creating a new one. I also have active the Stripe setting to only allow one subscription per user (see Theo's video), which makes the checkout page show a message if they try to checkout with a subscription active.
From Claude:
In
stripe.checkout.sessions.create
,client_reference_id
serves several important purposes:client_reference_id
, it will return the existing sessionImportant Notes:
Best Practices:
The text was updated successfully, but these errors were encountered: