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
|
|
|
// Refund describe a refund for a certain payment. |
13
|
|
|
type Refund struct { |
14
|
|
|
Resource string `json:"resource,omitempty"` |
15
|
|
|
ID string `json:"id,omitempty"` |
16
|
|
|
Amount *Amount `json:"amount,omitempty"` |
17
|
|
|
SettlementID string `json:"settlementId,omitempty"` |
18
|
|
|
SettlementAmount *Amount `json:"settlementAmount,omitempty"` |
19
|
|
|
Description string `json:"description,omitempty"` |
20
|
|
|
Metadata interface{} `json:"metadata,omitempty"` |
21
|
|
|
Status *RefundStatus `json:"status,omitempty"` |
22
|
|
|
Lines []*OrderLine `json:"lines,omitempty"` |
23
|
|
|
PaymentID string `json:"paymentId,omitempty"` |
24
|
|
|
OrderID string `json:"orderId,omitempty"` |
25
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
26
|
|
|
Links *RefundLinks `json:"_links,omitempty"` |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// RefundList describes how a list of refunds will be retrieved by Mollie. |
30
|
|
|
type RefundList struct { |
31
|
|
|
Count int `json:"count,omitempty"` |
32
|
|
|
Embedded struct { |
33
|
|
|
Refunds []Refund |
34
|
|
|
} `json:"_embedded,omitempty"` |
35
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// RefundStatus describes the status of the refund. |
39
|
|
|
type RefundStatus string |
40
|
|
|
|
41
|
|
|
// Valid refund status. |
42
|
|
|
const ( |
43
|
|
|
Queued RefundStatus = "queued" |
44
|
|
|
Pending RefundStatus = "pending" |
45
|
|
|
Processing RefundStatus = "processing" |
46
|
|
|
Refunded RefundStatus = "refunded" |
47
|
|
|
Failed RefundStatus = "failed" |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
// RefundLinks describes all the possible links to be returned with |
51
|
|
|
// a Refund object. |
52
|
|
|
type RefundLinks struct { |
53
|
|
|
Self *URL `json:"self,omitempty"` |
54
|
|
|
Payment *URL `json:"payment,omitempty"` |
55
|
|
|
Settlement *URL `json:"settlement,omitempty"` |
56
|
|
|
Order *URL `json:"order,omitempty"` |
57
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// RefundOptions describes refund endpoint valid query string parameters. |
61
|
|
|
// |
62
|
|
|
// See: https://docs.mollie.com/reference/v2/refunds-api/get-refund. |
63
|
|
|
type RefundOptions struct { |
64
|
|
|
Embed EmbedValue `url:"embed,omitempty"` |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// ListRefundOptions describes list refund endpoint valid query string parameters. |
68
|
|
|
// |
69
|
|
|
// See: https://docs.mollie.com/reference/v2/refunds-api/list-refunds. |
70
|
|
|
type ListRefundOptions struct { |
71
|
|
|
From string `url:"from,omitempty"` |
72
|
|
|
Limit string `url:"limit,omitempty"` |
73
|
|
|
ProfileID string `url:"profileId,omitempty"` |
74
|
|
|
Embed EmbedValue `url:"embed,omitempty"` |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// RefundsService instance operates over refund resources. |
78
|
|
|
type RefundsService service |
79
|
|
|
|
80
|
|
|
// Get retrieve a single refund by its ID. |
81
|
|
|
// |
82
|
|
|
// If you do not know the original payment’s ID, you can use the List payment refunds endpoint. |
83
|
|
|
func (rs *RefundsService) Get(paymentID, refundID string, options *RefundOptions) (refund Refund, err error) { |
84
|
1 |
|
u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID) |
85
|
1 |
|
if options != nil { |
86
|
1 |
|
v, _ := query.Values(options) |
87
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
req, err := rs.client.NewAPIRequest(http.MethodGet, u, options) |
91
|
1 |
|
if err != nil { |
92
|
1 |
|
return |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
res, err := rs.client.Do(req) |
96
|
1 |
|
if err != nil { |
97
|
1 |
|
return |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
if err = json.Unmarshal(res.content, &refund); err != nil { |
101
|
1 |
|
return |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
var ( |
108
|
|
|
requiredCreateParamRefund = "parameter required for creating a refund: %+v" |
109
|
|
|
) |
110
|
|
|
|
111
|
|
|
// Create a refund payment request. |
112
|
|
|
// |
113
|
|
|
// See https://docs.mollie.com/reference/v2/refunds-api/create-refund. |
114
|
|
|
func (rs *RefundsService) Create(paymentID string, re Refund, options *RefundOptions) (rf Refund, err error) { |
115
|
1 |
|
u := fmt.Sprintf("v2/payments/%s/refunds", paymentID) |
116
|
1 |
|
if options != nil { |
117
|
1 |
|
v, _ := query.Values(options) |
118
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
req, err := rs.client.NewAPIRequest(http.MethodPost, u, re) |
122
|
1 |
|
if err != nil { |
123
|
1 |
|
return |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
res, err := rs.client.Do(req) |
127
|
1 |
|
if err != nil { |
128
|
1 |
|
return |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
if err = json.Unmarshal(res.content, &rf); err != nil { |
132
|
1 |
|
return |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// Cancel try to cancel the refund request. |
139
|
|
|
// The refund can only be canceled while the refund’s status is either queued or pending. |
140
|
|
|
// See https://docs.mollie.com/reference/v2/refunds-api/cancel-refund |
141
|
|
|
func (rs *RefundsService) Cancel(paymentID, refundID string, options *RefundOptions) (err error) { |
142
|
1 |
|
u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID) |
143
|
1 |
|
if options != nil { |
144
|
1 |
|
v, _ := query.Values(options) |
145
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
req, err := rs.client.NewAPIRequest(http.MethodDelete, u, nil) |
149
|
1 |
|
if err != nil { |
150
|
1 |
|
return |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
_, err = rs.client.Do(req) |
154
|
1 |
|
if err != nil { |
155
|
1 |
|
return |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
return |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// ListRefund calls the top level https://api.mollie.com/v2/refunds. |
162
|
|
|
// |
163
|
|
|
// See https://docs.mollie.com/reference/v2/refunds-api/list-refunds. |
164
|
|
|
func (rs *RefundsService) ListRefund(options *ListRefundOptions) (rl *RefundList, err error) { |
165
|
1 |
|
u := fmt.Sprintf("v2/refunds") |
166
|
1 |
|
if options != nil { |
167
|
1 |
|
v, _ := query.Values(options) |
168
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
169
|
|
|
} |
170
|
1 |
|
return rs.list(u) |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// ListRefundPayment calls the payment-specific |
174
|
|
|
// https://api.mollie.com/v2/payments/*paymentId*/refunds. |
175
|
|
|
// Only refunds for that specific payment are returned. |
176
|
|
|
// See https://docs.mollie.com/reference/v2/refunds-api/list-refunds |
177
|
|
|
func (rs *RefundsService) ListRefundPayment(paymentID string, options *ListRefundOptions) (rl *RefundList, err error) { |
178
|
1 |
|
u := fmt.Sprintf("v2/payments/%s/refunds", paymentID) |
179
|
1 |
|
if options != nil { |
180
|
1 |
|
v, _ := query.Values(options) |
181
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
182
|
|
|
} |
183
|
1 |
|
return rs.list(u) |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
func (rs *RefundsService) list(uri string) (rl *RefundList, err error) { |
187
|
1 |
|
req, err := rs.client.NewAPIRequest(http.MethodGet, uri, nil) |
188
|
1 |
|
if err != nil { |
189
|
1 |
|
return |
190
|
|
|
} |
191
|
1 |
|
res, err := rs.client.Do(req) |
192
|
1 |
|
if err != nil { |
193
|
1 |
|
return |
194
|
|
|
} |
195
|
1 |
|
if err = json.Unmarshal(res.content, &rl); err != nil { |
196
|
1 |
|
return |
197
|
|
|
} |
198
|
1 |
|
return |
199
|
|
|
} |
200
|
|
|
|