billingstripe: Checkout collects a VAT number (tax_id_collection) so Stripe Tax applies reverse charge for EU businesses
PrepareCheckout adds tax_id_collection[enabled]=true and customer_update[name]=auto (Stripe requires the latter with the former when the session has a customer). tax_id_collection[required] is left unset: it defaults to never, so the VAT number stays optional. Only newly prepared requests carry the fields; a stored Prepared is resent byte for byte, and a test pins that.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+73
−4
docs/site/plans.md+1 −1internal/billingstripe/prepared.go+11 −2internal/billingstripe/prepared_test.go+61 −1
Intent
diff --git a/docs/site/plans.md b/docs/site/plans.md| index 8654bd99b..04dbc7565 100644 |
| --- a/docs/site/plans.md |
| +++ b/docs/site/plans.md |
| @@ -207,7 +207,7 @@ Where billing is on, an account's owners buy and change their plan on the accoun |
| ### Buying a plan |
| -Under **Choose a plan**, pick a currency (`usd`, `eur` or `gbp`), a plan, and any packs, then press **Continue to payment**. You go to Stripe to pay. When you come back, the page shows your new plan as soon as the payment is confirmed. Prices are for a year, and tax is added where it applies. |
| +Under **Choose a plan**, pick a currency (`usd`, `eur` or `gbp`), a plan, and any packs, then press **Continue to payment**. You go to Stripe to pay. When you come back, the page shows your new plan as soon as the payment is confirmed. Prices are for a year, and tax is added where it applies. Checkout asks business customers for their VAT number, and Stripe applies reverse charge. |
| The currency is fixed by the first purchase. After that the page says which currency the account pays in, and changing it means asking an operator. |
diff --git a/internal/billingstripe/prepared.go b/internal/billingstripe/prepared.go| index 5ecce0d52..fad603611 100644 |
| --- a/internal/billingstripe/prepared.go |
| +++ b/internal/billingstripe/prepared.go |
| @@ -41,8 +41,9 @@ func (p *Provider) PrepareCustomer(accountSqid, key string) (billing.Prepared, e |
| // PrepareCheckout encodes a Checkout Session create: subscription mode in |
| // the request's currency, its line items by price id, Adaptive Pricing |
| -// off, the billing address required and saved on the customer, automatic |
| -// tax, the provider-time expiry, the success and cancel URLs, the purchase |
| +// off, the billing address required and saved on the customer, the |
| +// customer's name saved and an optional tax id collected (so Stripe Tax can |
| +// reverse charge an EU business), automatic tax, the provider-time expiry, the success and cancel URLs, the purchase |
| // metadata on the session and on the subscription it makes, and either the |
| // resolved coupon (req.Coupon) or promotion codes. |
| func (p *Provider) PrepareCheckout(req billing.CheckoutRequest) (billing.Prepared, error) { |
| @@ -68,6 +69,14 @@ func (p *Provider) PrepareCheckout(req billing.CheckoutRequest) (billing.Prepare |
| } |
| form.Set("billing_address_collection", "required") |
| form.Set("customer_update[address]", "auto") |
| + // Optional VAT number: Stripe Tax applies reverse charge to an EU |
| + // business that gives one. tax_id_collection[required] stays unset (it |
| + // defaults to never). Stripe requires customer_update[name]=auto beside |
| + // tax_id_collection when the session has a customer, as ours always |
| + // does. These ride only on a newly prepared request: Send resends a |
| + // stored Prepared's bytes as they are. |
| + form.Set("customer_update[name]", "auto") |
| + form.Set("tax_id_collection[enabled]", "true") |
| form.Set("expires_at", strconv.FormatInt(req.ExpiresAt.Unix(), 10)) |
| return prepared(billing.PreparedCheckout, "/v1/checkout/sessions", form, req.IdempotencyKey) |
| } |
diff --git a/internal/billingstripe/prepared_test.go b/internal/billingstripe/prepared_test.go| index 4270b6c1a..47a46ea92 100644 |
| --- a/internal/billingstripe/prepared_test.go |
| +++ b/internal/billingstripe/prepared_test.go |
| @@ -259,7 +259,8 @@ func TestPrepareCheckoutCarriesResolvedCoupon(t *testing.T) { |
| "line_items[0][price]": {"price_p"}, "line_items[0][quantity]": {"1"}, |
| "line_items[1][price]": {"price_a"}, "line_items[1][quantity]": {"2"}, |
| "adaptive_pricing[enabled]": {"false"}, "billing_address_collection": {"required"}, |
| - "customer_update[address]": {"auto"}, "automatic_tax[enabled]": {"true"}, |
| + "customer_update[address]": {"auto"}, "customer_update[name]": {"auto"}, |
| + "tax_id_collection[enabled]": {"true"}, "automatic_tax[enabled]": {"true"}, |
| "expires_at": {strconv.FormatInt(providerDate.Add(time.Hour).Unix(), 10)}, |
| "success_url": {req.SuccessURL}, "cancel_url": {req.CancelURL}, |
| "allow_promotion_codes": {"true"}, |
| @@ -292,6 +293,65 @@ func TestPrepareCheckoutCarriesResolvedCoupon(t *testing.T) { |
| } |
| } |
| +// A Checkout create prepared before tax_id_collection existed is stored |
| +// with its bytes and resent as they are: Send never re-encodes a Prepared, |
| +// so the new fields reach only newly prepared requests. |
| +func TestSendStoredCheckoutWithoutTaxIDFieldsIsUnchanged(t *testing.T) { |
| + var c capture |
| + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + got := c.add(r) |
| + fmt.Fprint(w, okBody(got.path)) |
| + })) |
| + defer srv.Close() |
| + p := prepProvider(t, srv.URL, nil) |
| + |
| + // The body PrepareCheckout produced before this change, frozen. |
| + stored := billing.Prepared{ |
| + Kind: billing.PreparedCheckout, Method: http.MethodPost, Path: "/v1/checkout/sessions", Key: "key-checkout-1", |
| + Body: []byte("adaptive_pricing%5Benabled%5D=false&allow_promotion_codes=true&automatic_tax%5Benabled%5D=true" + |
| + "&billing_address_collection=required&cancel_url=https%3A%2F%2Fconsole.example.com%2Fbilling" + |
| + "¤cy=usd&customer=cus_1&customer_update%5Baddress%5D=auto&expires_at=1792933200" + |
| + "&line_items%5B0%5D%5Bprice%5D=price_p&line_items%5B0%5D%5Bquantity%5D=1" + |
| + "&line_items%5B1%5D%5Bprice%5D=price_a&line_items%5B1%5D%5Bquantity%5D=2" + |
| + "&metadata%5Baccount_sqid%5D=bab&metadata%5Bcheckout_intent%5D=ci_1&metadata%5Bcurrency%5D=usd" + |
| + "&metadata%5Bprice_book%5D=example-v1&mode=subscription" + |
| + "&subscription_data%5Bmetadata%5D%5Baccount_sqid%5D=bab&subscription_data%5Bmetadata%5D%5Bcheckout_intent%5D=ci_1" + |
| + "&subscription_data%5Bmetadata%5D%5Bcurrency%5D=usd&subscription_data%5Bmetadata%5D%5Bprice_book%5D=example-v1" + |
| + "&success_url=https%3A%2F%2Fconsole.example.com%2Fbilling%3Fdone%3D1"), |
| + } |
| + want := string(stored.Body) |
| + if _, err := p.Send(context.Background(), stored); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := c.last(); string(got.body) != want || got.key != "key-checkout-1" { |
| + t.Errorf("stored create was altered:\n got %q\nwant %q", got.body, want) |
| + } |
| + if string(stored.Body) != want { |
| + t.Errorf("Send changed the stored body") |
| + } |
| + |
| + // A newly prepared request carries the new fields, and differs from the |
| + // stored one by exactly those two. |
| + fresh, err := p.PrepareCheckout(checkoutRequest()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + fv, _ := url.ParseQuery(string(fresh.Body)) |
| + sv, _ := url.ParseQuery(want) |
| + for _, k := range []string{"customer_update[name]", "tax_id_collection[enabled]"} { |
| + if fv.Get(k) == "" || sv.Has(k) { |
| + t.Errorf("%s: fresh %q, stored %q", k, fv.Get(k), sv.Get(k)) |
| + } |
| + fv.Del(k) |
| + } |
| + if fv.Encode() != sv.Encode() { |
| + t.Errorf("fresh and stored differ by more than the new fields:\n %s\n %s", fv.Encode(), sv.Encode()) |
| + } |
| + if fv.Has("tax_id_collection[required]") { |
| + t.Errorf("tax id collection must stay optional") |
| + } |
| +} |
| + |
| func TestSendVerbatimOneRequest(t *testing.T) { |
| var c capture |
| var mode atomic.Value |