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:

RequirementBehavior
Authenticated userUnauthenticated requests return 401.
Stripe configuredMissing Stripe config returns 500.
Existing subscriptionUsers without a subscription receive 404.
Return URLThe portal returns to /billing.

Example request:

curl -X POST https://app.kommander.ai/api/stripe/portal

Example 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:

ActionImplementation
Edit billing profileGET, 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 methodPOST /api/payment-methods creates a SetupIntent for card or SEPA Direct Debit.
List payment methodsGET /api/payment-methods returns saved card and SEPA payment methods plus the default method.
Set default payment methodPUT /api/payment-methods updates Stripe customer invoice settings.
Remove payment methodDELETE /api/payment-methods detaches the Stripe payment method.
Manage assistant channelsPUT /api/organizations/{organizationId}/subscribe adds, removes, or re-quantifies channel subscription items and lets Stripe prorate.
View invoicesGET /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:

EventLocal behavior
checkout.session.completedCreates a local subscription and links the organization for legacy Checkout sessions.
customer.subscription.created / updatedUpdates local subscription status, period, price, plan, Stripe snapshot, and reconciles organization channel state from Stripe items.
customer.subscription.deletedMarks the local subscription canceled and clears stale Stripe item IDs from linked organizations.
customer.deletedClears the local customer reference.
invoice.payment_succeededSyncs successful payment state from Stripe.
invoice.payment_failedRecords failure state and sends the failed-payment email.
payment_method.attached / detachedKeeps 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 areaOwnerSource of truth
Channel enablementKommander billing UIOrganization channel fields and Stripe subscription items after reconciliation.
Payment method changesStripe portal and in-app payment method endpointsStripe customer invoice settings.
Billing profile and tax detailsKommander billing profile formLocal billing profile, then synced to Stripe customer details.
Invoice historyStripeStripe invoice objects surfaced through Kommander invoice APIs.
CancellationStripe portalStripe subscription status, mirrored locally by customer.subscription.deleted.
Failed-payment recoveryStripe Billing and Kommander email notificationsStripe 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.