console: gate the payments door on the payment record and say test mode
Final review finding E (Astra 5 and 6, Ruling 26). The payments door
listed rows (or none) whether or not the record had caught up, so the
CLI could print "no payments to show" before the backfill; and nothing
told test payments from real money.
GET /api/cli/billing/payments now answers {"available", "why" (only when
unavailable), "test_mode", "payments" (only when available)}. The list
is the money figure "payments" declaring inPayments (paymentsFigure),
guarded by checkGateInvariant in TestPaymentsFigureGateInvariant. The
CLI prints BDC-16 in test mode, then the list as before, or BDC-13 with
the payments reason (BDC-18) when the door refuses. TestAPIBillingPayments
and TestBillingPaymentsCLI cover the never-reconciled, stale, test-mode
and live cases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+136
−15
billing_cli.go+34 −8billing_cli_test.go+31 −3internal/console/apibilling.go+37 −2internal/console/apibilling_test.go+34 −2
diff --git a/billing_cli.go b/billing_cli.go| index 1d0478152..8c9109416 100644 |
| --- a/billing_cli.go |
| +++ b/billing_cli.go |
| @@ -146,11 +146,11 @@ func runBillingWithSession(ctx context.Context, sess *apiSession, args []string, |
| if status != http.StatusOK { |
| return apiErrorFromBody(status, body) |
| } |
| - var list []billingPaymentReply |
| - if err := json.Unmarshal(body, &list); err != nil { |
| + var reply billingPaymentsReply |
| + if err := json.Unmarshal(body, &reply); err != nil { |
| return fmt.Errorf("billing payments: unreadable response: %w", err) |
| } |
| - printBillingPayments(out, list) |
| + printBillingPaymentsReply(out, reply) |
| return nil |
| case "close": |
| id := fs.Int64("id", 0, cliBillingCopyFlagID) |
| @@ -289,6 +289,14 @@ var billingWhyText = map[string]string{ |
| "amounts": cliBillingCopyWhyAmounts, |
| } |
| +// billingWhy is the text of one of the door's reason codes, or the code. |
| +func billingWhy(code string) string { |
| + if why, ok := billingWhyText[code]; ok { |
| + return why |
| + } |
| + return code |
| +} |
| + |
| // printBillingRevenue prints the test mode line when it applies, then one |
| // line per figure: its label, two spaces and its value, or the reason it is |
| // not available yet. |
| @@ -301,11 +309,7 @@ func printBillingRevenue(w io.Writer, rev billingRevenueReply) { |
| if f.Available { |
| v = value() |
| } else { |
| - why, ok := billingWhyText[f.Why] |
| - if !ok { |
| - why = f.Why |
| - } |
| - v = fmt.Sprintf(cliBillingCopyNotAvailable, why) |
| + v = fmt.Sprintf(cliBillingCopyNotAvailable, billingWhy(f.Why)) |
| } |
| fmt.Fprintln(w, label+" "+v) |
| } |
| @@ -352,6 +356,15 @@ type billingPaymentReply struct { |
| SettledCurrency string `json:"settled_currency"` |
| } |
| +// billingPaymentsReply mirrors the payments door's answer: the list only |
| +// when Available, else Why. |
| +type billingPaymentsReply struct { |
| + Available bool `json:"available"` |
| + Why string `json:"why"` |
| + TestMode bool `json:"test_mode"` |
| + Payments []billingPaymentReply `json:"payments"` |
| +} |
| + |
| // billingPaymentKindFlags maps -kind's words to the door's kinds. |
| var billingPaymentKindFlags = map[string]string{ |
| "subscription": "subscription", "once-off": "once_off", "refund": "refund", "fee": "fee", "unmatched": "unmatched", |
| @@ -364,6 +377,19 @@ var billingPaymentKindWords = map[string]string{ |
| "refund": cliBillingCopyKindRefund, "fee": cliBillingCopyKindFee, |
| } |
| +// printBillingPaymentsReply prints the test mode line when it applies, |
| +// then the payments, or the reason they are not available yet. |
| +func printBillingPaymentsReply(w io.Writer, reply billingPaymentsReply) { |
| + if reply.TestMode { |
| + fmt.Fprintln(w, cliBillingCopyTestMode) |
| + } |
| + if !reply.Available { |
| + fmt.Fprintln(w, fmt.Sprintf(cliBillingCopyNotAvailable, billingWhy(reply.Why))) |
| + return |
| + } |
| + printBillingPayments(w, reply.Payments) |
| +} |
| + |
| // printBillingPayments prints the header and one line per payment: when |
| // (UTC), the account (or unmatched), the kind, the reason, what was charged |
| // and its tax in the charge's currency, what settled and the fee in the |
diff --git a/billing_cli_test.go b/billing_cli_test.go| index 9d10d8cfe..476cb2457 100644 |
| --- a/billing_cli_test.go |
| +++ b/billing_cli_test.go |
| @@ -221,11 +221,11 @@ func billingFeeRowJSON(t *testing.T, m billing.Movement) string { |
| func TestBillingPaymentsCLI(t *testing.T) { |
| var paths []string |
| - reply := `[{"movement":"mv_2","source":"re_1","at":"2026-11-06T09:30:00Z","account":"bab","kind":"refund","reason":"renewal","detail":"","amount":-1200,"tax":-200,"currency":"usd","settled":-1071,"fee":0,"settled_currency":"eur"},` + |
| + reply := `{"available":true,"test_mode":false,"payments":[{"movement":"mv_2","source":"re_1","at":"2026-11-06T09:30:00Z","account":"bab","kind":"refund","reason":"renewal","detail":"","amount":-1200,"tax":-200,"currency":"usd","settled":-1071,"fee":0,"settled_currency":"eur"},` + |
| `{"movement":"mv_1","source":"ch_1","at":"2026-11-02T08:00:00Z","account":"","kind":"once_off","reason":"domain","detail":"shop.example.com","amount":240000,"tax":0,"currency":"eur","settled":240000,"fee":3500,"settled_currency":"eur"},` + |
| billingFeeRowJSON(t, billing.Movement{ID: "mv_0", Kind: billing.MovementFee, Amount: -500, Currency: "eur", |
| At: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC)}) + `,` + |
| - `{"movement":"mv_s","source":"ch_s","at":"2026-11-01T00:00:00Z","account":"shop","kind":"subscription","reason":"first","detail":"","amount":1200,"tax":200,"currency":"gbp","settled":1150,"fee":40,"settled_currency":"eur"}]` |
| + `{"movement":"mv_s","source":"ch_s","at":"2026-11-01T00:00:00Z","account":"shop","kind":"subscription","reason":"first","detail":"","amount":1200,"tax":200,"currency":"gbp","settled":1150,"fee":40,"settled_currency":"eur"}]}` |
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| paths = append(paths, r.Method+" "+r.URL.RequestURI()) |
| io.WriteString(w, reply) |
| @@ -260,7 +260,7 @@ func TestBillingPaymentsCLI(t *testing.T) { |
| t.Fatalf("paths %v, want %v", paths, wantPaths) |
| } |
| - reply = `[]` |
| + reply = `{"available":true,"test_mode":false,"payments":[]}` |
| out.Reset() |
| if err := runBillingWithSession(context.Background(), s, []string{"payments"}, &out); err != nil { |
| t.Fatal(err) |
| @@ -269,6 +269,34 @@ func TestBillingPaymentsCLI(t *testing.T) { |
| t.Fatalf("empty: %q, want %q", got, cliBillingCopyNoPayments) |
| } |
| + // Test mode says so first. A record that has not caught up lists |
| + // nothing and says why, never "no payments to show". |
| + reply = `{"available":true,"test_mode":true,"payments":[]}` |
| + out.Reset() |
| + if err := runBillingWithSession(context.Background(), s, []string{"payments"}, &out); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, want := strings.TrimRight(out.String(), "\n"), cliBillingCopyTestMode+"\n"+cliBillingCopyNoPayments; got != want { |
| + t.Fatalf("test mode: %q, want %q", got, want) |
| + } |
| + reply = `{"available":false,"why":"payments","test_mode":true}` |
| + out.Reset() |
| + if err := runBillingWithSession(context.Background(), s, []string{"payments"}, &out); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, want := strings.TrimRight(out.String(), "\n"), |
| + cliBillingCopyTestMode+"\n"+fmt.Sprintf(cliBillingCopyNotAvailable, cliBillingCopyWhyPayments); got != want { |
| + t.Fatalf("not caught up: %q, want %q", got, want) |
| + } |
| + reply = `{"available":false,"why":"payments","test_mode":false}` |
| + out.Reset() |
| + if err := runBillingWithSession(context.Background(), s, []string{"payments"}, &out); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, want := strings.TrimRight(out.String(), "\n"), fmt.Sprintf(cliBillingCopyNotAvailable, cliBillingCopyWhyPayments); got != want { |
| + t.Fatalf("not caught up, live: %q, want %q", got, want) |
| + } |
| + |
| // A bad -month or -kind never reaches the console. |
| hits := len(paths) |
| for _, c := range []struct { |
diff --git a/internal/console/apibilling.go b/internal/console/apibilling.go| index 45a7c8114..8b1bcf738 100644 |
| --- a/internal/console/apibilling.go |
| +++ b/internal/console/apibilling.go |
| @@ -491,6 +491,26 @@ type billingPaymentReply struct { |
| SettledCurrency string `json:"settled_currency"` |
| } |
| +// billingPaymentsReply is GET /api/cli/billing/payments's answer. The list |
| +// is a money figure ("payments", declaring inPayments): Payments is sent |
| +// only when the payment record is current, else Why says what is not. |
| +// TestMode is the deployment's mode, so test payments are never taken |
| +// for real money. |
| +type billingPaymentsReply struct { |
| + Available bool `json:"available"` |
| + Why string `json:"why,omitempty"` |
| + TestMode bool `json:"test_mode"` |
| + Payments []billingPaymentReply `json:"payments,omitzero"` |
| +} |
| + |
| +// paymentsFigure puts the payments list through the money gate: it |
| +// declares the payment record and nothing else. Pure. |
| +func paymentsFigure(in moneyInputs) moneyFigure { |
| + u := moneyUse{Inputs: inPayments} |
| + ok, why := moneyReady(in, u) |
| + return moneyFigure{Name: "payments", Use: u, Shown: ok, Why: why} |
| +} |
| + |
| // billingPaymentsLimit is how many payments the door lists. |
| const billingPaymentsLimit = 500 |
| @@ -501,7 +521,8 @@ var billingPaymentKinds = map[string]bool{"": true, string(billing.PaymentSubscr |
| // handleAPIBillingPayments is GET /api/cli/billing/payments?month=YYYY-MM&kind=K: |
| // the recorded payments, newest first, up to billingPaymentsLimit; month |
| -// (UTC) and kind narrow them. |
| +// (UTC) and kind narrow them. While the payment record is not current |
| +// (paymentsFigure refuses) it lists none and says why. |
| func (s *Server) handleAPIBillingPayments(w http.ResponseWriter, r *http.Request) { |
| if _, ok := s.billingDoor(w, r); !ok { |
| return |
| @@ -520,6 +541,19 @@ func (s *Server) handleAPIBillingPayments(w http.ResponseWriter, r *http.Request |
| } |
| f.From, f.To = start, start.AddDate(0, 1, 0) |
| } |
| + // The list declares only the payments, so it reads no facts. |
| + in, err := s.moneyInputsFor(time.Now(), nil, arrTotals{}) |
| + if err != nil { |
| + log.Printf("console: api billing payments: %v", err) |
| + jsonError(w, http.StatusInternalServerError, "internal error") |
| + return |
| + } |
| + reply := billingPaymentsReply{TestMode: !s.billing.cfg.LiveMode} |
| + if fig := paymentsFigure(in); !fig.Shown { |
| + reply.Why = fig.Why |
| + writeJSON(w, http.StatusOK, reply) |
| + return |
| + } |
| rows, _, err := s.store.Payments(f) |
| if err != nil { |
| log.Printf("console: api billing payments: %v", err) |
| @@ -546,5 +580,6 @@ func (s *Server) handleAPIBillingPayments(w http.ResponseWriter, r *http.Request |
| Kind: string(p.Kind), Reason: p.Reason, Detail: p.Detail, Amount: p.Amount, Tax: p.Tax, Currency: p.Currency, |
| Settled: p.Settled, Fee: p.Fee, SettledCurrency: p.SettledCurrency}) |
| } |
| - writeJSON(w, http.StatusOK, out) |
| + reply.Available, reply.Payments = true, out |
| + writeJSON(w, http.StatusOK, reply) |
| } |
diff --git a/internal/console/apibilling_test.go b/internal/console/apibilling_test.go| index 7882efdf9..411c0002b 100644 |
| --- a/internal/console/apibilling_test.go |
| +++ b/internal/console/apibilling_test.go |
| @@ -132,6 +132,18 @@ func TestCloseRefusedWhileUnpaid(t *testing.T) { |
| } |
| } |
| +// TestPaymentsFigureGateInvariant: the payments door's list is a figure |
| +// declaring the payment record, and only that. |
| +func TestPaymentsFigureGateInvariant(t *testing.T) { |
| + good := goodMoneyInputs(time.Date(2026, 11, 9, 12, 0, 0, 0, time.UTC)) |
| + checkGateInvariant(t, good, func(in moneyInputs) []moneyFigure { |
| + return []moneyFigure{paymentsFigure(in)} |
| + }) |
| + if f := paymentsFigure(good); f.Name != "payments" || f.Use.Inputs != inPayments { |
| + t.Fatalf("payments figure %+v; want it to declare inPayments only", f) |
| + } |
| +} |
| + |
| // TestRevenueFiguresGateInvariant: every figure of the revenue door goes |
| // through the money gate with the inputs it declares, over a usd and an |
| // eur paying account. |
| @@ -270,17 +282,37 @@ func TestAPIBillingPayments(t *testing.T) { |
| recordPayment(t, bf.st, p) |
| } |
| op := planOperatorToken(t, bf.consoleFixture, "ops@keymail.dev", "code-billing-payments") |
| + |
| + // Before the payment record has caught up, the door lists nothing and |
| + // says why, whatever is recorded; and says test mode. |
| + if status, body := getRaw(t, bf.ts.URL+"/api/cli/billing/payments", op); status != http.StatusOK || |
| + body != `{"available":false,"why":"payments","test_mode":true}`+"\n" { |
| + t.Fatalf("never reconciled: %d %q; want refused for payments, no list", status, body) |
| + } |
| + if err := bf.st.SetPaymentsReconciled(time.Now().Add(-3 * time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if status, body := getRaw(t, bf.ts.URL+"/api/cli/billing/payments?kind=fee", op); status != http.StatusOK || |
| + body != `{"available":false,"why":"payments","test_mode":true}`+"\n" { |
| + t.Fatalf("reconciled 3 h ago: %d %q; want refused for payments", status, body) |
| + } |
| + if err := bf.st.SetPaymentsReconciled(time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| list := func(query string) []billingPaymentReply { |
| t.Helper() |
| status, body := getRaw(t, bf.ts.URL+"/api/cli/billing/payments"+query, op) |
| if status != http.StatusOK { |
| t.Fatalf("%s: %d %s", query, status, body) |
| } |
| - var out []billingPaymentReply |
| + var out billingPaymentsReply |
| if err := json.Unmarshal([]byte(body), &out); err != nil { |
| t.Fatalf("%s: decode %q: %v", query, body, err) |
| } |
| - return out |
| + if !out.Available || out.Why != "" || !out.TestMode || out.Payments == nil { |
| + t.Fatalf("%s: %q; want available, test mode, a list", query, body) |
| + } |
| + return out.Payments |
| } |
| movements := func(rows []billingPaymentReply) []string { |
| var out []string |