features
Billing & Subscriptions

Billing & Subscriptions

Stripe integration for subscriptions and metered billing.

Subscription Plans

HiveForge supports tiered pricing:

  • Free: $0/month
  • Pro: $29/month
  • Enterprise: Custom pricing

Setup

1. Configure Stripe

# .env
STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_...

2. Create Products in Stripe

  1. Go to Stripe Dashboard > Products
  2. Create products for each tier
  3. Copy price IDs

3. Subscribe Customer

import { createCheckoutSession } from '@/lib/stripe'
 
const session = await createCheckoutSession({
  organizationId: org.id,
  priceId: 'price_...',
  successUrl: '/dashboard/billing/success',
  cancelUrl: '/dashboard/billing',
})
 
// Redirect to checkout
window.location.href = session.url

Webhooks

Handle Stripe events:

# apps/api/app/routers/billing.py
@router.post("/webhooks/stripe")
async def stripe_webhook(request: Request):
    event = stripe.Webhook.construct_event(
        await request.body(),
        request.headers.get("stripe-signature"),
        settings.STRIPE_WEBHOOK_SECRET
    )
 
    if event.type == "customer.subscription.created":
        await handle_subscription_created(event.data.object)
    elif event.type == "customer.subscription.updated":
        await handle_subscription_updated(event.data.object)
    elif event.type == "invoice.payment_succeeded":
        await handle_payment_succeeded(event.data.object)
 
    return {"status": "success"}

Customer Portal

Allow customers to manage subscriptions:

const { url } = await createPortalSession({
  organizationId: org.id,
  returnUrl: '/dashboard/billing',
})
 
window.location.href = url

Metered Billing

Track usage for metered features:

from app.services.billing import BillingService
 
await BillingService.record_usage(
    organization_id=org_id,
    metric="api_calls",
    quantity=1
)

Testing

Use Stripe test cards:

  • Success: 4242 4242 4242 4242
  • Decline: 4000 0000 0000 0002