Skip to content

Commit

Permalink
Merge pull request #705 from stripe/remi-add-person
Browse files Browse the repository at this point in the history
Add support for the Person resource
  • Loading branch information
remi-stripe authored Oct 31, 2018
2 parents b91932a + 13085fb commit bdbd67e
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ cache:
env:
global:
# If changing this number, please also change it in `testing/testing.go`.
- STRIPE_MOCK_VERSION=0.33.0
- STRIPE_MOCK_VERSION=0.35.0

go:
- "1.8"
Expand Down
32 changes: 16 additions & 16 deletions bankaccount/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
)

func TestBankAccountDel_ByAccount(t *testing.T) {
bankAcount, err := Del("ba_123", &stripe.BankAccountParams{
bankAccount, err := Del("ba_123", &stripe.BankAccountParams{
Account: stripe.String("acct_123"),
})
assert.Nil(t, err)
assert.NotNil(t, bankAcount)
assert.NotNil(t, bankAccount)
}

func TestBankAccountDel_ByCustomer(t *testing.T) {
bankAcount, err := Del("ba_123", &stripe.BankAccountParams{
bankAccount, err := Del("ba_123", &stripe.BankAccountParams{
Customer: stripe.String("cus_123"),
})
assert.Nil(t, err)
assert.NotNil(t, bankAcount)
assert.NotNil(t, bankAccount)
}

func TestBankAccountGet_ByAccount(t *testing.T) {
bankAcount, err := Get("ba_123", &stripe.BankAccountParams{Account: stripe.String("acct_123")})
bankAccount, err := Get("ba_123", &stripe.BankAccountParams{Account: stripe.String("acct_123")})
assert.Nil(t, err)
assert.NotNil(t, bankAcount)
assert.NotNil(t, bankAccount)
}

func TestBankAccountGet_ByCustomer(t *testing.T) {
bankAcount, err := Get("ba_123", &stripe.BankAccountParams{Customer: stripe.String("cus_123")})
bankAccount, err := Get("ba_123", &stripe.BankAccountParams{Customer: stripe.String("cus_123")})
assert.Nil(t, err)
assert.NotNil(t, bankAcount)
assert.NotNil(t, bankAccount)
}

func TestBankAccountList_ByCustomer(t *testing.T) {
Expand All @@ -46,38 +46,38 @@ func TestBankAccountList_ByCustomer(t *testing.T) {
}

func TestBankAccountNew_ByAccount(t *testing.T) {
bankAcount, err := New(&stripe.BankAccountParams{
bankAccount, err := New(&stripe.BankAccountParams{
Account: stripe.String("acct_123"),
DefaultForCurrency: stripe.Bool(true),
Token: stripe.String("tok_123"),
})
assert.Nil(t, err)
assert.NotNil(t, bankAcount)
assert.NotNil(t, bankAccount)
}

func TestBankAccountNew_ByCustomer(t *testing.T) {
bankAcount, err := New(&stripe.BankAccountParams{
bankAccount, err := New(&stripe.BankAccountParams{
Customer: stripe.String("cus_123"),
Token: stripe.String("tok_123"),
})
assert.Nil(t, err)
assert.NotNil(t, bankAcount)
assert.NotNil(t, bankAccount)
}

func TestBankAccountUpdate_ByAccount(t *testing.T) {
bankAcount, err := Update("ba_123", &stripe.BankAccountParams{
bankAccount, err := Update("ba_123", &stripe.BankAccountParams{
Account: stripe.String("acct_123"),
DefaultForCurrency: stripe.Bool(true),
})
assert.Nil(t, err)
assert.NotNil(t, bankAcount)
assert.NotNil(t, bankAccount)
}

func TestBankAccountUpdate_ByCustomer(t *testing.T) {
bankAcount, err := Update("ba_123", &stripe.BankAccountParams{
bankAccount, err := Update("ba_123", &stripe.BankAccountParams{
AccountHolderName: stripe.String("New Name"),
Customer: stripe.String("cus_123"),
})
assert.Nil(t, err)
assert.NotNil(t, bankAcount)
assert.NotNil(t, bankAccount)
}
6 changes: 5 additions & 1 deletion issuing/cardholder/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func TestIssuingCardholderNew(t *testing.T) {

func TestIssuingCardholderUpdate(t *testing.T) {
cardholder, err := Update("ich_123", &stripe.IssuingCardholderParams{
Name: stripe.String("Updated name"),
Params: stripe.Params{
Metadata: map[string]string{
"foo": "bar",
},
},
})
assert.Nil(t, err)
assert.NotNil(t, cardholder)
Expand Down
1 change: 1 addition & 0 deletions issuing_cardholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type IssuingCardholderStatus string
const (
IssuingCardholderStatusActive IssuingCardholderStatus = "active"
IssuingCardholderStatusInactive IssuingCardholderStatus = "inactive"
IssuingCardholderStatusPending IssuingCardholderStatus = "pending"
)

// IssuingCardholderType is the type of an issuing cardholder.
Expand Down
7 changes: 1 addition & 6 deletions order/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ func TestOrderPay(t *testing.T) {
}

func TestOrderReturn(t *testing.T) {
order, err := Return("or_123", &stripe.OrderReturnParams{
Items: []*stripe.OrderItemParams{
{Amount: stripe.Int64(1), Description: stripe.String("Item 1")},
{Amount: stripe.Int64(1), Description: stripe.String("Item 2")},
},
})
order, err := Return("or_123", &stripe.OrderReturnParams{})
assert.Nil(t, err)
assert.NotNil(t, order)
}
Expand Down
124 changes: 124 additions & 0 deletions person.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package stripe

import "encoding/json"

// RelationshipParams is used to set the relationship between an account and a person.
type RelationshipParams struct {
AccountOpener *bool `form:"account_opener"`
Director *bool `form:"director"`
Executive *bool `form:"executive"`
Owner *bool `form:"owner"`
PercentOwnership *float64 `form:"percent_ownership"`
Title *string `form:"title"`
}

// PersonParams is the set of parameters that can be used when creating or updating a person.
// For more details see https://stripe.com/docs/api#create_person.
type PersonParams struct {
Params `form:"*"`
Account *string `form:"-"` // Included in URL
Address *AccountAddressParams `form:"address"`
AddressKana *AccountAddressParams `form:"address_kana"`
AddressKanji *AccountAddressParams `form:"address_kanji"`
DOB *DOBParams `form:"dob"`
Email *string `form:"email"`
FirstName *string `form:"first_name"`
FirstNameKana *string `form:"first_name_kana"`
FirstNameKanji *string `form:"first_name_kanji"`
Gender *string `form:"gender"`
IDNumber *string `form:"id_number"`
LastName *string `form:"last_name"`
LastNameKana *string `form:"last_name_kana"`
LastNameKanji *string `form:"last_name_kanji"`
MaidenName *string `form:"maiden_name"`
Phone *string `form:"phone"`
Relationship *RelationshipParams `form:"relationship"`
SSNLast4 *string `form:"ssn_last_4"`
}

// RelationshipListParams is used to filter persons by the relationship
type RelationshipListParams struct {
AccountOpener *bool `form:"account_opener"`
Director *bool `form:"director"`
Executive *bool `form:"executive"`
Owner *bool `form:"owner"`
}

// PersonListParams is the set of parameters that can be used when listing persons.
// For more detail see https://stripe.com/docs/api#list_persons.
type PersonListParams struct {
ListParams `form:"*"`
Account *string `form:"-"` // Included in URL
Relationship *RelationshipListParams `form:"relationship"`
}

// Relationship represents extra information needed for a Person.
type Relationship struct {
AccountOpener bool `json:"account_opener"`
Director bool `json:"director"`
Executive bool `json:"executive"`
Owner bool `json:"owner"`
PercentOwnership float64 `json:"percent_ownership"`
Title string `json:"title"`
}

// Requirements represents what's missing to verify a Person.
type Requirements struct {
CurrentlyDue []string `json:"currently_due"`
EventuallyDue []string `json:"eventually_due"`
PastDue []string `json:"past_due"`
}

// Person is the resource representing a Stripe person.
// For more details see https://stripe.com/docs/api#persons.
type Person struct {
Account string `json:"account"`
Address *AccountAddress `json:"address"`
AddressKana *AccountAddress `json:"address_kana"`
AddressKanji *AccountAddress `json:"address_kanji"`
Deleted bool `json:"deleted"`
DOB *DOB `json:"dob"`
Email string `json:"email"`
FirstName string `json:"first_name"`
FirstNameKana string `json:"first_name_kana"`
FirstNameKanji string `json:"first_name_kanji"`
Gender string `json:"gender"`
ID string `json:"id"`
IDNumberProvided bool `json:"id_number_provided"`
LastName string `json:"last_name"`
LastNameKana string `json:"last_name_kana"`
LastNameKanji string `json:"last_name_kanji"`
MaidenName string `json:"maiden_name"`
Metadata map[string]string `json:"metadata"`
Object string `json:"object"`
Phone string `json:"phone"`
Relationship *Relationship `json:"relationship"`
Requirements *Requirements `json:"requirements"`
SSNLast4Provided bool `json:"ssn_last_4_provided"`
Verification *IdentityVerification `json:"verification"`
}

// PersonList is a list of persons as retrieved from a list endpoint.
type PersonList struct {
ListMeta
Data []*Person `json:"data"`
}

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

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

*c = Person(v)
return nil
}
111 changes: 111 additions & 0 deletions person/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Package person provides the /accounts/persons APIs
package person

import (
"fmt"
"net/http"

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

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

// New creates a new account person.
func New(params *stripe.PersonParams) (*stripe.Person, error) {
return getC().New(params)
}

// New creates a new account person.
func (c Client) New(params *stripe.PersonParams) (*stripe.Person, error) {
path := stripe.FormatURLPath("/accounts/%s/persons", stripe.StringValue(params.Account))
person := &stripe.Person{}
err := c.B.Call(http.MethodPost, path, c.Key, params, person)
return person, err
}

// Get returns the details of a account person.
func Get(id string, params *stripe.PersonParams) (*stripe.Person, error) {
return getC().Get(id, params)
}

// Get returns the details of a account person.
func (c Client) Get(id string, params *stripe.PersonParams) (*stripe.Person, error) {
if params == nil {
return nil, fmt.Errorf("params cannot be nil, and params.Account must be set")
}

path := stripe.FormatURLPath("/accounts/%s/persons/%s",
stripe.StringValue(params.Account), id)
person := &stripe.Person{}
err := c.B.Call(http.MethodGet, path, c.Key, params, person)
return person, err
}

// Update updates a account person's properties.
func Update(id string, params *stripe.PersonParams) (*stripe.Person, error) {
return getC().Update(id, params)
}

// Update updates a account person's properties.
func (c Client) Update(id string, params *stripe.PersonParams) (*stripe.Person, error) {
path := stripe.FormatURLPath("/accounts/%s/persons/%s",
stripe.StringValue(params.Account), id)
person := &stripe.Person{}
err := c.B.Call(http.MethodPost, path, c.Key, params, person)
return person, err
}

// Del removes a person.
func Del(id string, params *stripe.PersonParams) (*stripe.Person, error) {
return getC().Del(id, params)
}

// Del removes a person.
func (c Client) Del(id string, params *stripe.PersonParams) (*stripe.Person, error) {
path := stripe.FormatURLPath("/accounts/%s/persons/%s",
stripe.StringValue(params.Account), id)
person := &stripe.Person{}
err := c.B.Call(http.MethodDelete, path, c.Key, params, person)
return person, err
}

// List returns a list of account persons.
func List(params *stripe.PersonListParams) *Iter {
return getC().List(params)
}

// List returns a list of account persons.
func (c Client) List(listParams *stripe.PersonListParams) *Iter {
path := stripe.FormatURLPath("/accounts/%s/persons", stripe.StringValue(listParams.Account))

return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.PersonList{}
err := c.B.CallRaw(http.MethodGet, path, 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 account persons.
type Iter struct {
*stripe.Iter
}

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

func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}
Loading

0 comments on commit bdbd67e

Please sign in to comment.