Files
Crussell/backend/handlers/payments/charge_helpers.go
T
popertots 5e3dc9b428 fix: comprehensive payment system hardening (4 review passes)
CRITICAL fixes:
- C1: JWT exp claim now validated via jwtauth.VerifyToken (was Decode)
- C2: OverrideAmount validated post-substitution (prevents negative money minting)
- C3: Terminal gift-card payments store gift_card_id; refund credits user balance
- C4: Refund dedup returns stored amount, not req.Amount (prevents admin mislead)
- C5: Booking recheck uses FOR UPDATE (prevents TOCTOU with cancellation)
- C6: processChargeGroup idempotency key stable (charge-only, prevents double-refund)

MAJOR fixes:
- M2: Gift-card refund UPDATE checks RowsAffected; 0 rows -> failed
- M3: ProcessCancellationRefund returns commit error (was swallowed)
- M5: Dispute webhook handling (created + state.updated + disputes table)

MEDIUM fixes:
- ME1: CORS restricted to FRONTEND_ORIGIN env var (was reflect-any)
- ME2: anonymize_user() scrubs users.notes, bookings.notes, name_history, refresh_tokens
- ME3: Webhook handlers now mutate state (payment.updated, refund.updated)

Frontend fixes:
- Same-key retry on 503 (ambiguous failure) wired to all 8 payment flows
- CHARGE_AND_STORE intent for save-card flows (SCA compliance)
- Nonce staleness check verified across all flows

Additional fixes from adversarial re-review:
- F1: Till-sale completed dedup echoes stored amount (C4-class)
- F2: Cash/giftcard terminal path uses FOR UPDATE (C5-class)
- F3: Square-success UPDATE checks RowsAffected (till sales)
- F4: Dispute reason truncated to 192 chars (prevents INSERT failure)
- F5: Booking-user lookup failure marks refund failed (prevents silent money loss)
- F6: Saved-card/tip rechecks wrapped in transaction (C5 residual)

Tests:
- 15 adversarial attack tests (negative override, zero override, terminal gift card,
  refund dedup, TOCTOU, deleted gift card, advisory lock, overcharge, zero/negative/huge
  amount, raw PAN, missing auth, gift card balance, concurrent refunds)
- 14 webhook state tests (dispute created/state, payment/refund updated)
- 3 CORS tests, 3 GDPR tests, 1 HTTP timeout test
- Full suite passes with -race (25 packages, 0 failures)

25 files changed, +1532/-275 lines
2026-08-22 00:34:49 +01:00

173 lines
7.8 KiB
Go

