Repository navigation
Payments: settle only pending payments for their own order, pay only what is owed - #34
Conversation
…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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| }); | ||
| } | ||
| // 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)); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
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:
Summary
POST /fees/invoices/:id/create-razorpay-order):INVOICE_NOT_PAYABLEwith the reason. An invoice its payments already cover returnsINVOICE_ALREADY_PAID.methodmust be ONLINE (the default), UPI or CARD. Cash or a bank transfer returns 400.POST /fees/payments/verify-razorpay):PAYMENT_NOT_PENDINGand is never changed, whatever the signature.razorpayOrderIdmust be the order this payment was created for (400ORDER_MISMATCH), and the signature is checked against that stored order.PAYMENT_ALREADY_RECORDEDinstead of a 500. The database's unique key on it stops one payment counting twice.REC-2026-3FA9C1B2D4, so a clash can't fail a payment.INVALID_INPUTwith the reason, instead of a plainErrorthat surfaced as a 500.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
amountDueis still what the Admin types.Validation
pnpm test(328 passed, 14 new),pnpm lint,pnpm typecheck,pnpm build,git diff --check.INV-2026-19D912385A.ORDER_MISMATCH.INVALID_PAYMENT_SIGNATUREand marked the payment FAILED. Verifying it again with a good signature returnedPAYMENT_NOT_PENDING.REC-2026-5AFEDD0DE9, and the invoice became PAID.PAYMENT_NOT_PENDING, with a good or a bad signature, and it stayed COMPLETED.INVOICE_ALREADY_PAID.INVOICE_NOT_PAYABLEwith their reasons.PAYMENT_ALREADY_RECORDED. Before the fix it was a 500, found in this run.