mirror of
https://github.com/hanzoai/commerce.git
synced 2026-11-23 13:31:30 +00:00
openWireReference was unexported and called only inside commerce's wire handler, so a caller other than that handler was given the payer's TOPUP- reference, which ReconcileWire does not find. ReadWireInstructions returns the instructions with a WIRE- reference, values in; the handler calls it. Testing: go test ./api/billing -run Wire: the reference starts WIRE-, is stable until paid, and ReconcileWire credits the payer it names. Risk: none; the endpoint answers as before. How to revert: git revert this commit. Co-authored-by: Hanzo Dev <[email protected]>
152 lines
5.6 KiB
Go
152 lines
5.6 KiB
Go
package billing
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/commerce/secrets"
|
|
"github.com/hanzoai/commerce/util/test/ae"
|
|
)
|
|
|
|
// wireReference must survive a bank memo field: uppercase [A-Z0-9-] only, and
|
|
// distinct payers must stay distinct after the trip (reconciliation depends on
|
|
// recovering the payer from exactly this alphabet).
|
|
func TestWireReference(t *testing.T) {
|
|
cases := []struct{ payer, want string }{
|
|
{"hanzo", "TOPUP-HANZO"},
|
|
{"hanzo/z", "TOPUP-HANZO-Z"},
|
|
{"acme-labs/alice.b", "TOPUP-ACME-LABS-ALICE-B"},
|
|
{"org_1", "TOPUP-ORG-1"},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := wireReference(tc.payer); got != tc.want {
|
|
t.Errorf("wireReference(%q) = %q, want %q", tc.payer, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// brandStore is the host's secret plane with cloud's tenancy rule: orgs/<org>/…
|
|
// answers only a call acting for <org>.
|
|
type brandStore map[string]string
|
|
|
|
func (s brandStore) GetSecret(ctx context.Context, ref string) ([]byte, error) {
|
|
rest, ok := strings.CutPrefix(ref, "orgs/")
|
|
if !ok {
|
|
return nil, errors.New("not a tenant ref")
|
|
}
|
|
if org, _, _ := strings.Cut(rest, "/"); zip.CallerOf(ctx).Org != org {
|
|
return nil, errors.New("refused: the call acts for another org")
|
|
}
|
|
if v, ok := s[ref]; ok {
|
|
return []byte(v), nil
|
|
}
|
|
return nil, errors.New("no such secret")
|
|
}
|
|
|
|
// The brand that owns the host is where the wire is sent, so a customer of any
|
|
// org paying on pay.lux.network reads lux's bank and one on pay.hanzo.ai reads
|
|
// Hanzo's — each from orgs/<brand>/wire, asked as the brand, whoever is asking.
|
|
func TestWireForReadsTheBrandsOwnBank(t *testing.T) {
|
|
ctx := ae.NewContext()
|
|
defer ctx.Close()
|
|
webhookOrg(t, ctx, "hanzo", true)
|
|
webhookOrg(t, ctx, "lux", true)
|
|
prev := secrets.Get()
|
|
secrets.Set(brandStore{
|
|
"orgs/hanzo/wire/WIRE_ACCOUNT_NUMBER": "000488108457",
|
|
"orgs/hanzo/wire/WIRE_BANK_NAME": "Hanzo Bank",
|
|
"orgs/lux/wire/WIRE_ACCOUNT_NUMBER": "000999900001",
|
|
"orgs/lux/wire/WIRE_BANK_NAME": "Lux Bank",
|
|
})
|
|
t.Cleanup(func() { secrets.Set(prev) })
|
|
|
|
payer := zip.WithCaller(ctx, zip.Caller{Org: "acme", User: "alice"})
|
|
for host, want := range map[string]string{"pay.hanzo.ai": "000488108457", "pay.lux.network": "000999900001"} {
|
|
w, err := WireFor(payer, host, "acme/alice")
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", host, err)
|
|
}
|
|
if w.AccountNumber != want || w.Reference != "TOPUP-ACME-ALICE" {
|
|
t.Fatalf("%s: account %q reference %q, want %q and the payer's reference", host, w.AccountNumber, w.Reference, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A brand with no bank in its secrets has nowhere to send the money: one refusal.
|
|
func TestWireForWithNoBankRefuses(t *testing.T) {
|
|
ctx := ae.NewContext()
|
|
defer ctx.Close()
|
|
webhookOrg(t, ctx, "hanzo", true)
|
|
prev := secrets.Get()
|
|
secrets.Set(brandStore{})
|
|
t.Cleanup(func() { secrets.Set(prev) })
|
|
if _, err := WireFor(ctx, "pay.hanzo.ai", "acme/alice"); !IsWireUnconfigured(err) {
|
|
t.Fatalf("err = %v, want the wire rail's one refusal", err)
|
|
}
|
|
}
|
|
|
|
// GET /v1/billing/wire, through the handler: the request's caller reaches the
|
|
// host's KMS (Forward), so a customer of acme paying on pay.hanzo.ai reads
|
|
// Hanzo's bank.
|
|
func TestWireEndpointReadsTheBrandsBankAsTheCaller(t *testing.T) {
|
|
ctx := ae.NewContext()
|
|
defer ctx.Close()
|
|
webhookOrg(t, ctx, "hanzo", true)
|
|
acme := webhookOrg(t, ctx, "acme", true)
|
|
prev := secrets.Get()
|
|
secrets.Set(brandStore{
|
|
"orgs/hanzo/wire/WIRE_ACCOUNT_NUMBER": "000488108457",
|
|
"orgs/hanzo/wire/WIRE_BANK_NAME": "Hanzo Bank",
|
|
})
|
|
t.Cleanup(func() { secrets.Set(prev) })
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/billing/wire", nil)
|
|
req.Host = "pay.hanzo.ai"
|
|
req.Header.Set(zip.HeaderOrg, "acme")
|
|
req.Header.Set(zip.HeaderUser, "alice")
|
|
resp := driveSeeded(func(c *zip.Ctx) {
|
|
c.SetContext(ctx)
|
|
c.Locals("organization", acme)
|
|
}, "/v1/billing/wire", req, GetBillingWireInstructions)
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
if resp.StatusCode != http.StatusOK || !strings.Contains(string(raw), "000488108457") {
|
|
t.Fatalf("GET /v1/billing/wire = %d %s, want Hanzo's bank", resp.StatusCode, raw)
|
|
}
|
|
}
|
|
|
|
// ReadWireInstructions hands out the transfer's own reference, which the reconcile finds; the
|
|
// same payer asking again before paying gets the same one.
|
|
func TestReadWireInstructionsCarriesAReferenceTheReconcileFinds(t *testing.T) {
|
|
ctx := ae.NewContext()
|
|
defer ctx.Close()
|
|
webhookOrg(t, ctx, "hanzo", true)
|
|
acme := webhookOrg(t, ctx, "acme", true)
|
|
prev := secrets.Get()
|
|
secrets.Set(brandStore{"orgs/hanzo/wire/WIRE_ACCOUNT_NUMBER": "000488108457", "orgs/hanzo/wire/WIRE_BANK_NAME": "Hanzo Bank"})
|
|
t.Cleanup(func() { secrets.Set(prev) })
|
|
led := newFakeLedger()
|
|
injectLedger(t, led)
|
|
|
|
payer := zip.WithCaller(ctx, zip.Caller{Org: "acme", User: "alice"})
|
|
w, err := ReadWireInstructions(payer, acme, "pay.hanzo.ai", "acme/alice")
|
|
if err != nil || !strings.HasPrefix(w.Reference, "WIRE-") || w.Memo != w.Reference {
|
|
t.Fatalf("instructions = %+v (%v); want a WIRE- reference in the reference and the memo", w, err)
|
|
}
|
|
if again, _ := ReadWireInstructions(payer, acme, "pay.hanzo.ai", "acme/alice"); again.Reference != w.Reference {
|
|
t.Fatalf("an unpaid wire changed its reference: %s -> %s", w.Reference, again.Reference)
|
|
}
|
|
if out, f := ReconcileWire(ctx, w.Reference, 5000, "bank-1", "[email protected]"); f != nil || !out.Credited || out.Subject != "acme/alice" {
|
|
t.Fatalf("the reconcile did not find the reference: %+v %v", out, f)
|
|
}
|
|
if _, err := ReadWireInstructions(ctx, acme, "pay.hanzo.ai", ""); !IsWireUnconfigured(err) {
|
|
t.Fatalf("a payer-less read = %v; want the rail's one refusal", err)
|
|
}
|
|
}
|