Harden ITN handling: validate paid amount and dedupe repeated notifications#16
Open
madsnorgaard wants to merge 1 commit into
Open
Conversation
Two defects in onNotify()/processOrder() that affect payment integrity: 1. Amount tampering. The ITN signature is verified, but the gross amount originates from the process-page POST the buyer controls. The handler recorded amount_gross verbatim without comparing it to the order total, so a completed payment could be booked for less than the cart value. Now the paid gross is compared to $order->getTotalPrice() (ZAR, 0.01 tolerance) and mismatches are logged and rejected. 2. Duplicate payments. Payfast re-sends the ITN until it receives a 200, so each retry created another completed payment for the same pf_payment_id. processOrder() now short-circuits when a payment with that remote_id already exists. loadPaymentByRemoteId() is corrected to query remote_id (the Payfast transaction id) instead of order_id, which every payment on the order shares, and to bail out safely if the payment storage cannot load.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two payment-integrity defects in `onNotify()` / `processOrder()`, independent of #12.
1. Amount tampering (security)
The ITN signature is verified, but `amount_gross` originates from the process-page POST the buyer controls. The handler recorded it verbatim, so a completed payment could be booked for less than the cart total. The paid gross is now compared to `$order->getTotalPrice()` (ZAR, 0.01 tolerance); mismatches are logged and rejected. This mirrors the amount check in PayFast's own ITN integration guidance.
2. Duplicate payments (idempotency)
PayFast re-sends the ITN until it receives a 200, so each retry created another
completedpayment for the samepf_payment_id.processOrder()now returns early when a payment with thatremote_idalready exists.Supporting fix
loadPaymentByRemoteId()queriedorder_id(shared by every payment on the order) despite its name; it now queriesremote_id(the PayFast transaction id) and returnsFALSEsafely if payment storage cannot load.Scope: one file,
OffsiteRedirect.php. No behaviour change for correctly-formed single ITNs.