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

108 lines
4.1 KiB
Go

// Package grpcserver implements card_issuance.proto's CardIssuance
// service by delegating directly to internal/cardsvc and internal/bridge
// — this package is the one place that would translate between wire
// types and a different internal representation, but since cardsvc and
// bridge already speak cardissuance.* wire types natively (unlike the
// old gateway, which had its own gateway.* types), every method here is
// a direct passthrough.
package grpcserver
import (
"context"
"errors"
cardissuancepb "github.com/caelum0x/ration/pkg/pb/cardissuance"
"github.com/caelum0x/ration/services/card-issuance/internal/airwallex"
"github.com/caelum0x/ration/services/card-issuance/internal/bridge"
"github.com/caelum0x/ration/services/card-issuance/internal/cardsvc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// wrapCardLookupError translates cardsvc's plain-wrapped
// airwallex.ErrNotFound (a card ID that doesn't exist) into a proper
// gRPC status.Error(codes.NotFound, ...) before it leaves this service.
// Without this, the gRPC runtime's default error conversion downgrades
// any non-status error to codes.Unknown, which every GatewayClient
// caller (TUI, CLI, sdk/go/ration, sdk/rust/ration-client, and the
// gateway's REST facade in services/gateway/internal/httpapi/cards.go)
// then has no way to distinguish from a genuine server failure — see
// that file's handleGetCard doc comment for the exact symptom this
// closes (a missing card surfacing as HTTP 500 instead of 404).
func wrapCardLookupError(err error) error {
if err == nil {
return nil
}
if errors.Is(err, airwallex.ErrNotFound) {
return status.Error(codes.NotFound, err.Error())
}
return err
}
// Server implements cardissuancepb.CardIssuanceServer.
type Server struct {
cardissuancepb.UnimplementedCardIssuanceServer
cards *cardsvc.Service
bridge *bridge.Service
}
// New constructs a Server wiring the given services.
func New(cards *cardsvc.Service, bridgeSvc *bridge.Service) *Server {
return &Server{cards: cards, bridge: bridgeSvc}
}
// IssueCard implements cardissuancepb.CardIssuanceServer.
func (s *Server) IssueCard(ctx context.Context, req *cardissuancepb.IssueCardRequest) (*cardissuancepb.Card, error) {
return s.cards.IssueCard(ctx, req)
}
// AdjustCardLimit implements cardissuancepb.CardIssuanceServer.
func (s *Server) AdjustCardLimit(ctx context.Context, req *cardissuancepb.AdjustCardLimitRequest) (*cardissuancepb.Card, error) {
card, err := s.cards.AdjustCardLimit(ctx, req)
if err != nil {
return nil, wrapCardLookupError(err)
}
return card, nil
}
// GetCard implements cardissuancepb.CardIssuanceServer.
func (s *Server) GetCard(ctx context.Context, req *cardissuancepb.GetCardRequest) (*cardissuancepb.Card, error) {
card, err := s.cards.GetCard(ctx, req.GetCardId())
if err != nil {
return nil, wrapCardLookupError(err)
}
return card, nil
}
// CloseCard implements cardissuancepb.CardIssuanceServer.
func (s *Server) CloseCard(ctx context.Context, req *cardissuancepb.CloseCardRequest) (*cardissuancepb.Card, error) {
card, err := s.cards.CloseCard(ctx, req.GetCardId())
if err != nil {
return nil, wrapCardLookupError(err)
}
return card, nil
}
// ListCards implements cardissuancepb.CardIssuanceServer.
func (s *Server) ListCards(ctx context.Context, req *cardissuancepb.ListCardsRequest) (*cardissuancepb.ListCardsResponse, error) {
cards, err := s.cards.ListCards(ctx, req.GetTenantId())
if err != nil {
return nil, err
}
return &cardissuancepb.ListCardsResponse{Cards: cards}, nil
}
// RationBridgeFunding implements cardissuancepb.CardIssuanceServer.
func (s *Server) RationBridgeFunding(ctx context.Context, req *cardissuancepb.RationBridgeFundingRequest) (*cardissuancepb.RationBridgeFundingResponse, error) {
result, err := s.bridge.RationBridgeFunding(ctx, req.GetTenantId(), req.GetRequestedMinorUnits(), req.GetReason())
if err != nil {
return nil, err
}
return &cardissuancepb.RationBridgeFundingResponse{
GrantedMinorUnits: result.GrantedMinorUnits,
FullyGranted: result.FullyGranted,
Explanation: result.Explanation,
}, nil
}