carlos / platform Public

billingstripe: an amount is known only for prices that recur yearly

Final review finding I(b) (Fable P3-3): annualOf summed unit amount times
quantity as a yearly figure without reading the price's interval, so a
monthly price attached in the Dashboard would understate ARR twelvefold.
SubscriptionItem now decodes price.recurring.interval and annualOf
answers unknown for any item whose price is not yearly. TestAnnualOf
gains a monthly-price row and a no-interval row.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 2983992f517d5288c4c423130873842c12ea6086 parent 43d96d4
3 files changed, +23 −11
  • internal/billingstripe/annual.go +3 −3
  • internal/billingstripe/annual_test.go +10 −8
  • internal/billingstripe/client.go +10 −0
diff --git a/internal/billingstripe/annual.go b/internal/billingstripe/annual.go
index 1085fef97..2798860db 100644
--- a/internal/billingstripe/annual.go
+++ b/internal/billingstripe/annual.go
@@ -11,8 +11,8 @@ import "math"
// the subtotal never goes below 0.
//
// known is false, and amount 0, whenever the read does not show the whole
-// answer: more items than were sent, an item with no unit amount or with
-// discounts of its own, a "once" discount still attached (whether it is
+// answer: more items than were sent, an item with no unit amount, with
+// discounts of its own or whose price does not recur yearly, a "once" discount still attached (whether it is
// spent is not in the read), a coupon limited to some products, a fixed
// amount off in another currency, or a coupon whose amount is not known.
func annualOf(s *Subscription) (amount int64, known bool) {
@@ -21,7 +21,7 @@ func annualOf(s *Subscription) (amount int64, known bool) {
}
var gross int64
for _, it := range s.Items {
- if it.UnitAmount == nil || it.HasDiscounts {
+ if it.UnitAmount == nil || it.HasDiscounts || it.Interval != "year" {
return 0, false
}
gross += *it.UnitAmount * it.Quantity
diff --git a/internal/billingstripe/annual_test.go b/internal/billingstripe/annual_test.go
index d79416146..648f789c5 100644
--- a/internal/billingstripe/annual_test.go
+++ b/internal/billingstripe/annual_test.go
@@ -17,8 +17,8 @@ func TestAnnualOf(t *testing.T) {
periodEnd := time.Date(2027, 10, 25, 0, 0, 0, 0, time.UTC)
i64 := func(v int64) *int64 { return &v }
f64 := func(v float64) *float64 { return &v }
- personal := SubscriptionItem{ID: "si_1", PriceID: "price_p", Quantity: 1, PriceCurrency: "usd", UnitAmount: i64(2800)}
- pack := SubscriptionItem{ID: "si_2", PriceID: "price_a", Quantity: 2, PriceCurrency: "usd", UnitAmount: i64(1200)}
+ personal := SubscriptionItem{ID: "si_1", PriceID: "price_p", Quantity: 1, PriceCurrency: "usd", UnitAmount: i64(2800), Interval: "year"}
+ pack := SubscriptionItem{ID: "si_2", PriceID: "price_a", Quantity: 2, PriceCurrency: "usd", UnitAmount: i64(1200), Interval: "year"}
sub := func(items []SubscriptionItem, discounts ...Discount) *Subscription {
return &Subscription{ID: "sub_1", Currency: "usd", CurrentPeriodEnd: periodEnd, Items: items, Discounts: discounts}
}
@@ -49,8 +49,10 @@ func TestAnnualOf(t *testing.T) {
{"amount off in another currency", sub([]SubscriptionItem{personal}, off(500, "eur")), 0, false},
{"product-limited coupon", sub([]SubscriptionItem{personal},
Discount{Coupon: Coupon{ID: "c", PercentOff: f64(50), Duration: "forever", AppliesToProducts: []string{"prod_x"}}}), 0, false},
- {"no unit amount", sub([]SubscriptionItem{{ID: "si_1", PriceID: "price_p", Quantity: 1}}), 0, false},
- {"item discount", sub([]SubscriptionItem{{ID: "si_1", PriceID: "price_p", Quantity: 1, UnitAmount: i64(2800), HasDiscounts: true}}), 0, false},
+ {"no unit amount", sub([]SubscriptionItem{{ID: "si_1", PriceID: "price_p", Quantity: 1, Interval: "year"}}), 0, false},
+ {"item discount", sub([]SubscriptionItem{{ID: "si_1", PriceID: "price_p", Quantity: 1, UnitAmount: i64(2800), HasDiscounts: true, Interval: "year"}}), 0, false},
+ {"monthly price", sub([]SubscriptionItem{personal, {ID: "si_3", PriceID: "price_m", Quantity: 1, PriceCurrency: "usd", UnitAmount: i64(300), Interval: "month"}}), 0, false},
+ {"price with no interval", sub([]SubscriptionItem{{ID: "si_1", PriceID: "price_p", Quantity: 1, PriceCurrency: "usd", UnitAmount: i64(2800)}}), 0, false},
}
for _, tc := range cases {
got, known := annualOf(tc.sub)
@@ -74,7 +76,7 @@ func TestSubscriptionDecodesDiscounts(t *testing.T) {
"current_period_end":1824422400,
"discounts":[{"id":"di_1","coupon":{"id":"half","valid":true,"livemode":false,"percent_off":50,"duration":"repeating"},"end":1830000000},
{"id":"di_2","coupon":{"id":"five","valid":true,"livemode":false,"amount_off":500,"currency":"usd","duration":"forever"},"end":null}],
- "items":{"has_more":false,"data":[{"id":"si_1","quantity":1,"price":{"id":"price_p","currency":"usd","unit_amount":2800}},
+ "items":{"has_more":false,"data":[{"id":"si_1","quantity":1,"price":{"id":"price_p","currency":"usd","unit_amount":2800,"recurring":{"interval":"year"}}},
{"id":"si_2","quantity":2,"discounts":["di_3"],"price":{"id":"price_a","currency":"usd","unit_amount":null}}]}}`)
}))
defer srv.Close()
@@ -100,10 +102,10 @@ func TestSubscriptionDecodesDiscounts(t *testing.T) {
if len(s.Items) != 2 {
t.Fatalf("items = %+v", s.Items)
}
- if it := s.Items[0]; it.UnitAmount == nil || *it.UnitAmount != 2800 || it.HasDiscounts {
+ if it := s.Items[0]; it.UnitAmount == nil || *it.UnitAmount != 2800 || it.HasDiscounts || it.Interval != "year" {
t.Errorf("item 0 = %+v", it)
}
- if it := s.Items[1]; it.UnitAmount != nil || !it.HasDiscounts {
+ if it := s.Items[1]; it.UnitAmount != nil || !it.HasDiscounts || it.Interval != "" {
t.Errorf("item 1 = %+v", it)
}
}
@@ -112,7 +114,7 @@ func TestSubscriptionAnnualReadsCoupons(t *testing.T) {
const subBody = `{"id":"sub_1","customer":"cus_1","status":"active","livemode":false,"currency":"usd",
"current_period_end":1824422400,"metadata":{"account_sqid":"bab"},
"discounts":[{"id":"di_1","coupon":{"id":"half","valid":true,"livemode":false,"percent_off":50,"duration":"forever"},"end":null}],
- "items":{"has_more":false,"data":[{"id":"si_1","quantity":1,"price":{"id":"price_p","currency":"usd","unit_amount":2800}}]}}`
+ "items":{"has_more":false,"data":[{"id":"si_1","quantity":1,"price":{"id":"price_p","currency":"usd","unit_amount":2800,"recurring":{"interval":"year"}}}]}}`
run := func(name string, coupon func(w http.ResponseWriter)) (*Provider, func()) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
diff --git a/internal/billingstripe/client.go b/internal/billingstripe/client.go
index 07f2bf222..02074b15d 100644
--- a/internal/billingstripe/client.go
+++ b/internal/billingstripe/client.go
@@ -468,6 +468,9 @@ type SubscriptionItem struct {
UnitAmount *int64
// HasDiscounts is the item carrying discounts of its own.
HasDiscounts bool
+ // Interval is the price's recurring interval ("year", "month", ...),
+ // "" when the response carried none.
+ Interval string
}
func (it *SubscriptionItem) UnmarshalJSON(b []byte) error {
@@ -479,6 +482,9 @@ func (it *SubscriptionItem) UnmarshalJSON(b []byte) error {
ID string `json:"id"`
Currency string `json:"currency"`
UnitAmount *int64 `json:"unit_amount"`
+ Recurring *struct {
+ Interval string `json:"interval"`
+ } `json:"recurring"`
} `json:"price"`
}
if err := json.Unmarshal(b, &raw); err != nil {
@@ -490,6 +496,10 @@ func (it *SubscriptionItem) UnmarshalJSON(b []byte) error {
it.PriceCurrency = raw.Price.Currency
it.UnitAmount = raw.Price.UnitAmount
it.HasDiscounts = len(raw.Discounts) > 0
+ it.Interval = ""
+ if raw.Price.Recurring != nil {
+ it.Interval = raw.Price.Recurring.Interval
+ }
return nil
}