1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"fmt" |
7
|
|
|
"time" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// BalancesService allows you to retrieve real-time as well as historical |
11
|
|
|
// information about your Mollie balance. |
12
|
|
|
// |
13
|
|
|
// Works with Organization access tokens and App access tokens. |
14
|
|
|
// |
15
|
|
|
// The API is in **BETA** so be careful and expect changes. |
16
|
|
|
// |
17
|
|
|
// See: https://docs.mollie.com/reference/v2/balances-api/overview |
18
|
|
|
type BalancesService service |
19
|
|
|
|
20
|
|
|
// BalanceStatus reflects whether a balance is operational or not. |
21
|
|
|
type BalanceStatus string |
22
|
|
|
|
23
|
|
|
// Possible values for type BalanceStatus. |
24
|
|
|
const ( |
25
|
|
|
BalanceActive BalanceStatus = "active" |
26
|
|
|
BalanceInactive BalanceStatus = "inactive" |
27
|
|
|
) |
28
|
|
|
|
29
|
|
|
// TransferFrequency reflects the frequency at which the available amount |
30
|
|
|
// on the balance will be settled to the configured transfer destination. |
31
|
|
|
type TransferFrequency string |
32
|
|
|
|
33
|
|
|
// Possible values for type TransferFrequency. |
34
|
|
|
const ( |
35
|
|
|
TransferDaily TransferFrequency = "daily" |
36
|
|
|
TransferTwiceAWeek TransferFrequency = "twice-a-week" |
37
|
|
|
TransferEveryMonday TransferFrequency = "every-monday" |
38
|
|
|
TransferEveryTuesday TransferFrequency = "every-tuesday" |
39
|
|
|
TransferEveryWednesday TransferFrequency = "every-wednesday" |
40
|
|
|
TransferEveryThursday TransferFrequency = "every-thursday" |
41
|
|
|
TransferEveryFriday TransferFrequency = "every-friday" |
42
|
|
|
TransferTwiceAMonth TransferFrequency = "twice-a-month" |
43
|
|
|
TransferMonthly TransferFrequency = "monthly" |
44
|
|
|
TransferNever TransferFrequency = "never" |
45
|
|
|
) |
46
|
|
|
|
47
|
|
|
// TransferDestination where the available amount will be automatically |
48
|
|
|
// transferred. |
49
|
|
|
type TransferDestination struct { |
50
|
|
|
Type string `json:"type,omitempty"` |
51
|
|
|
BankAccount string `json:"bankAccount,omitempty"` |
52
|
|
|
BeneficiaryName string `json:"beneficiaryName,omitempty"` |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// BalanceLinks holds URL objects relevant to the balance. |
56
|
|
|
type BalanceLinks struct { |
57
|
|
|
Self *URL `json:"self,omitempty"` |
58
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Balance holds the payments processed with Mollie once fees have been deducted. |
62
|
|
|
type Balance struct { |
63
|
|
|
ID string `json:"id,omitempty"` |
64
|
|
|
Resource string `json:"resource,omitempty"` |
65
|
|
|
Currency string `json:"currency,omitempty"` |
66
|
|
|
TransferReference string `json:"transferReference,omitempty"` |
67
|
|
|
Status BalanceStatus `json:"status,omitempty"` |
68
|
|
|
TransferFrequency TransferFrequency `json:"transferFrequency,omitempty"` |
69
|
|
|
TransferThreshold *Amount `json:"transferThreshold,omitempty"` |
70
|
|
|
AvailableAmount *Amount `json:"availableAmount,omitempty"` |
71
|
|
|
PendingAmount *Amount `json:"pendingAmount,omitempty"` |
72
|
|
|
TransferDestination *TransferDestination `json:"transferDestination,omitempty"` |
73
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
74
|
|
|
Links BalanceLinks `json:"_links,omitempty"` |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// BalancesList describes a list of captures. |
78
|
|
|
type BalancesList struct { |
79
|
|
|
Count int `json:"count,omitempty"` |
80
|
|
|
Embedded struct { |
81
|
|
|
Balances []*Balance `json:"balances,omitempty"` |
82
|
|
|
} `json:"_embedded,omitempty"` |
83
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// BalanceListOptions contains valid query parameters |
87
|
|
|
// for the list balances endpoint. |
88
|
|
|
type BalanceListOptions struct { |
89
|
|
|
Currency string `url:"currency,omitempty"` |
90
|
|
|
From string `url:"from,omitempty"` |
91
|
|
|
Limit int `url:"limit,omitempty"` |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// BalanceGroupingFormat defines a grouping mechanism for transactions included |
95
|
|
|
// in a balance report. |
96
|
|
|
type BalanceGroupingFormat string |
97
|
|
|
|
98
|
|
|
// Supported transaction grouping. |
99
|
|
|
const ( |
100
|
|
|
StatusBalancesGrouping BalanceGroupingFormat = "status-balances" |
101
|
|
|
TransactionCategoriesGrouping BalanceGroupingFormat = "transaction-categories" |
102
|
|
|
) |
103
|
|
|
|
104
|
|
|
// BalanceReportOptions contains valid query parameters |
105
|
|
|
// for the list balances endpoint. |
106
|
|
|
type BalanceReportOptions struct { |
107
|
|
|
Grouping string `url:"grouping,omitempty"` |
108
|
|
|
From *ShortDate `url:"from,omitempty"` |
109
|
|
|
Until *ShortDate `url:"until,omitempty"` |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Subtotal balance descriptor. |
113
|
|
|
type Subtotal struct { |
114
|
|
|
TransactionType string `json:"transactionType,omitempty"` |
115
|
|
|
Count int `json:"count,omitempty"` |
116
|
|
|
Amount *Amount `json:"amount,omitempty"` |
117
|
|
|
Subtotals []*Subtotal `json:"subtotals,omitempty"` |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// BalanceReportLinks holds URL objects relevant to the balance report. |
121
|
|
|
type BalanceReportLinks struct { |
122
|
|
|
Self *URL `json:"self,omitempty"` |
123
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
type BalanceReport struct { |
127
|
|
|
Resource string |
128
|
|
|
BalanceID string |
129
|
|
|
TimeZone string |
130
|
|
|
From *ShortDate |
131
|
|
|
Until *ShortDate |
132
|
|
|
Grouping BalanceGroupingFormat |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// GetBalance retrieves a balance by its id. |
136
|
|
|
func (bs *BalancesService) Get(ctx context.Context, balance string) (res *Response, b *Balance, err error) { |
137
|
1 |
|
return bs.get(ctx, balance) |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Primary retrieves the primary balance. This is the balance of your account’s |
141
|
|
|
// primary currency, where all payments are settled to by default. |
142
|
|
|
func (bs *BalancesService) Primary(ctx context.Context) (res *Response, b *Balance, err error) { |
143
|
|
|
const id = "primary" |
144
|
|
|
return bs.get(ctx, id) |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// List retrieves all the organization’s balances, including the primary |
148
|
|
|
// balance, ordered from newest to oldest. |
149
|
|
|
func (bs *BalancesService) List(ctx context.Context, options *BalanceListOptions) (res *Response, bl *BalancesList, err error) { |
150
|
|
|
return bs.list(ctx, "v2/balances", options) |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
func (bs *BalancesService) get(ctx context.Context, balance string) (res *Response, b *Balance, err error) { |
154
|
1 |
|
u := fmt.Sprintf("v2/balances/%s", balance) |
155
|
|
|
|
156
|
1 |
|
res, err = bs.client.get(ctx, u, nil) |
157
|
1 |
|
if err != nil { |
158
|
|
|
return |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
if err = json.Unmarshal(res.content, &b); err != nil { |
162
|
|
|
return |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
return |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
func (bs *BalancesService) list(ctx context.Context, uri string, options interface{}) (res *Response, lb *BalancesList, err error) { |
169
|
|
|
res, err = bs.client.get(ctx, uri, options) |
170
|
|
|
if err != nil { |
171
|
|
|
return |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if err = json.Unmarshal(res.content, &lb); err != nil { |
175
|
|
|
return |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return |
179
|
|
|
} |
180
|
|
|
|