caelum0x c3236f0bca feat(card-issuance,gateway,cli): add CloseCard — issued cards could never be shut off
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.
2026-10-29 11:35:23 +03:00

98 lines
3.6 KiB
Go

// Package airwallex defines card-issuance's abstraction over the
// Airwallex Issuing API and Global Accounts API. Two implementations
// exist: Live (live.go), a real HTTP client, and Mock (mock.go), a
// deterministic in-memory fake used when AIRWALLEX_MODE=mock (the
// default). internal/cardsvc and internal/bridge depend only on the
// Client interface, never on a concrete implementation, so flipping
// modes is a one-line change in cmd/card-issuance.
//
// This is card-issuance's own copy of the gateway's former
// internal/airwallex package (docs/adr/0004): webhook signature
// verification moved to services/webhook-ingestion instead, since
// card-issuance never receives webhook deliveries, so this copy has no
// VerifyWebhookSignature method and no webhook secret to thread through.
package airwallex
import (
"context"
"errors"
"time"
)
// ErrNotFound is returned when a card or transaction lookup finds nothing.
var ErrNotFound = errors.New("airwallex: not found")
// CardStatus mirrors the subset of Airwallex issuing-card statuses
// card-issuance cares about.
type CardStatus string
const (
CardStatusActive CardStatus = "ACTIVE"
CardStatusSuspended CardStatus = "SUSPENDED"
CardStatusClosed CardStatus = "CLOSED"
)
// Card is the Airwallex-side representation of an issued card. It is
// intentionally distinct from pb.Card (card-issuance's own wire type) so
// the mapping between "what Airwallex thinks" and "what we expose over
// gRPC" stays explicit at the internal/cardsvc boundary.
type Card struct {
ID string
CardholderName string
Status CardStatus
LimitMinorUnits int64
LimitInterval string // "daily" | "monthly"
Currency string
CreatedAt time.Time
}
// IssueCardParams are the inputs needed to create a new issuing card.
type IssueCardParams struct {
TenantID string
CardholderName string
LimitMinorUnits int64
LimitInterval string
Currency string
}
// CardControls restricts how a card can be spent — used both at issuance
// and when the agent tightens controls alongside a partial bridge grant.
type CardControls struct {
LimitMinorUnits int64
LimitInterval string
AllowedCategories []string // empty = unrestricted
PerTransactionCapMinorUnits int64 // 0 = unrestricted
}
// Transaction is a Global Account transaction (e.g. an incoming deposit).
type Transaction struct {
ID string
AmountMinorUnits int64
Currency string
Description string
OccurredAt time.Time
}
// ListGlobalAccountTransactionsParams scopes a transaction listing.
type ListGlobalAccountTransactionsParams struct {
TenantID string
Since time.Time
}
// Client is the interface the rest of card-issuance depends on. Both
// Live and Mock implement it identically so swapping AIRWALLEX_MODE
// never requires touching internal/cardsvc, internal/bridge or
// internal/grpcserver.
type Client interface {
IssueCard(ctx context.Context, params IssueCardParams) (*Card, error)
GetCard(ctx context.Context, cardID string) (*Card, error)
SetCardControls(ctx context.Context, cardID string, controls CardControls) (*Card, error)
// CloseCard permanently closes a card (CardStatusClosed) — the
// Airwallex-side counterpart to this package's own CardStatusClosed
// constant, which existed since the original build but, until this
// method, nothing ever called: SetCardControls can only change a
// card's limit/controls, never its status.
CloseCard(ctx context.Context, cardID string) (*Card, error)
ListGlobalAccountTransactions(ctx context.Context, params ListGlobalAccountTransactionsParams) ([]Transaction, error)
}