CARD_STATUS_SUSPENDED/CLOSED have existed in common.proto since the
original build, and the Airwallex client package even defined
CardStatusSuspended/CardStatusClosed constants, but nothing in this
repo ever transitioned a stored card into either state: card-issuance's
airwallex.Client interface only exposed IssueCard/GetCard/
SetCardControls (limit/controls only, never status), so a leaked
credential, an offboarded cardholder's card, or any card that should
stop spending forever had no real operator path to close — only
"adjust-limit" to a smaller (but still nonzero-possible, still active)
limit.
Adds card_issuance.proto/gateway.proto's CloseCard RPC, a real
airwallex.Client.CloseCard on both Mock and Live (POST
/api/v1/issuing/cards/{id}/close), cardsvc.Service.CloseCard (releases
the card's full remaining ledger HOLD back to the tenant's available
balance, idempotent on an already-closed card), the gateway fan-out,
and `ration card close --card <id>`. AdjustCardLimit now rejects a
closed card instead of silently reopening a zombie hold.
tenantauth/ratelimit needed zero changes: both resolve card_id via
reflection, so CloseCardRequest's card_id field is automatically
tenant-scoped and rate-limited the same way GetCard/AdjustCardLimit
already are.
Verified live against the real bare-metal 7-service stack
(scripts/run-demo.sh): issued a real card (Held 1,000.00 USD), closed
it (status CLOSED, Held back to 0.00, Available back to 7,500.00),
re-closed it (idempotent — Available unchanged, no double-release),
confirmed adjust-limit on the closed card is rejected, and confirmed
closing an unknown card_id surfaces the same NotFound-wrapped error
GetCard/AdjustCardLimit already do. verify-chain: intact (5 records).
`go build`/`go vet` clean across every Go module (gateway,
card-issuance, cli, tui, auth, tenant-registry, webhook-ingestion,
agent, audit-log, sdk/go/ration), full `make build` (Rust+Go+SDK)
clean, 0 regressions.
149 lines
4.2 KiB
Go
149 lines
4.2 KiB
Go
package airwallex
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Mock is a deterministic in-memory fake of the Airwallex Issuing +
|
|
// Global Accounts API, used when AIRWALLEX_MODE=mock (the default). It
|
|
// mirrors the Client interface byte-for-byte with Live so flipping to
|
|
// AIRWALLEX_MODE=live with real sandbox credentials is a one-line env
|
|
// change, not a rewrite.
|
|
//
|
|
// Determinism: card IDs are derived from a monotonic counter rather than
|
|
// random bytes, and Now is injectable, so demo output and tests are
|
|
// reproducible run to run.
|
|
type Mock struct {
|
|
mu sync.Mutex
|
|
cards map[string]*Card
|
|
nextID int
|
|
Now func() time.Time
|
|
|
|
// txByTenant lets the CLI/demo driver seed fake incoming deposits that
|
|
// ListGlobalAccountTransactions will then return, mirroring how a real
|
|
// Airwallex global account would surface a received deposit.
|
|
txByTenant map[string][]Transaction
|
|
}
|
|
|
|
// NewMock constructs a ready-to-use Mock client.
|
|
func NewMock() *Mock {
|
|
return &Mock{
|
|
cards: make(map[string]*Card),
|
|
txByTenant: make(map[string][]Transaction),
|
|
Now: time.Now,
|
|
}
|
|
}
|
|
|
|
func (m *Mock) now() time.Time {
|
|
if m.Now != nil {
|
|
return m.Now()
|
|
}
|
|
return time.Now()
|
|
}
|
|
|
|
// IssueCard creates a deterministic fake card and stores it in memory.
|
|
func (m *Mock) IssueCard(ctx context.Context, params IssueCardParams) (*Card, error) {
|
|
if params.CardholderName == "" {
|
|
return nil, fmt.Errorf("airwallex mock: cardholder_name is required")
|
|
}
|
|
if params.LimitMinorUnits < 0 {
|
|
return nil, fmt.Errorf("airwallex mock: limit_minor_units must be >= 0")
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
m.nextID++
|
|
card := &Card{
|
|
ID: fmt.Sprintf("aw_card_mock_%06d", m.nextID),
|
|
CardholderName: params.CardholderName,
|
|
Status: CardStatusActive,
|
|
LimitMinorUnits: params.LimitMinorUnits,
|
|
LimitInterval: params.LimitInterval,
|
|
Currency: params.Currency,
|
|
CreatedAt: m.now(),
|
|
}
|
|
m.cards[card.ID] = card
|
|
return cloneCard(card), nil
|
|
}
|
|
|
|
// GetCard returns the previously issued fake card.
|
|
func (m *Mock) GetCard(ctx context.Context, cardID string) (*Card, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
card, ok := m.cards[cardID]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
return cloneCard(card), nil
|
|
}
|
|
|
|
// SetCardControls updates the fake card's limit/controls in place.
|
|
func (m *Mock) SetCardControls(ctx context.Context, cardID string, controls CardControls) (*Card, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
card, ok := m.cards[cardID]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
if controls.LimitMinorUnits < 0 {
|
|
return nil, fmt.Errorf("airwallex mock: limit_minor_units must be >= 0")
|
|
}
|
|
card.LimitMinorUnits = controls.LimitMinorUnits
|
|
if controls.LimitInterval != "" {
|
|
card.LimitInterval = controls.LimitInterval
|
|
}
|
|
return cloneCard(card), nil
|
|
}
|
|
|
|
// CloseCard marks the fake card CardStatusClosed and zeroes its limit,
|
|
// mirroring what Live's real POST .../close does to Airwallex's own
|
|
// card resource.
|
|
func (m *Mock) CloseCard(ctx context.Context, cardID string) (*Card, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
card, ok := m.cards[cardID]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
card.Status = CardStatusClosed
|
|
card.LimitMinorUnits = 0
|
|
return cloneCard(card), nil
|
|
}
|
|
|
|
// SeedTransaction lets the demo/CLI layer inject a fake incoming global
|
|
// account transaction for a tenant, so ListGlobalAccountTransactions has
|
|
// something deterministic to return without a real Airwallex sandbox.
|
|
func (m *Mock) SeedTransaction(tenantID string, tx Transaction) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.txByTenant[tenantID] = append(m.txByTenant[tenantID], tx)
|
|
}
|
|
|
|
// ListGlobalAccountTransactions returns seeded fake transactions for a
|
|
// tenant, filtered by Since and sorted oldest-first.
|
|
func (m *Mock) ListGlobalAccountTransactions(ctx context.Context, params ListGlobalAccountTransactionsParams) ([]Transaction, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
all := m.txByTenant[params.TenantID]
|
|
out := make([]Transaction, 0, len(all))
|
|
for _, tx := range all {
|
|
if !params.Since.IsZero() && tx.OccurredAt.Before(params.Since) {
|
|
continue
|
|
}
|
|
out = append(out, tx)
|
|
}
|
|
sort.Slice(out, func(i, j int) bool { return out[i].OccurredAt.Before(out[j].OccurredAt) })
|
|
return out, nil
|
|
}
|
|
|
|
func cloneCard(c *Card) *Card {
|
|
cp := *c
|
|
return &cp
|
|
}
|
|
|
|
var _ Client = (*Mock)(nil)
|