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()Link to User (During Signup)
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 nullPre-Purchase Flow
- User completes Stripe checkout
- Webhook creates Billing record (no userId)
- User signs up with same email
- Signup links Billing to User
This allows users to purchase before creating an account.
Next Steps
Last updated on