1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"fmt" |
7
|
|
|
"time" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// Refund describe a refund for a certain payment. |
11
|
|
|
type Refund struct { |
12
|
|
|
Resource string `json:"resource,omitempty"` |
13
|
|
|
ID string `json:"id,omitempty"` |
14
|
|
|
Amount *Amount `json:"amount,omitempty"` |
15
|
|
|
SettlementID string `json:"settlementId,omitempty"` |
16
|
|
|
SettlementAmount *Amount `json:"settlementAmount,omitempty"` |
17
|
|
|
Description string `json:"description,omitempty"` |
18
|
|
|
Metadata interface{} `json:"metadata,omitempty"` |
19
|
|
|
Status RefundStatus `json:"status,omitempty"` |
20
|
|
|
Lines []*OrderLine `json:"lines,omitempty"` |
21
|
|
|
PaymentID string `json:"paymentId,omitempty"` |
22
|
|
|
OrderID string `json:"orderId,omitempty"` |
23
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
24
|
|
|
TestMode bool `json:"testmode,omitempty"` |
25
|
|
|
Links RefundLinks `json:"_links,omitempty"` |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// RefundList describes how a list of refunds will be retrieved by Mollie. |
29
|
|
|
type RefundList struct { |
30
|
|
|
Count int `json:"count,omitempty"` |
31
|
|
|
Embedded struct { |
32
|
|
|
Refunds []*Refund |
33
|
|
|
} `json:"_embedded,omitempty"` |
34
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// RefundStatus describes the status of the refund. |
38
|
|
|
type RefundStatus string |
39
|
|
|
|
40
|
|
|
// Valid refund status. |
41
|
|
|
const ( |
42
|
|
|
Queued RefundStatus = "queued" |
43
|
|
|
Pending RefundStatus = "pending" |
44
|
|
|
Processing RefundStatus = "processing" |
45
|
|
|
Refunded RefundStatus = "refunded" |
46
|
|
|
Failed RefundStatus = "failed" |
47
|
|
|
) |
48
|
|
|
|
49
|
|
|
// RefundLinks describes all the possible links to be returned with |
50
|
|
|
// a Refund object. |
51
|
|
|
type RefundLinks struct { |
52
|
|
|
Self *URL `json:"self,omitempty"` |
53
|
|
|
Payment *URL `json:"payment,omitempty"` |
54
|
|
|
Settlement *URL `json:"settlement,omitempty"` |
55
|
|
|
Order *URL `json:"order,omitempty"` |
56
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// RefundOptions describes refund endpoint valid query string parameters. |
60
|
|
|
// |
61
|
|
|
// See: https://docs.mollie.com/reference/v2/refunds-api/get-refund. |
62
|
|
|
type RefundOptions struct { |
63
|
|
|
Embed EmbedValue `url:"embed,omitempty"` |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// ListRefundOptions describes list refund endpoint valid query string parameters. |
67
|
|
|
// |
68
|
|
|
// See: https://docs.mollie.com/reference/v2/refunds-api/list-refunds. |
69
|
|
|
type ListRefundOptions struct { |
70
|
|
|
From string `url:"from,omitempty"` |
71
|
|
|
Limit int `url:"limit,omitempty"` |
72
|
|
|
ProfileID string `url:"profileId,omitempty"` |
73
|
|
|
Embed EmbedValue `url:"embed,omitempty"` |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// RefundsService instance operates over refund resources. |
77
|
|
|
type RefundsService service |
78
|
|
|
|
79
|
|
|
// Get retrieve a single refund by its ID. |
80
|
|
|
// |
81
|
|
|
// If you do not know the original payment’s ID, you can use the List payment refunds endpoint. |
82
|
|
|
func (rs *RefundsService) Get(ctx context.Context, paymentID, refundID string, opts *RefundOptions) (res *Response, refund *Refund, err error) { |
83
|
1 |
|
u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID) |
84
|
|
|
|
85
|
1 |
|
res, err = rs.client.get(ctx, u, opts) |
86
|
1 |
|
if err != nil { |
87
|
1 |
|
return |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
if err = json.Unmarshal(res.content, &refund); err != nil { |
91
|
1 |
|
return |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
return |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Create a refund payment request. |
98
|
|
|
// |
99
|
|
|
// See https://docs.mollie.com/reference/v2/refunds-api/create-refund. |
100
|
|
|
func (rs *RefundsService) Create(ctx context.Context, paymentID string, re Refund, options *RefundOptions) (res *Response, rf *Refund, err error) { |
101
|
1 |
|
uri := fmt.Sprintf("v2/payments/%s/refunds", paymentID) |
102
|
|
|
|
103
|
1 |
|
if rs.client.HasAccessToken() && rs.client.config.testing { |
104
|
1 |
|
re.TestMode = true |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
res, err = rs.client.post(ctx, uri, re, options) |
108
|
1 |
|
if err != nil { |
109
|
1 |
|
return |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
if err = json.Unmarshal(res.content, &rf); err != nil { |
113
|
1 |
|
return |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
return |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Cancel try to cancel the refund request. |
120
|
|
|
// The refund can only be canceled while the refund’s status is either queued or pending. |
121
|
|
|
// See https://docs.mollie.com/reference/v2/refunds-api/cancel-refund |
122
|
|
|
func (rs *RefundsService) Cancel(ctx context.Context, paymentID, refundID string) (res *Response, err error) { |
123
|
1 |
|
u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID) |
124
|
|
|
|
125
|
1 |
|
res, err = rs.client.delete(ctx, u, nil) |
126
|
1 |
|
if err != nil { |
127
|
1 |
|
return |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// ListRefund calls the top level https://api.mollie.com/v2/refunds. |
134
|
|
|
// |
135
|
|
|
// See https://docs.mollie.com/reference/v2/refunds-api/list-refunds. |
136
|
|
|
func (rs *RefundsService) ListRefund(ctx context.Context, opts *ListRefundOptions) (res *Response, rl *RefundList, err error) { |
137
|
1 |
|
u := "v2/refunds" |
138
|
|
|
|
139
|
1 |
|
return rs.list(ctx, u, opts) |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// ListRefundPayment calls the payment-specific |
143
|
|
|
// https://api.mollie.com/v2/payments/*paymentId*/refunds. |
144
|
|
|
// |
145
|
|
|
// Only refunds for that specific payment are returned. |
146
|
|
|
// See: https://docs.mollie.com/reference/v2/refunds-api/list-refunds |
147
|
|
|
func (rs *RefundsService) ListRefundPayment(ctx context.Context, paymentID string, opts *ListRefundOptions) (res *Response, rl *RefundList, err error) { |
148
|
1 |
|
u := fmt.Sprintf("v2/payments/%s/refunds", paymentID) |
149
|
|
|
|
150
|
1 |
|
return rs.list(ctx, u, opts) |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
func (rs *RefundsService) list(ctx context.Context, uri string, opts interface{}) (res *Response, rl *RefundList, err error) { |
154
|
1 |
|
res, err = rs.client.get(ctx, uri, opts) |
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
|
|
|
|