Monetizing TeaQL in Public — Day 3: Making Billing Visible Without Mixing Permissions
A developer can belong to several Organizations, each with different permissions and a different subscription. The console needs to show the right combination while payments and network requests complete asynchronously.
On Day 2, we implemented the Billing API. Today we connected it to our shared TypeScript frontend. Owners can see billing and developer access, Finance can see billing, and Developers can see their Organization's entitlement. Switching Organizations clears the previous view before the next response arrives.
This is still a local fixture demonstration. The screenshots use synthetic accounts, real IAM sessions, and real SQLite persistence. A locally signed fixture supplies the payment event. Real Paddle Sandbox checkout, HTTPS webhook delivery, and GitHub OAuth with a real account have not been exercised in this demo. No real payment was made.
Follow the user through the console
Alex owns Acme Developer Studio and Side Project Lab. Acme starts on Free. Alex prepares a local Plus checkout. The page shows that confirmation is pending, keeps access on Free, and disables a second checkout.
A local operator control then delivers a signed success fixture. Billing commits the subscription, expiring entitlement, consumed checkout, and receipt. The frontend reads that updated state and displays Plus with its validity end.
Sam, Acme's Finance member, can inspect the subscription but has no developer panel. Morgan, a Developer, can see current access but has no subscription reference or checkout controls. When Morgan switches to Side Project Lab, the plan is Free.
Those are separate business facts. Paying for a plan does not change a user's role, and belonging to two Organizations does not combine their subscriptions.
Reuse the model and add a read projection
We kept Day 2's eight-object Billing model. This entry adds no billing tables or new generation targets. The UI reads a projection of existing subscription, checkout, and entitlement records.
The billing projection deliberately separates three values:
| Display value | What it means |
|---|---|
| Purchased plan | The plan recorded on the subscription mirror |
| Current access | The entitlement that is valid now |
| Paid access until | The entitlement's authoritative validity end |
A historical Plus subscription can remain visible while current access is Free after expiry. A new test checks that exact case. The UI does not infer current access from the existence of a subscription ID.
The latest checkout is read within the already authorized Organization scope:
Q.checkoutIntents()
.filterByOrganizationAccount(accountId)
.orderByIdDescending()
.limit(1)
.comment('Read latest scoped checkout')
.purpose('Show server-confirmed checkout state after browser return')
.executeForOne(context);
We obtained the ordering API through current field-level Assist. Generated domain source remained unchanged. E expressions read the modeled values:
const validUntil = entitlement
? E.organizationEntitlement(entitlement).validUntil().eval()
: null;
The billing service performs authorization before creating that scoped read projection. The browser receives a small response shaped for its task.
Keep one frontend and separate backend responsibilities
The shared console sends requests to same-origin paths under /billing/.
An IAM-side proxy forwards a small allowlist of routes to the independently
configured Billing service.
Browser console
→ same-origin IAM proxy
→ Billing service
→ IAM session and permission checks
→ Billing database
The proxy validates the session and, for checkout, Origin and CSRF. It forwards only the headers needed for the session and request. It has a fixed configured backend origin, bounded request bodies, and a response timeout. Arbitrary backend URLs and webhook routes are outside its allowlist.
Billing still asks IAM to authorize the requested Organization operation. Tests send direct requests through this complete adapter chain and verify Finance, Developer, revoked-member, and cross-Organization denials. UI visibility is a usability decision; the backend checks every request.
The production entry points remain separate. The local demonstration uses one explicit demo orchestrator with two HTTP listeners and two database files.
Show the capabilities the user actually has
The frontend requests permission decisions for billing reads, subscription management, and developer execution. It renders the matching panels and fetches only their data:
| User capabilities | Console view |
|---|---|
| Owner | Account and billing, checkout controls, developer entitlement |
| Finance | Account and billing, checkout controls |
| Developer | Current developer entitlement |
| Explicit Finance + Developer grants | Both authorized views |
In the browser run, Sam sees the subscription reference and plan validity, without a developer panel:

