mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Square keeps an event's created_at across every retry, and the receiver refused anything older than five minutes, so every retry of an event first sent while cloud was down (Recreate, ten rollouts a day) was refused until Square gave up. The verifier now lives in payment/providers/square and needs only the key and the URL; thirdparty/square is the API client and no longer registers itself or verifies anything, so every registry holds one Square type. Replay is stopped by the event id instead: each verified event is recorded once for the whole deployment, keyed on the verifying processor and the event id with an insert-if-absent swap, so a repeat is a duplicate under any path or org. The path names the one processor that verifies, and only one that holds its webhook secret may: an unknown name, or a verifier that needs no secret, is refused rather than tried. The books an event lands in are the verifying account's, else the deployment's brand — never a header or session, which on a replay are the replayer's. An effect that did not land (a store or the ledger did not answer) leaves the delivery open with a 503, so the provider redelivers; it used to be acknowledged and never retried. A settlement reads the payment index before the org's books, because an in-session charge's receipt is in the payer's org. POST /v1/commerce/webhooks/:provider is declared once, in setupRoutes, so standalone and embedded commerce serve the same address; the /v1/billing/webhooks spelling is gone. thirdparty/kms read /v1/kms/orgs/<org>/secrets/..., which cloud no longer serves, and was off in production anyway, so per-org payment credentials never loaded. It is deleted. secrets.Org reads orgs/<org>/<name> through the host's reader, the caller re-pointed at that org with zip.ActingAs so the KMS tenancy check admits it, and payment.Hydrate loads an org's credentials where the org receives the money: storefront checkout, storefront subscriptions and wire, and the brand's bank for the billing wire rail. Hanzo's own billing no longer reads the paying org's credentials; it charges the deployment's account, as it did. The standalone binary reads through hanzoai/kms/sdk/go.
140 lines
5.4 KiB
Go
140 lines
5.4 KiB
Go
// Copyright (c) 2014-present Hanzo AI, Inc.
|
|
// Licensed under MIT OR Apache-2.0. See LICENSE-MIT and LICENSE-APACHE.
|
|
|
|
package commerce
|
|
|
|
import (
|
|
"io"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
luxlog "github.com/luxfi/log"
|
|
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// TestMount_RegistersHealth boots an empty zip.App, runs commerce.Mount,
|
|
// and asserts the native /v1/commerce/health route answers 200 with
|
|
// {"service":"commerce"}. Covers:
|
|
//
|
|
// - Mount() can be called against a fresh *zip.App
|
|
// - the native zip health route reaches Fiber and returns JSON
|
|
//
|
|
// The rest of the mounted surface is covered by TestMount_GinSurfaceReachable
|
|
// below.
|
|
func TestMount_RegistersHealth(t *testing.T) {
|
|
app := zip.New(zip.Config{Logger: luxlog.New("test")})
|
|
if err := Mount(app, t.TempDir(), luxlog.New("test"), nil); err != nil {
|
|
t.Fatalf("Mount: %v", err)
|
|
}
|
|
|
|
req := httptest.NewRequest("GET", "/v1/commerce/health", nil)
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Fiber Test: %v", err)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("status: got %d want 200", resp.StatusCode)
|
|
}
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatalf("read body: %v", err)
|
|
}
|
|
body := string(raw)
|
|
if !strings.Contains(body, `"service":"commerce"`) {
|
|
t.Fatalf("body: got %q, want service=commerce", body)
|
|
}
|
|
}
|
|
|
|
// TestMount_PublicSurfaceReachable proves the /v1/commerce group actually
|
|
// reaches commerce's handlers on the host's app — not just the health route
|
|
// Mount registers by hand.
|
|
//
|
|
// It asks for the public catalog, which needs no host resolution and no
|
|
// credential, so a 200 means the route resolved into the real handler. It used
|
|
// to ask for /v1/commerce/tenant and only assert the body was not the SPA
|
|
// fallback string; that route stopped existing when org-as-tenant became
|
|
// GET /v1/commerce/org, and a mounted app has no SPA fallback to produce that
|
|
// string anyway, so the assertion held no matter what — including if Mount
|
|
// registered nothing at all. Which is the regression it was written to catch.
|
|
func TestMount_PublicSurfaceReachable(t *testing.T) {
|
|
app := zip.New(zip.Config{Logger: luxlog.New("test")})
|
|
if err := Mount(app, t.TempDir(), luxlog.New("test"), nil); err != nil {
|
|
t.Fatalf("Mount: %v", err)
|
|
}
|
|
|
|
req := httptest.NewRequest("GET", "/v1/commerce/catalog", nil)
|
|
req.Host = "pay.example.test"
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Fiber Test: %v", err)
|
|
}
|
|
raw, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatalf("read body: %v", err)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("GET /v1/commerce/catalog = %d (%s); the mount did not register commerce's public group on the host app",
|
|
resp.StatusCode, strings.TrimSpace(string(raw)))
|
|
}
|
|
}
|
|
|
|
// TestMount_NoUngatedTypedOpSurface is a TRIPWIRE, not a feature test.
|
|
//
|
|
// zip auto-derives an MCP tool surface from the typed-op registry and mounts it
|
|
// at /mcp — "enabled by default", installed by prepare() whenever len(app.ops)
|
|
// > 0 (zip/mcp.go). Every typed op becomes a tool whose tools/call runs the op's
|
|
// invoke core DIRECTLY. That path does not traverse the Fiber route chain, so
|
|
// NO transport middleware runs on it — including the money-mint gate.
|
|
//
|
|
// Concretely: registering Deposit as a zip.Post[In,Out] would publish
|
|
// billing.deposit as an MCP tool that any caller can invoke with no credential,
|
|
// while middleware.PlatformOnly (mounted as route middleware) never fires. That
|
|
// mints unbounded spendable balance — the exact real-money-GA hole PlatformOnly
|
|
// exists to close.
|
|
//
|
|
// The only seam that gates the invoke core is App.Authorize (zip/authorize.go),
|
|
// which runs over REST and MCP alike. It landed in zip v1.8.3; NO earlier
|
|
// version — including the v1.8.2 the cloud binary pins — can gate a typed op at
|
|
// all. Authorize is also a single app-level slot (a setter, not an appender), so
|
|
// on a shared App the LAST subsystem to call it silently wins.
|
|
//
|
|
// Commerce therefore contributes ZERO typed ops today: its surface is the gin
|
|
// engine behind AdaptNetHTTP, whose middleware chain is intact. This test pins
|
|
// that. It fails the moment commerce registers a typed op — at which point the
|
|
// author MUST prove the money ops are gated at the invoke seam (App.Authorize on
|
|
// zip >= v1.8.3, or MCP disabled) before deleting this guard.
|
|
func TestMount_NoUngatedTypedOpSurface(t *testing.T) {
|
|
app := zip.New(zip.Config{Logger: luxlog.New("test")})
|
|
if err := Mount(app, t.TempDir(), luxlog.New("test"), nil); err != nil {
|
|
t.Fatalf("Mount: %v", err)
|
|
}
|
|
|
|
// Install the deferred projections (OpenAPI + MCP) exactly as a served app
|
|
// does. zip mounts /mcp here iff at least one typed op is registered.
|
|
if err := app.Build(); err != nil {
|
|
t.Fatalf("Build: %v", err)
|
|
}
|
|
|
|
req := httptest.NewRequest("POST", "/mcp",
|
|
strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/list"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Fiber Test: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 404 {
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
t.Fatalf("commerce.Mount now registers typed zip ops: /mcp answered %d (%s).\n"+
|
|
"Every typed op is an MCP tool whose tools/call BYPASSES route middleware —\n"+
|
|
"so middleware.PlatformOnly does NOT gate it. Before removing this guard,\n"+
|
|
"gate the money ops at the invoke seam via app.Authorize (zip >= v1.8.3)\n"+
|
|
"and prove an unauthenticated tools/call of a mint op is refused.",
|
|
resp.StatusCode, strings.TrimSpace(string(raw)))
|
|
}
|
|
}
|