| 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) |
| + } |
| +} |