Skip to content

Commit

Permalink
Add support for TaxRate resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Apr 24, 2019
1 parent de0fb2f commit db8217f
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
4 changes: 4 additions & 0 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/stripe/stripe-go/subschedule"
"github.com/stripe/stripe-go/subschedulerevision"
"github.com/stripe/stripe-go/taxid"
"github.com/stripe/stripe-go/taxrate"
terminalconnectiontoken "github.com/stripe/stripe-go/terminal/connectiontoken"
terminallocation "github.com/stripe/stripe-go/terminal/location"
terminalreader "github.com/stripe/stripe-go/terminal/reader"
Expand Down Expand Up @@ -178,6 +179,8 @@ type API struct {
SubscriptionScheduleRevisions *subschedulerevision.Client
// TaxIDs is the client used to invoke /tax_ids APIs.
TaxIDs *taxid.Client
// TaxRates is the client used to invoke /tax_rates APIs.
TaxRates *taxrate.Client
// TerminalConnectionTokens is the client used to invoke /terminal/connectiontokens related APIs.
TerminalConnectionTokens *terminalconnectiontoken.Client
// TerminalLocations is the client used to invoke /terminal/locations related APIs.
Expand Down Expand Up @@ -262,6 +265,7 @@ func (a *API) Init(key string, backends *stripe.Backends) {
a.SubscriptionSchedules = &subschedule.Client{B: backends.API, Key: key}
a.SubscriptionScheduleRevisions = &subschedulerevision.Client{B: backends.API, Key: key}
a.TaxIDs = &taxid.Client{B: backends.API, Key: key}
a.TaxRates = &taxrate.Client{B: backends.API, Key: key}
a.TerminalConnectionTokens = &terminalconnectiontoken.Client{B: backends.API, Key: key}
a.TerminalLocations = &terminallocation.Client{B: backends.API, Key: key}
a.TerminalReaders = &terminalreader.Client{B: backends.API, Key: key}
Expand Down
74 changes: 74 additions & 0 deletions taxrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package stripe

import "encoding/json"

// TaxRateParams is the set of parameters that can be used when creating a tax rate.
// For more details see https://stripe.com/docs/api/tax_rates/create.
type TaxRateParams struct {
Params `form:"*"`
Active *bool `form:"active"`
Description *string `form:"description"`
DisplayName *string `form:"display_name"`
Inclusive *bool `form:"inclusive"`
Jurisdiction *string `form:"jurisdiction"`
Percentage *float64 `form:"percentage"`
}

// TaxRatePercentageRangeQueryParams are used to filter tax rates by specific percentage values.
type TaxRatePercentageRangeQueryParams struct {
GreaterThan float64 `form:"gt"`
GreaterThanOrEqual float64 `form:"gte"`
LesserThan float64 `form:"lt"`
LesserThanOrEqual float64 `form:"lte"`
}

// TaxRateListParams is the set of parameters that can be used when listing tax rates.
// For more detail see https://stripe.com/docs/api/tax_rates/list.
type TaxRateListParams struct {
ListParams `form:"*"`
Active *bool `form:"active"`
Created *int64 `form:"created"`
CreatedRange *RangeQueryParams `form:"created"`
Inclusive *bool `form:"inclusive"`
Percentage *float64 `form:"percentage"`
PercentageRanger *TaxRatePercentageRangeQueryParams `form:"percentage"`
}

// TaxRate is the resource representing a Stripe tax rate.
// For more details see https://stripe.com/docs/api/tax_rates/object.
type TaxRate struct {
Active bool `json:"active"`
Created int64 `json:"created"`
Description string `json:"description"`
DisplayName string `json:"display_name"`
ID string `json:"id"`
Jurisdiction string `json:"jurisdiction"`
Livemode bool `json:"livemode"`
Metadata map[string]string `json:"metadata"`
Percentage float64 `json:"percent_off"`
}

// TaxRateList is a list of tax rates as retrieved from a list endpoint.
type TaxRateList struct {
ListMeta
Data []*TaxRate `json:"data"`
}

// UnmarshalJSON handles deserialization of a TaxRate.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
func (c *TaxRate) UnmarshalJSON(data []byte) error {
if id, ok := ParseID(data); ok {
c.ID = id
return nil
}

type taxrate TaxRate
var v taxrate
if err := json.Unmarshal(data, &v); err != nil {
return err
}

*c = TaxRate(v)
return nil
}
100 changes: 100 additions & 0 deletions taxrate/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Package taxrate provides the /tax_rates APIs
package taxrate

import (
"net/http"

stripe "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/form"
)

// Client is used to invoke /tax_rates APIs.
type Client struct {
B stripe.Backend
Key string
}

// New creates a new tr.
func New(params *stripe.TaxRateParams) (*stripe.TaxRate, error) {
return getC().New(params)
}

// New creates a new tr.
func (c Client) New(params *stripe.TaxRateParams) (*stripe.TaxRate, error) {
tr := &stripe.TaxRate{}
err := c.B.Call(http.MethodPost, "/v1/tax_rates", c.Key, params, tr)
return tr, err
}

// Get returns the details of a tax rate.
func Get(id string, params *stripe.TaxRateParams) (*stripe.TaxRate, error) {
return getC().Get(id, params)
}

// Get returns the details of a tax rate.
func (c Client) Get(id string, params *stripe.TaxRateParams) (*stripe.TaxRate, error) {
path := stripe.FormatURLPath("/v1/tax_rates/%s", id)
tr := &stripe.TaxRate{}
err := c.B.Call(http.MethodGet, path, c.Key, params, tr)
return tr, err
}

// Update updates a tax rate's properties.
func Update(id string, params *stripe.TaxRateParams) (*stripe.TaxRate, error) {
return getC().Update(id, params)
}

// Update updates a tax rate's properties.
func (c Client) Update(id string, params *stripe.TaxRateParams) (*stripe.TaxRate, error) {
path := stripe.FormatURLPath("/v1/tax_rates/%s", id)
tr := &stripe.TaxRate{}
err := c.B.Call(http.MethodPost, path, c.Key, params, tr)
return tr, err
}

// Del removes a tax rate.
func Del(id string, params *stripe.TaxRateParams) (*stripe.TaxRate, error) {
return getC().Del(id, params)
}

// Del removes a tax rate.
func (c Client) Del(id string, params *stripe.TaxRateParams) (*stripe.TaxRate, error) {
path := stripe.FormatURLPath("/v1/tax_rates/%s", id)
tr := &stripe.TaxRate{}
err := c.B.Call(http.MethodDelete, path, c.Key, params, tr)
return tr, err
}

// List returns a list of trs.
func List(params *stripe.TaxRateListParams) *Iter {
return getC().List(params)
}

// List returns a list of trs.
func (c Client) List(listParams *stripe.TaxRateListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.TaxRateList{}
err := c.B.CallRaw(http.MethodGet, "/v1/tax_rates", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
ret[i] = v
}

return ret, list.ListMeta, err
})}
}

