Use the in-app billing page for assistant channel changes. Use the Stripe portal for customer-level subscription management and cancellation.
Opening The Portal
POST /api/stripe/portal creates a Stripe Billing Portal session for the signed-in user’s Stripe customer and returns { url }.
Requirements:
| Requirement | Behavior |
|---|---|
| Authenticated user | Unauthenticated requests return 401. |
| Stripe configured | Missing Stripe config returns 500. |
| Existing subscription | Users without a subscription receive 404. |
| Return URL | The portal returns to /billing. |
Example request:
curl -X POST https://app.kommander.ai/api/stripe/portalExample response:
{
"url": "https://billing.stripe.com/p/session/..."
}The portal is customer-scoped. Kommander should not pass organization IDs to Stripe portal sessions; it resolves the signed-in user’s active subscription, opens the Stripe customer portal for that customer, and relies on webhook reconciliation after Stripe changes the subscription.
In-App Billing Management
The /billing page handles product-specific billing actions:
| Action | Implementation |
|---|---|
| Edit billing profile | GET, POST, and DELETE /api/billing-profile; admin membership required. Saved details sync to the Stripe customer and VAT tax ID when a customer exists. |
| Add payment method | POST /api/payment-methods creates a SetupIntent for card or SEPA Direct Debit. |
| List payment methods | GET /api/payment-methods returns saved card and SEPA payment methods plus the default method. |
| Set default payment method | PUT /api/payment-methods updates Stripe customer invoice settings. |
| Remove payment method | DELETE /api/payment-methods detaches the Stripe payment method. |
| Manage assistant channels | PUT /api/organizations/{organizationId}/subscribe adds, removes, or re-quantifies channel subscription items and lets Stripe prorate. |
| View invoices | GET /api/billing-profile/invoices lists the latest 10 Stripe invoices with hosted invoice and PDF links. |
Only organization admins can access billing profiles, payment methods, and invoices. Only organization owners can create subscriptions or change assistant channel billing.
Webhook Synchronization
Stripe webhooks keep local subscription and organization records aligned:
| Event | Local behavior |
|---|---|
checkout.session.completed | Creates a local subscription and links the organization for legacy Checkout sessions. |
customer.subscription.created / updated | Updates local subscription status, period, price, plan, Stripe snapshot, and reconciles organization channel state from Stripe items. |
customer.subscription.deleted | Marks the local subscription canceled and clears stale Stripe item IDs from linked organizations. |
customer.deleted | Clears the local customer reference. |
invoice.payment_succeeded | Syncs successful payment state from Stripe. |
invoice.payment_failed | Records failure state and sends the failed-payment email. |
payment_method.attached / detached | Keeps payment-method state consistent for billing screens. |
Webhook Signature Verification
Stripe webhook handlers must verify the stripe-signature header against the raw request body before parsing event data. In Next.js route handlers, use request.text() and pass that exact string into Stripe’s signature verifier.
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST(req: Request) {
const sig = req.headers.get('stripe-signature')!;
const body = await req.text();
const event = stripe.webhooks.constructEvent(body, sig, process.env.STRIPE_WEBHOOK_SECRET!);
// ... handle event
}Kommander’s implementation wraps this in constructWebhookEvent(body, signature). Missing signatures return 400, failed verification returns 400, and webhook processing does not depend on the browser session because Stripe is the caller.
Subscription Management
Subscription management is split between Kommander and Stripe:
| Management area | Owner | Source of truth |
|---|---|---|
| Channel enablement | Kommander billing UI | Organization channel fields and Stripe subscription items after reconciliation. |
| Payment method changes | Stripe portal and in-app payment method endpoints | Stripe customer invoice settings. |
| Billing profile and tax details | Kommander billing profile form | Local billing profile, then synced to Stripe customer details. |
| Invoice history | Stripe | Stripe invoice objects surfaced through Kommander invoice APIs. |
| Cancellation | Stripe portal | Stripe subscription status, mirrored locally by customer.subscription.deleted. |
| Failed-payment recovery | Stripe Billing and Kommander email notifications | Stripe invoice and subscription status, mirrored locally by invoice webhooks. |
After any portal-side update, the webhook must re-read the Stripe subscription items and update local subscription records plus organization Stripe item IDs. This prevents message-usage reporting from targeting deleted subscription items.
Cancellation And Pausing
Cancellation happens through the Stripe portal. Turning all channels off in-app pauses billing items for that assistant, but if removing those items would leave the Stripe subscription empty, Stripe cancellation is still handled in the portal.