Naalya Handbook
School featuresPayments

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:

ModeMeaning
ELIGIBILITYOffered to whoever matches the audience; they pay on demand. Nothing is created up front.
ASSIGNEDA 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.

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.

Payment flow: a payer's fee raises a charge, a pending transaction goes to SchoolPay or Pesapal, and the callback settles it into allocations.

  1. Define a payment item with an amount and an audience.
  2. Raise the charge. For an assigned item the worker has raised it ahead of time. Otherwise pay in payment-item.service.ts raises one now (LedgerService.raiseCharge), or reuses the open one. A per-term item pays through the current term's charge.
  3. Pay. LedgerService.pay checks the payer may pay into that account, then creates a transaction at PENDING that names the charges it intends to pay. A retry or a double click returns the pending transaction instead of a new one.
  4. Register. The transaction goes to the chosen gateway, which returns a redirect URL and an external reference.
  5. Settle. The gateway calls back. TransactionService.handleCallback does not trust the callback body: it asks the gateway again (the adapter's parseCallback). A success queues a SETTLE_PAYMENT job on the worker's ledger queue, a pending result waits for the reconciliation job to check again, and a failure marks the transaction FAILED. In the worker, LedgerSettlementService.settle locks the transaction, sets it to SUCCESS, and writes allocations to each open charge, capped at what that charge still owes. Then it emits paymentSettled.
  6. 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

apps/server/src/app/transaction/entities/transaction.entity.ts
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 extras

Provider-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

On this page