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
|
|
|
// BalanceReport contains the common fields between |
127
|
|
|
// different balance grouping options. |
128
|
|
|
type BalanceReport struct { |
129
|
|
|
Resource string `json:"resource,omitempty"` |
130
|
|
|
BalanceID string `json:"balanceId,omitempty"` |
131
|
|
|
TimeZone string `json:"timeZone,omitempty"` |
132
|
|
|
From *ShortDate `json:"from,omitempty"` |
133
|
|
|
Until *ShortDate `json:"until,omitempty"` |
134
|
|
|
Grouping BalanceGroupingFormat `json:"grouping,omitempty"` |
135
|
|
|
Links BalanceReportLinks `json:"_links,omitempty"` |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// BalanceAmount wraps the std amount type. |
139
|
|
|
type BalanceAmount struct { |
140
|
|
|
Amount *Amount `json:"amount,omitempty"` |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// BalanceReportDetail contains the breakdown |
144
|
|
|
// categories when grouping balance transactions. |
145
|
|
|
type BalanceReportDetail struct { |
146
|
|
|
Open *BalanceAmount `json:"open,omitempty"` |
147
|
|
|
Pending *BalanceAmount `json:"pending,omitempty"` |
148
|
|
|
MovedToAvailable *BalanceAmount `json:"movedToAvailable,omitempty"` |
149
|
|
|
ImmediatelyAvailable *BalanceAmount `json:"immediatelyAvailable,omitempty"` |
150
|
|
|
Close *BalanceAmount `json:"close,omitempty"` |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// GroupingReportStatusBalances contains the per status |
154
|
|
|
// grouped balances. |
155
|
|
|
// |
156
|
|
|
// It embeds all the fields in BalanceReport. |
157
|
|
|
type GroupingReportStatusBalances struct { |
158
|
|
|
BalanceReport |
159
|
|
|
PendingBalance *BalanceReportDetail `json:"pendingBalance,omitempty"` |
160
|
|
|
AvailableBalance *BalanceReportDetail `json:"availableBalance,omitempty"` |
161
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// GroupingReportCategoryBalances contains the per category |
165
|
|
|
// grouped balances. |
166
|
|
|
// |
167
|
|
|
// It embeds all the fields in BalanceReport. |
168
|
|
|
type GroupingReportCategoryBalances struct { |
169
|
|
|
BalanceReport |
170
|
|
|
Open *BalanceReportDetail `json:"open,omitempty"` |
171
|
|
|
Payments *BalanceReportDetail `json:"payments,omitempty"` |
172
|
|
|
Refunds *BalanceReportDetail `json:"refunds,omitempty"` |
173
|
|
|
Chargebacks *BalanceReportDetail `json:"chargebacks,omitempty"` |
174
|
|
|
Capital *BalanceReportDetail `json:"capital,omitempty"` |
175
|
|
|
Transfers *BalanceReportDetail `json:"transfers,omitempty"` |
176
|
|
|
FeePrePayments *BalanceReportDetail `json:"fee-prepayments,omitempty"` |
177
|
|
|
Corrections *BalanceReportDetail `json:"corrections,omitempty"` |
178
|
|
|
Close *BalanceReportDetail `json:"close,omitempty"` |
179
|
|
|
Links *PaginationLinks `json:"_links,omitempty"` |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// GetBalance retrieves a balance by its id. |
183
|
|
|
// |
184
|
|
|
// See: https://docs.mollie.com/reference/v2/balances-api/get-balance |
185
|
|
|
func (bs *BalancesService) Get(ctx context.Context, balance string) (res *Response, b *Balance, err error) { |
186
|
1 |
|
return bs.get(ctx, balance) |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// Primary retrieves the primary balance. This is the balance of your account’s |
190
|
|
|
// primary currency, where all payments are settled to by default. |
191
|
|
|
// |
192
|
|
|
// See: https://docs.mollie.com/reference/v2/balances-api/get-primary-balance |
193
|
|
|
func (bs *BalancesService) Primary(ctx context.Context) (res *Response, b *Balance, err error) { |
194
|
|
|
const id = "primary" |
195
|
|
|
return bs.get(ctx, id) |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// List retrieves all the organization’s balances, including the primary |
199
|
|
|
// balance, ordered from newest to oldest. |
200
|
|
|
// |
201
|
|
|
// See: https://docs.mollie.com/reference/v2/balances-api/list-balances |
202
|
|
|
func (bs *BalancesService) List(ctx context.Context, options *BalanceListOptions) (res *Response, bl *BalancesList, err error) { |
203
|
|
|
return bs.list(ctx, "v2/balances", options) |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// GetReport returns the balance report for the specified balance id. |
207
|
|
|
// |
208
|
|
|
// See: https://docs.mollie.com/reference/v2/balances-api/get-balance-report |
209
|
|
|
func (bs *BalancesService) GetReport(ctx context.Context, balance string, options *BalanceReportOptions) (res *Response, br *BalanceReport, err error) { |
210
|
|
|
return bs.getReport(ctx, balance, options) |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// GetPrimaryReport returns the report for the primary balance. |
214
|
|
|
// |
215
|
|
|
// See: https://docs.mollie.com/reference/v2/balances-api/get-primary-balance-report |
216
|
|
|
func (bs *BalancesService) GetPrimaryReport(ctx context.Context, options *BalanceReportOptions) (res *Response, br *BalanceReport, err error) { |
217
|
|
|
return bs.getReport(ctx, "primary", options) |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
func (bs *BalancesService) get(ctx context.Context, balance string) (res *Response, b *Balance, err error) { |
221
|
1 |
|
u := fmt.Sprintf("v2/balances/%s", balance) |
222
|
|
|
|
223
|
1 |
|
res, err = bs.client.get(ctx, u, nil) |
224
|
1 |
|
if err != nil { |
225
|
|
|
return |
226
|
|
|
} |
227
|
|
|
|
228
|
1 |
|
if err = json.Unmarshal(res.content, &b); err != nil { |
229
|
|
|
return |
230
|
|
|
} |
231
|
|
|
|
232
|
1 |
|
return |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
func (bs *BalancesService) list(ctx context.Context, uri string, options interface{}) (res *Response, lb *BalancesList, err error) { |
236
|
|
|
res, err = bs.client.get(ctx, uri, options) |
237
|
|
|
if err != nil { |
238
|
|
|
return |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
if err = json.Unmarshal(res.content, &lb); err != nil { |
242
|
|
|
return |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
func (bs *BalancesService) getReport(ctx context.Context, balance string, options *BalanceReportOptions) (res *Response, br *BalanceReport, err error) { |
249
|
|
|
u := fmt.Sprintf("v2/balances/%s/report", balance) |
250
|
|
|
|
251
|
|
|
res, err = bs.client.get(ctx, u, options) |
252
|
|
|
if err != nil { |
253
|
|
|
return |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if err = json.Unmarshal(res.content, &br); err != nil { |
257
|
|
|
return |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return |
261
|
|
|
} |
262
|
|
|
|