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.
Every route here needs Feature.BURSARY. @Public() skips that guard but not the checks inside the services. SchoolPay pay-code webhooks are a separate controller; see SchoolPay inbound.
Which route starts a payment
| Who | Route | Use it for |
|---|---|---|
| Any signed-in user | POST /payment-items/:id/pay (payPaymentItem) | A fee the school defined |
Account holder, verified guardian, or staff with create on ledger | POST /ledger/accounts/:accountUserId/pay (payLedgerAccount) | Paying what an account owes, or adding credit. The Hub's pay screen |
| Students and guardians | POST /transactions (createTransaction) | An ad-hoc payment with a reason |
| Anyone with the reference | POST /transactions/:reference/request-mobile-payment | Sending the phone prompt for a pending payment |
Every route returns a pending transaction with a provider reference. A phone prompt or the hosted page's redirectUrl comes next. Nothing is posted until the provider calls back.
Payment items
PaymentItemController at /api/v1/payment-items.
| Route | Operation id | Needs | Notes |
|---|---|---|---|
GET /me/eligible | listMyEligiblePaymentItems | Staff, student, guardian or guest | Active items the caller may pay, with paid status |
GET /me/charges | listMyPaymentCharges | Same | Charges for ASSIGNED items |
POST /:id/pay | payPaymentItem | Same | Optional provider (school_pay, pesapal, manual), resourceId (for resource-bound items), email, phoneNumber |
POST / | createPaymentItem | create payment_item | |
GET / | listPaymentItems | list payment_item | |
GET /charges | listPaymentCharges | list payment_item | |
GET /billing/preview | previewTermBilling | list payment_item and list ledger | Term not found., Term does not belong to this campus. |
POST /billing/raise | raiseTermFees | create ledger and create payment_item | Query campusId. Refused for a locked year. Jobs are idempotent |
GET, PATCH, DELETE /:id | getPaymentItem, updatePaymentItem, deletePaymentItem | read, update, delete payment_item | |
GET /:id/charges | listPaymentItemCharges | read payment_item | |
POST /:id/materialize | materializePaymentItemCharges | update payment_item | Queues charges. Refused unless the item is ASSIGNED |
The static me and billing paths are declared before /:id, so they are never read as ids.
Transactions
TransactionController at /api/v1/transactions.
| Route | Operation id | Who | Notes |
|---|---|---|---|
POST / | createTransaction | Student or guardian | reason (admission_fees, tuition_fees, school_uniform, other), campusId, amount, firstName, lastName; optional paymentItemId, resourceId, provider, email, phoneNumber |
GET / | listTransactions | list on transaction | |
GET /:reference | getTransactionByReference | Public | Looks up by reference across schools. Does not check the bursary flag |
POST /:reference/request-mobile-payment | requestMobilePaymentByReference | Public | Only for pending. Checks the bursary flag for that school and campus. The provider must support phone prompts |
POST /callback/:provider | transactionProviderCallback | Public | school_pay, pesapal or manual, else 400 |
From callback to posted money
Find the payment. handleCallback reads the provider's reference and finds the transaction by externalReference, or by a phone-prompt entry in providerMetadata.
Verify it. Campus credentials are loaded and parseCallback checks the provider's signature. A non-pending payment is acknowledged and left alone.
Record the outcome. applyOutcome: on success it stores the receipt fields and queues SETTLE_PAYMENT (job id settle-{transactionId}, removing a failed job with that id first). Pending stores metadata. Anything else marks the payment failed and emits paymentFailed.
Post it. The worker's LedgerSettlementService.settle locks the row and continues only for pending or cancelled. It sets success, then allocates to the intended charges in id order, each up to the smallest of the requested line, the charge's outstanding, and what is left of the payment. The rest is credit.
Announce it. After commit, paymentSettled fires once. A second settle on a success row does nothing, so nothing posts or notifies twice.
A 200 from the callback is not proof of payment
A missing or unknown reference, or a failed signature, returns acknowledged: false, but the HTTP reply is still Callback processed (Pesapal gets status 500 in its body). A queued SETTLE_PAYMENT job has not posted either. Only a committed settle has.
manual passes the controller's check but is not a registered adapter (only school_pay and pesapal are), so its callback is refused with Unsupported payment provider: manual. Manual payments are posted by settleManual (for pending or pending_verification) or recordAndSettle, not by a callback.
After the money is posted
paymentSettled sets off:
| What | To whom | Notes |
|---|---|---|
PAYMENT_RECEIVED email | The payer (payerUserId), not necessarily the account holder | Subject Payment received: plus the school. PDF receipt attached. Links to /guest/account, /guardian/payments or /staff-hub/account by user type; anyone else to / |
PAYMENT_RECEIVED WhatsApp | The payer | The school's saved wording, or the default if the saved one has missing or unknown tokens. No PDF |
| Receipt archive | A PDF job receipt-{transactionId}, 3 attempts | |
LEDGER_CHANGE | Staff screens on that account | Realtime refresh |
| Staff alert | The school's Teams route, and its WhatsApp route if set | Not the payer's notification |
None of these can unsettle a payment. Failures are logged and the money stays posted. The payload's balanceAfter is what the account owes minus credit, never below zero.
Reading an account
GET /ledger/accounts/me (getMyLedgerAccount) and GET /ledger/accounts/:accountUserId (getLedgerAccount) need read on ledger for that account. Students and staff can read their own; a verified guardian can read their children's. Anyone else gets Access denied to this resource.
| Field | Means |
|---|---|
| Open charges | open charges with outstanding above zero (amount minus waived minus allocated) |
overdue | Outstanding on charges whose dueAt has passed. No dueAt (an admission fee) is never overdue |
credit | Every successful payment's amount minus what was allocated, across the whole account |
balance | Outstanding minus credit. Negative when credit exceeds what's owed |
recentPayments | The latest 20 transactions, any status |
In the Hub
The account page (AccountPage):
| Situation | Wallet shows |
|---|---|
| Something owed | To pay, the amount owed, and any credit beside it |
| Nothing owed, charges remain | Balance, the credit, "Your credit covers what you owe" |
| No charges | Balance, "You are all paid up" |
Use credit appears when there is credit and an open charge. Payers aren't on the realtime bus, so the page polls every 10 seconds while any payment is pending, and refetches on focus.
The pay screen (PayAccountPage):
Choose what to pay. Every open charge starts ticked (or only the one in chargeId). With nothing owed, enter an amount; anything not put on a fee becomes credit.
Choose a campus, only for an amount with no fee when there's more than one payable campus.
Confirm with a phone number and optional email.
Choose how to pay, only when the campus has more than one provider. The Hub calls payLedgerAccount, which defers registering with the provider.
The result opens /pay/$reference: Pesapal goes on to redirectUrl; SchoolPay offers a phone prompt or the pay code; a paid admission fee returns to its application. Download receipt opens a signed link.
payLedgerAccount refuses a request with no allocations and no amount, lines that add up to more than the amount, a campus the account can't pay at, and anyone other than the holder, a verified guardian, or staff with create on ledger (the route decorator only asks for read).