From 63d6cfb08db9483db72eaf9fa3964d97c7b595cd Mon Sep 17 00:00:00 2001 From: Brandur Date: Wed, 25 Oct 2017 05:36:13 -0700 Subject: [PATCH] Make `account_holder_name`/`account_holder_type` truly optional When encoding a bank account we shouldn't send `account_holder_name` or `account_holder_type` unless they're given values. Sending an empty value will cause an API error in cases where the request would have otherwise been valid. Fixes the other part of #485. --- bankaccount.go | 13 ++++++++++-- bankaccount_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 bankaccount_test.go diff --git a/bankaccount.go b/bankaccount.go index c6f3b25d01..863cefaa05 100644 --- a/bankaccount.go +++ b/bankaccount.go @@ -71,11 +71,20 @@ func (a *BankAccountParams) AppendToAsSourceOrExternalAccount(body *form.Values) } else { body.Add(sourceType+"[object]", "bank_account") body.Add(sourceType+"[country]", a.Country) - body.Add(sourceType+"[account_holder_name]", a.AccountHolderName) - body.Add(sourceType+"[account_holder_type]", a.AccountHolderType) body.Add(sourceType+"[account_number]", a.Account) body.Add(sourceType+"[currency]", a.Currency) + // These are optional and the API will fail if we try to send empty + // values in for them, so make sure to check that they're actually set + // before encoding them. + if len(a.AccountHolderName) > 0 { + body.Add(sourceType+"[account_holder_name]", a.AccountHolderName) + } + + if len(a.AccountHolderType) > 0 { + body.Add(sourceType+"[account_holder_type]", a.AccountHolderType) + } + if len(a.Routing) > 0 { body.Add(sourceType+"[routing_number]", a.Routing) } diff --git a/bankaccount_test.go b/bankaccount_test.go new file mode 100644 index 0000000000..e2edea006c --- /dev/null +++ b/bankaccount_test.go @@ -0,0 +1,48 @@ +package stripe + +import ( + "testing" + + assert "github.com/stretchr/testify/require" + "github.com/stripe/stripe-go/form" +) + +func TestBankAccountParams_AppendToAsSourceOrExternalAccount(t *testing.T) { + // We should add more tests for all the various corner cases here ... + + // Includes account_holder_name + { + params := &BankAccountParams{AccountHolderName: "Tyrion"} + body := &form.Values{} + params.AppendToAsSourceOrExternalAccount(body) + t.Logf("body = %+v", body) + assert.Equal(t, []string{"Tyrion"}, body.Get("external_account[account_holder_name]")) + } + + // Does not include account_holder_name if empty + { + params := &BankAccountParams{} + body := &form.Values{} + params.AppendToAsSourceOrExternalAccount(body) + t.Logf("body = %+v", body) + assert.Equal(t, []string(nil), body.Get("external_account[account_holder_name]")) + } + + // Includes account_holder_name + { + params := &BankAccountParams{AccountHolderType: "individual"} + body := &form.Values{} + params.AppendToAsSourceOrExternalAccount(body) + t.Logf("body = %+v", body) + assert.Equal(t, []string{"individual"}, body.Get("external_account[account_holder_type]")) + } + + // Does not include account_holder_name if empty + { + params := &BankAccountParams{} + body := &form.Values{} + params.AppendToAsSourceOrExternalAccount(body) + t.Logf("body = %+v", body) + assert.Equal(t, []string(nil), body.Get("external_account[account_holder_type]")) + } +}