Skip to main content

Billing & Subscriptions

HostMetrics uses Stripe for subscription billing. The billing system manages plan tiers, payment methods, checkout, and subscription lifecycle.

Plan Tiers

Plans are stored in the plans table. Each tier defines:
  • Monthly and annual pricing
  • Feature limits (number of vehicles, investors, imports)
  • Access to premium features (reports, fleet pages, toll sync)
The BillingCycleToggle component lets users switch between monthly and annual pricing views. PlanComparisonModal shows a side-by-side feature comparison.

Checkout Flow

User selects plan → Stripe Checkout session created
→ User completes payment on Stripe → Webhook fires
→ Subscription record created → User gains access
  1. User clicks upgrade on CurrentPlanCard
  2. API creates a Stripe Checkout session
  3. User is redirected to Stripe’s hosted checkout page
  4. On success, Stripe sends a webhook to the app
  5. The webhook handler creates/updates the subscriptions record

Subscription Lifecycle

StatusMeaning
activeSubscription is current and paid
past_duePayment failed, grace period active
cancelledUser cancelled, access until period end
expiredSubscription period ended
The CancelSubscriptionDialog handles cancellation with confirmation and reason collection.

Payment Methods

Users manage saved cards via PaymentMethodsCard and AddPaymentMethodDialog. Payment methods are stored in Stripe and referenced in the payment_methods table.

Customer Portal

The StripeConnectButton redirects users to Stripe’s Customer Portal where they can:
  • Update payment methods
  • View invoice history
  • Download receipts
  • Manage subscription

Webhook Processing

The Stripe webhook route processes these events:
EventAction
checkout.session.completedCreate subscription record
invoice.paidUpdate subscription status, create invoice record
invoice.payment_failedMark subscription as past_due
customer.subscription.updatedSync plan changes
customer.subscription.deletedMark subscription as cancelled
The webhook endpoint must verify Stripe’s signature to prevent spoofing. The signing secret is configured via environment variable.

Key Components

ComponentPurpose
BillingSectionMain billing page layout
CurrentPlanCardShows active plan with upgrade option
PlanComparisonModalSide-by-side plan features
BillingCycleToggleMonthly/annual toggle
PaymentMethodsCardSaved payment methods
OrderHistoryTablePast invoices and receipts
UsageLimitsCardCurrent usage vs plan limits
PromoCodeCardApply promotional codes

Key Files

FilePurpose
src/lib/db/billing.tsSubscription and plan database operations
src/hooks/useBilling.tsBilling state and actions hook
src/app/api/stripe/webhook/route.tsStripe webhook handler