From 6e62f7182771ec266b864036c9a7c69a4c1721ab Mon Sep 17 00:00:00 2001 From: Brandur Date: Wed, 13 Jun 2018 17:12:27 -0700 Subject: [PATCH] More checks on whether expected `params` are `nil` + some comments --- bankaccount/client.go | 27 +++++++++++++++++++++++++-- card/client.go | 3 +++ order/client.go | 4 ---- paymentsource/client.go | 24 +++++++++++++++++++++++- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/bankaccount/client.go b/bankaccount/client.go index cb1f2b93cb..a74b15936c 100644 --- a/bankaccount/client.go +++ b/bankaccount/client.go @@ -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{} @@ -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, ¶ms.Params, ba) return ba, err @@ -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) @@ -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) @@ -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) @@ -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 { diff --git a/card/client.go b/card/client.go index 29dded70cc..f0021e30c9 100644 --- a/card/client.go +++ b/card/client.go @@ -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, ¶ms.Params, card) return card, err diff --git a/order/client.go b/order/client.go index c2a2bf6a0e..f4fb4db15e 100644 --- a/order/client.go +++ b/order/client.go @@ -51,10 +51,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) diff --git a/paymentsource/client.go b/paymentsource/client.go index bdf6b8b22c..ef08afa97c 100644 --- a/paymentsource/client.go +++ b/paymentsource/client.go @@ -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") } @@ -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") } @@ -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") } @@ -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") } @@ -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", @@ -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",