PAY-21Recommended
Record every money movement in an append-only ledger
Write each charge, refund, fee, dispute and payout as a new ledger row with the provider's object ID, and never update or delete a row.
How to do it
Each row holds the time, type, signed amount in minor units, currency, order ID and provider object ID. A unique constraint on the provider ID stops double entries. Corrections are new rows that reverse old ones. Balances are sums of rows. Give the app's database role only INSERT and SELECT on the ledger. Each row becomes one balanced journal entry in Book every money movement as balanced double-entry postings.
Examples
SQL example
CREATE TABLE ledger_entries (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
occurred_at timestamptz NOT NULL,
entry_type text NOT NULL CHECK (entry_type IN ('charge', 'refund', 'fee', 'dispute', 'dispute_reversal', 'payout', 'adjustment')),
amount_minor bigint NOT NULL,
currency char(3) NOT NULL,
order_id text,
provider_id text NOT NULL UNIQUE
);
GRANT SELECT, INSERT ON ledger_entries TO app_role;
How to check
As the app role, run an UPDATE and a DELETE on the ledger. Both fail with permission denied. For every order, the sum of its charge and refund rows equals the paid amount minus refunds on the order.
Related rules
- DAT-14 Wrap multi-step changes in a transaction in Data and databases
- PRC-18 Book every money movement as balanced double-entry postings in Pricing, plans and entitlements
- PAY-22 Reconcile every payout in Payments and billing
- PAY-38 Refund through the provider with a key and a reason in Payments and billing
- PAY-62 Report seller income to the tax authority in Payments and billing
- PAY-67 Record the rate and settled amount of every charge in Payments and billing
- CMP-26 Write an audit event for every sensitive action in Compliance and governance
- USG-6 Keep credits in a ledger instead of a balance you overwrite in Usage-based billing
- ECM-6 Keep one writer for each stock count in E-commerce and fulfilment
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/payments/payment-events-and-reconciliation/payment-ledger">Agent Wranglers payments and billing playbook</a>. In code you write, add a comment next to it with the same link.