Implement every finding from the deep payment review (P0-P2, minors, nitpicks), then close the post-implementation re-review items, then align card-form typography and roll out the Square trust badge. Backend - Square API alignment: - tip_settings.allow_tipping nested under device_options (was top-level: terminal tips were silently lost in prod) - CreateCardOnFile now accepts customerID and sends card.customer_id; saved-card (ccof:) charges forward square_customer_id as CustomerID - New SquareClient methods GetPayment, CreateCustomer, CancelCheckout - SCA verification_token accepted + forwarded in all charge paths - ExpMonth/ExpYear -> *int; URL-path id validation; CancelCheckout NOT_FOUND-only no-op (dropped unverified NOOP); exported ErrorCode/ ErrorDetail helpers; mock rejects raw PANs, RList locks, redacts emails, ForceRefundPending hook Backend - money safety: - sweepManualPendingSquareRefunds reconciles rows WITH square_refund_id instead of stranding them forever - SweepStalePendingPayments reconciles at Square before failing (tri-state: leave pending on transport error, rescue completed, fail definitively) - GetCheckoutStatus cancellation-recheck; terminal CANCELED resolution; SweepStaleTerminalCheckouts covers terminal_checkouts table - till gift-card clawback on definitive failure incl. retry path + INSUFFICIENT_FUNDS/ADDRESS_VERIFICATION_FAILURE/TRANSACTION_LIMIT - cross-user saved-card collision fixed (UNIQUE(user_id,square_card_id)) - customer provisioning (lazy, save-only); one-off/guest mint no customer - discount preview/apply unified in discounts.go (global-milestone visible in preview, N+1 eliminated, redemption counter preserved on failures) - webhook event_id dedup; refund loop dedup; stale comment fixes - test-isolation t.Cleanup on committed sweep tests Frontend: - SCA tokenizeWithVerification across all charge flows (amount as major-units decimal), 5-min token-expiry re-tokenize, verification_token in request bodies - PaymentModal synchronous double-click + zero/negative-amount guards - till online-card UI wired to /api/admin/till/sale - policyPopover generalised; new /privacy-policy route; consent checkbox copy + Square privacy link - Square card iframe styled to app typography (Inter 14px, oklch tokens); mock form md:text-sm parity - 'Secure payment powered by Square' badge on all 8 card-payment flows Schema/docs: terminal_checkouts + square_customer_id + per-user card constraint in init-script.sql; README migrations; P14 plan + backlog + Technical Manual updated. Includes 39 modified/new test files; full backend suite (25 pkgs), -race on payments+square, and frontend build are green.
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package payments
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// Valid payment types
|
|
var validPaymentTypes = map[string]bool{
|
|
"deposit": true,
|
|
"full": true,
|
|
"tip": true,
|
|
"balance": true,
|
|
"partial": true,
|
|
}
|
|
|
|
// ValidateAmount checks that amount is greater than 0 and has valid precision (max 2 decimal places when in pence)
|
|
func ValidateAmount(amount int64) error {
|
|
if amount <= 0 {
|
|
return errors.New("amount must be greater than 0")
|
|
}
|
|
if amount > 1_000_000 { // £10,000 in pence
|
|
return errors.New("amount exceeds maximum (£10,000)")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidatePartialAmount checks that the partial amount doesn't exceed the remaining balance
|
|
func ValidatePartialAmount(amountCents int64, remainingCents int64) error {
|
|
if amountCents > remainingCents {
|
|
return fmt.Errorf("partial amount (£%.2f) exceeds remaining balance (£%.2f)",
|
|
float64(amountCents)/100, float64(remainingCents)/100)
|
|
}
|
|
if amountCents <= 0 {
|
|
return errors.New("amount must be greater than 0")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidatePaymentType checks that payment type is valid
|
|
func ValidatePaymentType(pt string) error {
|
|
if !validPaymentTypes[pt] {
|
|
return errors.New("invalid payment type")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateRefundReason checks that refund reason is not empty
|
|
func ValidateRefundReason(reason string) error {
|
|
if reason == "" {
|
|
return errors.New("refund reason is required")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateCardInfo checks that exactly one of cardID or newCardToken is provided,
|
|
// non-nil, and non-empty.
|
|
func ValidateCardInfo(cardID, newCardToken *string) error {
|
|
hasCardID := cardID != nil && *cardID != ""
|
|
hasToken := newCardToken != nil && *newCardToken != ""
|
|
if hasCardID && hasToken {
|
|
return errors.New("provide either card_id or new_card_token, not both")
|
|
}
|
|
if !hasCardID && !hasToken {
|
|
return errors.New("either card_id or new_card_token is required")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateVerificationToken checks that a Square 3DS/SCA verification token is
|
|
// non-empty when present and within a sane length. Square's tokens are short
|
|
// opaque strings; the bound guards against absurd/malformed payloads before
|
|
// the token is forwarded to Square's API.
|
|
func ValidateVerificationToken(token *string) error {
|
|
if token == nil || *token == "" {
|
|
return nil
|
|
}
|
|
if len(*token) > 512 {
|
|
return errors.New("verification_token is too long")
|
|
}
|
|
return nil
|
|
}
|