# Client accounts, onboarding and billing

Agencies can offer Hyperchat as a self-serve product for their clients while keeping billing and AI keys under their own account. Each client signs up from an agency onboarding link, gets an isolated workspace and Zernio profile, completes a short setup checklist, and uses monthly AI credits included in their plan.

Implemented and tested with mocked Stripe, Whop, Zernio and OpenRouter endpoints. See "Not live-tested" below.

## Agency setup

Deployment administrators (matching `DEPLOYMENT_ADMIN_EMAIL`) and any team emails they add see a **Client accounts** section on the Workspaces screen (`/workspace?clients=1`).

1. **Your agency (`Settings` tab).** Set the business name clients see, the support email used in Help and on signup pages, optional help notes for the in-app Help assistant, and any team members who can manage clients.
2. **Your billing account (`Settings` tab).** Connect your own Stripe or Whop account by pasting your API/secret key and webhook signing secret, then register the displayed webhook URL (`/webhooks/billing/stripe` or `/webhooks/billing/whop`) in your billing dashboard. Payments go directly to your account.
3. **AI for your clients (`Settings` tab).** Save one OpenRouter API key and default models, plus an optional **HeyGen API key** for lip-synced AI spokesperson avatar videos in Campaign Studio. Client workspaces without their own keys use these keys and are capped by their plan's monthly AI credit allowance.
4. **Plans (`Plans` tab).** Create monthly or yearly plans with a display price, optional free trial length, monthly AI credits, feature bullets and the matching Stripe price ID (`price_...`) or Whop plan ID (`plan_...`).
5. **Onboarding links (`Onboarding links` tab).** Create either:
   - A **personal link** for one client's email (optionally pre-filling their business name and website, and optionally emailing the link when mail is configured).
   - A **shared link** for a sales page or community, with a maximum use count and expiry.
   Choose whether the client must subscribe before entering (`required`) or is billed outside the app (`none`), customize the welcome copy, and pick which setup steps appear on their checklist.

## Client signup and paywall

1. The client opens `/start/<token>` (shown once when the link is created).
2. They see the agency brand, welcome message, plan price/trial terms, and setup steps, then create a login or sign in. Open onboarding links allow client registration even when general signup is closed.
3. Claiming the link creates their isolated client workspace (agency creator is `owner`, client is `admin`) and schedules automatic Zernio profile + shared webhook provisioning (`clientProvision:run`).
4. If the link requires billing, the workspace starts locked (`pending_payment`) and shows the checkout paywall until the signed Stripe or Whop webhook confirms a `trialing` or `active` subscription. The agency owner keeps access while an account is paused so they can assist or open it manually.
5. Returning clients with a prior trial or subscription never receive a second free trial.

## Guided onboarding and website import

On **Home**, a new client sees their setup checklist until they hide it:

- **Tell us about your business:** paste a public `https://` website URL to extract basic business details (optionally summarized with AI when configured), review and edit them, and save. Saving writes the `"About the business"` and `"How we sound"` knowledge entries, keeps a `"My business"` brand kit in sync for Campaign Studio, and renames the workspace.
- **Connect Facebook and Instagram**, **Set up text messaging**, **Turn on your AI assistant**, **Create your first campaign**, and **Connect HighLevel:** each step links directly to its screen and marks itself done automatically once the underlying setup exists (or can be skipped / checked off manually).
- Once finished, a compact strip on Home keeps the client's subscription status, **Manage billing** link, and remaining monthly AI credits visible.

## AI credits

- Credit costs: writing / reply / help = `1`, image = `5`, video = `25`.
- Credits are reserved atomically before calling OpenRouter and refunded if an image or video generation fails before delivery.
- Unused monthly credits reset on the 1st of each UTC month. Agencies can add bonus credits for a client in the **Clients** tab.
- A client workspace that saves its own OpenRouter key in **Settings → AI tools** uses its own key instead of the agency credit pool.

## Not live-tested

Automated tests (`tests/client-accounts.test.ts`, `tests/client-accounts.spec.ts`, `tests/billing-providers.test.ts`) run against mocked Stripe, Whop, Zernio and OpenRouter responses. Live Stripe/Whop checkout redirects, billing portal links, webhook deliveries, and Zernio profile auto-provisioning should be verified once with real test keys in a staging deployment before opening client signups.

## Where the code lives

- `convex/clientModel.ts`: tables for `agencySettings`, `clientPlans`, `clientOnboardings`, `clientAccounts`, `aiCredits`, `aiCreditLedger`, `billingEvents`, and `siteImportLimits`.
- `convex/clientAgency.ts`, `convex/clientSignup.ts`, `convex/clientOnboarding.ts`, `convex/clientProvision.ts`, `convex/clientBilling.ts`, `convex/billingProviders.ts`, `convex/siteImport.ts`.
- `src/lib/client-accounts.ts`, `src/lib/site-import.ts`.
- `src/app/start/[token]/page.tsx`, `src/components/client-start.tsx`, `src/components/client-onboarding.tsx`, `src/components/agency-clients.tsx`, `src/app/client-accounts.css`.
- `tests/client-accounts.test.ts`, `tests/client-accounts.spec.ts`, `tests/billing-providers.test.ts`.
