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

Commit

Permalink
api: wire up tax collection for Checkout (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmizerany committed Mar 23, 2023
1 parent 10c6612 commit 5510564
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ func (h *Handler) serveCheckout(w http.ResponseWriter, r *http.Request) error {
Features: fs,
CancelURL: cr.CancelURL,
RequireBillingAddress: cr.RequireBillingAddress,
AutomaticTax: cr.Tax.Automatic,
CollectTaxID: cr.Tax.CollectID,
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions api/apitypes/apitypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Phase struct {

type Taxation struct {
Automatic bool `json:"automatic,omitempty"`
CollectID bool `json:"collect_id,omitempty"` // specifics is taxID is collected
}

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

func (c *Client) Checkout(ctx context.Context, org string, successURL string, p *CheckoutParams) (link string, err error) {
Expand All @@ -515,6 +516,7 @@ func (c *Client) Checkout(ctx context.Context, org string, successURL string, p
f.Set("customer", cid)
f.Set("success_url", successURL)
f.Set("automatic_tax", "enabled", p.AutomaticTax)
f.Set("tax_id_collection[enabled]", p.CollectTaxID)
if p.CancelURL != "" {
f.Set("cancel_url", p.CancelURL)
}
Expand Down
66 changes: 36 additions & 30 deletions control/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ func TestCheckoutRequiredAddress(t *testing.T) {
bac string // billing_address_collection
trialDays string
automaticTax string
taxID string
}

var mu sync.Mutex
Expand All @@ -977,6 +978,7 @@ func TestCheckoutRequiredAddress(t *testing.T) {
bac: r.FormValue("billing_address_collection"),
trialDays: r.FormValue("subscription_data[trial_period_days]"),
automaticTax: r.FormValue("automatic_tax[enabled]"),
taxID: r.FormValue("tax_id_collection[enabled]"),
})
mu.Unlock()
jsonEncode(t, w, msa{
Expand All @@ -1003,37 +1005,41 @@ func TestCheckoutRequiredAddress(t *testing.T) {
for _, withCancel := range TF {
for _, withTrial := range TF {
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)
for _, withTaxID := 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,
CollectTaxID: withTaxID,
})
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),
taxID: fmt.Sprintf("%v", withTaxID),
}})
}

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),
}})
}
}
}
Expand Down

0 comments on commit 5510564

Please sign in to comment.