/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>
153 lines
5.4 KiB
Go
153 lines
5.4 KiB
Go
package billing
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"supermarket/internal/db"
|
|
)
|
|
|
|
func TestPlans(t *testing.T) {
|
|
p, err := Load("", "price_m", "", "price_b")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
free, paid := p.Get("free"), p.Get("paid")
|
|
if !free.Includes("seo") || free.Includes("prospects") || !paid.Includes("prospects") || free.Paid() || !paid.Paid() {
|
|
t.Error("modules")
|
|
}
|
|
if free.Seats != 2 || free.Agents != 1 || paid.Agents != 5 || free.StorageBytes(0, 0) != 1<<30 || paid.StorageBytes(3, 1) != 40<<30 {
|
|
t.Errorf("limits %+v %+v", free, paid)
|
|
}
|
|
if free.CrawlInterval() != 7*24*time.Hour || paid.CrawlInterval() != 24*time.Hour || paid.PriceMonth != "price_m" {
|
|
t.Error("crawl or price")
|
|
}
|
|
if p.Get("nonsense") != free {
|
|
t.Error("unknown plan isn't free")
|
|
}
|
|
f := filepath.Join(t.TempDir(), "plans.yaml")
|
|
os.WriteFile(f, []byte("trial_days: 30\ntax: true\nplans:\n free: {name: Free, modules: [content], seats: 1}\n paid: {name: Team, modules: [all], price_month: price_x, price_year: price_y}\n"), 0o600)
|
|
p, err = Load(f, "price_m", "", "")
|
|
if err != nil || p.TrialDays != 30 || !p.Tax || p.Get("paid").PriceMonth != "price_x" || p.Get("paid").PriceYear != "price_y" {
|
|
t.Errorf("file: %+v %v", p, err)
|
|
}
|
|
os.WriteFile(f, []byte("plans:\n basic: {name: B, modules: [all]}\n"), 0o600)
|
|
if _, err := Load(f, "", "", ""); err == nil {
|
|
t.Error("no free plan accepted")
|
|
}
|
|
}
|
|
|
|
func TestAccounts(t *testing.T) {
|
|
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.Close()
|
|
p := Defaults()
|
|
now := time.Now()
|
|
if a, _ := Get(d, 9); a.Status != Free {
|
|
t.Errorf("no account: %+v", a)
|
|
}
|
|
StartTrial(d, 1, p, now)
|
|
a, _ := Get(d, 1)
|
|
if pl, ro := a.Effective(p, now); pl.ID != "paid" || ro {
|
|
t.Errorf("trial: %s %v", pl.ID, ro)
|
|
}
|
|
if pl, _ := a.Effective(p, now.Add(15*24*time.Hour)); pl.ID != "free" {
|
|
t.Errorf("after the trial: %s", pl.ID)
|
|
}
|
|
a.Status, a.PastDueSince = PastDue, now
|
|
Save(d, a, now)
|
|
if pl, ro := a.Effective(p, now.Add(13*24*time.Hour)); pl.ID != "paid" || ro {
|
|
t.Error("past due, under 14 days")
|
|
}
|
|
if _, ro := a.Effective(p, now.Add(15*24*time.Hour)); !ro {
|
|
t.Error("past due 15 days: not read-only")
|
|
}
|
|
a.Status, a.Customer = Canceled, "cus_1"
|
|
Save(d, a, now)
|
|
if b, err := ByCustomer(d, "cus_1"); err != nil || b.Team != 1 {
|
|
t.Errorf("by customer: %v", err)
|
|
}
|
|
if pl, _ := a.Effective(p, now); pl.ID != "free" {
|
|
t.Error("canceled isn't free")
|
|
}
|
|
if n, _ := SeenEvent(d, "evt_1", "x", now); !n {
|
|
t.Error("first event")
|
|
}
|
|
if n, _ := SeenEvent(d, "evt_1", "x", now); n {
|
|
t.Error("event twice")
|
|
}
|
|
}
|
|
|
|
func TestWebhookSignature(t *testing.T) {
|
|
body := []byte(`{"id":"evt_1"}`)
|
|
now := time.Now()
|
|
h := Sign(body, "whsec_x", now)
|
|
if err := VerifySignature(h, body, "whsec_x", now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if VerifySignature(h, body, "whsec_y", now) == nil || VerifySignature(h, []byte(`{"id":"evt_2"}`), "whsec_x", now) == nil {
|
|
t.Error("a wrong secret or body passed")
|
|
}
|
|
if VerifySignature(h, body, "whsec_x", now.Add(10*time.Minute)) == nil {
|
|
t.Error("an old event passed")
|
|
}
|
|
if VerifySignature("t=1,v0=abc", body, "whsec_x", now) == nil || VerifySignature("", body, "whsec_x", now) == nil {
|
|
t.Error("a malformed header passed")
|
|
}
|
|
// Stripe may send two signatures while a secret is rolled.
|
|
if err := VerifySignature(h+",v1=00ff", body, "whsec_x", now); err != nil {
|
|
t.Error("two signatures")
|
|
}
|
|
}
|
|
|
|
func TestApplyEvents(t *testing.T) {
|
|
p := Defaults()
|
|
now := time.Now()
|
|
acct := &Account{Team: 7, Status: Trialing, TrialEnds: now.Add(time.Hour)}
|
|
load := func(team int64, customer string) (*Account, error) {
|
|
if team == 7 || (customer != "" && customer == acct.Customer) {
|
|
return acct, nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
ev := func(typ, obj string) *Event {
|
|
e := &Event{ID: "evt", Type: typ}
|
|
e.Data.Object = []byte(obj)
|
|
return e
|
|
}
|
|
a, c, err := Apply(load, p, ev("checkout.session.completed", `{"client_reference_id":"7","customer":"cus_1","subscription":"sub_1"}`), now)
|
|
if err != nil || a.Customer != "cus_1" || a.Subscription != "sub_1" || c.Team != 7 {
|
|
t.Fatalf("checkout: %+v %v", a, err)
|
|
}
|
|
a, c, _ = Apply(load, p, ev("customer.subscription.updated", `{"id":"sub_1","customer":"cus_1","status":"active","metadata":{"team":"7"},"items":{"data":[{"quantity":4,"current_period_end":1900000000}]}}`), now)
|
|
if a.Status != Active || a.Seats != 4 || a.PeriodEnd.Unix() != 1900000000 || c.Before != Trialing {
|
|
t.Errorf("updated: %+v", a)
|
|
}
|
|
a, _, _ = Apply(load, p, ev("invoice.payment_failed", `{"customer":"cus_1"}`), now)
|
|
if a.Status != PastDue || !a.PastDueSince.Equal(now) {
|
|
t.Errorf("failed: %+v", a)
|
|
}
|
|
a, _, _ = Apply(load, p, ev("invoice.payment_failed", `{"customer":"cus_1"}`), now.Add(time.Hour))
|
|
if !a.PastDueSince.Equal(now) {
|
|
t.Error("past due since moved")
|
|
}
|
|
a, _, _ = Apply(load, p, ev("invoice.paid", `{"customer":"cus_1"}`), now)
|
|
if a.Status != Active || !a.PastDueSince.IsZero() {
|
|
t.Errorf("paid: %+v", a)
|
|
}
|
|
a, _, _ = Apply(load, p, ev("customer.subscription.deleted", `{"id":"sub_1","customer":"cus_1","status":"canceled","metadata":{"team":"7"}}`), now)
|
|
if a.Status != Canceled || a.Subscription != "" {
|
|
t.Errorf("deleted: %+v", a)
|
|
}
|
|
if a, c, err := Apply(load, p, ev("invoice.paid", `{"customer":"cus_other"}`), now); a != nil || c != nil || err != nil {
|
|
t.Error("another product's customer")
|
|
}
|
|
if a, _, _ := Apply(load, p, ev("charge.refunded", `{}`), now); a != nil {
|
|
t.Error("an event billing doesn't follow")
|
|
}
|
|
}
|