Building SaaS and AI Products with Prepaid Wallets, Auto-Renewal, and Tax Invoicing
Ship monetized SaaS and AI tools fast using Project Billing Kit (prepaid end-user wallets, auto-renew subscriptions, and zero revenue cut), unified AI Gateway routing with BYO keys, and automated e-invoicing.

When building a recurring SaaS product or credit-based AI app, Habify Base removes weeks of ledger and subscription plumbing. Project Billing Kit provisions plan tables, append-only end-user wallet ledgers, automated renewal reminders starting 7 days before expiry, and automated e-invoicing inside your project's own database.
Alongside billing, the unified AI Gateway lets your backend invoke leading LLM providers using your own BYO AI API keys with zero markup, while deducting credits cleanly against user subscriptions.
1. SaaS Billing & AI Usage Metering Architecture
Every step from frontend plan selection and top-up checkout to AI metering and invoice issuance works together seamlessly:
| SaaS / AI Component | Habify Base Capability | How It Works |
|---|---|---|
| Checkout & plan UI | <PlanPicker /> & <WalletTopup /> React components | Renders localized pricing and tax profiles automatically |
| Immutable prepaid ledger | Append-only billing_wallet_ledger in habify_sys | Row-level locks & non-negative balance CHECK constraints |
| Auto-renewal & dunning | Automated renewal reminders (7, 3, 1 days before & due date) & signed webhooks | Debits wallets automatically on renewal and notifies your app |
| Metered LLM inference | Unified AI Gateway with BYO API key pass-through | Zero token markup with per-project key isolation |
| Tax-ready e-invoices | 7 integrated e-invoice suppliers | Issues compliant receipts immediately upon top-up capture |
2. Tracking Per-User AI Quotas Alongside Subscription Plans
In your public schema, define a RLS-protected quota table so customers see live remaining credits in their dashboard, while your backend adjusts balances atomically during subscription webhooks and AI inference calls:
CREATE TABLE public.user_ai_quotas (
user_id uuid PRIMARY KEY,
plan_code text NOT NULL DEFAULT 'starter',
monthly_credits int NOT NULL DEFAULT 100,
used_credits int NOT NULL DEFAULT 0 CHECK (used_credits >= 0),
renews_at timestamptz NOT NULL
);
ALTER TABLE public.user_ai_quotas ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.user_ai_quotas FORCE ROW LEVEL SECURITY;
CREATE POLICY quota_user_read_own ON public.user_ai_quotas
FOR SELECT USING (user_id::text = current_setting('app.current_user_id', true));3. Wiring PlanPicker UI & AI Gateway Requests via @habifybase/sdk
Drop <PlanPicker /> into your pricing page for top-ups and subscriptions, then route server inference through habifyServer.ai.chat() after verifying active plan entitlements:
// 1. Frontend React (<PlanPicker /> đi kèm sẵn từ @habifybase/sdk/react)
import { PlanPicker } from "@habifybase/sdk/react";
<PlanPicker
client={habifyPublic}
locale="vi"
countryCode="VN"
isAuthenticated={Boolean(session)}
onRequireAuth={() => openLoginModal()}
/>;
// 2. Backend API Route: Kiểm tra gói active và gọi AI Gateway (hb_sk_*)
const subs = await habifyServer.billing.listSubscriptions({ endUserRef: userId });
const active = subs.find((s) => s.status === "active");
if (!active) {
return Response.json({ error: "Cần nâng cấp gói để dùng tính năng AI" }, { status: 402 });
}
const reply = await habifyServer.ai.chat({
feature: "document_assistant",
messages: [
{ role: "system", content: "Tóm tắt nội dung ngắn gọn, súc tích." },
{ role: "user", content: userPrompt },
],
byo_api_key: process.env.MY_COMPANY_LLM_KEY,
});4. Non-Custodial Merchant Settlement & Renewal Webhooks
All end-user top-ups settle directly through your own BYO merchant credentials into your business bank account with 0% platform cut and zero custodial risk.
Each top-up capture or automatic monthly renewal dispatches a dual-secret HMAC signed webhook so your app grants fresh monthly usage credits reliably.
Frequently Asked Questions
Does Habify Base mark up token costs if I supply my own AI API key?
No. BYO AI Keys carry zero platform token markup from Starter tier upward.
How does my app sync feature entitlements when a plan renews or cancels?
Consume signed billing.subscription.renewed and billing.subscription.cancelled webhooks using verifyWebhookSignature() or read active plans server-side.
Can household businesses without VAT status use Project Billing Kit?
Yes. Seller tax profiles natively support non-VAT sales receipts alongside corporate VAT rates.
Need a SePay Merchant Account?
Customer VietQR transfers and Visa/Mastercard/JCB card payments settle directly into your own bank account. Register with SePay before connecting credentials in your project.
Related Documentation & Guides
Launch This Architecture in Free Sandbox in 3 Minutes
Spin up an isolated PostgreSQL 18 database, grab your hb_pk_* / hb_sk_* keys, and wire @habifybase/sdk.