# Ledgerlock

> Credits and 30-day passes on Postgres and Stripe that never double-charge, double-credit or trap a user in a renewal.

Ledgerlock is a paid AI agent skill on Agensi by MAJI, in APIs & Backend. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: $19 (one-time purchase)
- Creator: [MAJI](https://www.agensi.io/creators/maji)
- Category: [APIs & Backend](https://www.agensi.io/skills/apis-backend)
- Version: 1.1
- Last updated: 2026-10-19
- Security: scan passed (2026-10-19); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/ledgerlock
- Tags: api, stripe

## About this skill

A user taps twice and pays twice. A Stripe webhook retries and the credits land twice. Two requests race and spend the same credit. Ask your agent to "add credits and a 30-day pass to the app" and Ledgerlock builds billing that counts every credit exactly once: a locked balance row, an append-only ledger, an idempotency key on every spend, grant and refund, and Stripe fulfilment that survives retries.

### Why it's different
- **Proven, not assumed:** a concurrency test fires 20 parallel spends at a balance of 5 and expects exactly 5 successes, and a replayed webhook must grant once.
- **Locked down by default:** money functions set `search_path`, lock the account row and revoke execute from client roles, so no browser can grant itself credits.
- **Honest billing:** price and renewal shown before pay, reminders before any renewal, one-tap cancel, no weekly auto-charge trials.
- **Built from real projects:** 9 failures that actually happened, each with its guard.

### What it does
- **Ledger schema:** account row with `check (balance >= 0)`, append-only ledger with unique idempotency keys, passes and a Stripe event table, plus a nightly `balance = sum(delta)` check.
- **Spend and refund flow:** deduct before the work, refund on failure with a key derived from the spend, and never refund a pass user who spent nothing.
- **Stripe Checkout and webhooks:** server-side price mapping, one fulfilment function for the webhook and the success page, raw-body signature check, event dedupe.
- **Regional pricing:** real per-region prices with `currency_options` or separate price ids, not just FX conversion.
- **Refunds and disputes:** claw back unspent credits, end the pass, freeze spending on a dispute.

### How it works
1. **Pricing table first:** packs, passes and plans per region, with the rule that an active pass makes covered actions free.
2. **Migration:** tables, locked RPCs and grants in one migration.
3. **Routes:** checkout, fulfilment and webhook handlers with idempotent grants.
4. **Billing UX:** price, renewal and cancel shown honestly.
5. **Prove it:** Vitest against local Supabase plus `stripe trigger` and `stripe listen` end to end.

### Frameworks & tools
Defaults to Postgres on Supabase, Stripe and Next.js route handlers, and follows your stack if it differs. Tests with Vitest, the Supabase CLI and the Stripe CLI.

### Use cases
- AI apps that charge per action with credit packs.
- A 30-day pass that wins over credits while it is active.
- Fixing balances that drift or webhooks that credit twice.
- Pricing the same product fairly in different countries.

### Known limitations
Not for in-game currencies traded between users or marketplace payouts (Stripe Connect is a different design). Needs a Stripe account and a Postgres database you control.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the Ledgerlock skill from Agensi" and describe your task.
3. Buy it on the listing or let your agent ask you first; your agent then loads it from Agensi when it needs it. Every paid skill has a money-back guarantee: https://www.agensi.io/docs/money-back-guarantee

## Frequently asked questions

### Does it work without Supabase?

Yes. The SQL is plain Postgres; Supabase is the default because of its RPC and role setup.

### Can it do subscriptions?

Yes, as a variant: a plan extends access only on a paid invoice for an active subscription and ends on cancellation.

### What stops a user from granting themselves credits?

Execute on the money functions is revoked from client roles and the user id comes from server auth, never from the request.

### What happens if Stripe sends the same event twice?

Each event id is recorded and every grant has a unique key, so it is granted once.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
