Metrics: total spend from Stripe's charges, as Stripe counts it

Customers who paid without an invoice (one-off Checkout payments) were
missed, and the Stripe card counted every customer record, deleted ones
too. The sync now reads charges (and follows their events); a customer's
total spend is their successful charges, captured less refunded. A
database synced before reads everything once (sync state version 2).
Until charges are in, paid invoices less refunds stand in.

The Stripe card's Customers now counts those who spent more than $0, "of
N in Stripe", and a Payments count is added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 12:35:00 -07:00
co-authored by Claude Opus 5.5
parent 6f2fbdf63a
commit 739f85fde1
7 changed files with 103 additions and 23 deletions
+28 -15
View File
@@ -169,7 +169,7 @@ func Matches(d *db.DB, model *Model) ([]Match, error) {
if err != nil {
return nil, err
}
paying, err := spenders(d)
paying, err := Spenders(d)
if err != nil {
return nil, err
}
@@ -307,17 +307,27 @@ func MatchSummary(ms []Match) map[string]int {
return out
}
// spenders are the customers whose total spend is above $0: what their
// paid invoices charged (amount_paid, so $0 and fully discounted invoices
// count as nothing), less what was refunded. Customers who only had a
// trial, or have only open invoices, aren't customers yet.
func spenders(d *db.DB) (map[string]bool, error) {
// Spenders are the customers whose total spend is above $0, as Stripe's
// dashboard counts it: their successful payments (charges), less what was
// refunded. Customers who only had a trial, or have only unpaid invoices,
// aren't customers yet. Until the charges are synced, paid invoices
// (amount_paid) less refunds stand in.
func Spenders(d *db.DB) (map[string]bool, error) {
var charges int
if err := d.QueryRow(`SELECT count(*) FROM stripe_objects WHERE kind = ?`, KindCharge).Scan(&charges); err != nil {
return nil, err
}
type row struct {
kind, customer, data string
amount int64
}
rows, err := d.Query(`SELECT kind, customer, amount, data FROM stripe_objects
WHERE customer != '' AND ((kind = 'invoice' AND status = 'paid') OR (kind = 'refund' AND status IN ('succeeded', 'pending')))`)
q := `SELECT kind, customer, amount, '' FROM stripe_objects
WHERE kind = 'charge' AND status = 'succeeded' AND customer != ''`
if charges == 0 {
q = `SELECT kind, customer, amount, data FROM stripe_objects
WHERE customer != '' AND ((kind = 'invoice' AND status = 'paid') OR (kind = 'refund' AND status IN ('succeeded', 'pending')))`
}
rows, err := d.Query(q)
if err != nil {
return nil, err
}
@@ -336,15 +346,18 @@ func spenders(d *db.DB) (map[string]bool, error) {
}
spend := map[string]int64{}
for _, r := range all {
if r.kind == KindRefund {
switch r.kind {
case KindCharge:
spend[r.customer] += r.amount // captured, less refunded
case KindRefund:
spend[r.customer] -= r.amount
continue
case KindInvoice:
var inv struct {
AmountPaid int64 `json:"amount_paid"`
}
_ = json.Unmarshal([]byte(r.data), &inv)
spend[r.customer] += inv.AmountPaid
}
var inv struct {
AmountPaid int64 `json:"amount_paid"`
}
_ = json.Unmarshal([]byte(r.data), &inv)
spend[r.customer] += inv.AmountPaid
}
out := map[string]bool{}
for c, v := range spend {