Phase 14, part 3: Stripe's events
/billing/stripe takes Stripe's webhooks, signatures checked, each event handled once, and keeps each team's subscription up to date: checkout, its status, seats and period, failed and paid invoices, cancellation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/audit"
|
||||
"supermarket/internal/billing"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
@@ -253,3 +255,54 @@ func TestBillingPage(t *testing.T) {
|
||||
}
|
||||
_ = ts
|
||||
}
|
||||
|
||||
// postEvent sends a signed Stripe event.
|
||||
func postEvent(t *testing.T, s *Server, h http.Handler, id, typ, obj string, sign bool) int {
|
||||
t.Helper()
|
||||
body := []byte(`{"id":"` + id + `","type":"` + typ + `","data":{"object":` + obj + `}}`)
|
||||
sig := "t=1,v1=00"
|
||||
if sign {
|
||||
sig = billing.Sign(body, s.Config.StripeWebhookSecret, time.Now())
|
||||
}
|
||||
rec := do(h, http.MethodPost, "/billing/stripe", bytes.NewReader(body), map[string]string{"Stripe-Signature": sig, "Content-Type": "application/json", csrfHeader: ""})
|
||||
return rec.Code
|
||||
}
|
||||
|
||||
func TestStripeWebhooks(t *testing.T) {
|
||||
s, h := newBilled(t)
|
||||
s.Config.StripeWebhookSecret = "whsec_test"
|
||||
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 := strconv.FormatInt(list[0].ID, 10)
|
||||
|
||||
if code := postEvent(t, s, h, "evt_0", "checkout.session.completed", `{"client_reference_id":"`+team+`","customer":"cus_9","subscription":"sub_9"}`, false); code != http.StatusBadRequest {
|
||||
t.Errorf("unsigned: %d", code)
|
||||
}
|
||||
if code := postEvent(t, s, h, "evt_1", "checkout.session.completed", `{"client_reference_id":"`+team+`","customer":"cus_9","subscription":"sub_9"}`, true); code != http.StatusOK {
|
||||
t.Fatalf("checkout: %d", code)
|
||||
}
|
||||
active := `{"id":"sub_9","customer":"cus_9","status":"active","metadata":{"team":"` + team + `"},"items":{"data":[{"quantity":3,"current_period_end":1900000000}]}}`
|
||||
postEvent(t, s, h, "evt_2", "customer.subscription.updated", active, true)
|
||||
a, _ := billing.Get(s.platform(), list[0].ID)
|
||||
if a.Status != billing.Active || a.Customer != "cus_9" || a.Seats != 3 {
|
||||
t.Fatalf("active: %+v", a)
|
||||
}
|
||||
postEvent(t, s, h, "evt_3", "invoice.payment_failed", `{"customer":"cus_9","subscription":"sub_9"}`, true)
|
||||
// evt_2 again (Stripe retries): handled once, so it doesn't undo evt_3.
|
||||
if code := postEvent(t, s, h, "evt_2", "customer.subscription.updated", active, true); code != http.StatusOK {
|
||||
t.Errorf("replay: %d", code)
|
||||
}
|
||||
if a, _ = billing.Get(s.platform(), list[0].ID); a.Status != billing.PastDue || a.PastDueSince.IsZero() {
|
||||
t.Errorf("past due: %+v", a)
|
||||
}
|
||||
// Someone else's customer on the same Stripe account: ignored.
|
||||
if code := postEvent(t, s, h, "evt_4", "invoice.paid", `{"customer":"cus_other"}`, true); code != http.StatusOK {
|
||||
t.Errorf("another customer: %d", code)
|
||||
}
|
||||
ts, _ := s.forTeam(list[0].ID)
|
||||
entries, _ := audit.List(ts.DB, audit.Filter{Action: "billing"}, 10)
|
||||
if len(entries) != 3 || !strings.Contains(entries[0].Target, "active → past_due") {
|
||||
t.Errorf("log: %+v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user