Billing (Stripe & Lemon Squeezy)
Relevant source files
The following files were used as context for generating this wiki page:
The billing system for Inbox Zero is designed to handle premium subscriptions through two third-party payment providers: Stripe and Lemon Squeezy. This dual-provider architecture allows for flexibility in payment processing. The system is responsible for creating checkout sessions, managing subscription lifecycle events via webhooks, and synchronizing subscription data with the application's local database.
At its core, the system abstracts the provider-specific details. It maps plans and subscriptions from both Stripe and Lemon Squeezy to a unified internal Premium data model and a PremiumTier enum. This ensures that the rest of the application can interact with a consistent representation of a user's subscription status, regardless of the payment provider used. Webhooks from each provider are crucial for maintaining real-time synchronization between the payment platform and the application's database.
Architecture Overview
The billing process involves the user selecting a premium plan within the application, being redirected to a secure checkout page hosted by either Stripe or Lemon Squeezy, and the application receiving a notification via a webhook upon successful payment or other subscription events.
This diagram illustrates the high-level data flow for a new subscription.
Configuration
The billing system relies on both environment variables for secrets and API keys, and a dedicated configuration file for defining pricing tiers and their mapping to provider-specific product IDs.
Environment Variables
Sensitive keys, secrets, and provider-specific IDs are configured via environment variables.
| Variable | Provider | Description |
|---|---|---|
STRIPE_SECRET_KEY | Stripe | The secret API key for authenticating Stripe API requests. |
STRIPE_WEBHOOK_SECRET | Stripe | The secret used to verify the authenticity of incoming webhooks from Stripe. |
NEXT_PUBLIC_STRIPE_*_PRICE_ID | Stripe | Publicly accessible Stripe Price IDs for various subscription plans (e.g., Business Monthly). |
LEMON_SQUEEZY_API_KEY | Lemon Squeezy | The API key for authenticating Lemon Squeezy API requests. |
LEMON_SQUEEZY_SIGNING_SECRET | Lemon Squeezy | The secret used to verify incoming webhooks from Lemon Squeezy. |
NEXT_PUBLIC_LEMON_STORE_ID | Lemon Squeezy | The ID of the store on Lemon Squeezy. |
NEXT_PUBLIC_*_VARIANT_ID | Lemon Squeezy | Publicly accessible Lemon Squeezy Variant IDs for different plans. |
Sources: apps/web/env.ts:80-83, apps/web/env.ts:133-138, apps/web/env.ts:77-78, apps/web/env.ts:141-149
Tier and Pricing Configuration
The file apps/web/app/(app)/premium/config.ts is the source of truth for all pricing tiers. It defines the features, pricing, and mapping between the internal PremiumTier enum and the corresponding IDs on Stripe and Lemon Squeezy.
TierType: Defines the structure for a pricing tier, including its name, features, and monthly/annual prices.STRIPE_PRICE_ID_CONFIG: An object that maps aPremiumTierto its active StripepriceIdand any legacyoldPriceIds.variantIdToTier: An object that maps a Lemon SqueezyvariantIdto its correspondingPremiumTier.getStripeSubscriptionTier(): A function to determine thePremiumTierfrom a given StripepriceId.getLemonSubscriptionTier(): A function to determine thePremiumTierfrom a given Lemon SqueezyvariantId.
Sources: apps/web/app/(app)/premium/config.ts:6-20, apps/web/app/(app)/premium/config.ts:60-101, apps/web/app/(app)/premium/config.ts:241-248
Stripe Integration
Checkout and Subscription Management
The Stripe integration is managed through a set of functions that initialize the client, create checkout sessions, and synchronize data.
getStripe(): Initializes and returns a singleton instance of the Stripe SDK using theSTRIPE_SECRET_KEY.createStripeCheckoutSession(): Creates aPremiumrecord in the database if one doesn't exist for the user, then generates a Stripe Checkout Session. It includes the user's ID and email in the session metadata for later identification.syncStripeDataToDb(): A crucial function that fetches the latest subscription data from Stripe for a givencustomerIdand updates the correspondingPremiumrecord in the local database. This is often triggered by webhooks to ensure data consistency. It also callssyncSeats()to update seat usage for the subscription.
Sources: apps/web/ee/billing/stripe/index.ts:13-16, apps/web/ee/billing/stripe/index.ts:25-72, apps/web/ee/billing/stripe/sync-stripe.ts:14-19
Stripe Webhook Handling
The endpoint at /api/stripe/webhook/route.ts listens for events from Stripe to automate subscription management.
- The request body is read and the
stripe-signatureheader is retrieved. - The webhook event is constructed and verified using
stripe.webhooks.constructEventwith theSTRIPE_WEBHOOK_SECRET. - A
switchstatement handles different event types.
The following diagram shows the sequence for a completed checkout.
Handled Stripe Events:
| Event Type | Action |
|---|---|
checkout.session.completed | The primary event for new subscriptions. It triggers syncStripeDataToDb to pull the full subscription details and update the database. |
customer.subscription.updated | Handles upgrades, downgrades, cancellations, and renewals. It also triggers syncStripeDataToDb. |
customer.subscription.deleted | Fired when a subscription is definitively deleted. It triggers syncStripeDataToDb to reflect the terminated status. |
Sources: apps/web/app/api/stripe/webhook/route.ts:21-50
Lemon Squeezy Integration
Checkout and Subscription Management
Similar to Stripe, the Lemon Squeezy integration provides functions for creating checkouts and managing subscriptions.
getLemonSqueezy(): Initializes and returns a singleton instance of the Lemon Squeezy client.createLemonSqueezyCheckout(): Creates a checkout session using the Lemon Squeezy API. It passes customer details and custom data (likeuserId) which are returned in the webhook payload.syncLemonSqueezySubscription(): Updates the localPremiumrecord with data from a Lemon Squeezy subscription object.
Sources: apps/web/ee/billing/lemon/index.ts:11-13, apps/web/ee/billing/lemon/index.ts:22-54, apps/web/ee/billing/lemon/index.ts:56-89
Lemon Squeezy Webhook Handling
The endpoint at /api/lemon-squeezy/webhook/route.ts processes real-time subscription events from Lemon Squeezy.
- The request signature is verified using the
LEMON_SQUEEZY_SIGNING_SECRET. - The
userIdis extracted from the webhook'smeta.custom_data. - A
switchstatement handles different event types by callingsyncLemonSqueezySubscriptionwith the event payload.
The following diagram shows the sequence for a new subscription creation.
Handled Lemon Squeezy Events:
| Event Name | Action |
|---|---|
subscription_created | Creates or updates the Premium record for a new subscription. |
subscription_updated | Updates the Premium record with the latest subscription status, plan, or renewal date. |
subscription_cancelled | Updates the subscription status to reflect cancellation. |
subscription_expired | Marks the subscription as expired in the database. |
Sources: apps/web/app/api/lemon-squeezy/webhook/route.ts:20-60
Database Model
The Premium table in the database serves as the central point for storing a user's or team's subscription status, linking it to both Stripe and Lemon Squeezy customer and subscription IDs.
This ER diagram shows the key billing-related fields in the Premium model.