-
Notifications
You must be signed in to change notification settings - Fork 1
/
customers.go
executable file
·327 lines (270 loc) · 11 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
// Code generated by `gogenitor`. DO NOT EDIT.
package sumup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Address is 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:"line1,omitempty"`
// Second line of the address with details of the building, unit, apartment, and floor numbers.
Line2 *string `json:"line2,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 the type definition for a Customer.
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 is 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.
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.
Token *string `json:"token,omitempty"`
// Type of the payment instrument.
Type *PaymentInstrumentResponseType `json:"type,omitempty"`
}
// PaymentInstrumentResponseCard is Details of the payment card.
type PaymentInstrumentResponseCard struct {
// Last 4 digits of the payment card number.
Last4Digits *string `json:"last_4_digits,omitempty"`
// Issuing card network of the payment card.
Type *PaymentInstrumentResponseCardType `json:"type,omitempty"`
}
// Issuing card network of the payment card.
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"
)
// Type of the payment instrument.
type PaymentInstrumentResponseType string
const (
PaymentInstrumentResponseTypeCard PaymentInstrumentResponseType = "card"
)
// PersonalDetails is Personal details for the customer.
type PersonalDetails struct {
// Profile's personal address information.
Address *Address `json:"address,omitempty"`
// Date of birth of the customer.
Birthdate *time.Time `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"`
}
// CreateCustomer request body.
type CreateCustomerBody struct {
// Unique ID of the customer.
CustomerId string `json:"customer_id"`
// Personal details for the customer.
PersonalDetails *PersonalDetails `json:"personal_details,omitempty"`
}
// ListPaymentInstrumentsResponse is the type definition for a ListPaymentInstrumentsResponse.
type ListPaymentInstrumentsResponse []PaymentInstrumentResponse
// UpdateCustomer request body.
type UpdateCustomerBody struct {
// Personal details for the customer.
PersonalDetails *PersonalDetails `json:"personal_details,omitempty"`
}
// DeactivatePaymentInstrumentResponse is the type definition for a DeactivatePaymentInstrumentResponse.
type DeactivatePaymentInstrumentResponse 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()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v Customer
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// 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) (*ListPaymentInstrumentsResponse, 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()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v ListPaymentInstrumentsResponse
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// 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()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v Customer
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// 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()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v Customer
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// 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) (*DeactivatePaymentInstrumentResponse, 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()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v DeactivatePaymentInstrumentResponse
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}