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:
@@ -57,6 +57,12 @@ func fakeStripe(t *testing.T, events *string) *httptest.Server {
|
||||
t.Errorf("refunds without their charge: %s", r.URL)
|
||||
}
|
||||
fmt.Fprint(w, `{"data":[{"id":"re_1","object":"refund","amount":2000,"currency":"usd","created":1760200000,"status":"succeeded","charge":{"id":"ch_1","customer":"cus_a"}}],"has_more":false}`)
|
||||
case "/v1/charges":
|
||||
fmt.Fprint(w, `{"data":[
|
||||
{"id":"ch_1","object":"charge","customer":"cus_a","created":1760000000,"status":"succeeded","currency":"usd","amount":4500,"amount_captured":4500,"amount_refunded":2000},
|
||||
{"id":"ch_3","object":"charge","customer":"cus_c","created":1760000000,"status":"succeeded","currency":"usd","amount":1000,"amount_captured":1000,"amount_refunded":0},
|
||||
{"id":"ch_4","object":"charge","customer":"cus_d","created":1760000000,"status":"succeeded","currency":"usd","amount":800,"amount_captured":800,"amount_refunded":800},
|
||||
{"id":"ch_5","object":"charge","customer":"cus_e","created":1760000000,"status":"failed","currency":"usd","amount":900,"amount_captured":0,"amount_refunded":0}],"has_more":false}`)
|
||||
case "/v1/refunds/re_2":
|
||||
fmt.Fprint(w, `{"id":"re_2","object":"refund","amount":300,"currency":"usd","created":1762000000,"status":"succeeded","charge":{"id":"ch_2","customer":"cus_b"}}`)
|
||||
case "/v1/products":
|
||||
@@ -113,6 +119,19 @@ func TestSync(t *testing.T) {
|
||||
if sub != "sub_2" || cust != "cus_b" || !recurring {
|
||||
t.Errorf("new line: %q %q %v", sub, cust, recurring)
|
||||
}
|
||||
// Total spend comes from the charges: cus_c paid without an invoice;
|
||||
// cus_d was refunded in full; cus_e's payment failed; cus_b only has an
|
||||
// open invoice.
|
||||
sp, err := Spenders(d)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(sp) != 2 || !sp["cus_a"] || !sp["cus_c"] {
|
||||
t.Errorf("spenders: %v", sp)
|
||||
}
|
||||
if counts[KindCharge] != 4 {
|
||||
t.Errorf("charges: %v", counts)
|
||||
}
|
||||
var refundCustomer string
|
||||
d.QueryRow(`SELECT customer FROM stripe_objects WHERE kind = 'refund' AND id = 're_1'`).Scan(&refundCustomer)
|
||||
if refundCustomer != "cus_a" {
|
||||
@@ -163,3 +182,25 @@ func TestSync(t *testing.T) {
|
||||
t.Errorf("without products: %+v %v", res, err)
|
||||
}
|
||||
}
|
||||
|
||||
// A database synced before charges were read gets one full read, then
|
||||
// goes back to events.
|
||||
func TestSyncReadsChargesOnce(t *testing.T) {
|
||||
d := openDB(t)
|
||||
events := `{"data":[],"has_more":false}`
|
||||
srv := fakeStripe(t, &events)
|
||||
defer srv.Close()
|
||||
c := stripe.New("rk_test").WithBase(srv.URL)
|
||||
now := time.Unix(1762100000, 0)
|
||||
if err := d.PutJSON(stateKey, syncState{Backfilled: now.Add(-time.Hour), LastEvent: now.Add(-time.Hour).Unix()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res, err := Sync(context.Background(), d, c, now, false)
|
||||
if err != nil || !res.Full || res.Counts[KindCharge] != 4 {
|
||||
t.Fatalf("first sync: %+v %v", res, err)
|
||||
}
|
||||
res, err = Sync(context.Background(), d, c, now.Add(time.Hour), false)
|
||||
if err != nil || res.Full {
|
||||
t.Fatalf("second sync: %+v %v", res, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user