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