diff --git a/backend/handlers/payments/vat_test.go b/backend/handlers/payments/vat_test.go index b09e53e..904bb4b 100644 --- a/backend/handlers/payments/vat_test.go +++ b/backend/handlers/payments/vat_test.go @@ -2994,14 +2994,21 @@ func TestVAT_DisableVATRegistration_Lifecycle(t *testing.T) { t.Fatalf("phase 3: expected 200, got %d: %s", w3.Code, w3.Body.String()) } - // Verify NO VAT on the second payment (the latest cash payment) + var phase3Resp CheckoutResponse + if err := json.Unmarshal(w3.Body.Bytes(), &phase3Resp); err != nil { + t.Fatalf("phase 3: failed to parse response: %v", err) + } + + // Verify NO VAT on the second payment (query by specific payment ID + // avoids non-deterministic ORDER BY created_at DESC when all payments + // share the same transaction start time from NOW()). var vat3 sql.NullFloat64 var net3 sql.NullFloat64 var vatApplicable3 bool var amount3 float64 - err = tx.QueryRow(ctx, `SELECT amount, is_vat_applicable, vat_amount, net_amount FROM payments WHERE booking_id = $1 AND payment_method = 'cash' ORDER BY created_at DESC LIMIT 1`, bookingID).Scan(&amount3, &vatApplicable3, &vat3, &net3) + err = tx.QueryRow(ctx, `SELECT amount, is_vat_applicable, vat_amount, net_amount FROM payments WHERE id = $1`, phase3Resp.CheckoutID).Scan(&amount3, &vatApplicable3, &vat3, &net3) if err != nil { - t.Fatalf("phase 3: failed to query latest payment: %v", err) + t.Fatalf("phase 3: failed to query payment: %v", err) } if vatApplicable3 { @@ -3138,14 +3145,21 @@ func TestVAT_DisableAndReEnable_Lifecycle(t *testing.T) { t.Fatalf("phase 2: expected 200, got %d: %s", w2.Code, w2.Body.String()) } - // Get the latest cash payment (the second one, without VAT) + var phase2Resp CheckoutResponse + if err := json.Unmarshal(w2.Body.Bytes(), &phase2Resp); err != nil { + t.Fatalf("phase 2: failed to parse response: %v", err) + } + + // Get the specific payment by its ID from the response (avoiding + // non-deterministic ORDER BY created_at DESC when NOW() returns the same + // transaction start time for all inserts within a transaction). var p2VAT sql.NullFloat64 var p2Net sql.NullFloat64 var p2VATApplicable bool var p2Amount float64 - err = tx.QueryRow(ctx, `SELECT amount, is_vat_applicable, vat_amount, net_amount FROM payments WHERE booking_id = $1 AND payment_method = 'cash' ORDER BY created_at DESC LIMIT 1`, bookingID).Scan(&p2Amount, &p2VATApplicable, &p2VAT, &p2Net) + err = tx.QueryRow(ctx, `SELECT amount, is_vat_applicable, vat_amount, net_amount FROM payments WHERE id = $1`, phase2Resp.CheckoutID).Scan(&p2Amount, &p2VATApplicable, &p2VAT, &p2Net) if err != nil { - t.Fatalf("phase 2: failed to query latest payment: %v", err) + t.Fatalf("phase 2: failed to query payment: %v", err) } if p2VATApplicable { @@ -3182,13 +3196,20 @@ func TestVAT_DisableAndReEnable_Lifecycle(t *testing.T) { t.Fatalf("phase 3: expected 200, got %d: %s", w3.Code, w3.Body.String()) } - // Get the latest cash payment (the third one, with VAT re-enabled) + var phase3Resp CheckoutResponse + if err := json.Unmarshal(w3.Body.Bytes(), &phase3Resp); err != nil { + t.Fatalf("phase 3: failed to parse response: %v", err) + } + + // Get the specific payment by its ID from the response (avoiding + // non-deterministic ORDER BY created_at DESC when NOW() returns the same + // transaction start time for all inserts within a transaction). var p3VAT sql.NullFloat64 var p3Net sql.NullFloat64 var p3VATApplicable bool - err = tx.QueryRow(ctx, `SELECT is_vat_applicable, vat_amount, net_amount FROM payments WHERE booking_id = $1 AND payment_method = 'cash' ORDER BY created_at DESC LIMIT 1`, bookingID).Scan(&p3VATApplicable, &p3VAT, &p3Net) + err = tx.QueryRow(ctx, `SELECT is_vat_applicable, vat_amount, net_amount FROM payments WHERE id = $1`, phase3Resp.CheckoutID).Scan(&p3VATApplicable, &p3VAT, &p3Net) if err != nil { - t.Fatalf("phase 3: failed to query latest payment: %v", err) + t.Fatalf("phase 3: failed to query payment: %v", err) } if !p3VATApplicable {