// Iter is an iterator for trs.
type Iter struct {
*stripe.Iter
}

// TaxRate returns the tr which the iterator is currently pointing to.
func (i *Iter) TaxRate() *stripe.TaxRate {
return i.Current().(*stripe.TaxRate)
}

func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}
46 changes: 46 additions & 0 deletions taxrate/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package taxrate

import (
"testing"

assert "github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go"
_ "github.com/stripe/stripe-go/testing"
)

func TestTaxRateGet(t *testing.T) {
tr, err := Get("txr_123", nil)
assert.Nil(t, err)
assert.NotNil(t, tr)
}

func TestTaxRateList(t *testing.T) {
i := List(&stripe.TaxRateListParams{})

// Verify that we can get at least one tr
assert.True(t, i.Next())
assert.Nil(t, i.Err())
assert.NotNil(t, i.TaxRate())
}

func TestTaxRateNew(t *testing.T) {
tr, err := New(&stripe.TaxRateParams{
DisplayName: stripe.String("name"),
Inclusive: stripe.Bool(false),
Percentage: stripe.Float64(10.15),
})
assert.Nil(t, err)
assert.NotNil(t, tr)
}

func TestTaxRateUpdate(t *testing.T) {
tr, err := Update("txr_123", &stripe.TaxRateParams{
Params: stripe.Params{
Metadata: map[string]string{
"foo": "bar",
},
},
})
assert.Nil(t, err)
assert.NotNil(t, tr)
}

0 comments on commit db8217f

Please sign in to comment.