package payments
import (
"context"
"errors"
"log"
"net/http"
"crussell/db"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
// resolveChargeSource resolves the Square payment source for a card charge,
// shared by CreateBookingPayment, CreateTipPayment, BuyGiftCard, and the
// CreateTerminalPayment saved-card branch (which passes the booking user's id
// and a nil new-card token).
//
// New-card path (cnon: nonce): the nonce is used DIRECTLY for one-off charges
// (no card-on-file is created — the old tokenize-then-charge flow left orphan
// cards at Square). When saveCard is true the user's Square customer is
// provisioned FIRST and the card is tokenized against it (a ccof: source MUST
// carry its customer — R6), then saved via SaveCardForUser.
//
// Saved-card path (ccof:): a saved-card row predating P14 has an empty
// square_customer_id; the user's Square customer is lazily provisioned and
// persisted on the row BEFORE charging (a ccof: source can never be charged
// without a CustomerID).
//
// On any error the helper writes the HTTP response and returns ok=false — the
// caller must return immediately.
func resolveChargeSource(ctx context.Context, w http.ResponseWriter, svc *PaymentService, userID string, newCardToken, cardID *string, saveCard bool, notFoundMsg string) (sourceID string, savedCardID *string, squareCustomerID string, ok bool) {
if newCardToken != nil && *newCardToken != "" {
if saveCard {
sqCustomerID, custErr := svc.EnsureSquareCustomer(ctx, userID)
if custErr != nil {
log.Printf("Failed to provision Square customer for user %s: %v", userID, custErr)
http.Error(w, "Failed to process card", http.StatusInternalServerError)
return "", nil, "", false
}
cardOnFile, err := SquareClient.CreateCardOnFile(ctx, userID, *newCardToken, sqCustomerID)
if err != nil {
log.Printf("Failed to create card on file: %v", err)
http.Error(w, "Failed to process card", http.StatusInternalServerError)
return "", nil, "", false
}
sourceID = cardOnFile.CardID
squareCustomerID = sqCustomerID
// CreateCardOnFile runs before the charge. If the subsequent payment
// fails, this card-on-file is intentionally NOT deleted: the pending
// record's retry re-creates it via the deterministic sha256
// idempotency key (the SAVE path), and Square returns the same card —
// deleting it would break that retry.
savedRowID, saveErr := svc.SaveCardForUser(ctx, userID, sqCustomerID, cardOnFile.CardID, cardOnFile.Brand, cardOnFile.Last4, cardOnFile.ExpMonth, cardOnFile.ExpYear, cardOnFile.Fingerprint)
if saveErr != nil {
log.Printf("Failed to save card: %v", saveErr)
} else {
savedCardID = &savedRowID
}
} else {
// One-off new-card charge: use the cnon: nonce DIRECTLY as the
// source. No card-on-file is created (nothing to orphan, no
// customer needed).
sourceID = *newCardToken
}
if savedCardID == nil && saveCard {
log.Printf("Card was not saved despite save_card=true for user %s", userID)
}
return sourceID, savedCardID, squareCustomerID, true
}
if cardID != nil {
card, err := svc.GetCardByID(ctx, *cardID, userID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
http.Error(w, notFoundMsg, http.StatusNotFound)
return "", nil, "", false
}
log.Printf("Failed to get card: %v", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return "", nil, "", false
}
if card.SquareCustomerID == "" {
if userID == "" {
// Defensive parity with the original saved-card block: a card
// with no bookable owner cannot be provisioned. Unreachable in
// practice — GetCardByID above filters on user_id and would
// have 404'd for an empty owner.
http.Error(w, "Saved card has no owner and cannot be charged", http.StatusBadRequest)
return "", nil, "", false
}
provisioned, provErr := svc.EnsureSquareCustomerForSavedCard(ctx, *cardID, userID)
if provErr != nil {
log.Printf("Failed to provision Square customer for saved card %s (user %s): %v", *cardID, userID, provErr)
http.Error(w, "Failed to process card", http.StatusInternalServerError)
return "", nil, "", false
}
card.SquareCustomerID = provisioned
}
return card.SquareCardID, cardID, card.SquareCustomerID, true
}
// Neither a new-card token nor a saved card — validation upstream
// (ValidateCardInfo) guarantees one of them is present.
return "", nil, "", false
}
// acquireBookingPaymentLock acquires a pinned pool connection and a bounded
// try-lock (R6) on lockKey, serializing payment attempts per booking (the core
// defence against the two-tab double-payment race). A blocking pg_advisory_lock
// would hold the pinned pool connection for the full Square round-trip of
// whichever request holds the lock; the bounded try-lock loop gives up after
// ~3s and surfaces a 409 instead of exhausting the pool. On any failure the
// helper writes the HTTP response and returns ok=false — the caller must
// return. On success the caller MUST defer releaseBookingPaymentLock(pinConn,
// lockKey): the lock and connection stay held for the whole handler so
// pg_advisory_unlock runs on the SAME session that acquired the lock.
func acquireBookingPaymentLock(ctx context.Context, w http.ResponseWriter, lockKey, conflictMsg string) (*pgxpool.Conn, bool) {
pinConn, err := db.Conn.Acquire(ctx)
if err != nil {
log.Printf("Failed to acquire connection for payment lock (%s): %v", lockKey, err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return nil, false
}
lockOK, err := acquireAdvisoryLock(ctx, pinConn, lockKey)
if err != nil {
pinConn.Release()
log.Printf("Failed to acquire payment serialization lock %s: %v", lockKey, err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return nil, false
}
if !lockOK {
pinConn.Release()
log.Printf("Payment serialization lock %s not acquired within bound — a payment is already in progress", lockKey)
http.Error(w, conflictMsg, http.StatusConflict)
return nil, false
}
return pinConn, true
}
// releaseBookingPaymentLock releases the advisory lock acquired by
// acquireBookingPaymentLock and returns the pinned connection to the pool.
// Both run on the same session that holds the lock.
func releaseBookingPaymentLock(pinConn *pgxpool.Conn, lockKey string) {
if _, err := pinConn.Exec(context.Background(), `
SELECT pg_advisory_unlock(hashtext($1))
`, lockKey); err != nil {
log.Printf("Failed to release payment serialization lock %s: %v", lockKey, err)
}
pinConn.Release()
}
// recheckBookingPayable re-reads the booking status after a Square charge
// succeeded (R9): a concurrent cancellation/eviction can move the booking out
// of a payable state between the pre-charge status check and the charge
// completing. A charge landing on a cancelled/lapsed/no-show booking must NOT
// be recorded as a completed payment — the cancellation refund path computes
// refunds from completed payments and would silently exclude it. Returns the
// re-read status and whether a completed payment is still allowed; the caller
// owns the CRITICAL logging, the mark-failed write (whose target and
// transaction semantics differ per path), and the 409 conflict response.
func recheckBookingPayable(ctx context.Context, q db.Querier, bookingID string) (string, bool, error) {
var status string
// FOR UPDATE (C5): a concurrent cancellation takes the same row lock and
// commits before this transaction commits, so the recheck cannot observe a
// status that changes between the read and the commit. Without the lock a
// cancellation could slip in between, leaving a completed payment on a
// cancelled booking with no refund.
if err := q.QueryRow(ctx, `SELECT status FROM bookings WHERE id = $1 FOR UPDATE`, bookingID).Scan(&status); err != nil {
return "", false, err
}
return status, bookingStatusAllowsCompletedPayment(status), nil
}