|
1
|
|
|
package mollie |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/json" |
|
5
|
|
|
"fmt" |
|
6
|
|
|
"net/http" |
|
7
|
|
|
"time" |
|
8
|
|
|
|
|
9
|
|
|
"github.com/google/go-querystring/query" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
// SettlementsService operates over settlements resource |
|
13
|
|
|
type SettlementsService service |
|
14
|
|
|
|
|
15
|
|
|
// SettlementStatus describes the status of the settlement |
|
16
|
|
|
type SettlementStatus string |
|
17
|
|
|
|
|
18
|
|
|
// Available settlement statuses |
|
19
|
|
|
const ( |
|
20
|
|
|
SettlementStatusOpen = "string" |
|
21
|
|
|
SettlementStatusPending = "pending" |
|
22
|
|
|
SettlementStatusPaidOut = "paidout" |
|
23
|
|
|
SettlementStatusFailed = "failed" |
|
24
|
|
|
) |
|
25
|
|
|
|
|
26
|
|
|
// SettlementRevenue objects contain the total revenue for each payment method during this period |
|
27
|
|
|
type SettlementRevenue struct { |
|
28
|
|
|
Description string `json:"description,omitempty"` |
|
29
|
|
|
AmountNet Amount `json:"amountNet,omitempty"` |
|
30
|
|
|
AmountVAT Amount `json:"amountVat,omitempty"` |
|
31
|
|
|
AmountGross Amount `json:"amountGross,omitempty"` |
|
32
|
|
|
Count int `json:"count,omitempty"` |
|
33
|
|
|
Method PaymentMethod `json:"method,omitempty"` |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// SettlementCosts contains information about costs related to a settlement |
|
37
|
|
|
type SettlementCosts struct { |
|
38
|
|
|
Description string `json:"description,omitempty"` |
|
39
|
|
|
AmountNet Amount `json:"amountNet,omitempty"` |
|
40
|
|
|
AmountVAT Amount `json:"amountVat,omitempty"` |
|
41
|
|
|
AmountGross Amount `json:"amountGross,omitempty"` |
|
42
|
|
|
Count int `json:"count,omitempty"` |
|
43
|
|
|
Rate Rate `json:"rate,omitempty"` |
|
44
|
|
|
Method PaymentMethod `json:"method,omitempty"` |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// SettlementPeriod describe the settlement by month in full detail |
|
48
|
|
|
type SettlementPeriod struct { |
|
49
|
|
|
Revenue []SettlementRevenue `json:"revenue,omitempty"` |
|
50
|
|
|
Costs []SettlementCosts `json:"costs,omitempty"` |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// SettlementObject nests as describes for settlement periods |
|
54
|
|
|
type SettlementObject map[string]map[string]SettlementPeriod |
|
55
|
|
|
|
|
56
|
|
|
// SettlementLinks is an object with several URL objects relevant to the settlement |
|
57
|
|
|
type SettlementLinks struct { |
|
58
|
|
|
Self URL `json:"self,omitempty"` |
|
59
|
|
|
Payments URL `json:"payments,omitempty"` |
|
60
|
|
|
Refunds URL `json:"refunds,omitempty"` |
|
61
|
|
|
Chargebacks URL `json:"chargebacks,omitempty"` |
|
62
|
|
|
Captures URL `json:"captures,omitempty"` |
|
63
|
|
|
Invoice URL `json:"invoice,omitempty"` |
|
64
|
|
|
Documentation URL `json:"documentation,omitempty"` |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Settlement contains successful payments, together with refunds, |
|
68
|
|
|
// captures and chargebacks into settlements |
|
69
|
|
|
type Settlement struct { |
|
70
|
|
|
ID string `json:"id,omitempty"` |
|
71
|
|
|
Resource string `json:"resource,omitempty"` |
|
72
|
|
|
Reference string `json:"reference,omitempty"` |
|
73
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
|
74
|
|
|
SettledAt *time.Time `json:"settledAt,omitempty"` |
|
75
|
|
|
Status SettlementStatus `json:"status,omitempty"` |
|
76
|
|
|
Amount Amount `json:"amount,omitempty"` |
|
77
|
|
|
Periods SettlementObject `json:"periods,omitempty"` |
|
78
|
|
|
InvoiceID string `json:"invoiceId,omitempty"` |
|
79
|
|
|
Links SettlementLinks `json:"_links,omitempty"` |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// SettlementsListOptions contains query parameters for settlement lists. |
|
83
|
|
|
type SettlementsListOptions struct { |
|
84
|
|
|
From *ShortDate `url:"from,omitempty"` |
|
85
|
|
|
Limit int `url:"limit,omitempty"` |
|
86
|
|
|
Embed EmbedValue `url:"embed,omitempty"` |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// SettlementsList describes a list of settlements. |
|
90
|
|
|
type SettlementsList struct { |
|
91
|
|
|
Count int `json:"count,omitempty"` |
|
92
|
|
|
Embedded struct { |
|
93
|
|
|
Settlements []Settlement |
|
94
|
|
|
} `json:"_embedded,omitempty"` |
|
95
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Get returns a settlement by its id or the bank reference id |
|
99
|
|
|
// |
|
100
|
|
|
// See: https://docs.mollie.com/reference/v2/settlements-api/get-settlement |
|
101
|
|
|
func (ss *SettlementsService) Get(id string) (s *Settlement, err error) { |
|
102
|
1 |
|
return ss.get(id) |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Next retrieves the details of the current settlement that has not yet been paid out. |
|
106
|
|
|
// |
|
107
|
|
|
// See: https://docs.mollie.com/reference/v2/settlements-api/get-next-settlement |
|
108
|
|
|
func (ss *SettlementsService) Next() (s *Settlement, err error) { |
|
109
|
1 |
|
return ss.get("next") |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// Open retrieves the details of the open balance of the organization. |
|
113
|
|
|
// This will return a settlement object representing your organization’s balance. |
|
114
|
|
|
// |
|
115
|
|
|
// See: https://docs.mollie.com/reference/v2/settlements-api/get-open-settlement |
|
116
|
|
|
func (ss *SettlementsService) Open() (s *Settlement, err error) { |
|
117
|
1 |
|
return ss.get("open") |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// List retrieves all settlements, ordered from new to old |
|
121
|
|
|
// |
|
122
|
|
|
// See: https://docs.mollie.com/reference/v2/settlements-api/list-settlements |
|
123
|
|
|
func (ss *SettlementsService) List(slo *SettlementsListOptions) (sl *SettlementsList, err error) { |
|
124
|
1 |
|
res, err := ss.list("", "", slo) |
|
125
|
1 |
|
if err != nil { |
|
126
|
1 |
|
return |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
|
130
|
1 |
|
return |
|
131
|
|
|
} |
|
132
|
1 |
|
return |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// GetPayments retrieves all payments included in a settlement. |
|
136
|
|
|
// |
|
137
|
|
|
// See: https://docs.mollie.com/reference/v2/settlements-api/list-settlement-payments |
|
138
|
|
|
func (ss *SettlementsService) GetPayments(id string, slo *SettlementsListOptions) (pl *PaymentList, err error) { |
|
139
|
1 |
|
res, err := ss.list(id, "payments", slo) |
|
140
|
1 |
|
if err != nil { |
|
141
|
1 |
|
return |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
if err = json.Unmarshal(res.content, &pl); err != nil { |
|
145
|
1 |
|
return |
|
146
|
|
|
} |
|
147
|
1 |
|
return |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// GetRefunds retrieves all refunds included in a settlement. |
|
151
|
|
|
// |
|
152
|
|
|
// See: https://docs.mollie.com/reference/v2/settlements-api/list-settlement-refunds |
|
153
|
|
|
func (ss *SettlementsService) GetRefunds(id string, slo *SettlementsListOptions) (rl *RefundList, err error) { |
|
154
|
1 |
|
res, err := ss.list(id, "refunds", slo) |
|
155
|
1 |
|
if err != nil { |
|
156
|
1 |
|
return |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
1 |
|
if err = json.Unmarshal(res.content, &rl); err != nil { |
|
160
|
1 |
|
return |
|
161
|
|
|
} |
|
162
|
1 |
|
return |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// GetChargebacks retrieves all chargebacks included in a settlement. |
|
166
|
|
|
// |
|
167
|
|
|
// See: https://docs.mollie.com/reference/v2/settlements-api/list-settlement-chargebacks |
|
168
|
|
|
func (ss *SettlementsService) GetChargebacks(id string, slo *SettlementsListOptions) (cl *ChargebackList, err error) { |
|
169
|
1 |
|
res, err := ss.list(id, "chargebacks", slo) |
|
170
|
1 |
|
if err != nil { |
|
171
|
1 |
|
return |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
if err = json.Unmarshal(res.content, &cl); err != nil { |
|
175
|
1 |
|
return |
|
176
|
|
|
} |
|
177
|
1 |
|
return |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
// GetCaptures retrieves all captures included in a settlement. |
|
181
|
|
|
// |
|
182
|
|
|
// See: https://docs.mollie.com/reference/v2/settlements-api/list-settlement-captures |
|
183
|
|
|
func (ss *SettlementsService) GetCaptures(id string, slo *SettlementsListOptions) (cl *CapturesList, err error) { |
|
184
|
1 |
|
res, err := ss.list(id, "captures", slo) |
|
185
|
1 |
|
if err != nil { |
|
186
|
1 |
|
return |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
if err = json.Unmarshal(res.content, &cl); err != nil { |
|
190
|
1 |
|
return |
|
191
|
|
|
} |
|
192
|
1 |
|
return |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
func (ss *SettlementsService) get(element string) (s *Settlement, err error) { |
|
196
|
1 |
|
u := fmt.Sprintf("v2/settlements/%s", element) |
|
197
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodGet, u, nil) |
|
198
|
1 |
|
if err != nil { |
|
199
|
1 |
|
return |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
1 |
|
res, err := ss.client.Do(req) |
|
203
|
1 |
|
if err != nil { |
|
204
|
1 |
|
return |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
|
208
|
1 |
|
return |
|
209
|
|
|
} |
|
210
|
1 |
|
return |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
func (ss *SettlementsService) list(id string, category string, slo *SettlementsListOptions) (res *Response, err error) { |
|
214
|
1 |
|
u := "v2/settlements" |
|
215
|
|
|
|
|
216
|
1 |
|
if id != "" { |
|
217
|
1 |
|
u = fmt.Sprintf("%s/%s", u, id) |
|
218
|
|
|
|
|
219
|
1 |
|
if category != "" { |
|
220
|
1 |
|
u = fmt.Sprintf("%s/%s", u, category) |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
1 |
|
if slo != nil { |
|
225
|
1 |
|
v, _ := query.Values(slo) |
|
226
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodGet, u, nil) |
|
230
|
1 |
|
if err != nil { |
|
231
|
1 |
|
return |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
1 |
|
res, err = ss.client.Do(req) |
|
235
|
1 |
|
if err != nil { |
|
236
|
1 |
|
return |
|
237
|
|
|
} |
|
238
|
1 |
|
return |
|
239
|
|
|
} |
|
240
|
|
|
|