Skip to content

Commit

Permalink
More checks on whether expected params are nil + some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brandur committed Jun 14, 2018
1 parent 22bc1f5 commit f085847
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
27 changes: 25 additions & 2 deletions bankaccount/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ func New(params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
}

func (c Client) New(params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

var path string
if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources", stripe.StringValue(params.Customer))
} else {
} else if params.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts", stripe.StringValue(params.Account))
} else {
return nil, errors.New("Invalid bank account params: either Customer or Account need to be set")
}

body := &form.Values{}
Expand All @@ -34,6 +40,9 @@ func (c Client) New(params *stripe.BankAccountParams) (*stripe.BankAccount, erro
// include some parameters that are undesirable here.
params.AppendToAsSourceOrExternalAccount(body)

// Because bank account creation uses the custom append above, we have to
// make an explicit call using a form and CallRaw instead of the standard
// Call (which takes a set of parameters).
ba := &stripe.BankAccount{}
err := c.B.CallRaw("POST", path, c.Key, body, &params.Params, ba)
return ba, err
Expand All @@ -45,6 +54,10 @@ func Get(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, erro
}

func (c Client) Get(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

var path string
if params != nil && params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
Expand All @@ -65,6 +78,10 @@ func Update(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, e
}

func (c Client) Update(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

var path string
if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
Expand All @@ -85,6 +102,10 @@ func Del(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, erro
}

func (c Client) Del(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

var path string
if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
Expand All @@ -108,7 +129,9 @@ func (c Client) List(listParams *stripe.BankAccountListParams) *Iter {
var path string
var outerErr error

if listParams.Customer != nil {
if listParams == nil {
outerErr = errors.New("params should not be nil")
} else if listParams.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources?object=bank_account",
stripe.StringValue(listParams.Customer))
} else if listParams.Account != nil {
Expand Down
3 changes: 3 additions & 0 deletions card/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func (c Client) New(params *stripe.CardParams) (*stripe.Card, error) {
// include some parameters that are undesirable here.
params.AppendToAsCardSourceOrExternalAccount(body, nil)

// Because card creation uses the custom append above, we have to make an
// explicit call using a form and CallRaw instead of the standard Call
// (which takes a set of parameters).
card := &stripe.Card{}
err := c.B.CallRaw("POST", path, c.Key, body, &params.Params, card)
return card, err
Expand Down
6 changes: 0 additions & 6 deletions order/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package order

import (
"errors"

stripe "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/form"
)
Expand Down Expand Up @@ -51,10 +49,6 @@ func Pay(id string, params *stripe.OrderPayParams) (*stripe.Order, error) {
// Pay pays an order
// For more details see https://stripe.com/docs/api#pay_order.
func (c Client) Pay(id string, params *stripe.OrderPayParams) (*stripe.Order, error) {
if params != nil && params.Source == nil && params.Customer == nil {
return nil, errors.New("Invalid order pay params: either customer or a source must be set")
}

path := stripe.FormatURLPath("/orders/%s/pay", id)
o := &stripe.Order{}
err := c.B.Call("POST", path, c.Key, params, o)
Expand Down
24 changes: 23 additions & 1 deletion paymentsource/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func New(params *stripe.CustomerSourceParams) (*stripe.PaymentSource, error) {
}

func (s Client) New(params *stripe.CustomerSourceParams) (*stripe.PaymentSource, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

if params.Customer == nil {
return nil, errors.New("Invalid source params: customer needs to be set")
}
Expand All @@ -38,6 +42,10 @@ func Get(id string, params *stripe.CustomerSourceParams) (*stripe.PaymentSource,
}

func (s Client) Get(id string, params *stripe.CustomerSourceParams) (*stripe.PaymentSource, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

if params.Customer == nil {
return nil, errors.New("Invalid source params: customer needs to be set")
}
Expand All @@ -55,6 +63,10 @@ func Update(id string, params *stripe.CustomerSourceParams) (*stripe.PaymentSour
}

func (s Client) Update(id string, params *stripe.CustomerSourceParams) (*stripe.PaymentSource, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

if params.Customer == nil {
return nil, errors.New("Invalid source params: customer needs to be set")
}
Expand All @@ -72,6 +84,10 @@ func Del(id string, params *stripe.CustomerSourceParams) (*stripe.PaymentSource,
}

func (s Client) Del(id string, params *stripe.CustomerSourceParams) (*stripe.PaymentSource, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

if params.Customer == nil {
return nil, errors.New("Invalid source params: customer needs to be set")
}
Expand All @@ -92,7 +108,9 @@ func (s Client) List(listParams *stripe.SourceListParams) *Iter {
var outerErr error
var path string

if listParams.Customer == nil {
if listParams == nil {
outerErr = errors.New("params should not be nil")
} else if listParams.Customer == nil {
outerErr = errors.New("Invalid source params: customer needs to be set")
} else {
path = stripe.FormatURLPath("/customers/%s/sources",
Expand Down Expand Up @@ -124,6 +142,10 @@ func Verify(id string, params *stripe.SourceVerifyParams) (*stripe.PaymentSource
}

func (s Client) Verify(id string, params *stripe.SourceVerifyParams) (*stripe.PaymentSource, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

var path string
if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s/verify",
Expand Down

0 comments on commit f085847

Please sign in to comment.