-
Notifications
You must be signed in to change notification settings - Fork 1
/
readers.go
executable file
·308 lines (252 loc) · 9.73 KB
/
readers.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
// Code generated by `gogenitor`. DO NOT EDIT.
package sumup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Meta is Set of user-defined key-value pairs attached to the object.
type Meta struct {
}
// Reader is A physical card reader device that can accept in-person payments.
type Reader struct {
// Reader creation timestamp.
CreatedAt time.Time `json:"created_at"`
// Information about the underlying physical device.
Device ReaderDevice `json:"device"`
// Unique identifier of the object.
//
// Note that this identifies the instance of the physical devices pairing with your SumUp account.
//
// If you DELETE a reader, and pair the device again, the ID will be different. Do not use this ID to refer to a physical device.
Id ReaderId `json:"id"`
// Set of user-defined key-value pairs attached to the object.
Meta *Meta `json:"meta,omitempty"`
// Custom human-readable, user-defined name for easier identification of the reader.
Name ReaderName `json:"name"`
// The status of the reader object gives information about the current state of the reader.
// Possible values:
// * `unknown` - The reader status is unknown.
// * `processing` - The reader is created and waits for the physical device to confirm the pairing.
// * `paired` - The reader is paired with a merchant account and can be used with SumUp APIs.
// * `expired` - The pairing is expired and no longer usable with the account. The ressource needs to get recreated
Status ReaderStatus `json:"status"`
// Reader last-modification timestamp.
UpdatedAt time.Time `json:"updated_at"`
}
// ReaderDevice is Information about the underlying physical device.
type ReaderDevice struct {
// A unique identifier of the physical device (e.g. serial number).
Identifier string `json:"identifier"`
// Identifier of the model of the device.
Model ReaderDeviceModel `json:"model"`
}
// Identifier of the model of the device.
type ReaderDeviceModel string
const (
ReaderDeviceModelSolo ReaderDeviceModel = "solo"
ReaderDeviceModelVirtualSolo ReaderDeviceModel = "virtual-solo"
)
// ReaderId is Unique identifier of the object.
//
// Note that this identifies the instance of the physical devices pairing with your SumUp account.
//
// If you DELETE a reader, and pair the device again, the ID will be different. Do not use this ID to refer to a physical device.
type ReaderId string
// ReaderName is Custom human-readable, user-defined name for easier identification of the reader.
type ReaderName string
// ReaderPairingCode is The pairing code is a 8 or 9 character alphanumeric string that is displayed on a SumUp Device after initiating the pairing.
// It is used to link the physical device to the created pairing.
type ReaderPairingCode string
// The status of the reader object gives information about the current state of the reader.
// Possible values:
// * `unknown` - The reader status is unknown.
// * `processing` - The reader is created and waits for the physical device to confirm the pairing.
// * `paired` - The reader is paired with a merchant account and can be used with SumUp APIs.
// * `expired` - The pairing is expired and no longer usable with the account. The ressource needs to get recreated
type ReaderStatus string
const (
ReaderStatusExpired ReaderStatus = "expired"
ReaderStatusPaired ReaderStatus = "paired"
ReaderStatusProcessing ReaderStatus = "processing"
ReaderStatusUnknown ReaderStatus = "unknown"
)
// ListReadersResponse is the type definition for a ListReadersResponse.
type ListReadersResponse struct {
Items []Reader `json:"items"`
}
// CreateReader request body.
type CreateReaderBody struct {
// Set of user-defined key-value pairs attached to the object.
Meta *Meta `json:"meta,omitempty"`
// Custom human-readable, user-defined name for easier identification of the reader.
Name *ReaderName `json:"name,omitempty"`
// The pairing code is a 8 or 9 character alphanumeric string that is displayed on a SumUp Device after initiating the pairing.
// It is used to link the physical device to the created pairing.
PairingCode ReaderPairingCode `json:"pairing_code"`
}
// GetReaderParams are query parameters for GetReader
type GetReaderParams struct {
IfModifiedSince *string `json:"If-Modified-Since,omitempty"`
}
// UpdateReader request body.
type UpdateReaderBody struct {
// Set of user-defined key-value pairs attached to the object.
Meta *Meta `json:"meta,omitempty"`
// Custom human-readable, user-defined name for easier identification of the reader.
Name *ReaderName `json:"name,omitempty"`
}
type ReadersService service
// ListReaders: List Readers
// Returns list of all readers of the merchant.
func (s *ReadersService) ListReaders(ctx context.Context, merchantCode string) (*ListReadersResponse, error) {
path := fmt.Sprintf("/v0.1/merchants/%v/readers", merchantCode)
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 ListReadersResponse
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// CreateReader: Create a Reader
// Create a new reader linked to the merchant account.
func (s *ReadersService) CreateReader(ctx context.Context, merchantCode string, body CreateReaderBody) (*Reader, 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/merchants/%v/readers", merchantCode)
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 Reader
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// DeleteReader: Delete a reader
// Deletes a Reader.
func (s *ReadersService) DeleteReader(ctx context.Context, merchantCode string, id ReaderId) error {
path := fmt.Sprintf("/v0.1/merchants/%v/readers/%v", merchantCode, id)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, http.NoBody)
if err != nil {
return fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return 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 fmt.Errorf("read error response: %s", err.Error())
}
return &apiErr
}
return nil
}
// GetReader: Retrieve a Reader
// Gets a Reader.
func (s *ReadersService) GetReader(ctx context.Context, merchantCode string, id ReaderId, params GetReaderParams) (*Reader, error) {
path := fmt.Sprintf("/v0.1/merchants/%v/readers/%v", merchantCode, id)
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 Reader
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// UpdateReader: Update a Reader
// Updates a Reader.
func (s *ReadersService) UpdateReader(ctx context.Context, merchantCode string, id ReaderId, body UpdateReaderBody) error {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return fmt.Errorf("encoding json body request failed: %v", err)
}
path := fmt.Sprintf("/v0.1/merchants/%v/readers/%v", merchantCode, id)
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, buf)
if err != nil {
return fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return 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 fmt.Errorf("read error response: %s", err.Error())
}
return &apiErr
}
return nil
}