Square payment integration: real HTTP client, tip flow rewrite, card UI/validation overhaul
Backend: - Create square_http_client.go: real Square REST API client (Payments, Terminal Checkouts, Refunds, Cards, Locations) with proper JSON types, auth, error handling - Update ProdClient in square.go to delegate to shared HTTP functions - Wire devProdClient in square_dev.go to also make real HTTP calls for sandbox/prod env - Rewrite CreateTipPayment handler: accept card_id OR new_card_token (+save_card), advisory lock, idempotency check, max amount validation - Add ValidateCardInfo, bump ValidateAmount max to £10,000 - Fix mock CreateCardOnFile to detect brand/last4 from raw card numbers - Fix mock RefundPayment to index by SquarePayID and accept unknown payment IDs - Remove dead types (ProcessingFee, sqAddress), add Deadline parity - Fix AMEX brand inconsistency (AMEX -> AMERICAN_EXPRESS) - Pre-existing fix: remove unused context import in giftcards.go Frontend: - CardInput.svelte: add onfieldblur/onfieldinput callbacks for blur-based validation - CardBrandIcon.svelte: brand SVGs for VISA, MC, AMEX, Discover, Diners, JCB, Square Gift Card, UnionPay, Interac, EFTPOS - tip/+page, pay-tip/[id], UserBookingModal tip: saved card list + CardInput + Luhn/expiry/CVC validation + blur-based errors + no-saved-cards edge case - UserPaymentModal, BookingFlow: card validation parity (blur-based, all-valid check) - account page: replace text brand badges with CardBrandIcon - Fix handleCustomTip bug (state mutations outside if block) - Remove dead pageState variable - Add tip modal scroll (max-h-[90vh] overflow-y-auto) - Submit button disabled on !isCardValid Tests: - 30 square package tests (+new: CreateCardOnFile raw number path, detectCardInfo variants) - 5 tip handler tests (HappyPath, NoPriorPayment, WrongOwner, MultipleTips, TxFailure) - All +-race clean, refund tests fixed
This commit is contained in:
@@ -4,6 +4,7 @@ package square
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -26,29 +27,22 @@ func TestDevClient_CreatePayment_ReturnsCompleted(t *testing.T) {
|
||||
}
|
||||
|
||||
result, err := client.CreatePayment(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePayment failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "COMPLETED", result.Status)
|
||||
assert.Equal(t, int64(5000), result.Amount)
|
||||
assert.Equal(t, "VISA", result.CardBrand)
|
||||
assert.Equal(t, "4242", result.CardLast4)
|
||||
assert.NotZero(t, result.Fees)
|
||||
|
||||
if result.Status != "COMPLETED" {
|
||||
t.Errorf("expected status COMPLETED, got %s", result.Status)
|
||||
}
|
||||
|
||||
if result.Amount != 5000 {
|
||||
t.Errorf("expected amount 5000, got %d", result.Amount)
|
||||
}
|
||||
|
||||
if result.CardBrand != "VISA" {
|
||||
t.Errorf("expected card brand VISA, got %s", result.CardBrand)
|
||||
}
|
||||
|
||||
if result.CardLast4 != "4242" {
|
||||
t.Errorf("expected last4 4242, got %s", result.CardLast4)
|
||||
}
|
||||
|
||||
if result.Fees == 0 {
|
||||
t.Error("expected fees to be calculated")
|
||||
}
|
||||
assert.NotEmpty(t, result.CardFingerprint)
|
||||
assert.Equal(t, 12, result.ExpMonth)
|
||||
assert.Equal(t, 2030, result.ExpYear)
|
||||
assert.Equal(t, "KEYED", result.EntryMethod)
|
||||
assert.Equal(t, "CVV_ACCEPTED", result.CVVStatus)
|
||||
assert.Equal(t, "AVS_ACCEPTED", result.AVSStatus)
|
||||
assert.NotEmpty(t, result.ReceiptNumber)
|
||||
assert.NotEmpty(t, result.CreatedAt)
|
||||
assert.NotEmpty(t, result.LocationID)
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCheckout_PendingThenCompleted(t *testing.T) {
|
||||
@@ -64,17 +58,13 @@ func TestDevClient_CreateCheckout_PendingThenCompleted(t *testing.T) {
|
||||
}
|
||||
|
||||
result, err := client.CreateCheckout(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateCheckout failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "PENDING", result.Status)
|
||||
assert.NotEmpty(t, result.ID)
|
||||
|
||||
if result.Status != "PENDING" {
|
||||
t.Errorf("expected status PENDING, got %s", result.Status)
|
||||
}
|
||||
|
||||
if result.ID == "" {
|
||||
t.Error("expected checkout ID to be set")
|
||||
}
|
||||
assert.Equal(t, int64(7500), result.AmountMoney)
|
||||
assert.Equal(t, "GBP", result.Currency)
|
||||
assert.NotEmpty(t, result.CreatedAt)
|
||||
|
||||
// Poll until the background goroutine completes using assert.Eventually
|
||||
var completed *PaymentResult
|
||||
@@ -84,13 +74,11 @@ func TestDevClient_CreateCheckout_PendingThenCompleted(t *testing.T) {
|
||||
return getErr == nil && completed.Status == "COMPLETED"
|
||||
}, 5*time.Second, 100*time.Millisecond, "expected checkout to complete")
|
||||
|
||||
if completed.Amount != 8000 {
|
||||
t.Errorf("expected amount 8000 (7500 + 500 tip), got %d", completed.Amount)
|
||||
}
|
||||
assert.Equal(t, int64(8000), completed.Amount, "expected amount 8000 (7500 + 500 tip)")
|
||||
assert.Equal(t, int64(500), completed.TipAmount)
|
||||
|
||||
if completed.TipAmount != 500 {
|
||||
t.Errorf("expected tip 500, got %d", completed.TipAmount)
|
||||
}
|
||||
assert.NotEmpty(t, completed.CardFingerprint)
|
||||
assert.NotEmpty(t, completed.EntryMethod)
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCheckout_NoTip(t *testing.T) {
|
||||
@@ -106,17 +94,13 @@ func TestDevClient_CreateCheckout_NoTip(t *testing.T) {
|
||||
}
|
||||
|
||||
result, err := client.CreateCheckout(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateCheckout failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "PENDING", result.Status)
|
||||
assert.NotEmpty(t, result.ID)
|
||||
|
||||
if result.Status != "PENDING" {
|
||||
t.Errorf("expected status PENDING, got %s", result.Status)
|
||||
}
|
||||
|
||||
if result.ID == "" {
|
||||
t.Error("expected checkout ID to be set")
|
||||
}
|
||||
assert.Equal(t, int64(5000), result.AmountMoney)
|
||||
assert.Equal(t, "GBP", result.Currency)
|
||||
assert.NotEmpty(t, result.CreatedAt)
|
||||
|
||||
var completed *PaymentResult
|
||||
assert.Eventually(t, func() bool {
|
||||
@@ -125,13 +109,11 @@ func TestDevClient_CreateCheckout_NoTip(t *testing.T) {
|
||||
return getErr == nil && completed.Status == "COMPLETED"
|
||||
}, 5*time.Second, 100*time.Millisecond, "expected checkout to complete")
|
||||
|
||||
if completed.Amount != 5000 {
|
||||
t.Errorf("expected amount 5000 (no tip), got %d", completed.Amount)
|
||||
}
|
||||
assert.Equal(t, int64(5000), completed.Amount, "expected amount 5000 (no tip)")
|
||||
assert.Equal(t, int64(0), completed.TipAmount)
|
||||
|
||||
if completed.TipAmount != 0 {
|
||||
t.Errorf("expected tip 0, got %d", completed.TipAmount)
|
||||
}
|
||||
assert.NotEmpty(t, completed.CardFingerprint)
|
||||
assert.NotEmpty(t, completed.EntryMethod)
|
||||
}
|
||||
|
||||
func TestDevClient_RefundPayment_ReturnsCompleted(t *testing.T) {
|
||||
@@ -149,9 +131,7 @@ func TestDevClient_RefundPayment_ReturnsCompleted(t *testing.T) {
|
||||
}
|
||||
|
||||
paymentResult, err := client.CreatePayment(ctx, paymentReq)
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePayment failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
refundReq := RefundPaymentReq{
|
||||
PaymentID: paymentResult.ID,
|
||||
@@ -161,17 +141,13 @@ func TestDevClient_RefundPayment_ReturnsCompleted(t *testing.T) {
|
||||
}
|
||||
|
||||
refundResult, err := client.RefundPayment(ctx, refundReq)
|
||||
if err != nil {
|
||||
t.Fatalf("RefundPayment failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "COMPLETED", refundResult.Status)
|
||||
assert.Equal(t, int64(5000), refundResult.Amount)
|
||||
|
||||
if refundResult.Status != "COMPLETED" {
|
||||
t.Errorf("expected status COMPLETED, got %s", refundResult.Status)
|
||||
}
|
||||
|
||||
if refundResult.Amount != 5000 {
|
||||
t.Errorf("expected amount 5000, got %d", refundResult.Amount)
|
||||
}
|
||||
assert.NotEmpty(t, refundResult.PaymentID)
|
||||
assert.Equal(t, "customer request", refundResult.Reason)
|
||||
assert.NotEmpty(t, refundResult.CreatedAt)
|
||||
}
|
||||
|
||||
func TestDevClient_CardOnFile_CreateAndGet(t *testing.T) {
|
||||
@@ -181,38 +157,22 @@ func TestDevClient_CardOnFile_CreateAndGet(t *testing.T) {
|
||||
userID := "user-test-123"
|
||||
|
||||
card, err := client.CreateCardOnFile(ctx, userID, "cnon:test-token")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateCardOnFile failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
if card.ID == "" {
|
||||
t.Error("expected card ID to be set")
|
||||
}
|
||||
assert.NotEmpty(t, card.ID)
|
||||
assert.Equal(t, "VISA", card.Brand)
|
||||
assert.Equal(t, "4242", card.Last4)
|
||||
assert.True(t, card.IsDefault)
|
||||
|
||||
if card.Brand != "VISA" {
|
||||
t.Errorf("expected brand VISA, got %s", card.Brand)
|
||||
}
|
||||
|
||||
if card.Last4 != "4242" {
|
||||
t.Errorf("expected last4 4242, got %s", card.Last4)
|
||||
}
|
||||
|
||||
if !card.IsDefault {
|
||||
t.Error("expected first card to be default")
|
||||
}
|
||||
assert.True(t, card.Enabled)
|
||||
assert.NotEmpty(t, card.CardholderName)
|
||||
assert.NotEmpty(t, card.CreatedAt)
|
||||
|
||||
cards, err := client.GetCardsOnFile(ctx, userID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCardsOnFile failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
if len(cards) != 1 {
|
||||
t.Errorf("expected 1 card, got %d", len(cards))
|
||||
}
|
||||
|
||||
if cards[0].ID != card.ID {
|
||||
t.Errorf("expected card ID %s, got %s", card.ID, cards[0].ID)
|
||||
}
|
||||
require.Len(t, cards, 1)
|
||||
assert.Equal(t, card.ID, cards[0].ID)
|
||||
}
|
||||
|
||||
func TestDevClient_CardOnFile_MultipleCards(t *testing.T) {
|
||||
@@ -222,31 +182,22 @@ func TestDevClient_CardOnFile_MultipleCards(t *testing.T) {
|
||||
userID := "user-test-multiple"
|
||||
|
||||
card1, err := client.CreateCardOnFile(ctx, userID, "cnon:token-1")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateCardOnFile failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
card2, err := client.CreateCardOnFile(ctx, userID, "cnon:token-2")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateCardOnFile failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, card1.Enabled)
|
||||
assert.True(t, card2.Enabled)
|
||||
assert.NotEmpty(t, card1.CreatedAt)
|
||||
assert.NotEmpty(t, card2.CreatedAt)
|
||||
|
||||
cards, err := client.GetCardsOnFile(ctx, userID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCardsOnFile failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
if len(cards) != 2 {
|
||||
t.Errorf("expected 2 cards, got %d", len(cards))
|
||||
}
|
||||
|
||||
if !card1.IsDefault {
|
||||
t.Error("first card should be default")
|
||||
}
|
||||
|
||||
if card2.IsDefault {
|
||||
t.Error("second card should not be default")
|
||||
}
|
||||
require.Len(t, cards, 2)
|
||||
assert.True(t, card1.IsDefault)
|
||||
assert.False(t, card2.IsDefault)
|
||||
}
|
||||
|
||||
func TestDevClient_CardOnFile_Delete(t *testing.T) {
|
||||
@@ -256,23 +207,16 @@ func TestDevClient_CardOnFile_Delete(t *testing.T) {
|
||||
userID := "user-test-delete"
|
||||
|
||||
card, err := client.CreateCardOnFile(ctx, userID, "cnon:token-delete")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateCardOnFile failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
err = client.DeleteCardOnFile(ctx, card.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteCardOnFile failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
cards, err := client.GetCardsOnFile(ctx, userID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCardsOnFile failed: %v", err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
if len(cards) != 0 {
|
||||
t.Errorf("expected 0 cards after delete, got %d", len(cards))
|
||||
}
|
||||
require.Len(t, cards, 1)
|
||||
assert.False(t, cards[0].Enabled)
|
||||
}
|
||||
|
||||
func TestDevClient_CardOnFile_DeleteNotFound(t *testing.T) {
|
||||
@@ -281,9 +225,7 @@ func TestDevClient_CardOnFile_DeleteNotFound(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
err := client.DeleteCardOnFile(ctx, "non-existent-card")
|
||||
if err == nil {
|
||||
t.Error("expected error when deleting non-existent card")
|
||||
}
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestDevClient_GetCheckout_NotFound(t *testing.T) {
|
||||
@@ -292,9 +234,7 @@ func TestDevClient_GetCheckout_NotFound(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := client.GetCheckout(ctx, "non-existent-checkout")
|
||||
if err == nil {
|
||||
t.Error("expected error when checkout not found")
|
||||
}
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCardOnFileRaw_Visa(t *testing.T) {
|
||||
@@ -304,6 +244,11 @@ func TestDevClient_CreateCardOnFileRaw_Visa(t *testing.T) {
|
||||
assert.Equal(t, "VISA", card.Brand)
|
||||
assert.Equal(t, "1111", card.Last4)
|
||||
assert.True(t, card.IsDefault)
|
||||
assert.True(t, card.Enabled)
|
||||
assert.Equal(t, 12, card.ExpMonth)
|
||||
assert.Equal(t, 2030, card.ExpYear)
|
||||
assert.NotEmpty(t, card.CreatedAt)
|
||||
assert.Greater(t, card.Version, int64(0))
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCardOnFileRaw_Mastercard(t *testing.T) {
|
||||
@@ -318,15 +263,25 @@ func TestDevClient_CreateCardOnFileRaw_Mastercard(t *testing.T) {
|
||||
assert.Equal(t, "MASTERCARD", card.Brand)
|
||||
assert.Equal(t, "4444", card.Last4)
|
||||
assert.False(t, card.IsDefault)
|
||||
assert.True(t, card.Enabled)
|
||||
assert.Equal(t, 12, card.ExpMonth)
|
||||
assert.Equal(t, 2030, card.ExpYear)
|
||||
assert.NotEmpty(t, card.CreatedAt)
|
||||
assert.Greater(t, card.Version, int64(0))
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCardOnFileRaw_Amex(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
card, err := client.CreateCardOnFileRaw(context.Background(), "user-raw-3", "378282246310005", 12, 2030, "123")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "AMEX", card.Brand)
|
||||
assert.Equal(t, "AMERICAN_EXPRESS", card.Brand)
|
||||
assert.Equal(t, "0005", card.Last4)
|
||||
assert.True(t, card.IsDefault)
|
||||
assert.True(t, card.Enabled)
|
||||
assert.Equal(t, 12, card.ExpMonth)
|
||||
assert.Equal(t, 2030, card.ExpYear)
|
||||
assert.NotEmpty(t, card.CreatedAt)
|
||||
assert.Greater(t, card.Version, int64(0))
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCardOnFileRaw_Discover(t *testing.T) {
|
||||
@@ -336,6 +291,11 @@ func TestDevClient_CreateCardOnFileRaw_Discover(t *testing.T) {
|
||||
assert.Equal(t, "DISCOVER", card.Brand)
|
||||
assert.Equal(t, "1117", card.Last4)
|
||||
assert.True(t, card.IsDefault)
|
||||
assert.True(t, card.Enabled)
|
||||
assert.Equal(t, 12, card.ExpMonth)
|
||||
assert.Equal(t, 2030, card.ExpYear)
|
||||
assert.NotEmpty(t, card.CreatedAt)
|
||||
assert.Greater(t, card.Version, int64(0))
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCardOnFileRaw_UnknownBrand(t *testing.T) {
|
||||
@@ -345,6 +305,11 @@ func TestDevClient_CreateCardOnFileRaw_UnknownBrand(t *testing.T) {
|
||||
assert.Equal(t, "UNKNOWN", card.Brand)
|
||||
assert.Equal(t, "9999", card.Last4)
|
||||
assert.True(t, card.IsDefault)
|
||||
assert.True(t, card.Enabled)
|
||||
assert.Equal(t, 12, card.ExpMonth)
|
||||
assert.Equal(t, 2030, card.ExpYear)
|
||||
assert.NotEmpty(t, card.CreatedAt)
|
||||
assert.Greater(t, card.Version, int64(0))
|
||||
}
|
||||
|
||||
func TestCreatePayment_ShouldFail(t *testing.T) {
|
||||
@@ -361,12 +326,8 @@ func TestCreatePayment_ShouldFail(t *testing.T) {
|
||||
}
|
||||
|
||||
result, err := client.CreatePayment(ctx, req)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when ShouldFail is true, got nil")
|
||||
}
|
||||
if result != nil {
|
||||
t.Errorf("expected nil result, got %+v", result)
|
||||
}
|
||||
require.Error(t, err, "expected error when ShouldFail is true")
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestRefundPayment_ShouldFail(t *testing.T) {
|
||||
@@ -382,12 +343,8 @@ func TestRefundPayment_ShouldFail(t *testing.T) {
|
||||
}
|
||||
|
||||
result, err := client.RefundPayment(ctx, req)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when ShouldFail is true, got nil")
|
||||
}
|
||||
if result != nil {
|
||||
t.Errorf("expected nil result, got %+v", result)
|
||||
}
|
||||
require.Error(t, err, "expected error when ShouldFail is true")
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestDevClient_ConcurrentPayments(t *testing.T) {
|
||||
@@ -407,17 +364,17 @@ func TestDevClient_ConcurrentPayments(t *testing.T) {
|
||||
Amount: int64(1000 + idx*100),
|
||||
Currency: "GBP",
|
||||
SourceID: "cnon:test-card",
|
||||
IdempotencyKey: "concurrent-key-" + string(rune('0'+idx)),
|
||||
IdempotencyKey: fmt.Sprintf("concurrent-key-%d", idx),
|
||||
ReferenceID: "booking-concurrent",
|
||||
Note: "full",
|
||||
}
|
||||
|
||||
result, err := client.CreatePayment(ctx, req)
|
||||
if err != nil {
|
||||
errors <- err
|
||||
payResult, payErr := client.CreatePayment(ctx, req)
|
||||
if payErr != nil {
|
||||
errors <- payErr
|
||||
return
|
||||
}
|
||||
results <- result
|
||||
results <- payResult
|
||||
}(i)
|
||||
}
|
||||
|
||||
@@ -426,24 +383,231 @@ func TestDevClient_ConcurrentPayments(t *testing.T) {
|
||||
close(errors)
|
||||
|
||||
errorCount := 0
|
||||
for err := range errors {
|
||||
t.Logf("Concurrent payment error: %v", err)
|
||||
for range errors {
|
||||
errorCount++
|
||||
}
|
||||
|
||||
if errorCount > 0 {
|
||||
t.Errorf("expected no errors, got %d", errorCount)
|
||||
}
|
||||
assert.Zero(t, errorCount, "expected no concurrent errors")
|
||||
|
||||
resultCount := 0
|
||||
for result := range results {
|
||||
if result.Status != "COMPLETED" {
|
||||
t.Errorf("expected status COMPLETED, got %s", result.Status)
|
||||
}
|
||||
for payResult := range results {
|
||||
assert.Equal(t, "COMPLETED", payResult.Status)
|
||||
assert.NotEmpty(t, payResult.CreatedAt)
|
||||
resultCount++
|
||||
}
|
||||
assert.Equal(t, 10, resultCount)
|
||||
}
|
||||
|
||||
if resultCount != 10 {
|
||||
t.Errorf("expected 10 results, got %d", resultCount)
|
||||
func TestDevClient_CreatePayment_WithTipMoney(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
ctx := context.Background()
|
||||
|
||||
tip := int64(1000)
|
||||
req := CreatePaymentReq{
|
||||
Amount: 5000,
|
||||
Currency: "GBP",
|
||||
SourceID: "cnon:test-card",
|
||||
IdempotencyKey: "test-key-tip",
|
||||
ReferenceID: "booking-tip",
|
||||
Note: "full",
|
||||
TipMoney: &tip,
|
||||
}
|
||||
|
||||
result, err := client.CreatePayment(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "COMPLETED", result.Status)
|
||||
assert.Equal(t, int64(6000), result.Amount)
|
||||
assert.Equal(t, int64(1000), result.TipAmount)
|
||||
}
|
||||
|
||||
func TestDevClient_CreatePayment_AutocompleteFalse(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
ctx := context.Background()
|
||||
|
||||
autocomplete := false
|
||||
req := CreatePaymentReq{
|
||||
Amount: 5000,
|
||||
Currency: "GBP",
|
||||
SourceID: "cnon:test-card",
|
||||
IdempotencyKey: "test-key-autocomplete",
|
||||
ReferenceID: "booking-autocomplete",
|
||||
Autocomplete: &autocomplete,
|
||||
}
|
||||
|
||||
result, err := client.CreatePayment(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "APPROVED", result.Status)
|
||||
assert.Equal(t, int64(5000), result.Amount)
|
||||
}
|
||||
|
||||
func TestDevClient_CreatePayment_WithBuyerEmail(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
ctx := context.Background()
|
||||
|
||||
req := CreatePaymentReq{
|
||||
Amount: 5000,
|
||||
Currency: "GBP",
|
||||
SourceID: "cnon:test-card",
|
||||
IdempotencyKey: "test-key-email",
|
||||
ReferenceID: "booking-email",
|
||||
BuyerEmail: "test@example.com",
|
||||
}
|
||||
|
||||
result, err := client.CreatePayment(ctx, req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "COMPLETED", result.Status)
|
||||
assert.Equal(t, "test@example.com", result.BuyerEmail)
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCardOnFile_WithNewFields(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
ctx := context.Background()
|
||||
userID := "user-new-fields"
|
||||
|
||||
card, err := client.CreateCardOnFile(ctx, userID, "cnon:test-token")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, card.Enabled)
|
||||
assert.NotEmpty(t, card.CardholderName)
|
||||
assert.Equal(t, userID, card.CustomerID)
|
||||
assert.Greater(t, card.Version, int64(0))
|
||||
assert.NotEmpty(t, card.CreatedAt)
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCardOnFileRaw_WithBrandDetection(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
ctx := context.Background()
|
||||
userID := "user-raw-brand-detect"
|
||||
|
||||
card, err := client.CreateCardOnFileRaw(ctx, userID, "4111111111111111", 12, 2030, "123")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "VISA", card.Brand)
|
||||
assert.Equal(t, "1111", card.Last4)
|
||||
assert.True(t, card.Enabled)
|
||||
assert.Equal(t, 12, card.ExpMonth)
|
||||
assert.Equal(t, 2030, card.ExpYear)
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCardOnFile_RawNumber(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cardNum string
|
||||
wantBrand string
|
||||
wantLast4 string
|
||||
}{
|
||||
{"visa formatted", "4111 1111 1111 1111", "VISA", "1111"},
|
||||
{"visa raw", "4111111111111111", "VISA", "1111"},
|
||||
{"mastercard", "5500 0000 0000 0004", "MASTERCARD", "0004"},
|
||||
{"amex", "3400 0000 0000 009", "AMERICAN_EXPRESS", "0009"},
|
||||
{"discover", "6011 0000 0000 0004", "DISCOVER", "0004"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
userID := fmt.Sprintf("user-raw-card-%s", tt.name)
|
||||
card, err := client.CreateCardOnFile(ctx, userID, tt.cardNum)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.wantBrand, card.Brand)
|
||||
assert.Equal(t, tt.wantLast4, card.Last4)
|
||||
assert.True(t, card.Enabled)
|
||||
assert.NotEmpty(t, card.CardholderName)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevClient_DeleteCardOnFile_SoftDelete(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
ctx := context.Background()
|
||||
userID := "user-soft-delete"
|
||||
|
||||
card, err := client.CreateCardOnFile(ctx, userID, "cnon:token-soft")
|
||||
require.NoError(t, err)
|
||||
|
||||
err = client.DeleteCardOnFile(ctx, card.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
cards, err := client.GetCardsOnFile(ctx, userID)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, cards, 1)
|
||||
assert.False(t, cards[0].Enabled)
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCardOnFileRaw_TooShort(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := client.CreateCardOnFileRaw(ctx, "user-too-short", "123", 12, 2030, "999")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "too short")
|
||||
}
|
||||
|
||||
func TestDevClient_GetCardsOnFile_Empty(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
ctx := context.Background()
|
||||
|
||||
cards, err := client.GetCardsOnFile(ctx, "user-no-cards")
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, cards)
|
||||
}
|
||||
|
||||
func TestDevClient_GetCheckout_StillPending(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
client.HoldCheckouts = true
|
||||
ctx := context.Background()
|
||||
|
||||
result, err := client.CreateCheckout(ctx, CreateCheckoutReq{
|
||||
Amount: 5000,
|
||||
Currency: "GBP",
|
||||
IdempotencyKey: "pending-checkout",
|
||||
ReferenceID: "pending-ref",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "PENDING", result.Status)
|
||||
|
||||
_, err = client.GetCheckout(ctx, result.ID)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "pending")
|
||||
}
|
||||
|
||||
func TestDevClient_CreateCheckout_HoldCheckouts(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
client.HoldCheckouts = true
|
||||
ctx := context.Background()
|
||||
|
||||
result, err := client.CreateCheckout(ctx, CreateCheckoutReq{
|
||||
Amount: 2500,
|
||||
Currency: "GBP",
|
||||
IdempotencyKey: "hold-checkout",
|
||||
ReferenceID: "hold-ref",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "PENDING", result.Status)
|
||||
|
||||
_, err = client.GetCheckout(ctx, result.ID)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestDetectCardInfo_Variants(t *testing.T) {
|
||||
tests := []struct {
|
||||
sourceID string
|
||||
wantBrand string
|
||||
wantLast4 string
|
||||
}{
|
||||
{"cnon:test-card", "VISA", "4242"},
|
||||
{"cnon:visa", "VISA", "1111"},
|
||||
{"cnon:mastercard", "MASTERCARD", "4444"},
|
||||
{"cnon:amex", "AMERICAN_EXPRESS", "0005"},
|
||||
{"unknown-source", "VISA", "4242"},
|
||||
{"", "VISA", "4242"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.sourceID, func(t *testing.T) {
|
||||
brand, last4 := detectCardInfo(tt.sourceID)
|
||||
assert.Equal(t, tt.wantBrand, brand)
|
||||
assert.Equal(t, tt.wantLast4, last4)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user