Skip to content

Payments: settle only pending payments for their own order, pay only what is owed - #34

Merged
anishmehta24 merged 2 commits into
mainfrom
fix/payment-verification
Nov 18, 2026
Merged

anishmehta24 merged 2 commits into
mainfrom
fix/payment-verification

Conversation

@anishmehta24

@anishmehta24 anishmehta24 commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to the audit of fees and payments. #16 limited the test signature to test mode and made students pay only their own invoices, but some gaps were left:

  • Verifying a payment didn't check its state, so a bad signature could turn a completed payment into FAILED.
  • The signature wasn't tied to the order the payment was made for.
  • A void or draft invoice could be paid.
  • The request chose the payment method, so an online payment could be recorded as cash.
  • A part-paid invoice was charged its full amount again.
  • Invoice and receipt numbers came from six random digits, so a clash failed the payment with a 500.

Summary

  • Orders (POST /fees/invoices/:id/create-razorpay-order):
    • Only ISSUED, PARTIALLY_PAID or OVERDUE invoices can be paid. Draft and void ones return 409 INVOICE_NOT_PAYABLE with the reason. An invoice its payments already cover returns INVOICE_ALREADY_PAID.
    • The order is for the balance still owed: the amount due minus completed payments.
    • From review: an invoice has at most one open order. A second tab or a retry gets the same order back, any other open order for it is closed, and one for an out-of-date amount is replaced. Orders for an invoice are made one at a time, with the invoice row locked. Without this, two tabs could each order the full balance and charge twice.
    • method must be ONLINE (the default), UPI or CARD. Cash or a bank transfer returns 400.
  • Verification (POST /fees/payments/verify-razorpay):
    • Only a PENDING payment is settled. A completed or failed one returns 409 PAYMENT_NOT_PENDING and is never changed, whatever the signature.
    • razorpayOrderId must be the order this payment was created for (400 ORDER_MISMATCH), and the signature is checked against that stored order.
    • A bad signature marks the payment FAILED only while it is still pending.
    • Settling claims the payment with a conditional update, so two verifications can't both complete it.
    • It then locks the invoice and adds up its completed payments: the invoice becomes PAID once they cover the amount due, and PARTIALLY_PAID before that.
    • A Razorpay payment id that was already recorded on another order returns 409 PAYMENT_ALREADY_RECORDED instead of a 500. The database's unique key on it stops one payment counting twice.
  • Numbers: invoices and receipts end in ten random hex characters, for example REC-2026-3FA9C1B2D4, so a clash can't fail a payment.
  • Bad input: the fee DTO parsers now return 400 INVALID_INPUT with the reason, instead of a plain Error that surfaced as a 500.
  • Page: Pay Now appears only on invoices that can be paid. Void ones say "Cancelled: nothing to pay." and draft ones "Not issued yet." The success message shows the amount actually charged.
  • Test fix: one announcement test from Announcements: history, edit, archive and restore for Admin #32 was timing-dependent. Its mock built a "published" row with a fresh new Date() that could land a millisecond after the service's clock and read as scheduled. It now uses a fixed past time.

Rules used

  1. Online payment methods are ONLINE, UPI and CARD. Cash and bank transfers are taken at the accounts office, which has no API yet.
  2. A payment whose order was paid twice in Razorpay is still recorded, so the invoice shows the real total. Refunds are out of scope.
  3. Not in this PR:
    • The real Razorpay Checkout and order API. The web still only completes simulated payments in test mode, and order ids are still made locally.
    • Fee structure amounts on invoices: amountDue is still what the Admin types.

Validation

  • pnpm test (328 passed, 14 new), pnpm lint, pnpm typecheck, pnpm build, git diff --check.
  • Locally, as the seeded student, using temporary 1000 invoices (deleted afterwards):
    • New numbers: invoice numbers look like INV-2026-19D912385A.
    • Failure cases:
      • A wrong order id returned ORDER_MISMATCH.
      • A bad signature returned INVALID_PAYMENT_SIGNATURE and marked the payment FAILED. Verifying it again with a good signature returned PAYMENT_NOT_PENDING.
    • A good payment:
      • It completed with receipt REC-2026-5AFEDD0DE9, and the invoice became PAID.
      • Verifying it again returned PAYMENT_NOT_PENDING, with a good or a bad signature, and it stayed COMPLETED.
      • A new order on the paid invoice returned INVOICE_ALREADY_PAID.
    • Unpayable invoices: void and draft ones returned INVOICE_NOT_PAYABLE with their reasons.
    • Part payment: with 400 paid in cash on a 1000 invoice, the order was for 600.
      • Reusing a Razorpay id that was already recorded returned PAYMENT_ALREADY_RECORDED. Before the fix it was a 500, found in this run.
      • Paying with a new id made the invoice PAID with 1000 in total.
    • Bad input: a CASH order returned 400, an empty verification 400, and a negative invoice amount 400.
    • After the review fix: ordering twice for one invoice returned the same payment and order id, leaving one pending payment. Paying it made the invoice PAID with 1000 in total.
    • In the browser: the student's Fees page showed "Cancelled: nothing to pay." on the void invoice and "Payment Complete" on the paid ones.

…only what is owed

Verification now settles a payment only while it is PENDING (conditional
update), only for the order it was created for, and never flips a completed
payment to FAILED. Orders refuse draft and void invoices, charge the balance
still owed, and take only online methods. The invoice becomes PAID once its
completed payments cover it. Invoice and receipt numbers can no longer clash,
a reused Razorpay payment id is a 409, and bad fee input is a 400.

Also pins a timing-dependent announcement test to a fixed time.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T09:38:58.997050Z df0381e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: df0381e2bf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/api/src/fees/fees.service.ts Outdated
});
}
// The order is for what is still owed, so an earlier part-payment is never charged again.
const balance = Number(invoice.amountDue) - (await paidSoFar(this.database, context.collegeId, invoice.id));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reserve pending invoice balances before creating another order

If a student creates two orders before either is verified—for example, from two tabs—paidSoFar excludes both pending payments, so each order is for the full remaining balance. If both are paid, they can charge more than the invoice amount. Reserve in-flight balances or prevent another pending order for the invoice.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8aa420a. An invoice now has at most one open order: a second tab or a retry gets the same order back, any other open order for it is closed, and one for an out-of-date amount is replaced. Orders for an invoice are made one at a time with the invoice row locked.

A second order for the same invoice, from another tab or a retry, now gets
the open order back instead of a new one, so the balance can't be ordered
and charged twice. Any other open order is closed, one for an out-of-date
amount is replaced, and orders for an invoice are made one at a time with
the invoice row locked.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant