|
1
|
|
|
package mollie |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"encoding/json" |
|
6
|
|
|
"fmt" |
|
7
|
|
|
"time" |
|
8
|
|
|
) |
|
9
|
|
|
|
|
10
|
|
|
// SubscriptionStatus contains references to valid subscription statuses. |
|
11
|
|
|
type SubscriptionStatus string |
|
12
|
|
|
|
|
13
|
|
|
// Available subscription statuses. |
|
14
|
|
|
const ( |
|
15
|
|
|
SubscriptionStatusPending SubscriptionStatus = "pending" |
|
16
|
|
|
SubscriptionStatusActive SubscriptionStatus = "active" |
|
17
|
|
|
SubscriptionStatusCanceled SubscriptionStatus = "canceled" |
|
18
|
|
|
SubscriptionStatusSuspended SubscriptionStatus = "suspended" |
|
19
|
|
|
SubscriptionStatusCompleted SubscriptionStatus = "completed" |
|
20
|
|
|
) |
|
21
|
|
|
|
|
22
|
|
|
// CreateSubscription contains the fields that are required to create a subscription. |
|
23
|
|
|
type CreateSubscription struct { |
|
24
|
|
|
Times int `json:"times,omitempty"` |
|
25
|
|
|
Interval string `json:"interval,omitempty"` |
|
26
|
|
|
Description string `json:"description,omitempty"` |
|
27
|
|
|
MandateID string `json:"mandateId,omitempty"` |
|
28
|
|
|
WebhookURL string `json:"webhookUrl,omitempty"` |
|
29
|
|
|
Amount *Amount `json:"amount,omitempty"` |
|
30
|
|
|
StartDate *ShortDate `json:"startDate,omitempty"` |
|
31
|
|
|
Method PaymentMethod `json:"method,omitempty"` |
|
32
|
|
|
Metadata any `json:"metadata,omitempty"` |
|
33
|
|
|
SubscriptionAccessTokenFields |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// SubscriptionAccessTokenFields contains the fields that are available when using an access token. |
|
37
|
|
|
type SubscriptionAccessTokenFields struct { |
|
38
|
|
|
Testmode bool `json:"testmode,omitempty"` |
|
39
|
|
|
ProfileID string `json:"profileId,omitempty"` |
|
40
|
|
|
ApplicationFee *ApplicationFee `json:"applicationFee,omitempty"` |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// UpdateSubscription contains the fields that are required to create a subscription. |
|
44
|
|
|
type UpdateSubscription struct { |
|
45
|
|
|
Times int `json:"times,omitempty"` |
|
46
|
|
|
Interval string `json:"interval,omitempty"` |
|
47
|
|
|
Description string `json:"description,omitempty"` |
|
48
|
|
|
MandateID string `json:"mandateId,omitempty"` |
|
49
|
|
|
WebhookURL string `json:"webhookUrl,omitempty"` |
|
50
|
|
|
Amount *Amount `json:"amount,omitempty"` |
|
51
|
|
|
StartDate *ShortDate `json:"startDate,omitempty"` |
|
52
|
|
|
Method PaymentMethod `json:"method,omitempty"` |
|
53
|
|
|
Metadata any `json:"metadata,omitempty"` |
|
54
|
|
|
SubscriptionAccessTokenFields |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// SubscriptionLinks contains several URL objects relevant to the subscription. |
|
58
|
|
|
type SubscriptionLinks struct { |
|
59
|
|
|
Self *URL `json:"self,omitempty"` |
|
60
|
|
|
Customer *URL `json:"customer,omitempty"` |
|
61
|
|
|
Profile *URL `json:"profile,omitempty"` |
|
62
|
|
|
Payments *URL `json:"payments,omitempty"` |
|
63
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Subscription contains information about a customer subscription. |
|
67
|
|
|
type Subscription struct { |
|
68
|
|
|
Times int `json:"times,omitempty"` |
|
69
|
|
|
TimesRemaining int `json:"timesRemaining,omitempty"` |
|
70
|
|
|
Resource string `json:"resource,omitempty"` |
|
71
|
|
|
ID string `json:"id,omitempty"` |
|
72
|
|
|
Interval string `json:"interval,omitempty"` |
|
73
|
|
|
Description string `json:"description,omitempty"` |
|
74
|
|
|
MandateID string `json:"mandateId,omitempty"` |
|
75
|
|
|
WebhookURL string `json:"webhookUrl,omitempty"` |
|
76
|
|
|
Amount *Amount `json:"amount,omitempty"` |
|
77
|
1 |
|
ApplicationFee *ApplicationFee `json:"applicationFee,omitempty"` |
|
78
|
|
|
StartDate *ShortDate `json:"startDate,omitempty"` |
|
79
|
1 |
|
NextPaymentDate *ShortDate `json:"nextPaymentDate,omitempty"` |
|
80
|
1 |
|
CreatedAT *time.Time `json:"createdAt,omitempty"` |
|
81
|
1 |
|
CanceledAt *time.Time `json:"canceledAt,omitempty"` |
|
82
|
|
|
Mode Mode `json:"mode,omitempty"` |
|
83
|
|
|
Status SubscriptionStatus `json:"status,omitempty"` |
|
84
|
1 |
|
Method PaymentMethod `json:"method,omitempty"` |
|
85
|
1 |
|
Metadata any `json:"metadata,omitempty"` |
|
86
|
|
|
Links SubscriptionLinks `json:"_links,omitempty"` |
|
87
|
|
|
} |
|
88
|
1 |
|
|
|
89
|
|
|
// SubscriptionsList describes the response for subscription list endpoints. |
|
90
|
|
|
type SubscriptionsList struct { |
|
91
|
|
|
Count int `json:"count,omitempty"` |
|
92
|
|
|
Embedded struct { |
|
93
|
|
|
Subscriptions []*Subscription |
|
94
|
|
|
} `json:"_embedded,omitempty"` |
|
95
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// ListSubscriptionsOptions holds query string parameters valid for subscription lists. |
|
99
|
1 |
|
type ListSubscriptionsOptions struct { |
|
100
|
|
|
Testmode bool `url:"testmode,omitempty"` |
|
101
|
1 |
|
Limit int `url:"limit,omitempty"` |
|
102
|
1 |
|
From string `url:"from,omitempty"` |
|
103
|
|
|
ProfileID string `url:"profileId,omitempty"` |
|
104
|
|
|
} |
|
105
|
1 |
|
|
|
106
|
1 |
|
// SubscriptionsService operates over subscriptions resource. |
|
107
|
1 |
|
type SubscriptionsService service |
|
108
|
|
|
|
|
109
|
|
|
// Get retrieves a customer's subscription |
|
110
|
1 |
|
// |
|
111
|
1 |
|
// See: https://docs.mollie.com/reference/get-subscription |
|
112
|
|
|
func (ss *SubscriptionsService) Get(ctx context.Context, customer, subscription string) ( |
|
113
|
|
|
res *Response, |
|
114
|
1 |
|
s *Subscription, |
|
115
|
|
|
err error, |
|
116
|
|
|
) { |
|
117
|
|
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s", customer, subscription) |
|
118
|
|
|
|
|
119
|
|
|
res, err = ss.client.get(ctx, u, nil) |
|
120
|
|
|
if err != nil { |
|
121
|
|
|
return |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if err = json.Unmarshal(res.content, &s); err != nil { |
|
125
|
1 |
|
return |
|
126
|
|
|
} |
|
127
|
1 |
|
|
|
128
|
1 |
|
return |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Create stores a new subscription for a given customer |
|
132
|
1 |
|
// |
|
133
|
1 |
|
// See: https://docs.mollie.com/reference/create-subscription |
|
134
|
|
|
func (ss *SubscriptionsService) Create(ctx context.Context, customer string, sc CreateSubscription) ( |
|
135
|
|
|
res *Response, |
|
136
|
1 |
|
s *Subscription, |
|
137
|
|
|
err error, |
|
138
|
|
|
) { |
|
139
|
|
|
uri := fmt.Sprintf("v2/customers/%s/subscriptions", customer) |
|
140
|
|
|
|
|
141
|
|
|
if ss.client.HasAccessToken() && ss.client.config.testing { |
|
142
|
|
|
sc.Testmode = true |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
res, err = ss.client.post(ctx, uri, sc, nil) |
|
146
|
|
|
if err != nil { |
|
147
|
1 |
|
return |
|
148
|
|
|
} |
|
149
|
1 |
|
|
|
150
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
|
151
|
1 |
|
return |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
return |
|
155
|
1 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
// Update changes fields on a subscription object |
|
158
|
1 |
|
// |
|
159
|
|
|
// See: https://docs.mollie.com/reference/update-subscription |
|
160
|
|
|
func (ss *SubscriptionsService) Update(ctx context.Context, customer, subscription string, sc UpdateSubscription) ( |
|
161
|
|
|
res *Response, |
|
162
|
|
|
s *Subscription, |
|
163
|
|
|
err error, |
|
164
|
|
|
) { |
|
165
|
|
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s", customer, subscription) |
|
166
|
|
|
|
|
167
|
|
|
res, err = ss.client.patch(ctx, u, sc) |
|
168
|
|
|
if err != nil { |
|
169
|
|
|
return |
|
170
|
|
|
} |
|
171
|
1 |
|
|
|
172
|
|
|
if err = json.Unmarshal(res.content, &s); err != nil { |
|
173
|
1 |
|
return |
|
174
|
1 |
|
} |
|
175
|
1 |
|
|
|
176
|
|
|
return |
|
177
|
|
|
} |
|
178
|
1 |
|
|
|
179
|
1 |
|
// Cancel cancels a subscription. |
|
180
|
|
|
// |
|
181
|
|
|
// See: https://docs.mollie.com/reference/cancel-subscription |
|
182
|
1 |
|
func (ss *SubscriptionsService) Cancel(ctx context.Context, customer, subscription string) ( |
|
183
|
|
|
res *Response, |
|
184
|
|
|
s *Subscription, |
|
185
|
|
|
err error, |
|
186
|
|
|
) { |
|
187
|
|
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s", customer, subscription) |
|
188
|
|
|
|
|
189
|
|
|
res, err = ss.client.delete(ctx, u) |
|
190
|
|
|
if err != nil { |
|
191
|
|
|
return |
|
192
|
|
|
} |
|
193
|
1 |
|
|
|
194
|
|
|
if err = json.Unmarshal(res.content, &s); err != nil { |
|
195
|
1 |
|
return |
|
196
|
1 |
|
} |
|
197
|
1 |
|
|
|
198
|
|
|
return |
|
199
|
|
|
} |
|
200
|
1 |
|
|
|
201
|
1 |
|
// All retrieves all subscriptions, ordered from newest to oldest. |
|
202
|
|
|
// By using an API key all the subscriptions created with the current website profile will be returned. |
|
203
|
|
|
// In the case of an OAuth Access Token relies the website profile on the profileId field |
|
204
|
1 |
|
// |
|
205
|
|
|
// See: https://docs.mollie.com/reference/list-all-subscriptions |
|
206
|
|
|
func (ss *SubscriptionsService) All(ctx context.Context, opts *ListSubscriptionsOptions) ( |
|
207
|
|
|
res *Response, |
|
208
|
|
|
sl *SubscriptionsList, |
|
209
|
|
|
err error, |
|
210
|
|
|
) { |
|
211
|
|
|
u := "v2/subscriptions" |
|
212
|
|
|
|
|
213
|
|
|
res, err = ss.list(ctx, u, opts) |
|
214
|
|
|
if err != nil { |
|
215
|
1 |
|
return |
|
216
|
|
|
} |
|
217
|
1 |
|
|
|
218
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
|
219
|
1 |
|
return |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
1 |
|
return |
|
223
|
1 |
|
} |
|
224
|
|
|
|
|
225
|
|
|
// List retrieves all subscriptions of a customer |
|
226
|
1 |
|
// |
|
227
|
|
|
// See: https://docs.mollie.com/reference/list-subscriptions |
|
228
|
|
|
func (ss *SubscriptionsService) List(ctx context.Context, customer string, opts *ListSubscriptionsOptions) ( |
|
229
|
|
|
res *Response, |
|
230
|
1 |
|
sl *SubscriptionsList, |
|
231
|
1 |
|
err error, |
|
232
|
1 |
|
) { |
|
233
|
|
|
u := fmt.Sprintf("v2/customers/%s/subscriptions", customer) |
|
234
|
|
|
|
|
235
|
1 |
|
res, err = ss.list(ctx, u, opts) |
|
236
|
|
|
if err != nil { |
|
237
|
|
|
return |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
|
241
|
|
|
return |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
// ListPayments retrieves all payments of a specific subscriptions of a customer |
|
248
|
|
|
// |
|
249
|
|
|
// See: https://docs.mollie.com/reference/list-subscription-payments |
|
250
|
|
|
func (ss *SubscriptionsService) ListPayments( |
|
251
|
|
|
ctx context.Context, |
|
252
|
|
|
customer, subscription string, |
|
253
|
|
|
opts *ListSubscriptionsOptions, |
|
254
|
|
|
) ( |
|
255
|
|
|
res *Response, |
|
256
|
|
|
sl *PaymentList, |
|
257
|
|
|
err error, |
|
258
|
|
|
) { |
|
259
|
|
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s/payments", customer, subscription) |
|
260
|
|
|
|
|
261
|
|
|
res, err = ss.list(ctx, u, opts) |
|
262
|
|
|
if err != nil { |
|
263
|
|
|
return |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
|
267
|
|
|
return |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
func (ss *SubscriptionsService) list(ctx context.Context, uri string, opts interface{}) (r *Response, err error) { |
|
274
|
|
|
r, err = ss.client.get(ctx, uri, opts) |
|
275
|
|
|
if err != nil { |
|
276
|
|
|
return |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
return |
|
280
|
|
|
} |
|
281
|
|
|
|