Naalya Handbook
School featuresPayments

Charges vs transactions

A charge is what is owed; a transaction is the money moving, how the two differ and connect.

A charge is what is owed. A transaction is the money moving. They are two different tables for two different jobs: one records the bill, the other records each payment attempt.

At a glance

Payment chargeTransaction
What it isA materialized obligation, one row per (item, user)A money-movement record, one payment attempt/settlement
Tablepayment_chargetransaction
When createdAhead of time for an ASSIGNED item (background job), or when someone pays any other itemWhen someone pays, through a gateway, or when staff record a payment
Paid statusWorked out, amount, minus waived, minus allocationsStored, its own status column
Exists for ELIGIBILITY items?Yes, once someone paysYes

Payment charge

A payment charge (entity payment_charge, in apps/server/src/app/payment-item/entities/payment-charge.entity.ts) is a materialized obligation: a pre-computed bill row that says "this user owes this amount for this item". Materialized means it is written to a real table ahead of time, not computed on the fly.

  • At most one open charge per item and account. Three partial unique indexes enforce it: once per item and user (UQ_payment_charge_item_user_once), once per term for a per-term item (UQ_payment_charge_item_user_term), and once per owned resource such as an application (UQ_payment_charge_item_user_resource). Each counts only rows with status open that are not deleted, so a waived or cancelled charge does not block a new one.
  • The worker's bulk insert uses ON CONFLICT ... DO NOTHING against the matching index, so re-running the sync only adds charges for new targets.
apps/server/src/app/payment-item/entities/payment-charge.entity.ts (fields)
reference: string;              // unique
paymentItemId: string;
paymentItemReference: string;
userId: string;                 // the account that owes it
studentId?: string;
campusId?: string;
campusAcademicYearId: string;
amount: number;
termId?: string | null;         // set for a per-term item
dueAt: Date | null;             // null for an admission fee: owed, never overdue
waivedAmount: number;           // default 0
adjustmentReason, adjustedBy, adjustedAt; // who last waived or changed it
resourceId?: string | null;     // the owned resource, such as an application
// plus due, overdue, and admission reminder timestamps
status: PaymentChargeStatus;    // open, waived, cancelled

The status column says whether a charge is open, waived, or cancelled. It does not say whether the charge is paid. That is its amount, minus waivedAmount, minus the sum of its payment_allocation rows; zero or less is paid (isItemPaid and listMyCharges in payment-item.service.ts).

How charges are created

Charges are written by a BullMQ background job: a task pushed onto a queue and run later by a worker, off the request path. The PaymentChargeProcessor on the PAYMENT_CHARGE_QUEUE (in apps/worker/src/processors/payment-charge/payment-charge.processor.ts) does the work.

Its payload carries schoolId explicitly:

libs/shared/src/payment-charge/payment-charge.types.ts
export interface PaymentChargeJobPayload {
  paymentItemId: string;
  schoolId: string;
}
  • The worker runs outside request context: there is no logged-in user and no tenant (the school the data belongs to) bound automatically. So the job carries schoolId and the processor binds the tenant from it, then loads the item scoped by it.
  • It resolves the item's audience (the rules naming who the item applies to) via raw SQL: students (through enrollment, filtered to the item's campusAcademicYearId, only that year's active enrollments become charges) and specific_users (direct). user_type rules are not materialized: they stay eligibility-based. Each charge row records the item's year.
  • It bulk-inserts charges in chunks of 500 rows (INSERT_CHUNK), with ON CONFLICT DO NOTHING so re-syncs only add new targets.

See Queues & Messaging for how jobs are enqueued and run, and Audiences for the rule shapes.

Transaction

A transaction (entity transaction, in apps/server/src/app/transaction/entities/transaction.entity.ts) is the canonical money-movement record: one payment attempt or settlement through a gateway (the external provider that actually moves the money).

apps/server/src/app/transaction/entities/transaction.entity.ts
@Entity('transaction')
@TenantScoped()
export class TransactionEntity extends DatabaseEntity {
  @Column({ type: 'numeric' }) amount: number;
  @Column({ type: 'enum', enum: TransactionStatus }) status: TransactionStatus;
  @Column({ type: 'enum', enum: TransactionReason }) reason: TransactionReason;
  @Column({ type: 'enum', enum: TransactionProvider }) provider: TransactionProvider;
  @Column({ name: 'user_id', type: 'uuid' }) userId: string;
  @Column({ name: 'payment_item_id', type: 'uuid', nullable: true }) paymentItemId?: string;
  @Column({ unique: true }) reference: string;
  @Column({ name: 'external_reference', nullable: true }) externalReference?: string;
  @Column({ name: 'receipt_number', nullable: true }) receiptNumber?: string;
  @Column({ name: 'redirect_url', type: 'text', nullable: true }) redirectUrl?: string;
  @Column({ name: 'provider_metadata', type: 'jsonb', nullable: true })
  providerMetadata?: Record<string, unknown>;
}

Key fields:

  • status (TransactionStatus), the stored lifecycle: PENDING → SUCCESS / FAILED (plus CANCELLED, REFUND_PENDING, REFUNDED)
  • provider (TransactionProvider), which gateway handled it; reason (TransactionReason), why the payment was made.
  • paymentItemId?: links back to the item being paid for (nullable; not every transaction maps to one item).
  • resourceId?: for a resource-bound item, the specific instance paid for (e.g. the applicationId). The (paymentItemId, resourceId) pair is what makes a retry reuse the same transaction instead of double-charging.
  • reference, our unique id for the attempt; externalReference, the provider's id; receiptNumber: the provider's receipt.
  • redirectUrl, where to send the user when the gateway needs a hosted page; providerMetadata, the raw provider payload (jsonb).

A transaction is created when someone pays, then driven through the gateway adapter (the class wrapping one provider): register the charge → instant result or redirect → the provider calls back via a webhook (an HTTP callback the provider POSTs to us) → handleCallback re-checks it with the gateway (parseCallback), and a success is settled by the worker, which sets SUCCESS and writes the allocations. See Gateways.

How they connect

Item typeChargeTransaction on paymentWhat marks it paid
ELIGIBILITYRaised by pay when someone pays, or the open one reusedyesallocations from a successful transaction cover what the charge owes
ASSIGNEDOne per account, raised ahead of time by the workeryesthe same: allocations cover what the charge owes
  • Both kinds end the same way. A successful transaction is split into payment_allocation rows against open charges, and a charge is paid when its amount, minus waivedAmount, minus its allocations reaches zero.
  • An ASSIGNED item has a charge per user; a successful transaction is what makes that charge show as paid.

The charge is the bill. The transaction is the payment. When money arrives, the charge row itself does not change: settlement adds payment_allocation rows against it, and those are what make it paid.

Where to go next

On this page