-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for TaxRate resource and APIs
- Loading branch information
1 parent
de0fb2f
commit db8217f
Showing
4 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |