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:
@@ -2,10 +2,13 @@ package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/audit"
|
||||
"supermarket/internal/billing"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
@@ -172,3 +175,73 @@ func (s *Server) billingPortal(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
http.Redirect(w, r, addr, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// stripeWebhook takes Stripe's events (POST /billing/stripe), with their
|
||||
// signatures checked; each is handled once.
|
||||
func (s *Server) stripeWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.billingOn() || s.Config == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 1<<20))
|
||||
if err != nil {
|
||||
http.Error(w, "too large", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
if err := billing.VerifySignature(r.Header.Get("Stripe-Signature"), body, s.Config.StripeWebhookSecret, now); err != nil {
|
||||
s.Log.Warn("billing: a webhook refused", "err", err, "ip", clientIP(r))
|
||||
http.Error(w, "bad signature", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var e billing.Event
|
||||
if err := json.Unmarshal(body, &e); err != nil || e.ID == "" {
|
||||
http.Error(w, "unreadable event", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var seen int
|
||||
_ = s.platform().QueryRow(`SELECT count(*) FROM stripe_events WHERE id = ?`, e.ID).Scan(&seen)
|
||||
if seen > 0 {
|
||||
w.WriteHeader(http.StatusOK) // already handled
|
||||
return
|
||||
}
|
||||
load := func(team int64, customer string) (*billing.Account, error) {
|
||||
if customer != "" {
|
||||
if a, err := billing.ByCustomer(s.platform(), customer); err == nil {
|
||||
return a, nil
|
||||
}
|
||||
}
|
||||
if team == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if _, err := users.GetTeam(s.platform(), team); err != nil {
|
||||
return nil, nil // not one of ours
|
||||
}
|
||||
return billing.Get(s.platform(), team)
|
||||
}
|
||||
a, change, err := billing.Apply(load, s.Plans, &e, now)
|
||||
if err != nil {
|
||||
s.Log.Error("billing: handling an event", "event", e.ID, "type", e.Type, "err", err)
|
||||
http.Error(w, "couldn't handle it", http.StatusInternalServerError) // Stripe tries again
|
||||
return
|
||||
}
|
||||
if a != nil {
|
||||
if err := billing.Save(s.platform(), a, now); err != nil {
|
||||
s.Log.Error("billing: saving", "event", e.ID, "err", err)
|
||||
http.Error(w, "couldn't save it", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if ts, err := s.forTeam(a.Team); err == nil {
|
||||
detail := e.Type
|
||||
if change.Note != "" {
|
||||
detail = change.Note
|
||||
}
|
||||
ts.record(audit.Entry{Kind: audit.System, Action: "billing", Target: change.Before + " → " + change.After, Detail: detail})
|
||||
}
|
||||
s.Log.Info("billing: event", "type", e.Type, "team", a.Team, "status", a.Status)
|
||||
}
|
||||
if _, err := billing.SeenEvent(s.platform(), e.ID, e.Type, now); err != nil {
|
||||
s.Log.Warn("billing: noting an event", "err", err)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user