Published on ·3 min read·By Manassé N'Dri
VITPAY: designing a financial core that never gets an amount wrong
Amounts as bigints, a double-entry ledger enforced by PostgreSQL, confirmation on server-side proof: the technical choices behind the VITPAY payment platform.
- VITPAY
- NestJS
- PostgreSQL
- Fintech
- Architecture

Context
VITPAY lets merchants in Morocco collect payments through payment links, QR codes or a checkout. The project is in its pilot phase in Casablanca, in sandbox mode.
Its founding principle: VITPAY never holds funds. Customers pay through a licensed payment provider, and VITPAY must know, without any room for error, what was paid, refunded, disputed and then paid out to the merchant. Here are the technical choices that make this reliable.
1. No floating-point numbers for money
Amounts go through a dedicated module, @vitpay/money:
- every amount is a
bigintin minor units (cents); the API carries them as strings; - the currency is always explicit: any operation between two different currencies throws an error;
- decimal places depend on the currency: 2 for MAD, EUR, USD and GBP, 0 for XOF and XAF;
- rounding is explicit:
HALF_EVENby default,HALF_UPfor the platform fee.
The module is covered by property-based tests (fast-check) that check the rules against thousands of generated values.
2. A double-entry ledger, enforced by the database
Every movement is a balanced accounting entry. The balance is not only checked in code: a deferred PostgreSQL trigger rejects any entry whose debits and credits differ, per currency.
- Immutability:
UPDATE,DELETEandTRUNCATEare revoked for the application role. Mistakes are fixed with reversing entries, never by editing. - Idempotency: each entry carries a unique key derived from the business event (for example
payment.succeeded:<attempt id>). Replaying an event creates nothing new. - Isolation: row-level security (RLS) keeps each organization's accounts apart.
Example: for a 500.00 MAD payment with a 1.5% fee, the ledger records 50,000 cents of gross sales, 750 of provider fees and 49,250 receivable. Once reconciled with the provider's report, those 49,250 move to "settled".
3. Confirming a payment only on server-side proof
A payment is confirmed only when all of these conditions are met:
- a webhook arrives with a valid HMAC signature (a secret specific to the merchant account), a timestamp less than 5 minutes old, and has not been processed before;
- VITPAY reads the payment back from the provider, server to server;
- the reference, amount and currency match exactly, otherwise a critical incident is opened and nothing is confirmed;
- the transition is allowed by the payment's state machine.
The browser redirect after payment is never trusted. If a webhook gets lost, a scheduled job queries the provider for attempts still pending.
4. Separate states so nothing gets mixed up
A payment request, an attempt and a confirmation are distinct objects, each with its own state machine. Above all, confirmed does not mean paid out: only reconciliation against the provider's report moves a payment to "settled". The statuses merchants see ("partially refunded", "disputed"…) are computed, never stored.
5. Preventing double payments
- a partial unique index allows only one active attempt per request;
- an idempotency key is mandatory to create a request or a refund;
- if a second payment still gets confirmed, a critical incident is opened: reality is recorded, not hidden;
- refunds are capped under a row lock, taking pending refunds into account.
What the tests prove
The ledger tests show that an unbalanced entry is rejected by the database, that 50 identical entries sent in parallel produce only one, and that 200 concurrent entries end with exact balances. In total, the API has 113 tests (unit and integration against a real PostgreSQL), plus 11 end-to-end browser tests with Playwright.
Takeaways
- Money is never represented with floating-point numbers.
- Critical rules belong in the database, not only in application code.
- A payment is confirmed on proof checked server-side, never on what the browser reports.