Skip to content
Open menu

Billing and webhooks

The adapter interface, idempotency, signature verification, refunds, and tax handling.

Nexo Starter ships two billing adapters: Stripe and Paddle. You enable exactly one per deployment. The application talks to the adapter interface, not to either SDK.

The adapter interface

// lib/billing/types.ts (excerpt)
export interface BillingAdapter {
  createCheckout(input: CheckoutInput): Promise<{ url: string }>;
  getSubscription(id: string): Promise<Subscription | null>;
  cancelSubscription(id: string): Promise<void>;
  verifyWebhook(raw: string, headers: Headers): Promise<NormalizedEvent>;
}

NormalizedEvent is a small discriminated union: checkout.completed, subscription.updated, subscription.canceled, and payment.refunded. Handlers in app/api/webhooks/[provider]/route.ts switch on this union, so adding a provider means implementing the interface and registering the route, not editing handler logic.

Idempotency keys

Every webhook handler writes an idempotency record before applying side effects. The key is the provider event ID. The write and the side effect happen in one transaction:

await db.transaction(async (tx) => {
  const inserted = await tx.insert(webhookEvents).values({ id: event.id, provider }).onConflictDoNothing();
  if (inserted.rowCount === 0) return; // already processed
  await applyEvent(tx, event);
});

Both providers retry deliveries. A retried event hits the conflict, returns 200, and changes nothing.

Signature verification

Verify the signature against the raw request body before parsing JSON. Parsing first and re-serializing breaks the signature.

const raw = await req.text();
const event = await adapter.verifyWebhook(raw, req.headers);

A failed verification returns 400 and is logged. Do not fall back to unverified processing in development; use the provider's CLI to forward signed events instead.

Refunds

Refunds are 14 days on first purchase. When a refund is issued, the license is revoked: the handler sets the license status to revoked and records the reason in the audit log. Revocation is immediate and does not wait for the next billing read. The kit itself is a one-time purchase with no subscription, so there is no proration logic to reconcile.

The single-charge rule

A checkout must produce exactly one charge. The adapter creates the checkout session with an idempotency key derived from the user ID and the plan ID, and the handler ignores a checkout.completed event if a license already exists for that user and plan. This guards against double submission and against a user opening two tabs. If you add a new plan, keep the key derivation stable; changing it after launch can produce a second charge for an in-flight session.

Tax handling

The two adapters differ, and the difference matters for accounting.

  • Stripe uses Stripe Tax. You are the merchant of record; tax is calculated and collected on your behalf, and you are responsible for registration and remittance. Enable Stripe Tax in the dashboard and set the tax behavior on prices.
  • Paddle is a merchant of record. Paddle calculates, collects, and remits tax, and handles invoices and VAT compliance. Do not enable a separate tax calculation on top of Paddle; you would double-charge.

Pick the adapter that matches how you want to handle tax obligations, then leave the other disabled. The BILLING_PROVIDER environment variable selects which adapter is active at boot.