Skip to Content
ServerModelsBilling Model

Billing Model

Payment and subscription information for users.

Schema

{ userId?: ObjectId // Reference to User (optional) email: string // For pre-purchase lookup stripeCustomerId: string // Stripe customer ID currentPlan: 'basic' | 'pro' | null oneTimePurchases: [{ chargeId: string productType: 'basic' | 'pro' amount: number // in cents currency: string purchasedAt: Date }] subscriptions: [{ stripeSubscriptionId: string stripePriceId: string status: SubscriptionStatus currentPeriodStart: Date currentPeriodEnd: Date cancelAtPeriodEnd: boolean canceledAt?: Date }] createdAt: Date updatedAt: Date }

Subscription Status

enum SubscriptionStatus { ACTIVE = 'active', PAST_DUE = 'past_due', UNPAID = 'unpaid', CANCELED = 'canceled', INCOMPLETE = 'incomplete', INCOMPLETE_EXPIRED = 'incomplete_expired', TRIALING = 'trialing', PAUSED = 'paused', }

Usage

Create Billing (Stripe Webhook)

const billing = new Billing({ userId: null, // Not signed up yet email: session.customer_email, stripeCustomerId: session.customer, currentPlan: 'pro', oneTimePurchases: [{ chargeId: charge.id, productType: 'pro', amount: charge.amount, currency: charge.currency, purchasedAt: new Date() }] }) await billing.save()
const billing = await Billing.findOne({ email }) billing.userId = user._id await billing.save()

Check User’s Plan

const billing = await Billing.findOne({ userId: user._id }) const plan = billing?.currentPlan // 'basic', 'pro', or null

Pre-Purchase Flow

  1. User completes Stripe checkout
  2. Webhook creates Billing record (no userId)
  3. User signs up with same email
  4. Signup links Billing to User

This allows users to purchase before creating an account.

Next Steps

Last updated on