console: backfill payments from a fixed 2010 floor, not account creation
Final review finding D (Astra 4, Ruling 25): Stripe documents /v1/account created as when the account was connected, not when it began trading, so a backfill starting there could miss earlier payments, and an absent value stopped the first reconcile. The backfill now starts at billingMoneyEpoch (2010-01-01 UTC, before Stripe existed): about 200 31-day windows, once. AccountCreated leaves billing.Provider, the Stripe adapter (merchant.go is back to its base shape), the fake (AccountOpened) and every test; TestReconcileBackfillsFromAccountCreation becomes TestReconcileBackfillsFromEpoch.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed,
+35
−115
internal/billing/billing.go+0 −2internal/billing/billingtest/fake.go+7 −9internal/billing/billingtest/fake_money.go+0 −19internal/billing/billingtest/fake_money_test.go+2 −12internal/billingstripe/merchant.go+4 −17internal/billingstripe/money.go+0 −13internal/billingstripe/money_test.go+0 −25internal/console/billingmoneypass.go+9 −4internal/console/billingmoneypass_test.go+13 −8internal/console/billingrevenue_test.go+0 −6
diff --git a/internal/billing/billing.go b/internal/billing/billing.go| index 1512c5db0..7ec9da49a 100644 |
| --- a/internal/billing/billing.go |
| +++ b/internal/billing/billing.go |
| @@ -377,8 +377,6 @@ type Provider interface { |
| // balance movement they made. ErrNotFound when the provider has no |
| // such charge; Captured false, and nothing else, when no money moved. |
| ChargePayments(ctx context.Context, charge string) (ChargeRecord, error) |
| - // AccountCreated is when the provider account was opened. |
| - AccountCreated(ctx context.Context) (time.Time, error) |
| } |
| // FindItem is the subscription's item whose price is for kind and name |
diff --git a/internal/billing/billingtest/fake.go b/internal/billing/billingtest/fake.go| index 88cd03c0e..f4ac277b8 100644 |
| --- a/internal/billing/billingtest/fake.go |
| +++ b/internal/billing/billingtest/fake.go |
| @@ -143,14 +143,12 @@ type Fake struct { |
| Invoices map[string]*Invoice |
| paymentSessions map[string]*paymentSession |
| - // Movements is the provider's balance movements, Charges its charges |
| - // with their refunds by charge id, and AccountOpened when the |
| - // provider account was created (see fake_money.go). |
| - Movements []billing.Movement |
| - Charges map[string]billing.ChargeRecord |
| - AccountOpened time.Time |
| - // MoneyErr fails only the three money reads (MoneyMovements, |
| - // ChargePayments, AccountCreated): a key without their permissions. |
| + // Movements is the provider's balance movements and Charges its |
| + // charges with their refunds by charge id (see fake_money.go). |
| + Movements []billing.Movement |
| + Charges map[string]billing.ChargeRecord |
| + // MoneyErr fails only the two money reads (MoneyMovements and |
| + // ChargePayments): a key without their permissions. |
| MoneyErr error |
| // ChargeErr fails ChargePayments for one charge only. |
| ChargeErr map[string]error |
| @@ -160,7 +158,7 @@ type Fake struct { |
| // "ClockProbe", and one per list page under |
| // "LiveSubscriptions", "CustomerCheckouts", "RenewalInvoice" (invoice |
| // pages), "RenewalInvoiceLines" (line pages) and "MoneyMovements". |
| - // "ChargePayments" and "AccountCreated" count one each. A request that fails |
| + // "ChargePayments" counts one each. A request that fails |
| // still counts; a CrashBeforeSend does not (nothing was sent). |
| CallsBy map[string]int |
| // Sent is every Prepared Send received, in order (not those stopped by |
diff --git a/internal/billing/billingtest/fake_money.go b/internal/billing/billingtest/fake_money.go| index ac97700e7..98fe0ba50 100644 |
| --- a/internal/billing/billingtest/fake_money.go |
| +++ b/internal/billing/billingtest/fake_money.go |
| @@ -76,22 +76,3 @@ func (f *Fake) ChargePayments(ctx context.Context, charge string) (billing.Charg |
| rec.Refunds = slices.Clone(rec.Refunds) |
| return rec, nil |
| } |
| - |
| -// AccountCreated returns AccountOpened; Err or MoneyErr fail it. |
| -func (f *Fake) AccountCreated(ctx context.Context) (time.Time, error) { |
| - f.mu.Lock() |
| - defer f.mu.Unlock() |
| - f.begin("AccountCreated") |
| - defer f.end() |
| - if f.Err != nil { |
| - return time.Time{}, f.Err |
| - } |
| - if f.MoneyErr != nil { |
| - return time.Time{}, f.MoneyErr |
| - } |
| - if f.timedOut() { |
| - return time.Time{}, fmt.Errorf("%w: timed out before headers", billing.ErrNoResponse) |
| - } |
| - f.respond(ctx) |
| - return f.AccountOpened, nil |
| -} |
diff --git a/internal/billing/billingtest/fake_money_test.go b/internal/billing/billingtest/fake_money_test.go| index ea493b879..9945becf5 100644 |
| --- a/internal/billing/billingtest/fake_money_test.go |
| +++ b/internal/billing/billingtest/fake_money_test.go |
| @@ -59,13 +59,12 @@ func TestFakeMoneyMovementsWindowAndPages(t *testing.T) { |
| } |
| } |
| -func TestFakeChargePaymentsAndAccountCreated(t *testing.T) { |
| +func TestFakeChargePayments(t *testing.T) { |
| ctx := context.Background() |
| f := newFake(t) |
| rec := billing.ChargeRecord{Captured: true, Charge: billing.Payment{Movement: "mv_1", Source: "ch_1", Kind: billing.PaymentSubscription, |
| Reason: billing.ReasonFirst, Customer: "cus_1", Charge: "ch_1", Amount: 2800, Tax: 400, Currency: "eur"}} |
| f.Charges = map[string]billing.ChargeRecord{"ch_1": rec} |
| - f.AccountOpened = t0.AddDate(-1, 0, 0) |
| got, err := f.ChargePayments(ctx, "ch_1") |
| if err != nil || !got.Captured || got.Charge.Movement != "mv_1" { |
| @@ -74,9 +73,6 @@ func TestFakeChargePaymentsAndAccountCreated(t *testing.T) { |
| if _, err := f.ChargePayments(ctx, "ch_none"); !errors.Is(err, billing.ErrNotFound) { |
| t.Fatalf("ChargePayments(ch_none) err = %v, want ErrNotFound", err) |
| } |
| - if at, err := f.AccountCreated(ctx); err != nil || !at.Equal(f.AccountOpened) { |
| - t.Fatalf("AccountCreated = %v, %v", at, err) |
| - } |
| f.Err = ErrInjected |
| if _, err := f.MoneyMovements(ctx, t0, t0.Add(time.Hour)); !errors.Is(err, ErrInjected) { |
| @@ -85,9 +81,6 @@ func TestFakeChargePaymentsAndAccountCreated(t *testing.T) { |
| if _, err := f.ChargePayments(ctx, "ch_1"); !errors.Is(err, ErrInjected) { |
| t.Fatalf("ChargePayments under Err: %v", err) |
| } |
| - if _, err := f.AccountCreated(ctx); !errors.Is(err, ErrInjected) { |
| - t.Fatalf("AccountCreated under Err: %v", err) |
| - } |
| } |
| func TestFakeParsesPaymentEvent(t *testing.T) { |
| @@ -123,7 +116,7 @@ func TestFakeMoneyErrAndChargeErr(t *testing.T) { |
| } |
| f.ChargeErr = nil |
| - // MoneyErr fails the three money reads and nothing else. |
| + // MoneyErr fails the two money reads and nothing else. |
| f.MoneyErr = ErrInjected |
| if _, err := f.MoneyMovements(ctx, t0, t0.Add(24*time.Hour)); !errors.Is(err, ErrInjected) { |
| t.Fatalf("MoneyMovements under MoneyErr: %v", err) |
| @@ -134,9 +127,6 @@ func TestFakeMoneyErrAndChargeErr(t *testing.T) { |
| if _, err := f.ChargePayments(ctx, "ch_1"); !errors.Is(err, ErrInjected) { |
| t.Fatalf("ChargePayments under MoneyErr: %v", err) |
| } |
| - if _, err := f.AccountCreated(ctx); !errors.Is(err, ErrInjected) { |
| - t.Fatalf("AccountCreated under MoneyErr: %v", err) |
| - } |
| f.Subs["sub_1"] = &billing.Subscription{ID: "sub_1", Customer: "cus_1"} |
| if _, err := f.Subscription(ctx, "sub_1"); err != nil { |
| t.Fatalf("Subscription under MoneyErr: %v", err) |
diff --git a/internal/billingstripe/merchant.go b/internal/billingstripe/merchant.go| index 098434350..18f0e3de6 100644 |
| --- a/internal/billingstripe/merchant.go |
| +++ b/internal/billingstripe/merchant.go |
| @@ -6,34 +6,21 @@ import ( |
| "errors" |
| "net/http" |
| "strings" |
| - "time" |
| ) |
| // MerchantAccount reads the account belonging to the credential, without a |
| // Connect account selector. The caller pins its expected merchant ID. |
| func (c *Client) MerchantAccount(ctx context.Context) (string, error) { |
| - id, _, err := c.merchantAccount(ctx) |
| - return id, err |
| -} |
| - |
| -// merchantAccount is MerchantAccount with the account's created time, zero |
| -// when the answer carried none. |
| -func (c *Client) merchantAccount(ctx context.Context) (string, time.Time, error) { |
| var account struct { |
| - ID string `json:"id"` |
| - Created int64 `json:"created"` |
| + ID string `json:"id"` |
| } |
| if err := c.do(ctx, http.MethodGet, "/v1/account", nil, "", &account); err != nil { |
| - return "", time.Time{}, err |
| + return "", err |
| } |
| if !validID(account.ID) || !strings.HasPrefix(account.ID, "acct_") { |
| - return "", time.Time{}, &Error{Code: CodeDecodeError} |
| - } |
| - var created time.Time |
| - if account.Created > 0 { |
| - created = time.Unix(account.Created, 0).UTC() |
| + return "", &Error{Code: CodeDecodeError} |
| } |
| - return account.ID, created, nil |
| + return account.ID, nil |
| } |
| type Product struct { |
diff --git a/internal/billingstripe/money.go b/internal/billingstripe/money.go| index 6302ec4a7..bbea89fce 100644 |
| --- a/internal/billingstripe/money.go |
| +++ b/internal/billingstripe/money.go |
| @@ -396,16 +396,3 @@ func (p *Provider) ChargePayments(ctx context.Context, charge string) (billing.C |
| } |
| return rec, nil |
| } |
| - |
| -// AccountCreated is when the provider account was opened: no money moved |
| -// before it. |
| -func (p *Provider) AccountCreated(ctx context.Context) (time.Time, error) { |
| - id, created, err := p.client.merchantAccount(ctx) |
| - if err != nil { |
| - return time.Time{}, err |
| - } |
| - if id != p.account || created.IsZero() { |
| - return time.Time{}, &Error{Code: CodeDecodeError} |
| - } |
| - return created, nil |
| -} |
diff --git a/internal/billingstripe/money_test.go b/internal/billingstripe/money_test.go| index 54fa10785..799959000 100644 |
| --- a/internal/billingstripe/money_test.go |
| +++ b/internal/billingstripe/money_test.go |
| @@ -516,28 +516,3 @@ func TestChargePaymentsRefundPages(t *testing.T) { |
| } |
| } |
| } |
| - |
| -func TestAccountCreated(t *testing.T) { |
| - created := time.Date(2019, 3, 4, 5, 6, 7, 0, time.UTC) |
| - for body, ok := range map[string]bool{ |
| - fmt.Sprintf(`{"id":"acct_1","object":"account","created":%d}`, created.Unix()): true, |
| - `{"id":"acct_1","object":"account"}`: false, |
| - fmt.Sprintf(`{"id":"acct_2","created":%d}`, created.Unix()): false, |
| - `null`: false, |
| - } { |
| - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| - if r.Method != http.MethodGet || r.URL.Path != "/v1/account" { |
| - t.Errorf("%s %s", r.Method, r.URL.Path) |
| - } |
| - fmt.Fprint(w, body) |
| - })) |
| - at, err := prepProvider(t, srv.URL, nil).AccountCreated(context.Background()) |
| - srv.Close() |
| - if ok && (err != nil || !at.Equal(created)) { |
| - t.Errorf("%s: %v %v", body, at, err) |
| - } |
| - if !ok && err == nil { |
| - t.Errorf("%s: accepted (%v)", body, at) |
| - } |
| - } |
| -} |
diff --git a/internal/console/billingmoneypass.go b/internal/console/billingmoneypass.go| index beedc4924..ce5332fc2 100644 |
| --- a/internal/console/billingmoneypass.go |
| +++ b/internal/console/billingmoneypass.go |
| @@ -22,6 +22,13 @@ const ( |
| billingMoneyOverlap = time.Hour |
| ) |
| +// billingMoneyEpoch is where the backfill starts: before any provider |
| +// account could have moved money (Ruling 25). The provider's account |
| +// metadata says when it was connected, not when it began trading, so it |
| +// is no lower bound; a fixed floor is, at the cost of one long, empty, |
| +// one-time read (about 200 windows). |
| +var billingMoneyEpoch = time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC) |
| + |
| // billingMoneyPass is the money half of the billing worker's round, after |
| // the subscription sync: the payment reconcile, the euro rates, then |
| // today's ARR snapshot from both. With billing off or not ready it does |
| @@ -49,7 +56,7 @@ func (s *Server) billingMoneyPass(ctx context.Context, now time.Time) { |
| // reconcilePayments records every balance movement from the cursor (less |
| // billingMoneyOverlap) to now, one window at a time; with no cursor it is |
| -// the backfill, from when the provider account was opened. A window whose |
| +// the backfill, from billingMoneyEpoch. A window whose |
| // list is too long is halved and read again from the same start, down to |
| // billingMoneyMinWindow. Each charge, refund or refund failure not yet |
| // recorded has its charge read once (recordCharge records every movement |
| @@ -67,9 +74,7 @@ func (s *Server) reconcilePayments(ctx context.Context, now time.Time) error { |
| } |
| from := through.Add(-billingMoneyOverlap) |
| if !ok { |
| - if from, err = cfg.Provider.AccountCreated(ctx); err != nil { |
| - return fmt.Errorf("reading when the account opened: %w", err) |
| - } |
| + from = billingMoneyEpoch |
| } |
| window := billingMoneyWindow |
| loggedTypes := map[string]bool{} |
diff --git a/internal/console/billingmoneypass_test.go b/internal/console/billingmoneypass_test.go| index 932045ad7..2b34df592 100644 |
| --- a/internal/console/billingmoneypass_test.go |
| +++ b/internal/console/billingmoneypass_test.go |
| @@ -120,13 +120,15 @@ func wantReconciled(t *testing.T, s *Store, want time.Time) { |
| } |
| } |
| -func TestReconcileBackfillsFromAccountCreation(t *testing.T) { |
| +// With no cursor the backfill starts at billingMoneyEpoch, before any |
| +// provider account could have moved money, and reads to now in contiguous |
| +// windows of billingMoneyWindow (the last cut short by now). |
| +func TestReconcileBackfillsFromEpoch(t *testing.T) { |
| bf := newBillingFixture(t) |
| now := billingNow |
| - opened := now.Add(-90 * 24 * time.Hour) |
| - bf.fake.AccountOpened = opened |
| - for i, ago := range []time.Duration{89 * 24 * time.Hour, 60 * 24 * time.Hour, 31 * 24 * time.Hour, 48 * time.Hour, time.Hour} { |
| - bf.seedCharge(fmt.Sprintf("ch_%d", i), now.Add(-ago), 1200) |
| + for i, at := range []time.Time{billingMoneyEpoch, billingMoneyEpoch.Add(40 * 24 * time.Hour), |
| + time.Date(2015, 6, 1, 0, 0, 0, 0, time.UTC), now.Add(-60 * 24 * time.Hour), now.Add(-time.Hour)} { |
| + bf.seedCharge(fmt.Sprintf("ch_%d", i), at, 1200) |
| } |
| spy := bf.spyWindows() |
| if err := bf.srv.reconcilePayments(context.Background(), now); err != nil { |
| @@ -141,12 +143,15 @@ func TestReconcileBackfillsFromAccountCreation(t *testing.T) { |
| t.Errorf("%s on account %d, want %d", mv, r.AccountID, bf.acct.ID) |
| } |
| } |
| - if len(spy.windows) != 3 || !spy.windows[0][0].Equal(opened) || !spy.windows[2][1].Equal(now) { |
| - t.Fatalf("windows = %v; want three from the account's opening to now", spy.windows) |
| + span := now.Sub(billingMoneyEpoch) |
| + wantWindows := int((span + billingMoneyWindow - 1) / billingMoneyWindow) |
| + if len(spy.windows) != wantWindows || !spy.windows[0][0].Equal(billingMoneyEpoch) || !spy.windows[len(spy.windows)-1][1].Equal(now) { |
| + t.Fatalf("%d windows from %v to %v; want %d from the epoch to now", len(spy.windows), |
| + spy.windows[0][0], spy.windows[len(spy.windows)-1][1], wantWindows) |
| } |
| for i := 1; i < len(spy.windows); i++ { |
| if !spy.windows[i][0].Equal(spy.windows[i-1][1]) { |
| - t.Fatalf("windows not contiguous: %v", spy.windows) |
| + t.Fatalf("windows %d and %d not contiguous", i-1, i) |
| } |
| } |
| if got := spy.windows[0][1].Sub(spy.windows[0][0]); got != billingMoneyWindow { |
diff --git a/internal/console/billingrevenue_test.go b/internal/console/billingrevenue_test.go| index faf7f3dfd..a86c03c52 100644 |
| --- a/internal/console/billingrevenue_test.go |
| +++ b/internal/console/billingrevenue_test.go |
| @@ -267,9 +267,6 @@ func newSnapFixture(t *testing.T, start time.Time) *snapFixture { |
| t.Helper() |
| bf := newBillingFixture(t) |
| bf.ready(t) |
| - bf.fake.Lock() |
| - bf.fake.AccountOpened = start.Add(-48 * time.Hour) |
| - bf.fake.Unlock() |
| if err := bf.st.PutEURRates(fxrate.Rates{Day: "2026-10-24", PerEUR: map[string]float64{"usd": 1.12}}, start); err != nil { |
| t.Fatal(err) |
| } |
| @@ -444,9 +441,6 @@ func TestSnapshotStartsAtFirstPass(t *testing.T) { |
| m, rec := testChargeRecord("ch_old", day.Add(-90*24*time.Hour), 2400) |
| sf.seedMovement(m) |
| sf.setCharge(m.Charge, rec) |
| - sf.fake.Lock() |
| - sf.fake.AccountOpened = day.Add(-100 * 24 * time.Hour) |
| - sf.fake.Unlock() |
| if d, err := sf.st.FirstSnapshotDay(); err != nil || d != "" { |
| t.Fatalf("before any pass: %q, %v", d, err) |
| } |