apps/web/guides/stripe-checkout.md
If you have a custom checkout flow that uses Stripe's checkout.sessions.create API, you'd want to associate the Stripe customer object with the user's unique ID in your database.
This will allow Dub to automatically listen for purchase events from Stripe and associate them with the original click event (and by extension, the link that the user came from).
Under the hood, Dub records the user as a customer and associates them with the click event that they came from.
Then, when the user makes a purchase, Dub will automatically associate the checkout session details (invoice amount, currency, etc.) with the customer – and by extension, the original click event.
Then, when you create a checkout session, pass your customer's unique user ID in your database as the dubCustomerExternalId value in the metadata field.
import { stripe } from "@/lib/stripe";
const user = {
id: "user_123",
email: "[email protected]",
teamId: "team_xxxxxxxxx",
};
const priceId = "price_xxxxxxxxx";
const stripeSession = await stripe.checkout.sessions.create({
customer_email: user.email,
success_url: "https://app.domain.com/success",
line_items: [{ price: priceId, quantity: 1 }],
mode: "subscription",
client_reference_id: user.teamId,
metadata: {
dubCustomerExternalId: user.id, // the unique user ID of the customer in your database
},
});
This way, when the customer completes their checkout session, Dub will automatically associate the checkout session details (invoice amount, currency, etc.) with the customer – and by extension, the original click event.