Files
commerce/api/billing/agent_test.go
zeekayandGitHub f220efc866 Headless agentic commerce through Stripe: shared payment tokens, stablecoin top-ups, token-backed subscriptions (#64)
* wip: stripe stablecoin + grant adapter

* Headless agentic commerce through Stripe: shared payment tokens, stablecoin top-ups, token-backed subscriptions

- GET /v1/billing/catalog and /discovery: a USD catalog of plans and credit packs, the payment
  types the account can take now, and the checkout verbs.
- Checkouts an agent creates, updates and completes in JSON (POST/PATCH/GET
  /v1/billing/checkouts, POST .../complete with an Idempotency-Key, GET /v1/billing/orders/:id).
  The charge is bound to the checkout, the buyer and the key; completing twice is one purchase.
- Shared payment tokens: the granted token is read and validated (seller, mode, currency,
  expiry, what is left on it, revocation) before any charge, then charged through the card
  adapter with payment_method_data[shared_payment_granted_token]. Refusals are typed
  (token_expired, token_revoked, token_over_limit, token_wrong_currency, token_wrong_merchant,
  token_not_recurring); a bank's authentication step is authentication_required with the
  intent and client secret, never a success.
- Subscriptions: a token whose usage limits carry a recurring interval is the subscription's
  payment method and renews it; a one-time token pays the first period and is dropped. A
  renewal the token cannot cover leaves the subscription past due and announces
  renewal.action_required with a code and the path to supply a fresh token
  (POST /v1/billing/invoices/:id/agent-token).
- Stablecoins through Stripe only, headless: POST /v1/billing/topup/crypto opens a deposit-mode
  PaymentIntent and returns the address per network (base, solana, tempo); USD only; credited
  once on payment_intent.succeeded. Crypto rails default to Stripe stablecoins, and a crypto
  method is shown only where the account can take stablecoins now.

Testing: go test ./api/... ./payment/... (non-live); live Stripe test mode with the sandbox
token helpers: top-up credits once and a replayed webhook adds nothing; over-limit, wrong
currency, revoked, expired and another seller's tokens are refused with no credit and the
checkout stays usable; 3-D Secure is a typed step; a subscription renews on a recurring token,
fails over its limit and when revoked with the renewal action, is paid by a fresh token, and a
one-time token never renews.
Risk: new endpoints; crypto rails default to Stripe stablecoins only.
How to revert: git revert this commit.
2026-11-14 18:42:36 -08:00

80 lines
2.7 KiB
Go

package billing
import (
"testing"
"time"
"github.com/hanzoai/commerce/payment/processor"
)
// The catalog is in dollars, lists packs within the top-up bounds, and offers a payment type
// only where the account can take it.
func TestTheCatalogIsUSDAndOffersOnlyWhatCanBePaid(t *testing.T) {
ctx := heldStore(t)
org := routingOrg(ctx, t)
useRails(t, false, true)
cat, err := ReadCatalog(ctx, org, "")
if err != nil {
t.Fatal(err)
}
if cat.Currency != "usd" || cat.Version != agentVersion || len(cat.CreditPacks) == 0 {
t.Fatalf("catalog = %+v", cat)
}
min, max := topupBounds()
for _, p := range cat.CreditPacks {
if p.UsdCents < min || p.UsdCents > max {
t.Fatalf("pack %+v is outside the top-up bounds", p)
}
}
for _, o := range cat.Payment {
if o.Currency != "usd" {
t.Fatalf("payment option %+v is not in dollars", o)
}
switch o.Type {
case "stablecoin":
if len(o.Networks) == 0 || o.Processor != "stripe" {
t.Fatalf("stablecoin option = %+v", o)
}
case "shared_payment_token":
t.Fatal("tokens offered by an account whose processor cannot read them")
}
}
for _, link := range []string{"catalog", "checkouts", "orders", "discovery"} {
if cat.Links[link] == "" {
t.Fatalf("no %s link", link)
}
}
}
// A grant is judged by its limits: what is left on it, its currency, expiry and revocation,
// and an off-session charge needs a recurring interval.
func TestAGrantIsJudgedByWhatItAllows(t *testing.T) {
g := func(f func(*processor.Grant)) *processor.Grant {
x := &processor.Grant{Active: true, Currency: "usd", MaxAmount: 3000, ExpiresAt: 4_000_000_000}
f(x)
return x
}
now := time.Now()
for _, tc := range []struct {
name string
g *processor.Grant
amt int64
cur string
off bool
want string
}{
{"usable", g(func(*processor.Grant) {}), 1500, "usd", false, ""},
{"revoked", g(func(x *processor.Grant) { x.Active, x.Why = false, "revoked" }), 1500, "usd", false, "token_revoked"},
{"expired", g(func(x *processor.Grant) { x.ExpiresAt = 1 }), 1500, "usd", false, "token_expired"},
{"wrong currency", g(func(*processor.Grant) {}), 1500, "eur", false, "token_wrong_currency"},
{"over its limit", g(func(*processor.Grant) {}), 3001, "usd", false, "token_over_limit"},
{"nothing left", g(func(x *processor.Grant) { x.Captured = 3000 }), 1, "usd", false, "token_over_limit"},
{"one-time, off session", g(func(*processor.Grant) {}), 1500, "usd", true, "token_not_recurring"},
{"recurring, off session", g(func(x *processor.Grant) { x.RecurringInterval = "month" }), 1500, "usd", true, ""},
} {
if code, _ := processor.GrantRefusal(tc.g, tc.amt, tc.cur, tc.off, now); code != tc.want {
t.Errorf("%s: code = %q; want %q", tc.name, code, tc.want)
}
}
}