carlos / platform Public

billingstripe: refuse money movements that name no charge

A charge, refund or refund-failure balance transaction whose source is
unexpanded, null, or names no usable charge now fails the whole list
instead of yielding a movement with an empty Charge. Tests also pin the
invalid-id refusal before any request, fractional [from, to) bounds and
same-second ordering by id.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev a5e8210237272e25d1531a0c2d6027a50d04e93d parent 763d2df
2 files changed, +107 −16
  • internal/billingstripe/money.go +17 −8
  • internal/billingstripe/money_test.go +90 −8
diff --git a/internal/billingstripe/money.go b/internal/billingstripe/money.go
index f7be233fe..fb4038d29 100644
--- a/internal/billingstripe/money.go
+++ b/internal/billingstripe/money.go
@@ -236,19 +236,28 @@ func (p *Provider) MoneyMovements(ctx context.Context, from, to time.Time) ([]bi
ID: t.ID, Kind: movementKindOf(t.Type), Type: t.Type, Source: t.Source.ID,
Amount: t.Amount, Fee: t.Fee, Currency: t.Currency, At: t.Created,
}
+ // A charge, refund or refund failure must name its charge: the
+ // money pass reads the charge to record and attribute it.
switch m.Kind {
case billing.MovementCharge:
+ if !validID(t.Source.ID) {
+ return "", errors.New("billingstripe: charge movement names no charge")
+ }
m.Charge = t.Source.ID
case billing.MovementRefund, billing.MovementRefundFailure:
- if t.Source.Object != nil {
- var r struct {
- Charge idOrExpanded `json:"charge"`
- }
- if err := json.Unmarshal(t.Source.Object, &r); err != nil {
- return "", err
- }
- m.Charge = r.Charge.ID
+ if t.Source.Object == nil || !validID(t.Source.ID) {
+ return "", errors.New("billingstripe: refund movement source not expanded")
+ }
+ var r struct {
+ Charge idOrExpanded `json:"charge"`
+ }
+ if err := json.Unmarshal(t.Source.Object, &r); err != nil {
+ return "", err
+ }
+ if !validID(r.Charge.ID) {
+ return "", errors.New("billingstripe: refund movement names no charge")
}
+ m.Charge = r.Charge.ID
}
out = append(out, m)
return t.ID, nil
diff --git a/internal/billingstripe/money_test.go b/internal/billingstripe/money_test.go
index 8e86f5359..776ebae0a 100644
--- a/internal/billingstripe/money_test.go
+++ b/internal/billingstripe/money_test.go
@@ -72,6 +72,66 @@ func TestMoneyMovementsMapsTypes(t *testing.T) {
}
}
+// The window is [from, to) to the nanosecond, though Stripe's times are
+// whole seconds: a fractional bound rounds up, so a movement at from's
+// second is out when from is past it, and one at to's second is in when to
+// is past it. Movements in the same second come back by id.
+func TestMoneyMovementsWindowAndTies(t *testing.T) {
+ var got url.Values
+ page := []string{
+ txnJSON("txn_b", "charge", 2, 0, "eur", moneyT0.Add(time.Hour), `"ch_b"`),
+ txnJSON("txn_c", "charge", 3, 0, "eur", moneyT0.Add(time.Hour), `"ch_c"`),
+ txnJSON("txn_a", "charge", 1, 0, "eur", moneyT0.Add(time.Hour), `"ch_a"`),
+ txnJSON("txn_0", "charge", 9, 0, "eur", moneyT0.Add(time.Minute), `"ch_0"`),
+ }
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ got = r.URL.Query()
+ fmt.Fprintf(w, `{"data":[%s],"has_more":false}`, strings.Join(page, ","))
+ }))
+ defer srv.Close()
+ p := prepProvider(t, srv.URL, nil)
+ for _, tc := range []struct {
+ name string
+ from, to time.Time
+ gte, lt int64
+ }{
+ {"whole seconds", moneyT0, moneyT0.Add(2 * time.Hour), moneyT0.Unix(), moneyT0.Add(2 * time.Hour).Unix()},
+ {"fractional from rounds up", moneyT0.Add(500 * time.Millisecond), moneyT0.Add(2 * time.Hour), moneyT0.Unix() + 1, moneyT0.Add(2 * time.Hour).Unix()},
+ {"fractional to rounds up", moneyT0, moneyT0.Add(2*time.Hour + time.Nanosecond), moneyT0.Unix(), moneyT0.Add(2*time.Hour).Unix() + 1},
+ {"before the epoch second", moneyT0.Add(-time.Nanosecond), moneyT0.Add(-time.Nanosecond), moneyT0.Unix(), moneyT0.Unix()},
+ } {
+ ms, err := p.MoneyMovements(context.Background(), tc.from, tc.to)
+ if err != nil {
+ t.Fatalf("%s: %v", tc.name, err)
+ }
+ if got.Get("created[gte]") != fmt.Sprint(tc.gte) || got.Get("created[lt]") != fmt.Sprint(tc.lt) {
+ t.Errorf("%s: created[gte]=%s created[lt]=%s, want %d %d", tc.name, got.Get("created[gte]"), got.Get("created[lt]"), tc.gte, tc.lt)
+ }
+ var ids []string
+ for _, m := range ms {
+ ids = append(ids, m.ID)
+ }
+ if !slices.Equal(ids, []string{"txn_0", "txn_a", "txn_b", "txn_c"}) {
+ t.Errorf("%s: order %v", tc.name, ids)
+ }
+ }
+ // A second boundary, checked against unixCeil directly: a time exactly
+ // on a second is that second; any fraction past it is the next.
+ for _, tc := range []struct {
+ at time.Time
+ want int64
+ }{
+ {moneyT0, moneyT0.Unix()},
+ {moneyT0.Add(time.Nanosecond), moneyT0.Unix() + 1},
+ {moneyT0.Add(999_999_999), moneyT0.Unix() + 1},
+ {time.Unix(-1, 500_000_000), 0},
+ } {
+ if got := unixCeil(tc.at); got != tc.want {
+ t.Errorf("unixCeil(%v) = %d, want %d", tc.at, got, tc.want)
+ }
+ }
+}
+
func TestMoneyMovementKinds(t *testing.T) {
for typ, want := range map[string]billing.MovementKind{
"charge": billing.MovementCharge, "payment": billing.MovementCharge,
@@ -106,16 +166,27 @@ func TestMoneyMovementsIncomplete(t *testing.T) {
func TestMoneyMovementsRefusesMalformed(t *testing.T) {
for name, obj := range map[string]string{
- "no id": `{"object":"balance_transaction","type":"charge","amount":1,"currency":"eur","created":1}`,
- "bad id": `{"id":"txn 1","type":"charge","amount":1,"currency":"eur","created":1}`,
- "no created": `{"id":"txn_1","type":"charge","amount":1,"currency":"eur"}`,
- "null": `null`,
+ "no id": `{"object":"balance_transaction","type":"charge","amount":1,"currency":"eur","created":1}`,
+ "bad id": `{"id":"txn 1","type":"charge","amount":1,"currency":"eur","created":1}`,
+ "no created": `{"id":"txn_1","type":"charge","amount":1,"currency":"eur"}`,
+ "null": `null`,
+ "refund source unexpanded": txnJSON("txn_1", "refund", -1, 0, "eur", moneyT0, `"re_1"`),
+ "refund source null": txnJSON("txn_1", "refund", -1, 0, "eur", moneyT0, `null`),
+ "refund source no charge": txnJSON("txn_1", "refund", -1, 0, "eur", moneyT0, `{"id":"re_1","object":"refund"}`),
+ "refund source null charge": txnJSON("txn_1", "refund", -1, 0, "eur", moneyT0, `{"id":"re_1","object":"refund","charge":null}`),
+ "refund source bad charge": txnJSON("txn_1", "refund", -1, 0, "eur", moneyT0, `{"id":"re_1","object":"refund","charge":"ch/1"}`),
+ "failure source unexpanded": txnJSON("txn_1", "refund_failure", 1, 0, "eur", moneyT0, `"re_1"`),
+ "failure source no charge": txnJSON("txn_1", "payment_failure_refund", 1, 0, "eur", moneyT0, `{"id":"re_1"}`),
+ "charge source null": txnJSON("txn_1", "charge", 1, 0, "eur", moneyT0, `null`),
+ // A good movement first: a later bad one still fails the whole list.
+ "bad after good": txnJSON("txn_0", "charge", 1, 0, "eur", moneyT0, `"ch_1"`) + "," +
+ txnJSON("txn_1", "refund", -1, 0, "eur", moneyT0, `"re_1"`),
} {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"data":[%s],"has_more":false}`, obj)
}))
- if _, err := prepProvider(t, srv.URL, nil).MoneyMovements(context.Background(), moneyT0, moneyT0.Add(time.Hour)); err == nil {
- t.Errorf("%s: accepted", name)
+ if ms, err := prepProvider(t, srv.URL, nil).MoneyMovements(context.Background(), moneyT0, moneyT0.Add(time.Hour)); err == nil || ms != nil {
+ t.Errorf("%s: accepted (%v)", name, ms)
}
srv.Close()
}
@@ -353,8 +424,19 @@ func TestChargePaymentsUncaptured(t *testing.T) {
if _, err := chargePayments(t, cs); !errors.Is(err, billing.ErrNotFound) {
t.Errorf("404: %v", err)
}
- if _, err := prepProvider(t, "http://127.0.0.1:1", nil).ChargePayments(context.Background(), "../ch_1"); err == nil || errors.Is(err, billing.ErrNotFound) {
- t.Errorf("bad id: %v", err)
+ // An unusable id is refused before any request.
+ requests := 0
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { requests++ }))
+ defer srv.Close()
+ for _, id := range []string{"../ch_1", "", "ch 1", "ch_1?expand[]=x"} {
+ _, err := prepProvider(t, srv.URL, nil).ChargePayments(context.Background(), id)
+ var se *Error
+ if !errors.As(err, &se) || se.Code != CodeInvalidID {
+ t.Errorf("bad id %q: %v, want %s", id, err, CodeInvalidID)
+ }
+ }
+ if requests != 0 {
+ t.Errorf("bad ids made %d requests", requests)
}
}