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
|
|
|
KBC PaymentMethod = "kbc" |
27
|
|
|
KlarnaPayLater PaymentMethod = "klarnapaylater" |
28
|
|
|
KlarnaLiceit PaymentMethod = "klarnaliceit" |
29
|
|
|
MyBank PaymentMethod = "mybank" |
30
|
|
|
PayPal PaymentMethod = "paypal" |
31
|
|
|
PaySafeCard PaymentMethod = "paysafecard" |
32
|
|
|
PRZelewy24 PaymentMethod = "przelewy24" |
33
|
|
|
Sofort PaymentMethod = "sofort" |
34
|
|
|
) |
35
|
|
|
|
36
|
|
|
// SequenceType indicates which type of payment this is in a recurring sequence. |
37
|
|
|
type SequenceType string |
38
|
|
|
|
39
|
|
|
// Valid sequence types |
40
|
|
|
const ( |
41
|
|
|
OneOffSequence SequenceType = "oneoff" |
42
|
|
|
FirstSequence SequenceType = "first" |
43
|
|
|
RecurringSequence SequenceType = "recurring" |
44
|
|
|
) |
45
|
|
|
|
46
|
|
|
// Payment describes a transaction between a customer and a merchant. |
47
|
|
|
type Payment struct { |
48
|
|
|
IsCancellable bool `json:"isCancellable,omitempty"` |
49
|
|
|
TestMode bool `json:"testmode,omitempty"` |
50
|
|
|
DigitalGoods bool `json:"digitalGoods,omitempty"` |
51
|
|
|
ApplePayPaymentToken string `json:"applePayPaymentToken,omitempty"` |
52
|
|
|
BillingEmail string `json:"billingEmail,omitempty"` |
53
|
|
|
CardToken string `json:"cardToken,omitempty"` |
54
|
|
|
Issuer string `json:"issuer,omitempty"` |
55
|
|
|
VoucherNumber string `json:"voucherNumber,omitempty"` |
56
|
|
|
VoucherPin string `json:"voucherPin,omitempty"` |
57
|
|
|
ExtraMerchantData string `json:"extraMerchantData,omitempty"` |
58
|
|
|
SessionID string `json:"sessionId,omitempty"` |
59
|
|
|
CustomerReference string `json:"customerReference,omitempty"` |
60
|
|
|
ConsumerName string `json:"consumerName,omitempty"` |
61
|
|
|
ConsumerAccount string `json:"consumerAccount,omitempty"` |
62
|
|
|
WebhookURL string `json:"webhookUrl,omitempty"` |
63
|
|
|
Resource string `json:"resource,omitempty"` |
64
|
|
|
ID string `json:"id,omitempty"` |
65
|
|
|
MandateID string `json:"mandateId,omitempty"` |
66
|
|
|
OrderID string `json:"orderId,omitempty"` |
67
|
|
|
ProfileID string `json:"profileId,omitempty"` |
68
|
|
|
SettlementID string `json:"settlementId,omitempty"` |
69
|
|
|
CustomerID string `json:"customerId,omitempty"` |
70
|
|
|
Status string `json:"status,omitempty"` |
71
|
|
|
Description string `json:"description,omitempty"` |
72
|
|
|
RedirectURL string `json:"redirectUrl,omitempty"` |
73
|
|
|
CountryCode string `json:"countryCode,omitempty"` |
74
|
|
|
SubscriptionID string `json:"subscriptionId,omitempty"` |
75
|
|
|
Metadata interface{} `json:"metadata,omitempty"` |
76
|
|
|
Amount *Amount `json:"amount,omitempty"` |
77
|
|
|
AmountRefunded *Amount `json:"amountRefunded,omitempty"` |
78
|
|
|
AmountRemaining *Amount `json:"amountRemaining,omitempty"` |
79
|
|
|
AmountCaptured *Amount `json:"amountCaptured,omitempty"` |
80
|
|
|
SettlementAmount *Amount `json:"settlementAmount,omitempty"` |
81
|
|
|
ApplicationFee *ApplicationFee `json:"applicationFee,omitempty"` |
82
|
|
|
Details *PaymentDetails `json:"details,omitempty"` |
83
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
84
|
|
|
AuthorizedAt *time.Time `json:"authorizedAt,omitempty"` |
85
|
|
|
PaidAt *time.Time `json:"paidAt,omitempty"` |
86
|
|
|
CanceledAt *time.Time `json:"canceledAt,omitempty"` |
87
|
|
|
ExpiresAt *time.Time `json:"expiresAt,omitempty"` |
88
|
|
|
ExpiredAt *time.Time `json:"expiredAt,omitempty"` |
89
|
|
|
FailedAt *time.Time `json:"failedAt,omitempty"` |
90
|
|
|
DueDate *ShortDate `json:"dueDate,omitempty"` |
91
|
|
|
BillingAddress *Address `json:"billingAddress,omitempty"` |
92
|
|
|
ShippingAddress *PaymentDetailsAddress `json:"shippingAddress,omitempty"` |
93
|
|
|
Mode Mode `json:"mode,omitempty"` |
94
|
|
|
Locale Locale `json:"locale,omitempty"` |
95
|
|
|
RestrictPaymentMethodsToCountry Locale `json:"restrictPaymentMethodsToCountry,omitempty"` |
96
|
|
|
Method PaymentMethod `json:"method,omitempty"` |
97
|
|
|
Links PaymentLinks `json:"_links,omitempty"` |
98
|
|
|
SequenceType SequenceType `json:"sequenceType,omitempty"` |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// PaymentLinks describes all the possible links to be returned with |
102
|
|
|
// a payment object. |
103
|
|
|
type PaymentLinks struct { |
104
|
|
|
Self *URL `json:"self,omitempty"` |
105
|
|
|
Checkout *URL `json:"checkout,omitempty"` |
106
|
|
|
ChangePaymentState *URL `json:"changePaymentState,omitempty"` |
107
|
|
|
Refunds *URL `json:"refunds,omitempty"` |
108
|
|
|
ChargeBacks *URL `json:"chargebacks,omitempty"` |
109
|
|
|
Captures *URL `json:"captures,omitempty"` |
110
|
|
|
Settlement *URL `json:"settlement,omitempty"` |
111
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
112
|
|
|
Mandate *URL `json:"mandate,omitempty"` |
113
|
|
|
Subscription *URL `json:"subscription,omitempty"` |
114
|
|
|
Customer *URL `json:"customer,omitempty"` |
115
|
|
|
Order *URL `json:"order,omitempty"` |
116
|
|
|
Dashboard *URL `json:"dashboard,omitempty"` |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// PaymentOptions describes payments endpoint valid query string parameters. |
120
|
|
|
// |
121
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/get-payment |
122
|
|
|
type PaymentOptions struct { |
123
|
|
|
Include string `url:"include,omitempty"` |
124
|
|
|
Embed string `url:"embed,omitempty"` |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// ListPaymentOptions describes list payments endpoint valid query string parameters. |
128
|
|
|
type ListPaymentOptions struct { |
129
|
|
|
Limit int `url:"limit,omitempty"` |
130
|
|
|
Include string `url:"include,omitempty"` |
131
|
|
|
Embed string `url:"embed,omitempty"` |
132
|
|
|
ProfileID string `url:"profileId,omitempty"` |
133
|
|
|
From string `url:"from,omitempty"` |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// PaymentsService instance operates over payment resources |
137
|
|
|
type PaymentsService service |
138
|
|
|
|
139
|
|
|
// Get retrieves a single payment object by its payment token. |
140
|
|
|
func (ps *PaymentsService) Get(id string, options *PaymentOptions) (p Payment, err error) { |
141
|
1 |
|
u := fmt.Sprintf("v2/payments/%s", id) |
142
|
1 |
|
if options != nil { |
143
|
1 |
|
v, _ := query.Values(options) |
144
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
145
|
|
|
} |
146
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodGet, u, nil) |
147
|
1 |
|
if err != nil { |
148
|
1 |
|
return |
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, &p); err != nil { |
155
|
1 |
|
return |
156
|
|
|
} |
157
|
1 |
|
return |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Create stores a new payment object attached to your Mollie account. |
161
|
|
|
// |
162
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/create-payment# |
163
|
|
|
func (ps *PaymentsService) Create(p Payment, options *PaymentOptions) (np Payment, err error) { |
164
|
1 |
|
u := "v2/payments" |
165
|
1 |
|
if options != nil { |
166
|
|
|
v, _ := query.Values(options) |
167
|
|
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
if ps.client.HasAccessToken() && ps.client.config.testing { |
171
|
1 |
|
p.TestMode = true |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodPost, u, p) |
175
|
1 |
|
if err != nil { |
176
|
1 |
|
return |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
res, err := ps.client.Do(req) |
180
|
1 |
|
if err != nil { |
181
|
1 |
|
return |
182
|
|
|
} |
183
|
1 |
|
if err = json.Unmarshal(res.content, &np); err != nil { |
184
|
1 |
|
return |
185
|
|
|
} |
186
|
1 |
|
return |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// Cancel removes a payment (if possible) from your Mollie account. |
190
|
|
|
// |
191
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/cancel-payment |
192
|
|
|
func (ps *PaymentsService) Cancel(id string) (p Payment, err error) { |
193
|
1 |
|
u := fmt.Sprintf("v2/payments/%s", id) |
194
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodDelete, u, nil) |
195
|
1 |
|
if err != nil { |
196
|
1 |
|
return |
197
|
|
|
} |
198
|
1 |
|
res, err := ps.client.Do(req) |
199
|
1 |
|
if err != nil { |
200
|
1 |
|
return |
201
|
|
|
} |
202
|
1 |
|
if err = json.Unmarshal(res.content, &p); err != nil { |
203
|
1 |
|
return |
204
|
|
|
} |
205
|
1 |
|
return |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Update can be used to update some details of a created payment. |
209
|
|
|
// |
210
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/update-payment# |
211
|
|
|
func (ps *PaymentsService) Update(id string, up Payment) (p Payment, err error) { |
212
|
1 |
|
u := fmt.Sprintf("v2/payments/%s", id) |
213
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodPatch, u, up) |
214
|
1 |
|
if err != nil { |
215
|
1 |
|
return |
216
|
|
|
} |
217
|
1 |
|
res, err := ps.client.Do(req) |
218
|
1 |
|
if err != nil { |
219
|
1 |
|
return |
220
|
|
|
} |
221
|
1 |
|
if err = json.Unmarshal(res.content, &p); err != nil { |
222
|
1 |
|
return |
223
|
|
|
} |
224
|
1 |
|
return |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// PaymentList describes how a list of payments will be retrieved by Mollie. |
228
|
|
|
type PaymentList struct { |
229
|
|
|
Count int `json:"count,omitempty"` |
230
|
|
|
Embedded struct { |
231
|
|
|
Payments []Payment |
232
|
|
|
} `json:"_embedded,omitempty"` |
233
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// List retrieves a list of payments associated with your account/organization. |
237
|
|
|
// |
238
|
|
|
// See: https://docs.mollie.com/reference/v2/payments-api/list-payments |
239
|
|
|
func (ps *PaymentsService) List(options *ListPaymentOptions) (pl PaymentList, err error) { |
240
|
1 |
|
u := "v2/payments" |
241
|
1 |
|
if options != nil { |
242
|
1 |
|
v, _ := query.Values(options) |
243
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
244
|
|
|
} |
245
|
1 |
|
req, err := ps.client.NewAPIRequest(http.MethodGet, u, nil) |
246
|
1 |
|
if err != nil { |
247
|
1 |
|
return |
248
|
|
|
} |
249
|
1 |
|
res, err := ps.client.Do(req) |
250
|
1 |
|
if err != nil { |
251
|
1 |
|
return |
252
|
|
|
} |
253
|
1 |
|
if err = json.Unmarshal(res.content, &pl); err != nil { |
254
|
1 |
|
return |
255
|
|
|
} |
256
|
1 |
|
return |
257
|
|
|
} |
258
|
|
|
|