Wallet
Overview
A prepaid tenant wallet and ledger. Confirming an order enqueues an order-fee debit. Razorpay is the payment dependency for top-up intents (INR only for now). Stripe appears as a WhatsApp checkout adapter, not as the wallet’s primary client.
Money model
Wallet.balanceand every ledger row are whole tokens. Each wallet currency has a fixed token value that never changes: INR 100 tokens = ₹1; USD, AUD, GBP and EUR 1000 tokens = 1 unit. Seewallet-money.util.ts.- The order fee is
TenantSubscription.tokensPerOrder(default 100 = ₹1.00). Platform admins change it withPATCH /v1/finance/admin/wallets/:tenantId/pricing, which writes anaudit.logentry with the old and new value and a reason. - Top-ups take a money
amountin the wallet currency (tax-inclusive, at most 2 decimals, at least the per-currency minimum). Tokens credited = amount × token value. - Order fees always post, even below zero (
allowNegative). There is no lower limit; the next top-up clears the debt. Admin manual debits keep the balance check. - API responses add money fields next to tokens:
balanceAmount,amountDue,orderFee,ordersLeft, andamount/balanceBeforeAmount/balanceAfterAmounton ledger rows. A wallet currency without a token value getsnullmoney fields.
Code map
| Piece | Path |
|---|---|
| Feature | src/modules/finance/wallet |
| Listener | wallet.listener.ts |
| Schema | 52-wallet.prisma |
| Razorpay package | src/integrations/payments |
Entities: Wallet, WalletLedgerEntry, PaymentIntent, PaymentEvent.
HTTP: /finance/wallets, /finance/admin.
Business rules
Implementation behavior
order.confirmedis handled byWalletListener, which callsWalletService.deductToken. The order HTTP response does not wait for the debit.Status
Listener is loaded for the worker because it consumes an outbox event.
Implementation behavior
Errors propagate and the outbox retries. Low balance is never a reason to fail: the fee posts and the balance goes negative. Only a missing wallet or a database error makes the debit fail; after 10 attempts the event is DEAD and the order is not charged. See Wallet debit.
The debit is idempotent per order: the key order-token-deduction:{orderId} is unique per tenant (@@unique([tenantId, idempotencyKey])). The fee is not refunded when an order is cancelled or returned.
Events
Only order.confirmed was found as the wallet trigger. Label failure does not, by itself, enqueue a reversing outbox event in the producer list that was scanned.
Integrations
Razorpay env keys and webhook secret. Setting the key id makes the secret and webhook secret mandatory at boot. WhatsApp payment webhooks accept Stripe and Razorpay signatures. See Payments.