Description

Stripe Billing powers Kommander payment collection, customer portal access, subscriptions, and per-channel message usage. New onboarding saves a card or SEPA Direct Debit payment method, then creates or extends a Stripe subscription with one base item and one metered message item for each enabled channel. The legacy Checkout route remains available for older flows.

Prerequisites

  • A Stripe account with Billing enabled.
  • Stripe secret key and webhook signing secret for the deployed environment.
  • Price IDs for internal and external base and metered message items.
  • Optional Stripe promotion codes for discount-code flows.
  • A public HTTPS webhook endpoint at /api/stripe/webhook.

Setup

Representative Stripe Billing setup screen with API key status, webhook endpoint, price IDs, and portal link.
Stripe setup validates API credentials, price IDs, webhook delivery, and billing portal access.
  1. Create Stripe products, recurring base prices, metered message prices, and Billing meters for the internal and external channels.
  2. Configure the deployment environment with STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET, STRIPE_PRICE_INTERNAL_BASE, STRIPE_PRICE_INTERNAL_MESSAGES, STRIPE_PRICE_EXTERNAL_BASE, and STRIPE_PRICE_EXTERNAL_MESSAGES.
  3. In Stripe, add a webhook endpoint pointing to https://YOUR_DOMAIN/api/stripe/webhook.
  4. Subscribe the endpoint to checkout.session.completed, customer.subscription.created, customer.subscription.updated, customer.subscription.deleted, customer.deleted, invoice.payment_succeeded, invoice.payment_failed, payment_method.attached, and payment_method.detached.
  5. Complete an onboarding subscription with both a card and, where supported, SEPA Direct Debit in test mode.
  6. Open the billing portal and confirm subscription changes reconcile back to the organization.

Architecture Diagram

Configuration

SettingRequiredNotes
STRIPE_SECRET_KEYYesServer-side key used by Stripe SDK calls.
STRIPE_WEBHOOK_SECRETYesUsed to verify the raw body and stripe-signature header.
STRIPE_PRICE_INTERNAL_BASEYes for internal channelFlat recurring item for internal assistants.
STRIPE_PRICE_INTERNAL_MESSAGESYes for internal channelMetered recurring item for internal message usage.
STRIPE_PRICE_EXTERNAL_BASEYes for external channelFlat recurring item for external assistants.
STRIPE_PRICE_EXTERNAL_MESSAGESYes for external channelMetered recurring item for external message usage.
STRIPE_VAT_TAX_RATE_IDOptionalReuses an existing Italian VAT tax rate; otherwise the app can create one from VAT_RATE.

Webhook Payload Example

Stripe sends the full event envelope. Always verify the signature against the raw request body before reading event.data.object.

{ "id": "evt_1Example", "type": "customer.subscription.updated", "data": { "object": { "id": "sub_123", "customer": "cus_123", "status": "active", "items": { "data": [ { "id": "si_internal_base", "price": { "id": "price_internal_base" } }, { "id": "si_internal_messages", "price": { "id": "price_internal_messages" } } ] } } } }

Webhook Handler Example

import { NextRequest, NextResponse } from 'next/server'; import Stripe from 'stripe'; import { constructWebhookEvent } from '@/backend/lib/stripe'; export async function POST(request: NextRequest) { const rawBody = await request.text(); const signature = request.headers.get('stripe-signature'); if (!signature) { return NextResponse.json({ error: 'No signature provided' }, { status: 400 }); } let event: Stripe.Event; try { event = constructWebhookEvent(rawBody, signature); } catch { return NextResponse.json({ error: 'Invalid signature' }, { status: 400 }); } if (event.type === 'customer.subscription.updated') { const subscription = event.data.object as Stripe.Subscription; // Re-derive channel state from subscription.items and update the organization. console.log('Subscription updated:', subscription.id); } return NextResponse.json({ received: true }); }

Limitations

  • Enterprise accounts may be billed outside standard Stripe channel items.
  • Legacy Checkout supports older flows; new onboarding uses saved payment methods and direct subscription creation.
  • Metered usage depends on the organization retaining current Stripe item IDs after subscription or portal changes.

Troubleshooting

  • If webhook events fail with Invalid signature, confirm STRIPE_WEBHOOK_SECRET belongs to the exact endpoint and environment.
  • If onboarding cannot create a subscription, confirm all required price IDs are set and point to recurring prices.
  • If channel toggles do not match Stripe, replay a customer.subscription.updated event and verify the webhook can update the organization.
  • If message usage is not billed, confirm the channel’s metered item ID is present on the organization and the Stripe meter event name matches the seeded meter.