Skip to main content
This guide walks through implementing subscription billing with ZendFi. You will create plans, subscribe customers, handle lifecycle events, and manage cancellations.

Subscription Architecture

Create a Subscription Plan

Plans define the pricing, billing interval, and trial period. Create them once and reuse for all subscribers.

Plan Intervals

Trial Periods

Set trial_days to give customers free access before the first charge:
During the trial, the subscription status is trialing. No charges are made. When the trial ends, the first payment is created automatically.

Subscribe a Customer

Handle Subscription Webhooks

Subscription lifecycle events are delivered via webhooks. Set up handlers for each event to keep your system in sync.
app/api/webhooks/zendfi/route.ts

Subscription Lifecycle

Status Reference

Cancellation

Cancel immediately

Cancel at period end

To let the customer keep access until the end of their current billing period, handle this in your application logic:

Access Control Middleware

Gate features based on subscription status:
middleware.ts

Pricing Page Example

Build a pricing page that links to subscription checkout:

Testing

Use test mode (zfi_test_ key) to simulate the full subscription lifecycle without real charges. Test keys work against Solana Devnet where transactions are free.