Phase 14, part 2: the owner's Billing page

Settings → Billing shows the team's plan, trial, people, agents, storage
and next invoice. The owner subscribes through Stripe Checkout (monthly, or
yearly when there's a price) and pays or reads invoices in Stripe's
Customer Portal; admins see the plan only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 13:19:30 -07:00
co-authored by Claude Opus 5.5
parent a47990f9bf
commit 8fb79220e3
8 changed files with 498 additions and 2 deletions
+115
View File
@@ -2,9 +2,11 @@ package web
import (
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"sync"
"testing"
"time"
@@ -138,3 +140,116 @@ func TestNoBillingNoLimits(t *testing.T) {
t.Errorf("an account without billing: %+v", a)
}
}
// fakeStripe is the platform's Stripe for tests: it records the calls.
type fakeStripe struct {
srv *httptest.Server
mu sync.Mutex
calls []string // "POST /checkout/sessions price=…&quantity=…"
qty string
}
func newFakeStripe(t *testing.T, s *Server) *fakeStripe {
t.Helper()
f := &fakeStripe{}
f.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
f.mu.Lock()
f.calls = append(f.calls, r.Method+" "+r.URL.Path+" "+r.Form.Encode())
f.mu.Unlock()
if u, _, ok := r.BasicAuth(); !ok || u != "sk_test_x" {
w.WriteHeader(http.StatusUnauthorized)
return
}
switch {
case r.URL.Path == "/customers":
w.Write([]byte(`{"id":"cus_123"}`))
case r.URL.Path == "/checkout/sessions":
w.Write([]byte(`{"url":"https://checkout.stripe.example/c/1"}`))
case r.URL.Path == "/billing_portal/sessions":
w.Write([]byte(`{"url":"https://billing.stripe.example/p/1"}`))
case r.URL.Path == "/invoices/upcoming":
w.Write([]byte(`{"amount_due":4500,"currency":"usd","next_payment_attempt":1800000000}`))
case strings.HasPrefix(r.URL.Path, "/subscriptions/"):
w.Write([]byte(`{"items":{"data":[{"id":"si_1"}]}}`))
case strings.HasPrefix(r.URL.Path, "/subscription_items/"):
f.mu.Lock()
f.qty = r.PostForm.Get("quantity")
f.mu.Unlock()
w.Write([]byte(`{}`))
default:
http.NotFound(w, r)
}
}))
t.Cleanup(f.srv.Close)
s.Config.StripeKey = "sk_test_x"
st := billing.NewStripe("sk_test_x")
st.Base = f.srv.URL
s.st().stripe = st
return f
}
func (f *fakeStripe) last(path string) string {
f.mu.Lock()
defer f.mu.Unlock()
for i := len(f.calls) - 1; i >= 0; i-- {
if strings.Contains(f.calls[i], " "+path+" ") {
return f.calls[i]
}
}
return ""
}
func TestBillingPage(t *testing.T) {
s, h := newBilled(t)
f := newFakeStripe(t, s)
c := signUp(t, h, "olu@acme.example", "Acme", "acme.example")
u, _ := users.ByEmail(s.platform(), "olu@acme.example")
list, _ := users.TeamsOf(s.platform(), u.ID)
team := list[0].ID
body := as(h, c, http.MethodGet, "/settings/billing", nil).Body.String()
if !strings.Contains(body, "Trial") || !strings.Contains(body, "$15") || !strings.Contains(body, "Subscribe with Stripe") || !strings.Contains(body, `value="year"`) {
t.Errorf("billing page: %s", body)
}
rec := as(h, c, http.MethodPost, "/settings/billing/checkout", url.Values{"interval": {"year"}})
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "https://checkout.stripe.example/c/1" {
t.Fatalf("checkout: %d %s", rec.Code, rec.Body.String())
}
call := f.last("/checkout/sessions")
for _, want := range []string{"line_items%5B0%5D%5Bprice%5D=price_year", "line_items%5B0%5D%5Bquantity%5D=1", "customer=cus_123", "client_reference_id=" + strconv.FormatInt(team, 10), "mode=subscription"} {
if !strings.Contains(call, want) {
t.Errorf("checkout lacks %s: %s", want, call)
}
}
if strings.Contains(call, "automatic_tax") {
t.Error("tax without asking for it")
}
if a, _ := billing.Get(s.platform(), team); a.Customer != "cus_123" {
t.Errorf("customer not kept: %+v", a)
}
// With a subscription: the next invoice, and the portal.
setAccount(t, s, team, func(a *billing.Account) { a.Status, a.Subscription = billing.Active, "sub_1" })
if body := as(h, c, http.MethodGet, "/settings/billing", nil).Body.String(); !strings.Contains(body, "$45") || strings.Contains(body, "Subscribe with Stripe") {
t.Errorf("active: %s", body)
}
if rec := as(h, c, http.MethodPost, "/settings/billing/portal", url.Values{}); rec.Header().Get("Location") != "https://billing.stripe.example/p/1" {
t.Errorf("portal: %d", rec.Code)
}
// An admin sees the plan, not the payments; a member, nothing.
ts, _ := s.forTeam(team)
admin := addUserIn(t, s, team, "ada@acme.example", users.Admin)
adc := sessionIn(t, s, admin, team)
body = as(h, adc, http.MethodGet, "/settings/billing", nil).Body.String()
if !strings.Contains(body, "Paid") || strings.Contains(body, "$45") || strings.Contains(body, "Open Stripe") {
t.Errorf("admin's view: %s", body)
}
if rec := as(h, adc, http.MethodPost, "/settings/billing/portal", url.Values{}); rec.Code != http.StatusForbidden {
t.Errorf("admin to the portal: %d", rec.Code)
}
mem := addUserIn(t, s, team, "sam@acme.example", users.Member)
if rec := as(h, sessionIn(t, s, mem, team), http.MethodGet, "/settings/billing", nil); rec.Code != http.StatusForbidden {
t.Errorf("member: %d", rec.Code)
}
_ = ts
}