github.com/stripe/stripe-go/v76@v76.25.0/customer/client.go (about) 1 // 2 // 3 // File generated from our OpenAPI spec 4 // 5 // 6 7 // Package customer provides the /customers APIs 8 package customer 9 10 import ( 11 "net/http" 12 13 stripe "github.com/stripe/stripe-go/v76" 14 "github.com/stripe/stripe-go/v76/form" 15 ) 16 17 // Client is used to invoke /customers APIs. 18 type Client struct { 19 B stripe.Backend 20 Key string 21 } 22 23 // New creates a new customer. 24 func New(params *stripe.CustomerParams) (*stripe.Customer, error) { 25 return getC().New(params) 26 } 27 28 // New creates a new customer. 29 func (c Client) New(params *stripe.CustomerParams) (*stripe.Customer, error) { 30 customer := &stripe.Customer{} 31 err := c.B.Call(http.MethodPost, "/v1/customers", c.Key, params, customer) 32 return customer, err 33 } 34 35 // Get returns the details of a customer. 36 func Get(id string, params *stripe.CustomerParams) (*stripe.Customer, error) { 37 return getC().Get(id, params) 38 } 39 40 // Get returns the details of a customer. 41 func (c Client) Get(id string, params *stripe.CustomerParams) (*stripe.Customer, error) { 42 path := stripe.FormatURLPath("/v1/customers/%s", id) 43 customer := &stripe.Customer{} 44 err := c.B.Call(http.MethodGet, path, c.Key, params, customer) 45 return customer, err 46 } 47 48 // Update updates a customer's properties. 49 func Update(id string, params *stripe.CustomerParams) (*stripe.Customer, error) { 50 return getC().Update(id, params) 51 } 52 53 // Update updates a customer's properties. 54 func (c Client) Update(id string, params *stripe.CustomerParams) (*stripe.Customer, error) { 55 path := stripe.FormatURLPath("/v1/customers/%s", id) 56 customer := &stripe.Customer{} 57 err := c.B.Call(http.MethodPost, path, c.Key, params, customer) 58 return customer, err 59 } 60 61 // Del removes a customer. 62 func Del(id string, params *stripe.CustomerParams) (*stripe.Customer, error) { 63 return getC().Del(id, params) 64 } 65 66 // Del removes a customer. 67 func (c Client) Del(id string, params *stripe.CustomerParams) (*stripe.Customer, error) { 68 path := stripe.FormatURLPath("/v1/customers/%s", id) 69 customer := &stripe.Customer{} 70 err := c.B.Call(http.MethodDelete, path, c.Key, params, customer) 71 return customer, err 72 } 73 74 // CreateFundingInstructions is the method for the `POST /v1/customers/{customer}/funding_instructions` API. 75 func CreateFundingInstructions(id string, params *stripe.CustomerCreateFundingInstructionsParams) (*stripe.FundingInstructions, error) { 76 return getC().CreateFundingInstructions(id, params) 77 } 78 79 // CreateFundingInstructions is the method for the `POST /v1/customers/{customer}/funding_instructions` API. 80 func (c Client) CreateFundingInstructions(id string, params *stripe.CustomerCreateFundingInstructionsParams) (*stripe.FundingInstructions, error) { 81 path := stripe.FormatURLPath("/v1/customers/%s/funding_instructions", id) 82 fundinginstructions := &stripe.FundingInstructions{} 83 err := c.B.Call(http.MethodPost, path, c.Key, params, fundinginstructions) 84 return fundinginstructions, err 85 } 86 87 // DeleteDiscount is the method for the `DELETE /v1/customers/{customer}/discount` API. 88 func DeleteDiscount(id string, params *stripe.CustomerDeleteDiscountParams) (*stripe.Customer, error) { 89 return getC().DeleteDiscount(id, params) 90 } 91 92 // DeleteDiscount is the method for the `DELETE /v1/customers/{customer}/discount` API. 93 func (c Client) DeleteDiscount(id string, params *stripe.CustomerDeleteDiscountParams) (*stripe.Customer, error) { 94 path := stripe.FormatURLPath("/v1/customers/%s/discount", id) 95 customer := &stripe.Customer{} 96 err := c.B.Call(http.MethodDelete, path, c.Key, params, customer) 97 return customer, err 98 } 99 100 // RetrievePaymentMethod is the method for the `GET /v1/customers/{customer}/payment_methods/{payment_method}` API. 101 func RetrievePaymentMethod(id string, params *stripe.CustomerRetrievePaymentMethodParams) (*stripe.PaymentMethod, error) { 102 return getC().RetrievePaymentMethod(id, params) 103 } 104 105 // RetrievePaymentMethod is the method for the `GET /v1/customers/{customer}/payment_methods/{payment_method}` API. 106 func (c Client) RetrievePaymentMethod(id string, params *stripe.CustomerRetrievePaymentMethodParams) (*stripe.PaymentMethod, error) { 107 path := stripe.FormatURLPath( 108 "/v1/customers/%s/payment_methods/%s", 109 stripe.StringValue(params.Customer), 110 id, 111 ) 112 paymentmethod := &stripe.PaymentMethod{} 113 err := c.B.Call(http.MethodGet, path, c.Key, params, paymentmethod) 114 return paymentmethod, err 115 } 116 117 // List returns a list of customers. 118 func List(params *stripe.CustomerListParams) *Iter { 119 return getC().List(params) 120 } 121 122 // List returns a list of customers. 123 func (c Client) List(listParams *stripe.CustomerListParams) *Iter { 124 return &Iter{ 125 Iter: stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) { 126 list := &stripe.CustomerList{} 127 err := c.B.CallRaw(http.MethodGet, "/v1/customers", c.Key, b, p, list) 128 129 ret := make([]interface{}, len(list.Data)) 130 for i, v := range list.Data { 131 ret[i] = v 132 } 133 134 return ret, list, err 135 }), 136 } 137 } 138 139 // Iter is an iterator for customers. 140 type Iter struct { 141 *stripe.Iter 142 } 143 144 // Customer returns the customer which the iterator is currently pointing to. 145 func (i *Iter) Customer() *stripe.Customer { 146 return i.Current().(*stripe.Customer) 147 } 148 149 // CustomerList returns the current list object which the iterator is 150 // currently using. List objects will change as new API calls are made to 151 // continue pagination. 152 func (i *Iter) CustomerList() *stripe.CustomerList { 153 return i.List().(*stripe.CustomerList) 154 } 155 156 // ListPaymentMethods is the method for the `GET /v1/customers/{customer}/payment_methods` API. 157 func ListPaymentMethods(params *stripe.CustomerListPaymentMethodsParams) *PaymentMethodIter { 158 return getC().ListPaymentMethods(params) 159 } 160 161 // ListPaymentMethods is the method for the `GET /v1/customers/{customer}/payment_methods` API. 162 func (c Client) ListPaymentMethods(listParams *stripe.CustomerListPaymentMethodsParams) *PaymentMethodIter { 163 path := stripe.FormatURLPath( 164 "/v1/customers/%s/payment_methods", 165 stripe.StringValue(listParams.Customer), 166 ) 167 return &PaymentMethodIter{ 168 Iter: stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) { 169 list := &stripe.PaymentMethodList{} 170 err := c.B.CallRaw(http.MethodGet, path, c.Key, b, p, list) 171 172 ret := make([]interface{}, len(list.Data)) 173 for i, v := range list.Data { 174 ret[i] = v 175 } 176 177 return ret, list, err 178 }), 179 } 180 } 181 182 // PaymentMethodIter is an iterator for payment methods. 183 type PaymentMethodIter struct { 184 *stripe.Iter 185 } 186 187 // PaymentMethod returns the payment method which the iterator is currently pointing to. 188 func (i *PaymentMethodIter) PaymentMethod() *stripe.PaymentMethod { 189 return i.Current().(*stripe.PaymentMethod) 190 } 191 192 // PaymentMethodList returns the current list object which the iterator is 193 // currently using. List objects will change as new API calls are made to 194 // continue pagination. 195 func (i *PaymentMethodIter) PaymentMethodList() *stripe.PaymentMethodList { 196 return i.List().(*stripe.PaymentMethodList) 197 } 198 199 // Search returns a search result containing customers. 200 func Search(params *stripe.CustomerSearchParams) *SearchIter { 201 return getC().Search(params) 202 } 203 204 // Search returns a search result containing customers. 205 func (c Client) Search(params *stripe.CustomerSearchParams) *SearchIter { 206 return &SearchIter{ 207 SearchIter: stripe.GetSearchIter(params, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.SearchContainer, error) { 208 list := &stripe.CustomerSearchResult{} 209 err := c.B.CallRaw(http.MethodGet, "/v1/customers/search", c.Key, b, p, list) 210 211 ret := make([]interface{}, len(list.Data)) 212 for i, v := range list.Data { 213 ret[i] = v 214 } 215 216 return ret, list, err 217 }), 218 } 219 } 220 221 // SearchIter is an iterator for customers. 222 type SearchIter struct { 223 *stripe.SearchIter 224 } 225 226 // Customer returns the customer which the iterator is currently pointing to. 227 func (i *SearchIter) Customer() *stripe.Customer { 228 return i.Current().(*stripe.Customer) 229 } 230 231 // CustomerSearchResult returns the current list object which the iterator is 232 // currently using. List objects will change as new API calls are made to 233 // continue pagination. 234 func (i *SearchIter) CustomerSearchResult() *stripe.CustomerSearchResult { 235 return i.SearchResult().(*stripe.CustomerSearchResult) 236 } 237 238 func getC() Client { 239 return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key} 240 }