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:
@@ -21,6 +21,7 @@ const (
|
||||
KindInvoice = "invoice"
|
||||
KindCreditNote = "credit_note"
|
||||
KindRefund = "refund"
|
||||
KindCharge = "charge" // a payment: what total spend is counted from
|
||||
KindProduct = "product"
|
||||
)
|
||||
|
||||
@@ -31,8 +32,13 @@ const eventsKept = 29 * 24 * time.Hour
|
||||
type syncState struct {
|
||||
Backfilled time.Time `json:"backfilled"`
|
||||
LastEvent int64 `json:"last_event"` // created time of the last event applied
|
||||
Version int `json:"version"` // syncVersion when last backfilled
|
||||
}
|
||||
|
||||
// syncVersion goes up when a backfill reads a new kind of object (2:
|
||||
// charges), so the next sync reads everything once.
|
||||
const syncVersion = 2
|
||||
|
||||
const stateKey = "metrics:stripe-state"
|
||||
|
||||
// SyncResult is what a sync did.
|
||||
@@ -78,7 +84,7 @@ func sync(ctx context.Context, d *db.DB, c *stripe.Client, now time.Time, forceF
|
||||
if _, err := d.GetJSON(stateKey, &st); err != nil {
|
||||
return err
|
||||
}
|
||||
if forceFull || st.LastEvent == 0 || now.Sub(time.Unix(st.LastEvent, 0)) > eventsKept {
|
||||
if forceFull || st.LastEvent == 0 || st.Version < syncVersion || now.Sub(time.Unix(st.LastEvent, 0)) > eventsKept {
|
||||
res.Full = true
|
||||
// Events from the moment the backfill starts are applied next time,
|
||||
// so nothing falls between the two.
|
||||
@@ -91,6 +97,7 @@ func sync(ctx context.Context, d *db.DB, c *stripe.Client, now time.Time, forceF
|
||||
{KindInvoice, "/v1/invoices", nil},
|
||||
{KindCreditNote, "/v1/credit_notes", nil},
|
||||
{KindRefund, "/v1/refunds", url.Values{"expand[]": {"data.charge"}}},
|
||||
{KindCharge, "/v1/charges", nil},
|
||||
{KindProduct, "/v1/products", nil},
|
||||
} {
|
||||
raws, err := c.ListRaw(ctx, l.path, l.q, 0)
|
||||
@@ -110,7 +117,7 @@ func sync(ctx context.Context, d *db.DB, c *stripe.Client, now time.Time, forceF
|
||||
res.Counts[l.kind]++
|
||||
}
|
||||
}
|
||||
st = syncState{Backfilled: now, LastEvent: start.Unix()}
|
||||
st = syncState{Backfilled: now, LastEvent: start.Unix(), Version: syncVersion}
|
||||
return d.PutJSON(stateKey, st)
|
||||
}
|
||||
// Products change rarely and have no events followed: read them all.
|
||||
@@ -157,7 +164,8 @@ func sync(ctx context.Context, d *db.DB, c *stripe.Client, now time.Time, forceF
|
||||
obj = full
|
||||
}
|
||||
case "charge":
|
||||
// charge.refunded: its refunds are what matter.
|
||||
kind = KindCharge
|
||||
// charge.refunded: its refunds matter too.
|
||||
for _, r := range head.Refunds.Data {
|
||||
var rh struct {
|
||||
ID string `json:"id"`
|
||||
@@ -219,6 +227,15 @@ func store(ctx context.Context, d *db.DB, c *stripe.Client, kind string, raw jso
|
||||
}
|
||||
_ = json.Unmarshal(o.Charge, &ch)
|
||||
customer = stripe.IDOf(ch.Customer)
|
||||
case KindCharge:
|
||||
// What the customer was charged and kept paying: captured, less
|
||||
// refunded.
|
||||
var ch struct {
|
||||
AmountCaptured int64 `json:"amount_captured"`
|
||||
AmountRefunded int64 `json:"amount_refunded"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &ch)
|
||||
amount = ch.AmountCaptured - ch.AmountRefunded
|
||||
case KindInvoice:
|
||||
if o.Status == "draft" {
|
||||
return nil // not billed yet
|
||||
@@ -290,9 +307,10 @@ func currencies(d *db.DB) ([]string, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Counts are how many objects of each kind are stored.
|
||||
// Counts are how many objects of each kind are stored (deleted customers
|
||||
// left out).
|
||||
func Counts(d *db.DB) (map[string]int, error) {
|
||||
rows, err := d.Query(`SELECT kind, count(*) FROM stripe_objects GROUP BY kind`)
|
||||
rows, err := d.Query(`SELECT kind, count(*) FROM stripe_objects WHERE status != 'deleted' GROUP BY kind`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user