Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
api: support tax ID collection on checkout (#285)
Browse files Browse the repository at this point in the history
Also: support for enabling automatic tax on Checkout.
  • Loading branch information
bmizerany committed Mar 22, 2023
1 parent 9076354 commit 10c6612
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
1 change: 1 addition & 0 deletions api/apitypes/apitypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type CheckoutRequest struct {
SuccessURL string `json:"success_url"`
CancelURL string `json:"cancel_url"`
RequireBillingAddress bool `json:"require_billing_address"`
Tax Taxation `json:"tax"`
}

type ScheduleRequest struct {
Expand Down
2 changes: 2 additions & 0 deletions control/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ type CheckoutParams struct {
Features []Feature
CancelURL string
RequireBillingAddress bool
AutomaticTax bool
}

func (c *Client) Checkout(ctx context.Context, org string, successURL string, p *CheckoutParams) (link string, err error) {
Expand All @@ -513,6 +514,7 @@ func (c *Client) Checkout(ctx context.Context, org string, successURL string, p
var f stripe.Form
f.Set("customer", cid)
f.Set("success_url", successURL)
f.Set("automatic_tax", "enabled", p.AutomaticTax)
if p.CancelURL != "" {
f.Set("cancel_url", p.CancelURL)
}
Expand Down
78 changes: 42 additions & 36 deletions control/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,10 +948,11 @@ func TestLookupPaymentMethods(t *testing.T) {

func TestCheckoutRequiredAddress(t *testing.T) {
type G struct {
successURL string
cancelURL string
bac string // billing_address_collection
trialDays string
successURL string
cancelURL string
bac string // billing_address_collection
trialDays string
automaticTax string
}

var mu sync.Mutex
Expand All @@ -971,10 +972,11 @@ func TestCheckoutRequiredAddress(t *testing.T) {
case they.Want(r, "POST", "/v1/checkout/sessions"):
mu.Lock()
got = append(got, G{
successURL: r.FormValue("success_url"),
cancelURL: r.FormValue("cancel_url"),
bac: r.FormValue("billing_address_collection"),
trialDays: r.FormValue("subscription_data[trial_period_days]"),
successURL: r.FormValue("success_url"),
cancelURL: r.FormValue("cancel_url"),
bac: r.FormValue("billing_address_collection"),
trialDays: r.FormValue("subscription_data[trial_period_days]"),
automaticTax: r.FormValue("automatic_tax[enabled]"),
})
mu.Unlock()
jsonEncode(t, w, msa{
Expand All @@ -1000,35 +1002,39 @@ func TestCheckoutRequiredAddress(t *testing.T) {
for _, withFeatures := range TF {
for _, withCancel := range TF {
for _, withTrial := range TF {
got = nil

var (
bac = values.ReturnIf(withAddress, "required")
cancelURL = values.ReturnIf(withCancel, "https://c.com")
features = values.ReturnIf(withFeatures, []Feature{{}})
trialDays = values.ReturnIf(withTrial, 14)
)

link, err := cc.Checkout(context.Background(), "org:demo", "http://s.com", &CheckoutParams{
Features: features,
RequireBillingAddress: withAddress,
CancelURL: cancelURL,
TrialDays: trialDays,
})
if err != nil {
t.Fatal(err)
for _, withTax := range TF {
got = nil

var (
bac = values.ReturnIf(withAddress, "required")
cancelURL = values.ReturnIf(withCancel, "https://c.com")
features = values.ReturnIf(withFeatures, []Feature{{}})
trialDays = values.ReturnIf(withTrial, 14)
)

link, err := cc.Checkout(context.Background(), "org:demo", "http://s.com", &CheckoutParams{
Features: features,
RequireBillingAddress: withAddress,
CancelURL: cancelURL,
TrialDays: trialDays,
AutomaticTax: withTax,
})
if err != nil {
t.Fatal(err)
}

if want := "http://co.com/123"; link != want {
t.Errorf("link = %q; want %q", link, want)
}

diff.Test(t, t.Errorf, got, []G{{
successURL: "http://s.com",
cancelURL: cancelURL,
bac: bac,
trialDays: values.ReturnIf(withTrial && withFeatures, strconv.Itoa(trialDays)),
automaticTax: fmt.Sprintf("%v", withTax),
}})
}

if want := "http://co.com/123"; link != want {
t.Errorf("link = %q; want %q", link, want)
}

diff.Test(t, t.Errorf, got, []G{{
successURL: "http://s.com",
cancelURL: cancelURL,
bac: bac,
trialDays: values.ReturnIf(withTrial && withFeatures, strconv.Itoa(trialDays)),
}})
}
}
}
Expand Down

0 comments on commit 10c6612

Please sign in to comment.