carlos / platform Public

billingstripe: compound discounts in order; once discounts are unknown

Fix round 1 for the yearly amount. Discounts apply in listed order to the
running subtotal, flooring at 0 after each. A "once" discount still
attached makes the amount unknown, since the read cannot show whether it
is spent. The coupon-failure test now captures and asserts the log line
instead of printing it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev c309c80fc489409733be586b1614d26631181b37 parent 914d462
2 files changed, +32 −8
  • internal/billingstripe/annual.go +11 −7
  • internal/billingstripe/annual_test.go +21 −1
diff --git a/internal/billingstripe/annual.go b/internal/billingstripe/annual.go
index d054f6a6e..1085fef97 100644
--- a/internal/billingstripe/annual.go
+++ b/internal/billingstripe/annual.go
@@ -5,13 +5,16 @@ import "math"
// annualOf is what the subscription will charge for its next year: every
// item's unit amount times its quantity, less each subscription discount
// that still applies at the next renewal, before tax, in minor units of the
-// subscription's currency, never below 0. Prices here are yearly, so one
-// renewal is one year.
+// subscription's currency. Prices here are yearly, so one renewal is one
+// year. Discounts apply in the order listed, each to the subtotal the ones
+// before it left (a percentage of that subtotal, or a fixed amount), and
+// 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 coupon limited to some products, a fixed amount
-// off in another currency, or a coupon whose amount is not known.
+// discounts of its own, 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) {
if s.ItemsHasMore {
return 0, false
@@ -36,7 +39,7 @@ func annualOf(s *Subscription) (amount int64, known bool) {
continue // over before the next renewal
}
case "once":
- continue // spent on the first invoice
+ return 0, false
default:
return 0, false
}
@@ -45,7 +48,7 @@ func annualOf(s *Subscription) (amount int64, known bool) {
}
switch {
case c.PercentOff != nil:
- total -= int64(math.Round(float64(gross) * *c.PercentOff / 100))
+ total -= int64(math.Round(float64(total) * *c.PercentOff / 100))
case c.AmountOff != nil:
if c.Currency != s.Currency {
return 0, false
@@ -54,6 +57,7 @@ func annualOf(s *Subscription) (amount int64, known bool) {
default:
return 0, false
}
+ total = max(total, 0)
}
- return max(total, 0), true
+ return total, true
}
diff --git a/internal/billingstripe/annual_test.go b/internal/billingstripe/annual_test.go
index d0382c212..d79416146 100644
--- a/internal/billingstripe/annual_test.go
+++ b/internal/billingstripe/annual_test.go
@@ -1,11 +1,14 @@
package billingstripe
import (
+ "bytes"
"context"
"fmt"
+ "log"
"net/http"
"net/http/httptest"
"reflect"
+ "strings"
"testing"
"time"
)
@@ -37,7 +40,10 @@ func TestAnnualOf(t *testing.T) {
{"50% forever", sub([]SubscriptionItem{personal}, pct(50, "forever", time.Time{})), 1400, true},
{"amount off", sub([]SubscriptionItem{personal}, off(500, "usd")), 2300, true},
{"amount off over the total", sub([]SubscriptionItem{personal}, off(5000, "usd")), 0, true},
- {"once", sub([]SubscriptionItem{personal}, pct(50, "once", time.Time{})), 2800, true},
+ {"once still attached", sub([]SubscriptionItem{personal}, pct(50, "once", time.Time{})), 0, false},
+ {"amount off then percent", sub([]SubscriptionItem{personal}, off(500, "usd"), pct(50, "forever", time.Time{})), 1150, true},
+ {"percent then amount off", sub([]SubscriptionItem{personal}, pct(50, "forever", time.Time{}), off(500, "usd")), 900, true},
+ {"floored before a later discount", sub([]SubscriptionItem{personal}, off(5000, "usd"), pct(50, "forever", time.Time{})), 0, true},
{"repeating past the renewal", sub([]SubscriptionItem{personal}, pct(50, "repeating", periodEnd.AddDate(0, 1, 0))), 1400, true},
{"repeating ends before the renewal", sub([]SubscriptionItem{personal}, pct(50, "repeating", periodEnd.AddDate(0, -1, 0))), 2800, true},
{"amount off in another currency", sub([]SubscriptionItem{personal}, off(500, "eur")), 0, false},
@@ -124,6 +130,13 @@ func TestSubscriptionAnnualReadsCoupons(t *testing.T) {
}))
return prepProvider(t, srv.URL, nil), srv.Close
}
+ // The coupon failure is logged: capture the log so it is asserted, not
+ // printed.
+ var logged bytes.Buffer
+ prevOut, prevFlags := log.Writer(), log.Flags()
+ log.SetOutput(&logged)
+ log.SetFlags(0)
+ t.Cleanup(func() { log.SetOutput(prevOut); log.SetFlags(prevFlags) })
p, done := run("half", func(w http.ResponseWriter) {
fmt.Fprint(w, `{"id":"half","valid":true,"livemode":false,"percent_off":50,"duration":"forever"}`)
@@ -142,6 +155,9 @@ func TestSubscriptionAnnualReadsCoupons(t *testing.T) {
if err != nil || sub.AnnualKnown || sub.Annual != 0 || !sub.FreshRead {
t.Errorf("product-limited: %+v, %v", sub, err)
}
+ if logged.Len() != 0 {
+ t.Errorf("readable coupons logged: %q", logged.String())
+ }
p, done = run("coupon fails", func(w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
@@ -155,6 +171,10 @@ func TestSubscriptionAnnualReadsCoupons(t *testing.T) {
if sub.AnnualKnown || sub.Annual != 0 || !sub.FreshRead {
t.Errorf("coupon fails: %+v", sub)
}
+ const want = `billingstripe: subscription sub_1: reading coupon "half": billingstripe: api_error status=500 stripe_type=api_error; its yearly amount is unknown`
+ if got := strings.TrimSpace(logged.String()); got != want {
+ t.Errorf("coupon fails: logged %q, want %q", got, want)
+ }
if sub.ID != "sub_1" || sub.Customer != "cus_1" || sub.Account != "bab" || sub.Currency != "usd" || sub.ProviderStatus != "active" ||
len(sub.Items) != 1 || sub.Items[0].PriceID != "price_p" || !sub.PeriodEnd.Equal(time.Unix(1824422400, 0).UTC()) {
t.Errorf("coupon fails: other fields not mapped: %+v", sub)