Payments
How the platform charges for things, items, audiences, charges, transactions, and the gateways that move the money.
The payments domain is how a school charges for things, a trip fee, an application fee, and tracks who has paid. It splits the job across six pieces. Learn the pieces once and the rest of the domain reads cleanly.
The five pieces
- Payment item, a fee definition: the thing a school charges for. It carries an amount and an audience (a rule for who it applies to). Lives in
payment_item. - Audience: the declarative rule describing who an item is for (a user type, a named list of users, an owned resource). Stored as JSON on the item and resolved when a payer asks.
- Payment charge, a per-account obligation: "this account owes this amount for this item." Raised ahead of time for assigned items, and on the spot when someone pays any other item. Lives in
payment_charge, with an amount, a waived amount, and a status (open,waived,cancelled). - Transaction, the actual money movement: one payment attempt. Lives in
transaction, and carries the allocations it intends to make. - Allocation: the part of a successful transaction applied to one charge. Lives in
payment_allocation. One payment can pay down several charges, and money not yet allocated stays as credit on the account. - Gateway: an external payment provider (Pesapal, SchoolPay) that takes the money. Each school configures its gateways per campus, with encrypted credentials.
The first five are yours (rows in your database). The gateway is theirs (a third party). A transaction is the bridge: you create it, hand it to a gateway, and the gateway tells you when it settled.
Two ways to collect
A payment item is collected one of two ways, its collection mode:
| Mode | Meaning |
|---|---|
ELIGIBILITY | Offered to whoever matches the audience; they pay on demand. Nothing is created up front. |
ASSIGNED | A payment charge is created up front per matching user: an explicit obligation they can see they owe. |
For an ASSIGNED item, the charges are materialized (the rule is expanded into one concrete payment_charge row per matching user) by a background worker: see enqueueMaterialization in apps/server/src/app/payment-item/payment-item.service.ts. Materialization is idempotent (safe to re-run; it won't double-create), so editing the audience or backfilling late joiners just re-runs the job.
Paid is worked out from allocations
A charge does not store a paid flag. What it still owes is its amount, minus any waived amount, minus every allocation against it. It is paid when that reaches zero (isItemPaid in apps/server/src/app/payment-item/payment-item.service.ts, using LedgerService.allocatedByChargeIds). A waiver lowers what is owed without any money moving.
The one-line why: money is the source of truth. Allocations are written in the same database transaction that marks the payment successful, so the two cannot disagree.
The end-to-end flow
This picture is one payment, from a fee to a settled charge.
- Define a payment item with an amount and an audience.
- Raise the charge. For an assigned item the worker has raised it ahead of time. Otherwise
payinpayment-item.service.tsraises one now (LedgerService.raiseCharge), or reuses the open one. A per-term item pays through the current term's charge. - Pay.
LedgerService.paychecks the payer may pay into that account, then creates atransactionatPENDINGthat names the charges it intends to pay. A retry or a double click returns the pending transaction instead of a new one. - Register. The transaction goes to the chosen gateway, which returns a redirect URL and an external reference.
- Settle. The gateway calls back.
TransactionService.handleCallbackdoes not trust the callback body: it asks the gateway again (the adapter'sparseCallback). A success queues aSETTLE_PAYMENTjob on the worker's ledger queue, a pending result waits for the reconciliation job to check again, and a failure marks the transactionFAILED. In the worker,LedgerSettlementService.settlelocks the transaction, sets it toSUCCESS, and writes allocations to each open charge, capped at what that charge still owes. Then it emitspaymentSettled. - Tell people. Listeners send the payer a payment-received notice, alert staff, refresh open Hub screens, and check whether a referral commission is now earned.
Staff can also record a cash or bank payment (settleManual), and a SchoolPay pay code can arrive without a pending transaction (SchoolPay inbound). Both settle through the same service.
Read at API a30a88580.
A transaction in one glance
amount: number;
status: TransactionStatus; // PENDING → SUCCESS | FAILED | ...
provider: TransactionProvider; // school_pay | pesapal | manual
paymentItemId?: string; // the fee this settles, if any
reference: string; // our id, unique
externalReference?: string; // the gateway's id
providerMetadata?: Record<string, unknown>; // provider-specific extrasProvider-agnostic fields and provider-specific ones (a SchoolPay receipt number, say) sit side by side, so reporting queries can treat every transaction uniformly regardless of which gateway took the money.
Where to go next
Payment Items
The fee definition: scope, amount, collection mode, and the assigned-charge lifecycle.
Audiences
The rules that decide who a payment item is for, and how eligibility is resolved.
Charges vs Transactions
Obligation vs money movement, and how allocations make a charge paid.
Gateways
The provider adapters that register and settle transactions.
Gateway Config
Per-campus provider setup with encrypted credentials.
Campus bursary
Accounts, raising fees, recording a payment, defaulters, and the SchoolPay inbox screen.
SchoolPay inbound
The pay-code webhook, the three-day pull, and the staff inbox.
Item and transaction routes
Create a fee, pay it, and the public transaction lookup and callback.
Enquiries
Public website enquiries, signed-in applicant threads, staff updates, and the WhatsApp enquiry link.
Payment item and transaction operations
The bursary routes that define a fee, start a payment and take the provider callback, what happens when the money is posted, and how the Hub shows an account.