-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreaders.go
executable file
·725 lines (615 loc) · 25.6 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
// Code generated by `gogenitor`. DO NOT EDIT.
package sumup
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
)
// Affiliate: Affiliate metadata for the transaction.
// It is an optional field that allow for integrators to track the source of the transaction.
type Affiliate struct {
// Application ID of the affiliate.
// It is a unique identifier for the application and should be set by the integrator in the [Affiliate Keys](https://developer.sumup.com/affiliate-keys) page.
AppId string `json:"app_id"`
// Foreign transaction ID of the affiliate.
// It is a unique identifier for the transaction.
// It can be used later to fetch the transaction details via the [Transactions API](https://developer.sumup.com/api/transactions/get).
ForeignTransactionId string `json:"foreign_transaction_id"`
// Key of the affiliate.
// It is a unique identifier for the key and should be generated by the integrator in the [Affiliate Keys](https://developer.sumup.com/affiliate-keys) page.
//
// Format: uuid
Key string `json:"key"`
// Additional metadata for the transaction.
// It is key-value object that can be associated with the transaction.
Tags *AffiliateTags `json:"tags,omitempty"`
}
// AffiliateTags: Additional metadata for the transaction.
// It is key-value object that can be associated with the transaction.
type AffiliateTags map[string]any
// CreateReaderCheckout: Reader Checkout
type CreateReaderCheckout struct {
// Affiliate metadata for the transaction.
// It is an optional field that allow for integrators to track the source of the transaction.
Affiliate *Affiliate `json:"affiliate,omitempty"`
// The card type of the card used for the transaction.
// Is is required only for some countries (e.g: Brazil).
CardType *CreateReaderCheckoutCardType `json:"card_type,omitempty"`
// Description of the checkout to be shown in the Merchant Sales
Description *string `json:"description,omitempty"`
// Number of installments for the transaction.
// It may vary according to the merchant country.
// For example, in Brazil, the maximum number of installments is 12.
Installments *int `json:"installments,omitempty"`
// Webhook URL to which the payment result will be sent.
// It must be a HTTPS url.
// Format: uri
ReturnUrl *string `json:"return_url,omitempty"`
// List of tipping rates to be displayed to the cardholder.
// The rates are in percentage and should be between 0.01 and 0.99.
// The list should be sorted in ascending order.
TipRates *[]float64 `json:"tip_rates,omitempty"`
// Amount of the transaction.
// The amount is represented as an integer value altogether with the currency and the minor unit.
// For example, EUR 1.00 is represented as value 100 with minor unit of 2.
TotalAmount CreateReaderCheckoutAmount `json:"total_amount"`
}
// CreateReaderCheckoutCardType: The card type of the card used for the transaction.
// Is is required only for some countries (e.g: Brazil).
type CreateReaderCheckoutCardType string
const (
CreateReaderCheckoutCardTypeCredit CreateReaderCheckoutCardType = "credit"
CreateReaderCheckoutCardTypeDebit CreateReaderCheckoutCardType = "debit"
)
// CreateReaderCheckoutAmount: Amount of the transaction.
// The amount is represented as an integer value altogether with the currency and the minor unit.
// For example, EUR 1.00 is represented as value 100 with minor unit of 2.
type CreateReaderCheckoutAmount struct {
// Currency ISO 4217 code
Currency string `json:"currency"`
// The minor units of the currency. It represents the number of decimals of the currency.
// For the currencies CLP, COP and HUF, the minor unit is 0.
MinorUnit int `json:"minor_unit"`
// Total amount of the transaction.
// It must be a positive integer.
Value int `json:"value"`
}
// Meta: Set of user-defined key-value pairs attached to the object.
// Max properties: 50
type Meta map[string]any
// Reader: 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.
// Min length: 30
// Max length: 30
Id ReaderId `json:"id"`
// Set of user-defined key-value pairs attached to the object.
// Max properties: 50
Meta *Meta `json:"meta,omitempty"`
// Custom human-readable, user-defined name for easier identification of the reader.
// Max length: 500
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 resource needs to get recreated
Status ReaderStatus `json:"status"`
// Reader last-modification timestamp.
UpdatedAt time.Time `json:"updated_at"`
}
// ReaderDevice: 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"`
}
// ReaderDeviceModel: Identifier of the model of the device.
type ReaderDeviceModel string
const (
ReaderDeviceModelSolo ReaderDeviceModel = "solo"
ReaderDeviceModelVirtualSolo ReaderDeviceModel = "virtual-solo"
)
// ReaderId: 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.
//
// Min length: 30
// Max length: 30
type ReaderId string
// ReaderName: Custom human-readable, user-defined name for easier identification of the reader.
//
// Max length: 500
type ReaderName string
// ReaderPairingCode: 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.
//
// Min length: 8
// Max length: 9
type ReaderPairingCode string
// ReaderStatus: 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 resource needs to get recreated
type ReaderStatus string
const (
ReaderStatusExpired ReaderStatus = "expired"
ReaderStatusPaired ReaderStatus = "paired"
ReaderStatusProcessing ReaderStatus = "processing"
ReaderStatusUnknown ReaderStatus = "unknown"
)
// CreateReaderBody is a schema definition.
type CreateReaderBody struct {
// Set of user-defined key-value pairs attached to the object.
// Max properties: 50
Meta *Meta `json:"meta,omitempty"`
// Custom human-readable, user-defined name for easier identification of the reader.
// Max length: 500
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.
// Min length: 8
// Max length: 9
PairingCode ReaderPairingCode `json:"pairing_code"`
}
// CreateReaderCheckoutBody: Reader Checkout
type CreateReaderCheckoutBody struct {
// Affiliate metadata for the transaction.
// It is an optional field that allow for integrators to track the source of the transaction.
Affiliate *Affiliate `json:"affiliate,omitempty"`
// The card type of the card used for the transaction.
// Is is required only for some countries (e.g: Brazil).
CardType *CreateReaderCheckoutBodyCardType `json:"card_type,omitempty"`
// Description of the checkout to be shown in the Merchant Sales
Description *string `json:"description,omitempty"`
// Number of installments for the transaction.
// It may vary according to the merchant country.
// For example, in Brazil, the maximum number of installments is 12.
Installments *int `json:"installments,omitempty"`
// Webhook URL to which the payment result will be sent.
// It must be a HTTPS url.
// Format: uri
ReturnUrl *string `json:"return_url,omitempty"`
// List of tipping rates to be displayed to the cardholder.
// The rates are in percentage and should be between 0.01 and 0.99.
// The list should be sorted in ascending order.
TipRates *[]float64 `json:"tip_rates,omitempty"`
// Amount of the transaction.
// The amount is represented as an integer value altogether with the currency and the minor unit.
// For example, EUR 1.00 is represented as value 100 with minor unit of 2.
TotalAmount CreateReaderCheckoutAmount `json:"total_amount"`
}
// CreateReaderCheckoutBodyCardType: The card type of the card used for the transaction.
// Is is required only for some countries (e.g: Brazil).
type CreateReaderCheckoutBodyCardType string
const (
CreateReaderCheckoutBodyCardTypeCredit CreateReaderCheckoutBodyCardType = "credit"
CreateReaderCheckoutBodyCardTypeDebit CreateReaderCheckoutBodyCardType = "debit"
)
// UpdateReaderBody is a schema definition.
type UpdateReaderBody struct {
// Set of user-defined key-value pairs attached to the object.
// Max properties: 50
Meta *Meta `json:"meta,omitempty"`
// Custom human-readable, user-defined name for easier identification of the reader.
// Max length: 500
Name *ReaderName `json:"name,omitempty"`
}
// GetReaderParams: query parameters for GetReader
type GetReaderParams struct {
// Return the reader only if it has been modified after the specified timestamp given in the headers.
//
// Timestamps are accepted in the following formats:
// - HTTP Standard: [IMF format (RFC 5322)](https://www.rfc-editor.org/rfc/rfc5322#section-3.3), sometimes also
// referred to as [RFC 7231](https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1).
// - RFC 3339: Used for timestamps in JSON payloads on this API.
IfModifiedSince *string
}
// QueryValues converts [GetReaderParams] into [url.Values].
func (p *GetReaderParams) QueryValues() url.Values {
q := make(url.Values)
if p.IfModifiedSince != nil {
q.Set("If-Modified-Since", *p.IfModifiedSince)
}
return q
}
// ListReaders200Response is a schema definition.
type ListReaders200Response struct {
Items []Reader `json:"items"`
}
var _ error = (*CreateReaderTerminate422Response)(nil)
// CreateReaderTerminate422Response is a schema definition.
type CreateReaderTerminate422Response struct {
Errors *CreateReaderTerminate422ResponseErrors `json:"errors,omitempty"`
}
// CreateReaderTerminate422ResponseErrors is a schema definition.
type CreateReaderTerminate422ResponseErrors map[string]any
func (e *CreateReaderTerminate422Response) Error() string {
return fmt.Sprintf("errors=%v", e.Errors)
}
var _ error = (*CreateReaderTerminate500Response)(nil)
// CreateReaderTerminate500Response is a schema definition.
type CreateReaderTerminate500Response struct {
Errors *CreateReaderTerminate500ResponseErrors `json:"errors,omitempty"`
}
// CreateReaderTerminate500ResponseErrors is a schema definition.
type CreateReaderTerminate500ResponseErrors struct {
Detail *string `json:"detail,omitempty"`
}
func (e *CreateReaderTerminate500Response) Error() string {
return fmt.Sprintf("errors=%v", e.Errors)
}
var _ error = (*CreateReaderTerminate502Response)(nil)
// CreateReaderTerminate502Response is a schema definition.
type CreateReaderTerminate502Response struct {
Errors *CreateReaderTerminate502ResponseErrors `json:"errors,omitempty"`
}
// CreateReaderTerminate502ResponseErrors is a schema definition.
type CreateReaderTerminate502ResponseErrors struct {
Detail *string `json:"detail,omitempty"`
}
func (e *CreateReaderTerminate502Response) Error() string {
return fmt.Sprintf("errors=%v", e.Errors)
}
var _ error = (*CreateReaderTerminate504Response)(nil)
// CreateReaderTerminate504Response is a schema definition.
type CreateReaderTerminate504Response struct {
Errors *CreateReaderTerminate504ResponseErrors `json:"errors,omitempty"`
}
// CreateReaderTerminate504ResponseErrors is a schema definition.
type CreateReaderTerminate504ResponseErrors struct {
Detail *string `json:"detail,omitempty"`
}
func (e *CreateReaderTerminate504Response) Error() string {
return fmt.Sprintf("errors=%v", e.Errors)
}
// CreateReaderCheckout201Response is a schema definition.
type CreateReaderCheckout201Response struct {
Data *CreateReaderCheckout201ResponseData `json:"data,omitempty"`
}
// CreateReaderCheckout201ResponseData is a schema definition.
type CreateReaderCheckout201ResponseData struct {
// The client transaction ID is a unique identifier for the transaction that is generated for the client.
// It can be used later to fetch the transaction details via the [Transactions API](https://developer.sumup.com/api/transactions/get).
// Format: uuid
ClientTransactionId *string `json:"client_transaction_id,omitempty"`
}
var _ error = (*CreateReaderCheckout400Response)(nil)
// CreateReaderCheckout400Response is a schema definition.
type CreateReaderCheckout400Response struct {
Errors *CreateReaderCheckout400ResponseErrors `json:"errors,omitempty"`
}
// CreateReaderCheckout400ResponseErrors is a schema definition.
type CreateReaderCheckout400ResponseErrors struct {
Detail *string `json:"detail,omitempty"`
}
func (e *CreateReaderCheckout400Response) Error() string {
return fmt.Sprintf("errors=%v", e.Errors)
}
var _ error = (*CreateReaderCheckout422Response)(nil)
// CreateReaderCheckout422Response is a schema definition.
type CreateReaderCheckout422Response struct {
Errors *CreateReaderCheckout422ResponseErrors `json:"errors,omitempty"`
}
// CreateReaderCheckout422ResponseErrors is a schema definition.
type CreateReaderCheckout422ResponseErrors map[string]any
func (e *CreateReaderCheckout422Response) Error() string {
return fmt.Sprintf("errors=%v", e.Errors)
}
var _ error = (*CreateReaderCheckout500Response)(nil)
// CreateReaderCheckout500Response is a schema definition.
type CreateReaderCheckout500Response struct {
Errors *CreateReaderCheckout500ResponseErrors `json:"errors,omitempty"`
}
// CreateReaderCheckout500ResponseErrors is a schema definition.
type CreateReaderCheckout500ResponseErrors struct {
Detail *string `json:"detail,omitempty"`
}
func (e *CreateReaderCheckout500Response) Error() string {
return fmt.Sprintf("errors=%v", e.Errors)
}
var _ error = (*CreateReaderCheckout502Response)(nil)
// CreateReaderCheckout502Response is a schema definition.
type CreateReaderCheckout502Response struct {
Errors *CreateReaderCheckout502ResponseErrors `json:"errors,omitempty"`
}
// CreateReaderCheckout502ResponseErrors is a schema definition.
type CreateReaderCheckout502ResponseErrors struct {
Detail *string `json:"detail,omitempty"`
}
func (e *CreateReaderCheckout502Response) Error() string {
return fmt.Sprintf("errors=%v", e.Errors)
}
var _ error = (*CreateReaderCheckout504Response)(nil)
// CreateReaderCheckout504Response is a schema definition.
type CreateReaderCheckout504Response struct {
Errors *CreateReaderCheckout504ResponseErrors `json:"errors,omitempty"`
}
// CreateReaderCheckout504ResponseErrors is a schema definition.
type CreateReaderCheckout504ResponseErrors struct {
Detail *string `json:"detail,omitempty"`
}
func (e *CreateReaderCheckout504Response) Error() string {
return fmt.Sprintf("errors=%v", e.Errors)
}
type ReadersService service
// List: List Readers
// Returns list of all readers of the merchant.
func (s *ReadersService) List(ctx context.Context, merchantCode string) (*ListReaders200Response, 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()
switch resp.StatusCode {
case http.StatusOK:
var v ListReaders200Response
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// Create: Create a Reader
// Create a new reader linked to the merchant account.
func (s *ReadersService) Create(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()
switch resp.StatusCode {
case http.StatusCreated:
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
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// TerminateCheckout: Create a Reader Terminate action
// Create a Terminate action for a Reader.
//
// It stops the current transaction on the target device.
//
// This process is asynchronous and the actual termination may take some time to be performed on the device.
//
// There are some caveats when using this endpoint:
// * The target device must be online, otherwise terminate won't be accepted
// * The action will succeed only if the device is waiting for cardholder action: e.g: waiting for card, waiting
// for PIN, etc.
// * There is no confirmation of the termination.
//
// If a transaction is successfully terminated and `return_url` was provided on Checkout, the transaction status
// will be sent as `failed` to the provided URL.
//
// **Note**: If the target device is a Solo, it must be in version 3.3.28.0 or higher.
func (s *ReadersService) TerminateCheckout(ctx context.Context, merchantCode string, id string) error {
path := fmt.Sprintf("/v0.1/merchants/%v/readers/%v/terminate", merchantCode, id)
req, err := s.client.NewRequest(ctx, http.MethodPost, 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()
switch resp.StatusCode {
case http.StatusAccepted:
return nil
case http.StatusUnprocessableEntity:
var apiErr CreateReaderTerminate422Response
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return fmt.Errorf("read error response: %s", err.Error())
}
return &apiErr
case http.StatusInternalServerError:
var apiErr CreateReaderTerminate500Response
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return fmt.Errorf("read error response: %s", err.Error())
}
return &apiErr
case http.StatusBadGateway:
var apiErr CreateReaderTerminate502Response
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return fmt.Errorf("read error response: %s", err.Error())
}
return &apiErr
case http.StatusGatewayTimeout:
var apiErr CreateReaderTerminate504Response
if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil {
return fmt.Errorf("read error response: %s", err.Error())
}
return &apiErr
default:
return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// CreateCheckout: Create a Reader Checkout
// Create a Checkout for a Reader.
//
// This process is asynchronous and the actual transaction may take some time to be stared on the device.
//
// There are some caveats when using this endpoint:
// * The target device must be online, otherwise checkout won't be accepted
// * After the checkout is accepted, the system has 60 seconds to start the payment on the target device. During
// this time, any other checkout for the same device will be rejected.
//
// **Note**: If the target device is a Solo, it must be in version 3.3.24.3 or higher.
func (s *ReadersService) CreateCheckout(ctx context.Context, merchantCode string, id string, body CreateReaderCheckoutBody) (*CreateReaderCheckout201Response, 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/%v/checkout", merchantCode, id)
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 CreateReaderCheckout201Response
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusBadRequest:
var apiErr CreateReaderCheckout400Response
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.StatusUnprocessableEntity:
var apiErr CreateReaderCheckout422Response
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.StatusInternalServerError:
var apiErr CreateReaderCheckout500Response
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.StatusBadGateway:
var apiErr CreateReaderCheckout502Response
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.StatusGatewayTimeout:
var apiErr CreateReaderCheckout504Response
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))
}
}
// 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()
switch resp.StatusCode {
case http.StatusOK:
return nil
default:
return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// Get: Retrieve a Reader
// Gets a Reader.
func (s *ReadersService) Get(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)
}
req.URL.RawQuery = params.QueryValues().Encode()
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 Reader
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusBadRequest:
return nil, errors.New("Malformed request.")
case http.StatusNotFound:
return nil, errors.New("The requested Reader resource does not exists.")
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// Update: Update a Reader
// Updates a Reader.
func (s *ReadersService) Update(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()
switch resp.StatusCode {
case http.StatusNotModified:
return errors.New("The update request was successful, but the reader didn't get updated.")
case http.StatusForbidden:
return errors.New("The reader is not linked to the merchant account.")
default:
return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}