-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcustomers.go
executable file
·400 lines (342 loc) · 13.5 KB
/
customers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Code generated by `go-sdk-gen`. DO NOT EDIT.
package sumup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Address: Profile's personal address information.
type Address struct {
// City name from the address.
City *string `json:"city,omitempty"`
// Two letter country code formatted according to [ISO3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
Country string `json:"country"`
// First line of the address with details of the street name and number.
Line1 *string `json:"line_1,omitempty"`
// Second line of the address with details of the building, unit, apartment, and floor numbers.
Line2 *string `json:"line_2,omitempty"`
// Postal code from the address.
PostalCode *string `json:"postal_code,omitempty"`
// State name or abbreviation from the address.
State *string `json:"state,omitempty"`
}
// Customer is a schema definition.
type Customer struct {
// Unique ID of the customer.
CustomerId string `json:"customer_id"`
// Personal details for the customer.
PersonalDetails *PersonalDetails `json:"personal_details,omitempty"`
}
// PaymentInstrumentResponse: Payment Instrument Response
type PaymentInstrumentResponse struct {
// Indicates whether the payment instrument is active and can be used for payments. To deactivate it, send a
// `DELETE` request to the resource endpoint.
// Read only
// Default: true
Active *bool `json:"active,omitempty"`
// Details of the payment card.
Card *PaymentInstrumentResponseCard `json:"card,omitempty"`
// Creation date of payment instrument. Response format expressed according to [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) code.
CreatedAt *time.Time `json:"created_at,omitempty"`
// Created mandate
Mandate *MandateResponse `json:"mandate,omitempty"`
// Unique token identifying the saved payment card for a customer.
// Read only
Token *string `json:"token,omitempty"`
// Type of the payment instrument.
Type *PaymentInstrumentResponseType `json:"type,omitempty"`
}
// PaymentInstrumentResponseCard: Details of the payment card.
type PaymentInstrumentResponseCard struct {
// Last 4 digits of the payment card number.
// Read only
// Min length: 4
// Max length: 4
Last4Digits *string `json:"last_4_digits,omitempty"`
// Issuing card network of the payment card.
// Read only
Type *PaymentInstrumentResponseCardType `json:"type,omitempty"`
}
// PaymentInstrumentResponseCardType: Issuing card network of the payment card.
// Read only
type PaymentInstrumentResponseCardType string
const (
PaymentInstrumentResponseCardTypeAmex PaymentInstrumentResponseCardType = "AMEX"
PaymentInstrumentResponseCardTypeCup PaymentInstrumentResponseCardType = "CUP"
PaymentInstrumentResponseCardTypeDiners PaymentInstrumentResponseCardType = "DINERS"
PaymentInstrumentResponseCardTypeDiscover PaymentInstrumentResponseCardType = "DISCOVER"
PaymentInstrumentResponseCardTypeElo PaymentInstrumentResponseCardType = "ELO"
PaymentInstrumentResponseCardTypeElv PaymentInstrumentResponseCardType = "ELV"
PaymentInstrumentResponseCardTypeHipercard PaymentInstrumentResponseCardType = "HIPERCARD"
PaymentInstrumentResponseCardTypeJcb PaymentInstrumentResponseCardType = "JCB"
PaymentInstrumentResponseCardTypeMaestro PaymentInstrumentResponseCardType = "MAESTRO"
PaymentInstrumentResponseCardTypeMastercard PaymentInstrumentResponseCardType = "MASTERCARD"
PaymentInstrumentResponseCardTypeUnknown PaymentInstrumentResponseCardType = "UNKNOWN"
PaymentInstrumentResponseCardTypeVisa PaymentInstrumentResponseCardType = "VISA"
PaymentInstrumentResponseCardTypeVisaElectron PaymentInstrumentResponseCardType = "VISA_ELECTRON"
PaymentInstrumentResponseCardTypeVisaVpay PaymentInstrumentResponseCardType = "VISA_VPAY"
)
// PaymentInstrumentResponseType: Type of the payment instrument.
type PaymentInstrumentResponseType string
const (
PaymentInstrumentResponseTypeCard PaymentInstrumentResponseType = "card"
)
// PersonalDetails: Personal details for the customer.
type PersonalDetails struct {
// Profile's personal address information.
Address *Address `json:"address,omitempty"`
// Date of birth of the customer.
// Format: date
Birthdate *Date `json:"birthdate,omitempty"`
// Email address of the customer.
Email *string `json:"email,omitempty"`
// First name of the customer.
FirstName *string `json:"first_name,omitempty"`
// Last name of the customer.
LastName *string `json:"last_name,omitempty"`
// Phone number of the customer.
Phone *string `json:"phone,omitempty"`
}
// CreateCustomerBody is a schema definition.
type CreateCustomerBody struct {
// Unique ID of the customer.
CustomerId string `json:"customer_id"`
// Personal details for the customer.
PersonalDetails *PersonalDetails `json:"personal_details,omitempty"`
}
// UpdateCustomerBody is a schema definition.
type UpdateCustomerBody struct {
// Personal details for the customer.
PersonalDetails *PersonalDetails `json:"personal_details,omitempty"`
}
// ListPaymentInstruments200Response is a schema definition.
type ListPaymentInstruments200Response []PaymentInstrumentResponse
// DeactivatePaymentInstrument204Response is a schema definition.
type DeactivatePaymentInstrument204Response struct {
}
type CustomersService service
// Create: Create a customer
// Creates a new saved customer resource which you can later manipulate and save payment instruments to.
func (s *CustomersService) Create(ctx context.Context, body CreateCustomerBody) (*Customer, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, fmt.Errorf("encoding json body request failed: %v", err)
}
path := fmt.Sprintf("/v0.1/customers")
req, err := s.client.NewRequest(ctx, http.MethodPost, path, buf)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusCreated:
var v Customer
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusUnauthorized:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusForbidden:
var apiErr ErrorForbidden
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusConflict:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// ListPaymentInstruments: List payment instruments
// Lists all payment instrument resources that are saved for an identified customer.
func (s *CustomersService) ListPaymentInstruments(ctx context.Context, customerId string) (*ListPaymentInstruments200Response, error) {
path := fmt.Sprintf("/v0.1/customers/%v/payment-instruments", customerId)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
var v ListPaymentInstruments200Response
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusUnauthorized:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusForbidden:
var apiErr ErrorForbidden
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusNotFound:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// Get: Retrieve a customer
// Retrieves an identified saved customer resource through the unique `customer_id` parameter, generated upon
// customer creation.
func (s *CustomersService) Get(ctx context.Context, customerId string) (*Customer, error) {
path := fmt.Sprintf("/v0.1/customers/%v", customerId)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
var v Customer
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusUnauthorized:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusForbidden:
var apiErr ErrorForbidden
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusNotFound:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// Update: Update a customer
// Updates an identified saved customer resource's personal details.
//
// The request only overwrites the parameters included in the request, all other parameters will remain with
// their initially assigned values.
func (s *CustomersService) Update(ctx context.Context, customerId string, body UpdateCustomerBody) (*Customer, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, fmt.Errorf("encoding json body request failed: %v", err)
}
path := fmt.Sprintf("/v0.1/customers/%v", customerId)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, buf)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
var v Customer
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusUnauthorized:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusForbidden:
var apiErr ErrorForbidden
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusNotFound:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// DeactivatePaymentInstrument: Deactivate a payment instrument
// Deactivates an identified card payment instrument resource for a customer.
func (s *CustomersService) DeactivatePaymentInstrument(ctx context.Context, customerId string, token string) (*DeactivatePaymentInstrument204Response, error) {
path := fmt.Sprintf("/v0.1/customers/%v/payment-instruments/%v", customerId, token)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusNoContent:
var v DeactivatePaymentInstrument204Response
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusUnauthorized:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusForbidden:
var apiErr ErrorForbidden
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
case http.StatusNotFound:
var apiErr Error
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}