mirror of
https://github.com/hanzoai/commerce.git
synced 2026-11-24 10:35:19 +00:00
Three agentic-commerce protocols as Go packages behind commerce's headless checkout (api/billing), with their specifications' own examples as test fixtures: agentic/acp (Agentic Commerce Protocol API 2026-04-17, seller checkout), agentic/ap2 (Agent Payments Protocol v0.2 mandates, ES256 SD-JWT) and agentic/mpp (the "Payment" HTTP authentication scheme, Internet-Draft -01). Nothing is chosen for the operator: no price, payee, key, cap, retention, lifetime or secret in any package. Not mounted by commerced or the cloud host: each needs operator values (HIP-1223 section 6). UCP follows in its own change (branch agentic-ucp). api/billing, opt-in (zero checks nothing): SubscribeIn.ExpectedCents refuses a moved plan price (price_changed) before the card is touched; PaymentIn.Quote refuses a checkout swapped after verification (checkout_changed) before the Pending swap. Each package was reviewed adversarially and reduced under the owner's rule; findings were fixed test-first (replay re-running a paid handler, no durable replay store, free credit beside a paid resource, unbound challenge, signature not covering timestamp/method/path, plan quote not bound to the charge, charge not pinned to the verified cart, malleable open-mandate identity, claim released on unknown outcome, plan becoming a subscription, and others). Testing: go build ./..., go vet ./agentic/... ./api/billing/ clean; go test -race ./agentic/... passes (acp, ap2, mpp); api/billing agent/quote/grant/catalog/subscribe/checkout/sale/gift tests pass except TestLiveStripe_* and TestRed_SaleWebhook*, which need STRIPE_TEST_SECRET_KEY and are red without it on any branch. Risk: no behavior change for existing routes; the only edits to money code are the two opt-in checks. The packages take no traffic until a host mounts them; no module dependency added. How to revert: revert this commit. Claude-Session: https://claude.ai/code/session_015XYKLn69gF4x75RWePpDBV
77 lines
3.3 KiB
Go
77 lines
3.3 KiB
Go
package billing
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hanzoai/commerce/models/checkoutsession"
|
|
"github.com/hanzoai/commerce/models/organization"
|
|
"github.com/hanzoai/commerce/payment/processor"
|
|
"github.com/hanzoai/commerce/util/test/ae"
|
|
)
|
|
|
|
// A completion that carries the Quote its caller verified is taken only if the checkout is
|
|
// still that line at that total. A buyer who changes the checkout between the caller's check
|
|
// and the completion, as UpdateCheckout lets them, is refused before any money moves, and the
|
|
// checkout stays usable.
|
|
func TestCompleteCheckout_AQuotedLineIsChargedAsQuotedOrNotAtAll(t *testing.T) {
|
|
ctx := ae.NewContext()
|
|
defer ctx.Close()
|
|
org, _, _, ch := stripeBuyer(t, ctx, "agent-changed", "US", 1)
|
|
rail := grantRail{ch}
|
|
old := processorsForOrg
|
|
processorsForOrg = func(*organization.Organization) *processor.Registry {
|
|
reg := processor.NewRegistry(processor.DefaultConfig())
|
|
reg.Register(rail)
|
|
return reg
|
|
}
|
|
t.Cleanup(func() { processorsForOrg = old })
|
|
min, _ := topupBounds()
|
|
|
|
v, f := CreateCheckout(ctx, org, "agent-changed", "", []ItemIn{{Kind: "credit", UsdCents: min}})
|
|
if f != nil {
|
|
t.Fatal(f)
|
|
}
|
|
quote := Quote{Kind: "credit", UsdCents: min, TotalCents: min}
|
|
// The buyer swaps the line for a larger one after the caller verified it.
|
|
if _, f := UpdateCheckout(ctx, org, "agent-changed", "", v.ID, []ItemIn{{Kind: "credit", UsdCents: 2 * min}}); f != nil {
|
|
t.Fatal(f)
|
|
}
|
|
pay := PaymentIn{Type: "shared_payment_token", Token: "spt_changed", Quote: quote}
|
|
out := CompleteCheckout(ctx, org, "agent-changed", "", v.ID, "k-1", pay)
|
|
if out.Status != 409 || out.View.Error == nil || out.View.Error.Code != "checkout_changed" || out.View.Order != nil {
|
|
t.Fatalf("a changed checkout answered %d %+v", out.Status, out.View)
|
|
}
|
|
if len(ch.reqs) != 0 {
|
|
t.Fatalf("money moved on a refused completion: %d charges", len(ch.reqs))
|
|
}
|
|
// A quote of another total, of another kind, and of a plan are each not this checkout.
|
|
for _, q := range []Quote{
|
|
{Kind: "credit", UsdCents: 2 * min, TotalCents: min},
|
|
{Kind: "plan", Plan: "go", TotalCents: 2 * min},
|
|
{Kind: "credit", UsdCents: min, TotalCents: 2 * min},
|
|
} {
|
|
pay.Quote = q
|
|
if out = CompleteCheckout(ctx, org, "agent-changed", "", v.ID, "k-2", pay); out.View.Error == nil || out.View.Error.Code != "checkout_changed" {
|
|
t.Fatalf("quote %+v answered %d %+v", q, out.Status, out.View)
|
|
}
|
|
}
|
|
if s, _, _ := checkoutsession.Get(v.ID); s.Status != checkoutsession.Failed {
|
|
t.Fatalf("the refused checkout is %s; it stays usable as failed", s.Status)
|
|
}
|
|
// The line as it now is, quoted, is charged; and a completion with no quote still is.
|
|
pay.Quote = Quote{Kind: "credit", UsdCents: 2 * min, TotalCents: 2 * min}
|
|
if out = CompleteCheckout(ctx, org, "agent-changed", "", v.ID, "k-3", pay); out.Status != 200 || out.View.Order == nil {
|
|
t.Fatalf("the checkout as quoted answered %d %+v", out.Status, out.View)
|
|
}
|
|
if len(ch.reqs) != 1 || int64(ch.reqs[0].Amount) != 2*min {
|
|
t.Fatalf("charges = %+v; want the one at the quote", ch.reqs)
|
|
}
|
|
w, f := CreateCheckout(ctx, org, "agent-changed", "", []ItemIn{{Kind: "credit", UsdCents: min}})
|
|
if f != nil {
|
|
t.Fatal(f)
|
|
}
|
|
if out = CompleteCheckout(ctx, org, "agent-changed", "", w.ID, "k-4", PaymentIn{Type: "shared_payment_token", Token: "spt_changed"}); out.Status != 200 {
|
|
t.Fatalf("a completion with no quote answered %d %+v", out.Status, out.View)
|
|
}
|
|
}
|