CTRL K
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
- Create Stripe products, recurring base prices, metered message prices, and Billing meters for the internal and external channels.
- Configure the deployment environment with
STRIPE_SECRET_KEY,STRIPE_WEBHOOK_SECRET,STRIPE_PRICE_INTERNAL_BASE,STRIPE_PRICE_INTERNAL_MESSAGES,STRIPE_PRICE_EXTERNAL_BASE, andSTRIPE_PRICE_EXTERNAL_MESSAGES. - In Stripe, add a webhook endpoint pointing to
https://YOUR_DOMAIN/api/stripe/webhook. - 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, andpayment_method.detached. - Complete an onboarding subscription with both a card and, where supported, SEPA Direct Debit in test mode.
- Open the billing portal and confirm subscription changes reconcile back to the organization.
Architecture Diagram
Configuration
| Setting | Required | Notes |
|---|---|---|
STRIPE_SECRET_KEY | Yes | Server-side key used by Stripe SDK calls. |
STRIPE_WEBHOOK_SECRET | Yes | Used to verify the raw body and stripe-signature header. |
STRIPE_PRICE_INTERNAL_BASE | Yes for internal channel | Flat recurring item for internal assistants. |
STRIPE_PRICE_INTERNAL_MESSAGES | Yes for internal channel | Metered recurring item for internal message usage. |
STRIPE_PRICE_EXTERNAL_BASE | Yes for external channel | Flat recurring item for external assistants. |
STRIPE_PRICE_EXTERNAL_MESSAGES | Yes for external channel | Metered recurring item for external message usage. |
STRIPE_VAT_TAX_RATE_ID | Optional | Reuses 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, confirmSTRIPE_WEBHOOK_SECRETbelongs 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.updatedevent 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.