Skip to main content

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.balance and 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. See wallet-money.util.ts.
  • The order fee is TenantSubscription.tokensPerOrder (default 100 = ₹1.00). Platform admins change it with PATCH /v1/finance/admin/wallets/:tenantId/pricing, which writes an audit.log entry with the old and new value and a reason.
  • Top-ups take a money amount in 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, and amount / balanceBeforeAmount / balanceAfterAmount on ledger rows. A wallet currency without a token value gets null money fields.

Code map​

PiecePath
Featuresrc/modules/finance/wallet
Listenerwallet.listener.ts
Schema52-wallet.prisma
Razorpay packagesrc/integrations/payments

Entities: Wallet, WalletLedgerEntry, PaymentIntent, PaymentEvent.

HTTP: /finance/wallets, /finance/admin.

Business rules​

Implementation behavior

order.confirmed is handled by WalletListener, which calls WalletService.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.