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
|
|
|
// PaymentMethod is a payment method supported by Mollie. |
13
|
|
|
type PaymentMethod string |
14
|
|
|
|
15
|
|
|
// Supported payment methods |
16
|
|
|
const ( |
17
|
|
|
Bancontact PaymentMethod = "bancontact" |
18
|
|
|
BankTransfer PaymentMethod = "banktransfer" |
19
|
|
|
Belfius PaymentMethod = "belfius" |
20
|
|
|
CreditCard PaymentMethod = "creditcard" |
21
|
|
|
DirectDebit PaymentMethod = "directdebit" |
22
|
|
|
EPS PaymentMethod = "eps" |
23
|
|
|
GiftCard PaymentMethod = "giftcard" |
24
|
|
|
GiroPay PaymentMethod = "giropay" |
25
|
|
|
IDeal PaymentMethod = "ideal" |
26
|
|
|
INGHomePay PaymentMethod = "inghomepay" |
27
|
|
|
KBC PaymentMethod = "kbc" |
28
|
|
|
KlarnaPayLater PaymentMethod = "klarnapaylater" |
29
|
|
|
KlarnaLiceit PaymentMethod = "klarnaliceit" |
30
|
|
|
MyBank PaymentMethod = "mybank" |
31
|
|
|
PayPal PaymentMethod = "paypal" |
32
|
|
|
PaySafeCard PaymentMethod = "paysafecard" |
33
|
|
|
PRZelewy24 PaymentMethod = "przelewy24" |
34
|
|
|
Sofort PaymentMethod = "sofort" |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
// SequenceType indicates which type of payment this is in a recurring sequence. |
38
|
|
|
type SequenceType string |
39
|
|
|
|
40
|
|
|
// Valid sequence types |
41
|
|
|
const ( |
42
|
|
|
OneOffSequence SequenceType = "oneoff" |
43
|
|
|
FirstSequence SequenceType = "first" |
44
|
|
|
RecurringSequence SequenceType = "recurring" |
45
|
|
|
) |
46
|
|
|
|
47
|
|
|
// Payment describes a transaction between a customer and a merchant |
48
|
|
|
type Payment struct { |
49
|
|
|
Resource string `json:"resource,omitempty"` |
50
|
|
|
ID string `json:"id,omitempty"` |
51
|
|
|
Mode Mode `json:"mode,omitempty"` |
52
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
53
|
|
|
Status string `json:"status,omitempty"` |
54
|
|
|
IsCancellable bool `json:"isCancellable,omitempty"` |
55
|
|
|
AuthorizedAt *time.Time `json:"authorizedAt,omitempty"` |
56
|
|
|
PaidAt *time.Time `json:"paidAt,omitempty"` |
57
|
|
|
CanceledAt *time.Time `json:"canceledAt,omitempty"` |
58
|
|
|
ExpiresAt *time.Time `json:"expiresAt,omitempty"` |
59
|
|
|
ExpiredAt *time.Time `json:"expiredAt,omitempty"` |
60
|
|
|
FailedAt *time.Time `json:"failedAt,omitempty"` |
61
|
|
|
Amount *Amount `json:"amount,omitempty"` |
62
|
|
|
AmountRefunded *Amount `json:"amountRefunded,omitempty"` |
63
|
|
|
AmountRemaining *Amount `json:"amountRemaining,omitempty"` |
64
|
|
|
AmountCaptured *Amount `json:"amountCaptured,omitempty"` |
65
|
|
|
Description string `json:"description,omitempty"` |
66
|
|
|
RedirectURL string `json:"redirectUrl,omitempty"` |
67
|
|
|
WebhookURL string `json:"webhookUrl,omitempty"` |
68
|
|
|
Method *PaymentMethod `json:"method,omitempty"` |
69
|
|
|
Metadata interface{} `json:"metadata,omitempty"` |
70
|
|
|
Locale *Locale `json:"locale,omitempty"` |
71
|
|
|
CountryCode string `json:"countryCode,omitempty"` |
72
|
|
|
ProfileID string `json:"profileId,omitempty"` |
73
|
|
|
SettlementAmount *Amount `json:"settlementAmount,omitempty"` |
74
|
|
|
SettlementID string `json:"settlementId,omitempty"` |
75
|
|
|
CustomerID string `json:"customerId,omitempty"` |
76
|
|
|
SequenceType *SequenceType `json:"sequenceType,omitempty"` |
77
|
|
|
MandateID string `json:"mandateId,omitempty"` |
78
|
|
|
OrderID string `json:"orderId,omitempty"` |
79
|
|
|
ApplicationFee *ApplicationFee `json:"applicationFee,omitempty"` |
80
|
|
|
Links *PaymentLinks `json:"_links,omitempty"` |
81
|
|
|
Details *PaymentDetails `json:"details,omitempty"` |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// PaymentLinks describes all the possible links to be returned with |
85
|
|
|
// a payment object. |
86
|
|
|
type PaymentLinks struct { |
87
|
|
|
Self URL `json:"self,omitempty"` |
88
|
|
|
Checkout URL `json:"checkout,omitempty"` |
89
|
|
|
ChangePaymentState URL `json:"changePaymentState,omitempty"` |
90
|
|
|
Refunds URL `json:"refunds,omitempty"` |
91
|
|
|
ChargeBacks URL `json:"chargebacks,omitempty"` |
92
|
|
|
Captures URL `json:"captures,omitempty"` |
93
|
|
|
Settlement URL `json:"settlement,omitempty"` |
94
|
|
|
Documentation URL `json:"documentation,omitempty"` |
95
|
|
|
Mandate URL `json:"mandate,omitempty"` |
96
|
|
|
Subscription URL `json:"subscription,omitempty"` |
97
|
|
|
Customer URL `json:"customer,omitempty"` |
98
|
|
|
Order URL `json:"order,omitempty"` |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// PaymentOptions describes payments endpoint valid query string parameters. |
102
|
|
|
// |
103
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/get-payment |
104
|
|
|
type PaymentOptions struct { |
105
|
|
|
Include string `url:"include,omitempty"` |
106
|
|
|
Embed string `url:"embed,omitempty"` |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// ListPaymentOptions describes list payments endpoint valid query string parameters. |
110
|
|
|
type ListPaymentOptions struct { |
111
|
|
|
Include string `url:"include,omitempty"` |
112
|
|
|
Embed string `url:"embed,omitempty"` |
113
|
|
|
ProfileID string `url:"profileId,omitempty"` |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// PaymentsService instance operates over payment resources |
117
|
|
|
type PaymentsService service |
118
|
|
|
|
119
|
|
|
// Get retrieves a single payment object by its payment token. |
120
|
|
|
func (ps *PaymentsService) Get(id string, options *PaymentOptions) (p Payment, err error) { |
121
|
1 |
|
u := fmt.Sprintf("v2/payments/%s", id) |
122
|
1 |
|
if options != nil { |
123
|
1 |
|
v, _ := query.Values(options) |
124
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
125
|
|
|
} |
126
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodGet, u, nil) |
127
|
1 |
|
if err != nil { |
128
|
1 |
|
return |
129
|
|
|
} |
130
|
1 |
|
res, err := ps.client.Do(req) |
131
|
1 |
|
if err != nil { |
132
|
1 |
|
return |
133
|
|
|
} |
134
|
1 |
|
if err = json.Unmarshal(res.content, &p); err != nil { |
135
|
1 |
|
return |
136
|
|
|
} |
137
|
1 |
|
return |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Create stores a new payment object attached to your Mollie account. |
141
|
|
|
// |
142
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/create-payment# |
143
|
|
|
func (ps *PaymentsService) Create(p Payment) (np Payment, err error) { |
144
|
1 |
|
u := fmt.Sprintf("v2/payments") |
145
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodPost, u, p) |
146
|
1 |
|
if err != nil { |
147
|
1 |
|
return |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
res, err := ps.client.Do(req) |
151
|
1 |
|
if err != nil { |
152
|
1 |
|
return |
153
|
|
|
} |
154
|
1 |
|
if err = json.Unmarshal(res.content, &np); err != nil { |
155
|
1 |
|
return |
156
|
|
|
} |
157
|
1 |
|
return |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Cancel removes a payment (if possible) from your Mollie account. |
161
|
|
|
// |
162
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/cancel-payment |
163
|
|
|
func (ps *PaymentsService) Cancel(id string) (p Payment, err error) { |
164
|
1 |
|
u := fmt.Sprintf("v2/payments/%s", id) |
165
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodDelete, u, nil) |
166
|
1 |
|
if err != nil { |
167
|
1 |
|
return |
168
|
|
|
} |
169
|
1 |
|
res, err := ps.client.Do(req) |
170
|
1 |
|
if err != nil { |
171
|
1 |
|
return |
172
|
|
|
} |
173
|
1 |
|
if err = json.Unmarshal(res.content, &p); err != nil { |
174
|
1 |
|
return |
175
|
|
|
} |
176
|
1 |
|
return |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Update can be used to update some details of a created payment. |
180
|
|
|
// |
181
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/update-payment# |
182
|
|
|
func (ps *PaymentsService) Update(id string, up Payment) (p Payment, err error) { |
183
|
1 |
|
u := fmt.Sprintf("v2/payments/%s", id) |
184
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodPatch, u, up) |
185
|
1 |
|
if err != nil { |
186
|
1 |
|
return |
187
|
|
|
} |
188
|
1 |
|
res, err := ps.client.Do(req) |
189
|
1 |
|
if err != nil { |
190
|
1 |
|
return |
191
|
|
|
} |
192
|
1 |
|
if err = json.Unmarshal(res.content, &p); err != nil { |
193
|
1 |
|
return |
194
|
|
|
} |
195
|
1 |
|
return |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// PaymentList describes how a list of payments will be retrieved by Mollie. |
199
|
|
|
type PaymentList struct { |
200
|
|
|
Count int `json:"count,omitempty"` |
201
|
|
|
Embedded struct { |
202
|
|
|
Payments []Payment |
203
|
|
|
} `json:"_embedded,omitempty"` |
204
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
// List retrieves a list of payments associated with your account/organization. |
208
|
|
|
// |
209
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/list-payments |
210
|
|
|
func (ps *PaymentsService) List(options *ListPaymentOptions) (pl PaymentList, err error) { |
211
|
1 |
|
u := fmt.Sprint("v2/payments") |
212
|
1 |
|
if options != nil { |
213
|
1 |
|
v, _ := query.Values(options) |
214
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
215
|
|
|
} |
216
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodGet, u, nil) |
217
|
1 |
|
if err != nil { |
218
|
1 |
|
return |
219
|
|
|
} |
220
|
1 |
|
res, err := ps.client.Do(req) |
221
|
1 |
|
if err != nil { |
222
|
1 |
|
return |
223
|
|
|
} |
224
|
1 |
|
if err = json.Unmarshal(res.content, &pl); err != nil { |
225
|
1 |
|
return |
226
|
|
|
} |
227
|
1 |
|
return |
228
|
|
|
} |
229
|
|
|
|