-
Notifications
You must be signed in to change notification settings - Fork 285
Description
Issue Description
The current documentation recommends creating Stripe customers before creating checkout sessions, but doesn't provide the technical reasoning behind this recommendation. This creates confusion for developers who might be tempted to let Stripe handle customer creation automatically.
Missing Context
The documentation would benefit from explaining the specific technical challenges that arise when letting Stripe automatically create customers during checkout:
Technical Reasons for Pre-Creating Customers
1. Metadata Timing Issues
- Problem: Metadata is only available in the
checkout.session.completedwebhook event - Issue: By the time
checkout.session.completedfires, thecustomer.createdevent has already been processed - Result: The
customer.createdevent arrives without any custom metadata, making it impossible to associate the customer with your application's domain objects at creation time
2. Limited Customer Identification Options
- Problem: When Stripe auto-creates customers, the only reliable identifier available is the email address
- Issue: Many applications have 1:n user-to-email relationships (users can have multiple emails, shared emails, etc.)
- Result: Email-based customer matching becomes unreliable and can lead to incorrect customer associations
3. Loss of Control Over Customer Creation Logic
- Problem: Stripe's automatic customer creation logic is opaque and may use factors beyond email (browser fingerprinting, cookies, etc.)
- Issue: This can lead to unexpected customer creation behavior that doesn't align with your application's user model
- Result: Inconsistent customer-to-user mapping and potential data integrity issues
Proposed Documentation Enhancement
The documentation should include a section explaining these technical considerations, helping developers understand:
- When to create customers (before checkout vs. automatic)
- Why pre-creation is recommended for most use cases
- How to properly handle the customer creation workflow
- What metadata and identification strategies work best
Example Workflow
// Recommended: Create customer first with metadata
const customer = await stripe.customers.create({
email: user.email,
metadata: {
user_id: user.id,
account_type: user.accountType,
// Other domain-specific identifiers
}
});
// Then create checkout session with customer
const session = await stripe.checkout.sessions.create({
customer: customer.id,
// ... other session config
});This approach ensures that:
- Customer creation events include proper metadata
- Your webhook handlers can immediately associate customers with domain objects
- You maintain full control over customer creation logic
Impact
Adding this explanation would help developers make informed architectural decisions and avoid common integration pitfalls that can be difficult to fix after implementation.