-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.go
123 lines (95 loc) · 4.13 KB
/
collection.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
package swervpay
import (
"context"
"net/http"
)
// CollectionHistory represents the history of a collection.
type CollectionHistory struct {
Amount float64 `json:"amount"` // The amount of the collection.
Charges float64 `json:"charges"` // The charges associated with the collection.
CreatedAt string `json:"created_at"` // The creation date of the collection.
Currency string `json:"currency"` // The currency of the collection.
ID string `json:"id"` // The ID of the collection.
PaymentMethod string `json:"payment_method"` // The payment method used for the collection.
Reference string `json:"reference"` // The reference of the collection.
UpdatedAt string `json:"updated_at"` // The last update date of the collection.
}
// CreateCollectionBody represents the body of a create collection request.
type CreateCollectionBody struct {
CustomerID string `json:"customer_id"` // The ID of the customer.
Currency string `json:"currency"` // The currency of the collection.
MerchantName string `json:"merchant_name"` // The name of the merchant.
Amount float64 `json:"amount"` // The amount of the collection.
Type string `json:"type"` // The type of the collection.
}
// CollectionInt is an interface that defines the operations that can be performed on collections.
type CollectionInt interface {
Gets(ctx context.Context, query *PageAndLimitQuery) (*[]Wallet, error) // Gets a list of wallets.
Get(ctx context.Context, id string) (*Wallet, error) // Gets a specific wallet.
Create(ctx context.Context, body *CreateCollectionBody) (*Wallet, error) // Creates a new wallet.
Transactions(ctx context.Context, id string, query *PageAndLimitQuery) (*[]CollectionHistory, error) // Gets the transactions of a specific wallet.
}
// CollectionIntImpl is an implementation of the CollectionInt interface.
type CollectionIntImpl struct {
client *SwervpayClient // The client used to interact with the Swervpay API.
}
// Verify that CollectionIntImpl implements CollectionInt.
var _ CollectionInt = &CollectionIntImpl{}
// Gets retrieves a list of wallets.
// https://docs.swervpay.co/api-reference/collections/get-all-collections
func (c CollectionIntImpl) Gets(ctx context.Context, query *PageAndLimitQuery) (*[]Wallet, error) {
path := GenerateURLPath("collections", query)
req, err := c.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
response := new([]Wallet)
_, err = c.client.Perform(req, response)
if err != nil {
return nil, err
}
return response, nil
}
// Get retrieves a specific wallet.
// https://docs.swervpay.co/api-reference/collections/get
func (c CollectionIntImpl) Get(ctx context.Context, id string) (*Wallet, error) {
req, err := c.client.NewRequest(ctx, http.MethodGet, "collections/"+id, nil)
if err != nil {
return nil, err
}
response := new(Wallet)
_, err = c.client.Perform(req, response)
if err != nil {
return nil, err
}
return response, nil
}
// Create creates a new wallet.
// https://docs.swervpay.co/api-reference/collections/create
func (c CollectionIntImpl) Create(ctx context.Context, body *CreateCollectionBody) (*Wallet, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, "collections", body)
if err != nil {
return nil, err
}
response := new(Wallet)
_, err = c.client.Perform(req, response)
if err != nil {
return nil, err
}
return response, nil
}
// Transactions retrieves the transactions of a specific wallet.
// https://docs.swervpay.co/api-reference/collections/transaction
func (c CollectionIntImpl) Transactions(ctx context.Context, id string, query *PageAndLimitQuery) (*[]CollectionHistory, error) {
path := GenerateURLPath("collections/"+id+"/transactions", query)
req, err := c.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
response := new([]CollectionHistory)
_, err = c.client.Perform(req, response)
if err != nil {
return nil, err
}
return response, nil
}