carlos / platform Public

console: domain refunds take their charge's tax share

Fix round 1 for the payment record. Tax derived from a domain purchase's
price applies to charges only; refund debits and reversals carrying the
charge's payment intent now inherit the charge's share, so a partial
refund and its reversal cancel before tax. A refund debit matched by the
purchase gets reason domain; a reversal keeps reversed. Rule 3 copies an
unmatched charge's subscription too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 973872bb04efecdc87abce703ff8e69703cfcc05 parent 63cd798
2 files changed, +80 −9
  • internal/console/billingpayments_store.go +12 −9
  • internal/console/billingpayments_store_test.go +68 −0
diff --git a/internal/console/billingpayments_store.go b/internal/console/billingpayments_store.go
index be5392cf3..217fe18de 100644
--- a/internal/console/billingpayments_store.go
+++ b/internal/console/billingpayments_store.go
@@ -63,11 +63,11 @@ type sqlQueryer interface {
}
// matchPayment finds a payment's account, first hit wins: its payment
-// intent is a domain purchase's (the reason becomes domain, the detail the
-// domain, and a charge with no tax above the quoted price carries the
-// difference as tax); its customer is an account's; its charge is a
-// recorded charge row's (whose subscription it takes when it has none).
-// Unmatched leaves AccountID 0.
+// intent is a domain purchase's (the reason becomes domain, but for a
+// reversal; the detail the domain; and a charge with no tax above the
+// quoted price carries the difference as tax); its customer is an account's; its charge is a
+// recorded charge row's (whose subscription it takes when it has none,
+// even when that charge is itself unmatched). Unmatched leaves AccountID 0.
func matchPayment(q sqlQueryer, r *paymentRow) error {
p := &r.Payment
if p.PaymentIntent != "" {
@@ -78,11 +78,14 @@ func matchPayment(q sqlQueryer, r *paymentRow) error {
switch {
case err == nil:
r.AccountID, r.Detail = account, domain
- // A refund keeps its own reason: a reversal stays reversed.
- if p.Kind != billing.PaymentRefund {
+ // A reversal keeps its reason: it stays reversed.
+ if p.Reason != billing.ReasonReversed {
p.Reason = billing.ReasonDomain
}
- if p.Tax == 0 && strings.EqualFold(currency, p.Currency) && p.Amount > amount {
+ // Only a charge's tax comes from the quoted price; refunds and
+ // reversals take their charge's share (inheritRefundTax).
+ isCharge := p.Kind == billing.PaymentSubscription || p.Kind == billing.PaymentOnceOff
+ if isCharge && p.Tax == 0 && strings.EqualFold(currency, p.Currency) && p.Amount > amount {
p.Tax = p.Amount - amount
p.SettledTax = billing.SettledShare(p.Tax, p.Amount, p.Settled)
}
@@ -106,7 +109,7 @@ func matchPayment(q sqlQueryer, r *paymentRow) error {
var account int64
var subscription string
err := q.QueryRow(`SELECT account_id, subscription FROM billing_payments
- WHERE source_id = ? AND kind IN ('subscription', 'once_off') AND account_id != 0
+ WHERE source_id = ? AND kind IN ('subscription', 'once_off')
ORDER BY at, movement_id LIMIT 1`, p.Charge).Scan(&account, &subscription)
switch {
case err == nil:
diff --git a/internal/console/billingpayments_store_test.go b/internal/console/billingpayments_store_test.go
index 11c7540f5..9870ddd65 100644
--- a/internal/console/billingpayments_store_test.go
+++ b/internal/console/billingpayments_store_test.go
@@ -348,3 +348,71 @@ func TestPaymentsSurviveAccountReap(t *testing.T) {
t.Errorf("after reap: %+v, %d, %v; want the row kept", rows, n, err)
}
}
+
+// A partial refund of a domain charge, and its reversal, carry the charge's
+// payment intent: both match the purchase, but neither takes tax from the
+// purchase price; each takes the charge's share, so they cancel.
+func TestRecordPaymentDomainPartialRefundAndReversal(t *testing.T) {
+ s, ids := planStore(t, "bab")
+ if err := s.CreateDomainPurchase(DomainPurchase{AccountID: ids["bab"], Domain: "shop.example.com", App: "web",
+ Currency: "eur", Amount: 10000, PaymentIntent: "pi_dom"}); err != nil {
+ t.Fatal(err)
+ }
+ recordPayment(t, s, billing.Payment{
+ Movement: "mv_dom", Source: "ch_dom", Kind: billing.PaymentOnceOff, Reason: billing.ReasonOther,
+ PaymentIntent: "pi_dom", Charge: "ch_dom",
+ Amount: 12000, Currency: "eur", Settled: 13000, Fee: 400, SettledCurrency: "usd", At: paymentAt,
+ })
+ debit := billing.Payment{
+ Movement: "mv_re", Source: "re_dom", Kind: billing.PaymentRefund, Reason: billing.ReasonOther,
+ PaymentIntent: "pi_dom", Charge: "ch_dom", Amount: -11000, Currency: "eur",
+ Settled: -11917, SettledCurrency: "usd", At: paymentAt.Add(time.Hour),
+ }
+ recordPayment(t, s, debit)
+ reversal := debit
+ reversal.Movement, reversal.Reason, reversal.Amount, reversal.Settled = "mv_rv", billing.ReasonReversed, 11000, 11917
+ reversal.At = paymentAt.Add(2 * time.Hour)
+ recordPayment(t, s, reversal)
+
+ c, d, v := paymentByMovement(t, s, "mv_dom"), paymentByMovement(t, s, "mv_re"), paymentByMovement(t, s, "mv_rv")
+ if c.Tax != 2000 || c.Reason != billing.ReasonDomain {
+ t.Fatalf("charge tax %d reason %q; want 2000, domain", c.Tax, c.Reason)
+ }
+ wantTax := billing.SettledShare(2000, 12000, -11000) // -1833
+ if d.Tax != wantTax || v.Tax != -wantTax {
+ t.Errorf("debit tax %d reversal tax %d; want %d, %d (the charge's share)", d.Tax, v.Tax, wantTax, -wantTax)
+ }
+ if d.SettledTax != billing.SettledShare(wantTax, -11000, -11917) || v.SettledTax != -d.SettledTax {
+ t.Errorf("debit settled tax %d reversal %d; want %d and its negation", d.SettledTax, v.SettledTax,
+ billing.SettledShare(wantTax, -11000, -11917))
+ }
+ if d.Reason != billing.ReasonDomain || v.Reason != billing.ReasonReversed {
+ t.Errorf("debit reason %q reversal reason %q; want domain, reversed", d.Reason, v.Reason)
+ }
+ if d.AccountID != ids["bab"] || v.AccountID != ids["bab"] || d.Detail != "shop.example.com" || v.Detail != "shop.example.com" {
+ t.Errorf("debit %d %q reversal %d %q; want bab, shop.example.com", d.AccountID, d.Detail, v.AccountID, v.Detail)
+ }
+ if net := (d.Settled - d.SettledTax) + (v.Settled - v.SettledTax); net != 0 {
+ t.Errorf("debit plus reversal before tax = %d, want 0", net)
+ }
+ if sum, want := (c.Settled-c.SettledTax)+(d.Settled-d.SettledTax)+(v.Settled-v.SettledTax), c.Settled-c.SettledTax; sum != want {
+ t.Errorf("charge+debit+reversal before tax = %d, want the charge's %d", sum, want)
+ }
+}
+
+// Rule 3 takes an unmatched charge's subscription even though there is no
+// account to take.
+func TestRecordPaymentRefundTakesUnmatchedChargeSubscription(t *testing.T) {
+ s, _ := planStore(t, "bab")
+ c := testCharge("mv_1", "ch_1")
+ c.Customer, c.Subscription = "cus_nobody", "sub_9"
+ recordPayment(t, s, c)
+ recordPayment(t, s, billing.Payment{
+ Movement: "mv_re", Source: "re_1", Kind: billing.PaymentRefund, Reason: billing.ReasonOther,
+ Charge: "ch_1", Amount: -600, Tax: -100, Currency: "eur",
+ Settled: -600, SettledTax: -100, SettledCurrency: "eur", At: paymentAt.Add(time.Hour),
+ })
+ if got := paymentByMovement(t, s, "mv_re"); got.AccountID != 0 || got.Subscription != "sub_9" {
+ t.Errorf("refund account %d subscription %q; want 0, sub_9", got.AccountID, got.Subscription)
+ }
+}