Morgan sees the current plan and validity, without financial references or a checkout button:

There is no numeric usage allowance to display yet. The page states that limits are not configured and that metered model processing is not enabled by this preview.
Treat the payment return as a prompt to check
The browser return parameter starts a status check. It never writes an entitlement, trusts a plan supplied in the URL, or creates another transaction.
While the server reports a pending or ready checkout, the page displays “Waiting for payment confirmation.” It performs at most four scheduled polls at two-second intervals. A completed checkout with active backend access ends the wait. If confirmation takes longer, the page stops automatic polling and offers a manual refresh.

After the signed fixture has been processed, the backend-confirmed plan and validity are visible together:

Checkout controls are disabled while a request is in flight and when an outstanding checkout is already recorded. The server independently enforces its one-open-checkout rule. An uncertain network result leads to a status refresh, with no automatic retry of the payment-creation POST.
Protect Organization changes from stale responses
Cancellation alone is not enough. A response can complete just as an abort is issued, and a test adapter or transport may ignore cancellation.
Our controller uses both an Organization ID and a monotonically increasing view generation. Selecting an Organization immediately clears previous data, aborts old requests, cancels polling, and starts a new generation:
this.generation++;
this.active.abort();
this.stopTimer();
this.active = new AbortController();
Before an asynchronous result can update the view, it must satisfy the same check used by the implementation:
private current(generation: number, org: string) {
return !this.disposed
&& generation === this.generation
&& org === this.state.organizationId;
}
The generation matters for A → B → A. An old response for A has the correct Organization ID again, but belongs to the wrong visit. A dedicated test resolves that old response last and verifies that it cannot overwrite the latest view.
The same rule covers a checkout response arriving after the user switches Organizations: it cannot redirect the browser into the previous Organization's checkout.
In the browser demonstration, switching from Acme's Plus access to Side Project Lab produces the separate Free entitlement:

Fail visibly when current status is unavailable
A failed request is not evidence of a Free plan. On network failure, permission loss, or an inconsistent Organization in the response, the controller clears previously loaded values and displays a refresh or access error. Session expiry returns the user to sign-in.
The console also keeps its integration mode visible. The demo says that it uses local fixtures and makes no real charge. The configured Sandbox mode says that real checkout verification is still pending.
Real browser testing caught a problem that the DOM tests did not cover: the demo operator's ordinary form submission was rejected by CSRF validation. We changed that demo-only control to same-origin fetch while keeping the Origin and token checks. The next browser run delivered the signed fixture and showed the backend-confirmed plan. Production has no demo sign-in or fixture-delivery routes.
What we verified
| Verification | Result |
|---|---|
| Frontend state, transport, and DOM tests | 23 passed |
| Billing and same-origin integration suite | 33 passed, including Day 2 regressions |
| Existing IAM regression suite | 45 passed |
| Real browser role views | Owner, Finance, Developer inspected |
| Browser payment-state flow | Waiting → locally delivered signed fixture → Plus |
| Browser Organization switch | Acme Plus → Side Project Lab Free |
The tests cover responses arriving out of order, A → B → A navigation, bounded polling, duplicate clicks, late checkout responses, CSRF rejection, role revocation, unavailable Billing, and purchase/access separation after expiry. They also retain the earlier signed-event, deduplication, and rollback coverage.
The local app is now useful to demonstrate and inspect. Real Paddle Sandbox checkout, HTTPS delivery, and production deployment still need their own verification. Refunds, upgrades, downgrades, and usage deduction remain outside this entry.
What other SaaS builders can reuse
Separate the purchased subscription from current product access. Shape reads for the user's permitted task. Make pending payment and unavailable status explicit. Guard every asynchronous continuation with the selected Organization and view generation, including navigation into checkout.
Those patterns give a billing interface a clear relationship to backend facts, while preserving authorization in the services that own it.
