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