mirror of
https://github.com/hanzoai/commerce.git
synced 2026-11-23 05:38:11 +00:00
* 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.
114 lines
4.1 KiB
Go
114 lines
4.1 KiB
Go
package processor
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// A Grant is a scoped, revocable permission from a payer (or the agent acting for them) to
|
|
// charge a payment method once, or on a schedule, within limits. A processor that issues
|
|
// them to sellers implements GrantReader; commerce reads the grant before every charge
|
|
// made on it.
|
|
type Grant struct {
|
|
ID string
|
|
// Active says the grant can still be used: it has not been revoked or deactivated.
|
|
// Why is the processor's reason when it is not ("revoked", "expired", ...).
|
|
Active bool
|
|
Why string
|
|
// Limits the payer set. MaxAmount is in minor units of Currency; ExpiresAt is unix
|
|
// seconds; RecurringInterval is "" for a one-time grant, else the period it may be
|
|
// charged on ("month").
|
|
Currency string
|
|
MaxAmount int64
|
|
// Captured is what has already been charged on the token, against MaxAmount.
|
|
Captured int64
|
|
ExpiresAt int64
|
|
RecurringInterval string
|
|
// What can be said of the payment method behind it without the credentials.
|
|
Country string
|
|
Brand string
|
|
Last4 string
|
|
// Test says the grant was made in the processor's test mode.
|
|
Test bool
|
|
}
|
|
|
|
// GrantReader is a processor that can say what a grant allows. An error is the
|
|
// processor's refusal to know the grant (not found, not ours), and means no charge.
|
|
type GrantReader interface {
|
|
Grant(ctx context.Context, id string) (*Grant, error)
|
|
}
|
|
|
|
// Stablecoin deposits: a payment made by sending a stablecoin to an address the processor
|
|
// shows, with no page in between.
|
|
|
|
// StablecoinReady says what the processor can take in stablecoins now.
|
|
type StablecoinReady struct {
|
|
// On says the account can take stablecoin payments now.
|
|
On bool
|
|
// Networks the processor's deposit mode accepts, in the processor's names, and the
|
|
// currency stablecoin payments are presented in ("usd" only).
|
|
Networks []string
|
|
Currency string
|
|
// Why is the reason when On is false.
|
|
Why string
|
|
}
|
|
|
|
// StablecoinDepositIn opens a stablecoin payment of Amount (minor units of Currency, usd).
|
|
type StablecoinDepositIn struct {
|
|
Amount int64
|
|
Currency string
|
|
Network string
|
|
Key string // idempotency key
|
|
Metadata map[string]interface{}
|
|
Desc string
|
|
}
|
|
|
|
// StablecoinDeposit is where to send the payment: the intent that tracks it and, per
|
|
// network, the address and the token contract to send.
|
|
type StablecoinDeposit struct {
|
|
ID string
|
|
Amount int64
|
|
Currency string
|
|
Addresses []DepositAddress
|
|
}
|
|
|
|
// DepositAddress is one network's address and the tokens it accepts.
|
|
type DepositAddress struct {
|
|
Network string
|
|
Address string
|
|
Tokens []DepositToken
|
|
}
|
|
|
|
// DepositToken is one token a deposit address accepts: its symbol and contract (or mint).
|
|
type DepositToken struct {
|
|
Currency string
|
|
Contract string
|
|
}
|
|
|
|
// StablecoinDepositor is a processor that takes stablecoin payments by deposit.
|
|
type StablecoinDepositor interface {
|
|
StablecoinReady(ctx context.Context) StablecoinReady
|
|
OpenStablecoinDeposit(ctx context.Context, in StablecoinDepositIn) (*StablecoinDeposit, error)
|
|
}
|
|
|
|
// GrantRefusal says why a grant cannot be charged amount of cur at now ("" when it can),
|
|
// with a code: token_revoked, token_expired, token_wrong_currency, token_over_limit or
|
|
// token_not_recurring. offSession is a charge without the buyer there, which needs a token
|
|
// that carries a recurring interval: a one-time token pays once.
|
|
func GrantRefusal(g *Grant, amount int64, cur string, offSession bool, now time.Time) (code, why string) {
|
|
switch {
|
|
case !g.Active:
|
|
return "token_revoked", "the token is " + g.Why
|
|
case g.ExpiresAt > 0 && now.Unix() >= g.ExpiresAt:
|
|
return "token_expired", "the token has expired"
|
|
case g.Currency != "" && g.Currency != strings.ToLower(cur):
|
|
return "token_wrong_currency", "the token is for " + strings.ToUpper(g.Currency) + ", not " + strings.ToUpper(cur)
|
|
case g.MaxAmount > 0 && amount > g.MaxAmount-g.Captured:
|
|
return "token_over_limit", "the amount is over what the token has left"
|
|
case offSession && g.RecurringInterval == "":
|
|
return "token_not_recurring", "the token carries no recurring authorization, so it pays once"
|
|
}
|
|
return "", ""
|
|
}
|