|
1
|
|
|
package mollie |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"encoding/json" |
|
6
|
|
|
"fmt" |
|
7
|
|
|
"time" |
|
8
|
|
|
) |
|
9
|
|
|
|
|
10
|
|
|
// CreatePaymentRefund describes the payload to create a refund associated to a payment. |
|
11
|
|
|
type CreatePaymentRefund struct { |
|
12
|
|
|
Description string `json:"description,omitempty"` |
|
13
|
|
|
Metadata any `json:"metadata,omitempty"` |
|
14
|
|
|
Amount *Amount `json:"amount,omitempty"` |
|
15
|
|
|
PaymentRefundAccessTokenFields |
|
16
|
|
|
PaymentRefundMollieConnectFields |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
// CreateOrderRefund describes the payload to create a refund associated to an order. |
|
20
|
|
|
type CreateOrderRefund struct { |
|
21
|
|
|
Description string `json:"description,omitempty"` |
|
22
|
|
|
Metadata any `json:"metadata,omitempty"` |
|
23
|
|
|
Lines []*OrderRefundLine `json:"lines,omitempty"` |
|
24
|
|
|
PaymentRefundAccessTokenFields |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// OrderRefundLine describes the payload to create a refund associated to an order line. |
|
28
|
|
|
type OrderRefundLine struct { |
|
29
|
|
|
Quantity int `json:"quantity,omitempty"` |
|
30
|
|
|
ID string `json:"id,omitempty"` |
|
31
|
|
|
Amount *Amount `json:"amount,omitempty"` |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// PaymentRefundAccessTokenFields describes the access token available fields for a refund. |
|
35
|
|
|
type PaymentRefundAccessTokenFields struct { |
|
36
|
|
|
Testmode bool `json:"testmode,omitempty"` |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// PaymentRefundMollieConnectFields describes the Mollie Connect available fields for a refund. |
|
40
|
|
|
type PaymentRefundMollieConnectFields struct { |
|
41
|
|
|
ReverseRouting bool `json:"reverseRouting,omitempty"` |
|
42
|
|
|
RoutingReversals []*RoutingReversal `json:"routingReversals,omitempty"` |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// RoutingReversal describes the payload to be sent to the reverse routing endpoint. |
|
46
|
|
|
type RoutingReversal struct { |
|
47
|
|
|
Amount *Amount `json:"amount,omitempty"` |
|
48
|
|
|
Source string `json:"source,omitempty"` |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// RoutingSource describes the source of the routing. |
|
52
|
|
|
type RoutingSource struct { |
|
53
|
|
|
Type string `json:"type,omitempty"` |
|
54
|
|
|
OrganizationID string `json:"organizationId,omitempty"` |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Refund describe a refund for a certain payment. |
|
58
|
|
|
type Refund struct { |
|
59
|
|
|
Resource string `json:"resource,omitempty"` |
|
60
|
|
|
ID string `json:"id,omitempty"` |
|
61
|
|
|
SettlementID string `json:"settlementId,omitempty"` |
|
62
|
|
|
Description string `json:"description,omitempty"` |
|
63
|
|
|
PaymentID string `json:"paymentId,omitempty"` |
|
64
|
|
|
OrderID string `json:"orderId,omitempty"` |
|
65
|
|
|
Amount *Amount `json:"amount,omitempty"` |
|
66
|
|
|
SettlementAmount *Amount `json:"settlementAmount,omitempty"` |
|
67
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
|
68
|
|
|
Lines []*OrderLine `json:"lines,omitempty"` |
|
69
|
|
|
Metadata any `json:"metadata,omitempty"` |
|
70
|
|
|
Status RefundStatus `json:"status,omitempty"` |
|
71
|
|
|
Links RefundLinks `json:"_links,omitempty"` |
|
72
|
|
|
PaymentRefundAccessTokenFields |
|
73
|
|
|
PaymentRefundMollieConnectFields |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// RefundsList describes how a list of refunds will be retrieved by Mollie. |
|
77
|
|
|
type RefundsList struct { |
|
78
|
|
|
Count int `json:"count,omitempty"` |
|
79
|
|
|
Embedded struct { |
|
80
|
|
|
Refunds []*Refund |
|
81
|
|
|
} `json:"_embedded,omitempty"` |
|
82
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// RefundStatus describes the status of the refund. |
|
86
|
|
|
type RefundStatus string |
|
87
|
1 |
|
|
|
88
|
|
|
// Valid refund status. |
|
89
|
1 |
|
const ( |
|
90
|
1 |
|
Queued RefundStatus = "queued" |
|
91
|
1 |
|
Pending RefundStatus = "pending" |
|
92
|
|
|
Processing RefundStatus = "processing" |
|
93
|
|
|
Refunded RefundStatus = "refunded" |
|
94
|
1 |
|
Failed RefundStatus = "failed" |
|
95
|
1 |
|
) |
|
96
|
|
|
|
|
97
|
|
|
// RefundLinks describes all the possible links to be returned with |
|
98
|
1 |
|
// a Refund object. |
|
99
|
|
|
type RefundLinks struct { |
|
100
|
|
|
Self *URL `json:"self,omitempty"` |
|
101
|
|
|
Payment *URL `json:"payment,omitempty"` |
|
102
|
|
|
Settlement *URL `json:"settlement,omitempty"` |
|
103
|
|
|
Order *URL `json:"order,omitempty"` |
|
104
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// PaymentRefundOptions describes payment refund endpoint valid query string parameters. |
|
108
|
|
|
type PaymentRefundOptions struct { |
|
109
|
1 |
|
Embed []EmbedValue `url:"embed,omitempty"` |
|
110
|
|
|
} |
|
111
|
1 |
|
|
|
112
|
1 |
|
// ListRefundsOptions describes payment and order refunds list endpoint valid query string parameters. |
|
113
|
|
|
type ListRefundsOptions struct { |
|
114
|
|
|
Limit int `url:"limit,omitempty"` |
|
115
|
1 |
|
From string `url:"from,omitempty"` |
|
116
|
1 |
|
ProfileID string `url:"profileId,omitempty"` |
|
117
|
1 |
|
Embed []EmbedValue `url:"embed,omitempty"` |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
// RefundsService instance operates over refund resources. |
|
121
|
1 |
|
type RefundsService service |
|
122
|
|
|
|
|
123
|
|
|
// List retrieves all refunds. |
|
124
|
1 |
|
// |
|
125
|
|
|
// See https://docs.mollie.com/reference/list-all-refunds. |
|
126
|
|
|
func (rs *RefundsService) List(ctx context.Context, opts *ListRefundsOptions) ( |
|
127
|
|
|
res *Response, |
|
128
|
|
|
rl *RefundsList, |
|
129
|
|
|
err error, |
|
130
|
|
|
) { |
|
131
|
1 |
|
res, err = rs.client.get(ctx, "v2/refunds", opts) |
|
132
|
|
|
if err != nil { |
|
133
|
1 |
|
return |
|
134
|
1 |
|
} |
|
135
|
1 |
|
|
|
136
|
|
|
if err = json.Unmarshal(res.content, &rl); err != nil { |
|
137
|
|
|
return |
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
return |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// GetPaymentRefund retrieves a specific refund for a specific payment. |
|
144
|
|
|
// |
|
145
|
|
|
// See: https://docs.mollie.com/reference/get-refund |
|
146
|
|
|
func (rs *RefundsService) GetPaymentRefund( |
|
147
|
|
|
ctx context.Context, |
|
148
|
|
|
paymentID, refundID string, |
|
149
|
1 |
|
opts *PaymentRefundOptions, |
|
150
|
|
|
) ( |
|
151
|
1 |
|
res *Response, |
|
152
|
|
|
refund *Refund, |
|
153
|
|
|
err error, |
|
154
|
|
|
) { |
|
155
|
|
|
u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID) |
|
156
|
|
|
|
|
157
|
|
|
res, err = rs.client.get(ctx, u, opts) |
|
158
|
|
|
if err != nil { |
|
159
|
|
|
return |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
if err = json.Unmarshal(res.content, &refund); err != nil { |
|
163
|
|
|
return |
|
164
|
1 |
|
} |
|
165
|
|
|
|
|
166
|
1 |
|
return |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// ListPaymentRefunds retrieves all refunds for a specific payment. |
|
170
|
|
|
// |
|
171
|
|
|
// See: https://docs.mollie.com/reference/list-refunds |
|
172
|
|
|
func (rs *RefundsService) ListPaymentRefunds( |
|
173
|
|
|
ctx context.Context, |
|
174
|
1 |
|
paymentID string, |
|
175
|
1 |
|
opts *ListRefundsOptions, |
|
176
|
1 |
|
) ( |
|
177
|
|
|
res *Response, |
|
178
|
|
|
rl *RefundsList, |
|
179
|
1 |
|
err error, |
|
180
|
1 |
|
) { |
|
181
|
|
|
u := fmt.Sprintf("v2/payments/%s/refunds", paymentID) |
|
182
|
|
|
|
|
183
|
1 |
|
res, err = rs.client.get(ctx, u, opts) |
|
184
|
|
|
if err != nil { |
|
185
|
|
|
return |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
if err = json.Unmarshal(res.content, &rl); err != nil { |
|
189
|
|
|
return |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// CreatePaymentRefund performs a refund payment request. |
|
196
|
|
|
// |
|
197
|
|
|
// See https://docs.mollie.com/reference/create-refund |
|
198
|
|
|
func (rs *RefundsService) CreatePaymentRefund( |
|
199
|
|
|
ctx context.Context, |
|
200
|
|
|
paymentID string, |
|
201
|
|
|
re CreatePaymentRefund, |
|
202
|
|
|
options *PaymentRefundOptions, |
|
203
|
|
|
) ( |
|
204
|
|
|
res *Response, |
|
205
|
|
|
rf *Refund, |
|
206
|
|
|
err error, |
|
207
|
|
|
) { |
|
208
|
|
|
uri := fmt.Sprintf("v2/payments/%s/refunds", paymentID) |
|
209
|
|
|
|
|
210
|
|
|
if rs.client.HasAccessToken() && rs.client.config.testing { |
|
211
|
|
|
re.Testmode = true |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
res, err = rs.client.post(ctx, uri, re, options) |
|
215
|
|
|
if err != nil { |
|
216
|
|
|
return |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
if err = json.Unmarshal(res.content, &rf); err != nil { |
|
220
|
|
|
return |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// CancelPaymentRefund cancels a refund for a specific payment. |
|
227
|
|
|
// |
|
228
|
|
|
// See https://docs.mollie.com/reference/cancel-refund |
|
229
|
|
|
func (rs *RefundsService) CancelPaymentRefund( |
|
230
|
|
|
ctx context.Context, paymentID, refundID string, |
|
231
|
|
|
) (res *Response, err error) { |
|
232
|
|
|
return rs.client.delete(ctx, fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID)) |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
// CreateOrderRefund creates a refund for a specific order. |
|
236
|
|
|
// |
|
237
|
|
|
// See https://docs.mollie.com/reference/create-order-refund |
|
238
|
|
|
func (rs *RefundsService) CreateOrderRefund( |
|
239
|
|
|
ctx context.Context, |
|
240
|
|
|
orderID string, |
|
241
|
|
|
r CreateOrderRefund, |
|
242
|
|
|
) ( |
|
243
|
|
|
res *Response, |
|
244
|
|
|
rf *Refund, |
|
245
|
|
|
err error, |
|
246
|
|
|
) { |
|
247
|
|
|
uri := fmt.Sprintf("v2/orders/%s/refunds", orderID) |
|
248
|
|
|
|
|
249
|
|
|
if rs.client.HasAccessToken() && rs.client.config.testing { |
|
250
|
|
|
r.Testmode = true |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
res, err = rs.client.post(ctx, uri, r, nil) |
|
254
|
|
|
if err != nil { |
|
255
|
|
|
return |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
if err = json.Unmarshal(res.content, &rf); err != nil { |
|
259
|
|
|
return |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
return |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
// ListOrderRefunds retrieves all refunds for a specific order. |
|
266
|
|
|
// |
|
267
|
|
|
// See https://docs.mollie.com/reference/list-order-refunds-1 |
|
268
|
|
|
func (rs *RefundsService) ListOrderRefunds( |
|
269
|
|
|
ctx context.Context, |
|
270
|
|
|
orderID string, |
|
271
|
|
|
opts *ListRefundsOptions, |
|
272
|
|
|
) ( |
|
273
|
|
|
res *Response, |
|
274
|
|
|
rl *RefundsList, |
|
275
|
|
|
err error, |
|
276
|
|
|
) { |
|
277
|
|
|
u := fmt.Sprintf("v2/orders/%s/refunds", orderID) |
|
278
|
|
|
|
|
279
|
|
|
res, err = rs.client.get(ctx, u, opts) |
|
280
|
|
|
if err != nil { |
|
281
|
|
|
return |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
if err = json.Unmarshal(res.content, &rl); err != nil { |
|
285
|
|
|
return |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
return |
|
289
|
|
|
} |
|
290
|
|
|
|