Subscriptions
Redirect to Stripe Checkout, handle subscription lifecycle states, and manage trial periods.
Subscriptions are a separate surface from one-time card checkout. This page is about recurring plan subscriptions, which still run on Stripe today (legacy, per ADR 2026-09-26). One-time card purchases (e.g. credits) go through Creem instead — see Creem integration.
Checkout flow
Call createCheckoutSession from @nebutra/billing to generate a Stripe-hosted checkout URL, then redirect the user to it. The helper takes a Stripe customerId (from getOrCreateCustomer), not a raw tenant id, and the trial field is trialPeriodDays.
import { createCheckoutSession, getOrCreateCustomer } from "@nebutra/billing";
import { redirect } from "next/navigation";
import { getCurrentTenant } from "@nebutra/tenant";
export async function GET() {
const tenant = getCurrentTenant();
const customer = await getOrCreateCustomer(tenant.tenantId, tenant.ownerEmail);
const session = await createCheckoutSession({
customerId: customer.id,
priceId: process.env.STRIPE_PRICE_ID_PRO_MONTHLY!,
successUrl: `${process.env.NEXT_PUBLIC_APP_URL}/billing/success`,
cancelUrl: `${process.env.NEXT_PUBLIC_APP_URL}/billing`,
trialPeriodDays: 14,
});
redirect(session.url);
}Stripe handles the payment form, SCA/3DS, and error states. You do not need to build a payment form.
On a successful payment, Stripe redirects the user to your successUrl. Show a confirmation UI — but do not update the plan here. Plan activation is confirmed by the webhook.
export default function BillingSuccessPage() {
return (
<div>
<h1>You're on PRO!</h1>
<p>
Your plan is being activated. This usually takes a few seconds.
Refresh the page if your new limits aren't reflected immediately.
</p>
</div>
);
}Stripe fires checkout.session.completed. The webhook handler in @nebutra/billing updates the tenant plan in the database. Quota limits update automatically.
Do not update the plan based on a successful redirect URL alone. A redirect can be faked. Always wait for the checkout.session.completed webhook to confirm the payment.
Checking subscription status
Use getStripeSubscription (by Stripe subscription id) or getCustomerSubscriptions (by Stripe customer id) from @nebutra/billing to fetch the current subscription state:
import { getCustomerSubscriptions, mapStripeStatusToLocal } from "@nebutra/billing";
const [subscription] = await getCustomerSubscriptions(customer.id);
// Stripe.Subscription — map its status through mapStripeStatusToLocal()
// for the local SubscriptionStatus enum used elsewhere in @nebutra/billing.Subscription lifecycle states
| State | Meaning | User impact |
|---|---|---|
trialing | Within the 14-day trial period | Full PRO access, no charge yet |
active | Subscription is current and paid | Full plan access |
past_due | Latest invoice payment failed | Plan remains active; user is prompted to update payment |
canceled | Subscription was canceled | Downgraded to FREE at period end |
unpaid | Multiple failed attempts; Stripe paused subscription | Access revoked; user must update payment method |
Displaying status in the UI
import { getCustomerSubscriptions, mapStripeStatusToLocal } from "@nebutra/billing";
export default async function BillingSettingsPage({ stripeCustomerId }: { stripeCustomerId: string }) {
const [subscription] = await getCustomerSubscriptions(stripeCustomerId);
const status = subscription ? mapStripeStatusToLocal(subscription.status) : "CANCELED";
return (
<div>
<p>Status: {status}</p>
{status === "PAST_DUE" && (
<a href="/billing/portal">Update payment method</a>
)}
</div>
);
}Trial periods
PRO subscriptions include a 14-day free trial. No credit card is charged until the trial ends.
- Trial state maps to
TRIALINGthroughmapStripeStatusToLocal(subscription.status). - When the trial expires, Stripe charges the card on file and moves the status to
active. - If no payment method is provided before trial end, the subscription moves to
canceled.
// Pass trialPeriodDays when creating the checkout session to activate a trial
const session = await createCheckoutSession({
customerId: customer.id,
priceId: process.env.STRIPE_PRICE_ID_PRO_MONTHLY!,
successUrl: "https://app.nebutra.com/billing/success",
cancelUrl: "https://app.nebutra.com/billing",
trialPeriodDays: 14,
});Omit trialPeriodDays (or pass 0) to skip the trial for specific users (e.g., users migrating from a legacy plan or those using promo codes).
Cancellation and self-service
Customers can cancel, upgrade, or update their payment method via the Stripe Customer Portal, using createBillingPortalSession from @nebutra/billing:
import { createBillingPortalSession } from "@nebutra/billing";
import { redirect } from "next/navigation";
export async function GET({ stripeCustomerId }: { stripeCustomerId: string }) {
const session = await createBillingPortalSession(
stripeCustomerId,
`${process.env.NEXT_PUBLIC_APP_URL}/settings/billing`,
);
redirect(session.url);
}For a full cancel/pause/resume surface instead of the hosted portal, @nebutra/billing also exports cancelStripeSubscription, pauseStripeSubscription, resumeStripeSubscription, unpauseStripeSubscription, updateStripeSubscription, and previewSubscriptionChange.
Related
How is this guide?
Last updated on