github.com/stripe/stripe-go/v76@v76.25.0/card/client.go (about) 1 // 2 // 3 // File generated from our OpenAPI spec 4 // 5 // 6 7 // Package card provides the card related APIs 8 package card 9 10 import ( 11 "fmt" 12 "net/http" 13 14 stripe "github.com/stripe/stripe-go/v76" 15 "github.com/stripe/stripe-go/v76/form" 16 ) 17 18 // Client is used to invoke card related APIs. 19 type Client struct { 20 B stripe.Backend 21 Key string 22 } 23 24 // New creates a new card. 25 func New(params *stripe.CardParams) (*stripe.Card, error) { 26 return getC().New(params) 27 } 28 29 // New creates a new card. 30 func (c Client) New(params *stripe.CardParams) (*stripe.Card, error) { 31 if params == nil { 32 return nil, fmt.Errorf("params should not be nil") 33 } 34 35 var path string 36 if (params.Account != nil && params.Customer != nil) || (params.Account == nil && params.Customer == nil) { 37 return nil, fmt.Errorf("Invalid card params: exactly one of Account or Customer need to be set") 38 } else if params.Account != nil { 39 path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts", stripe.StringValue(params.Account)) 40 } else if params.Customer != nil { 41 path = stripe.FormatURLPath("/v1/customers/%s/sources", stripe.StringValue(params.Customer)) 42 } 43 44 body := &form.Values{} 45 46 // Note that we call this special append method instead of the standard one 47 // from the form package. We should not use form's because doing so will 48 // include some parameters that are undesirable here. 49 params.AppendToAsCardSourceOrExternalAccount(body, nil) 50 51 // Because card creation uses the custom append above, we have to 52 // make an explicit call using a form and CallRaw instead of the standard 53 // Call (which takes a set of parameters). 54 card := &stripe.Card{} 55 err := c.B.CallRaw(http.MethodPost, path, c.Key, body, ¶ms.Params, card) 56 return card, err 57 } 58 59 // Get returns the details of a card. 60 func Get(id string, params *stripe.CardParams) (*stripe.Card, error) { 61 return getC().Get(id, params) 62 } 63 64 // Get returns the details of a card. 65 func (c Client) Get(id string, params *stripe.CardParams) (*stripe.Card, error) { 66 if params == nil { 67 return nil, fmt.Errorf("params should not be nil") 68 } 69 70 var path string 71 if (params.Account != nil && params.Customer != nil) || (params.Account == nil && params.Customer == nil) { 72 return nil, fmt.Errorf("Invalid card params: exactly one of Account or Customer need to be set") 73 } else if params.Account != nil { 74 path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id) 75 } else if params.Customer != nil { 76 path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id) 77 } 78 79 card := &stripe.Card{} 80 err := c.B.Call(http.MethodGet, path, c.Key, params, card) 81 return card, err 82 } 83 84 // Update updates a card's properties. 85 func Update(id string, params *stripe.CardParams) (*stripe.Card, error) { 86 return getC().Update(id, params) 87 } 88 89 // Update updates a card's properties. 90 func (c Client) Update(id string, params *stripe.CardParams) (*stripe.Card, error) { 91 if params == nil { 92 return nil, fmt.Errorf("params should not be nil") 93 } 94 95 var path string 96 if (params.Account != nil && params.Customer != nil) || (params.Account == nil && params.Customer == nil) { 97 return nil, fmt.Errorf("Invalid card params: exactly one of Account or Customer need to be set") 98 } else if params.Account != nil { 99 path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id) 100 } else if params.Customer != nil { 101 path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id) 102 } 103 104 card := &stripe.Card{} 105 err := c.B.Call(http.MethodPost, path, c.Key, params, card) 106 return card, err 107 } 108 109 // Del removes a card. 110 func Del(id string, params *stripe.CardParams) (*stripe.Card, error) { 111 return getC().Del(id, params) 112 } 113 114 // Del removes a card. 115 func (c Client) Del(id string, params *stripe.CardParams) (*stripe.Card, error) { 116 if params == nil { 117 return nil, fmt.Errorf("params should not be nil") 118 } 119 120 var path string 121 if (params.Account != nil && params.Customer != nil) || (params.Account == nil && params.Customer == nil) { 122 return nil, fmt.Errorf("Invalid card params: exactly one of Account or Customer need to be set") 123 } else if params.Account != nil { 124 path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id) 125 } else if params.Customer != nil { 126 path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id) 127 } 128 129 card := &stripe.Card{} 130 err := c.B.Call(http.MethodDelete, path, c.Key, params, card) 131 return card, err 132 } 133 134 // List returns a list of cards. 135 func List(params *stripe.CardListParams) *Iter { 136 return getC().List(params) 137 } 138 139 // List returns a list of cards. 140 func (c Client) List(listParams *stripe.CardListParams) *Iter { 141 var path string 142 var outerErr error 143 144 // There's no cards list URL, so we use one sources or external 145 // accounts. An override on CardListParam's `AppendTo` will add the 146 // filter `object=card` to make sure that only cards come 147 // back with the response. 148 if listParams == nil { 149 outerErr = fmt.Errorf("params should not be nil") 150 } else if (listParams.Account != nil && listParams.Customer != nil) || (listParams.Account == nil && listParams.Customer == nil) { 151 outerErr = fmt.Errorf("Invalid card params: exactly one of Account or Customer need to be set") 152 } else if listParams.Account != nil { 153 path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts", 154 stripe.StringValue(listParams.Account)) 155 } else if listParams.Customer != nil { 156 path = stripe.FormatURLPath("/v1/customers/%s/sources", 157 stripe.StringValue(listParams.Customer)) 158 } 159 return &Iter{ 160 Iter: stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) { 161 list := &stripe.CardList{} 162 163 if outerErr != nil { 164 return nil, list, outerErr 165 } 166 167 err := c.B.CallRaw(http.MethodGet, path, c.Key, b, p, list) 168 169 ret := make([]interface{}, len(list.Data)) 170 for i, v := range list.Data { 171 ret[i] = v 172 } 173 174 return ret, list, err 175 }), 176 } 177 } 178 179 // Iter is an iterator for cards. 180 type Iter struct { 181 *stripe.Iter 182 } 183 184 // Card returns the card which the iterator is currently pointing to. 185 func (i *Iter) Card() *stripe.Card { 186 return i.Current().(*stripe.Card) 187 } 188 189 // CardList returns the current list object which the iterator is 190 // currently using. List objects will change as new API calls are made to 191 // continue pagination. 192 func (i *Iter) CardList() *stripe.CardList { 193 return i.List().(*stripe.CardList) 194 } 195 196 func getC() Client { 197 return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key} 198 }