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
|
|
|
// SubscriptionsService operates over subscriptions resource |
13
|
|
|
type SubscriptionsService service |
14
|
|
|
|
15
|
|
|
// SubscriptionStatus contains references to valid subscription statuses |
16
|
|
|
type SubscriptionStatus string |
17
|
|
|
|
18
|
|
|
// Available subscription statuses |
19
|
|
|
const ( |
20
|
|
|
SubscriptionStatusPending SubscriptionStatus = "pending" |
21
|
|
|
SubscriptionStatusActive SubscriptionStatus = "active" |
22
|
|
|
SubscriptionStatusCanceled SubscriptionStatus = "canceled" |
23
|
|
|
SubscriptionStatusSuspended SubscriptionStatus = "suspended" |
24
|
|
|
SubscriptionStatusCompleted SubscriptionStatus = "completed" |
25
|
|
|
) |
26
|
|
|
|
27
|
|
|
// SubscriptionLinks contains several URL objects relevant to the subscription |
28
|
|
|
type SubscriptionLinks struct { |
29
|
|
|
Self URL `json:"self,omitempty"` |
30
|
|
|
Customer URL `json:"customer,omitempty"` |
31
|
|
|
Payments URL `json:"payments,omitempty"` |
32
|
|
|
Documentation URL `json:"documentation,omitempty"` |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Subscription contains information about a customer subscription |
36
|
|
|
type Subscription struct { |
37
|
|
|
Resource string `json:"resource,omitempty"` |
38
|
|
|
ID string `json:"id,omitempty"` |
39
|
|
|
Mode Mode `json:"mode,omitempty"` |
40
|
|
|
CreatedAT *time.Time `json:"createdAt,omitempty"` |
41
|
|
|
Status SubscriptionStatus `json:"status,omitempty"` |
42
|
|
|
Amount Amount `json:"amount,omitempty"` |
43
|
|
|
Times int `json:"times,omitempty"` |
44
|
|
|
TimesRemaining int `json:"timesRemaining,omitempty"` |
45
|
|
|
Interval string `json:"interval,omitempty"` |
46
|
|
|
StartDate *ShortDate `json:"startDate,omitempty"` |
47
|
|
|
NextPaymentDate *ShortDate `json:"nextPaymentDate,omitempty"` |
48
|
|
|
Description string `json:"description,omitempty"` |
49
|
|
|
Method PaymentMethod `json:"method,omitempty"` |
50
|
|
|
CanceledAt *time.Time `json:"canceledAt,omitempty"` |
51
|
|
|
WebhookURL string `json:"webhookUrl,omitempty"` |
52
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"` |
53
|
|
|
ApplicationFee ApplicationFee `json:"applicationFee,omitempty"` |
54
|
|
|
Links SubscriptionLinks `json:"_links,omitempty"` |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// SubscriptionList describes the response for subscription list endpoints |
58
|
|
|
type SubscriptionList struct { |
59
|
|
|
Count int `json:"count,omitempty"` |
60
|
|
|
Embedded struct { |
61
|
|
|
Subscriptions []Subscription |
62
|
|
|
} `json:"_embedded,omitempty"` |
63
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// SubscriptionListOptions holds query string parameters valid for subscription lists |
67
|
|
|
type SubscriptionListOptions struct { |
68
|
|
|
From string `url:"from,omitempty"` |
69
|
|
|
Limit int `url:"limit,omitempty"` |
70
|
|
|
ProfileID string `url:"profileId,omitempty"` |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Get retrieves a customer's subscription |
74
|
|
|
// |
75
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/get-subscription |
76
|
|
|
func (ss *SubscriptionsService) Get(cID, sID string) (s *Subscription, err error) { |
77
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s", cID, sID) |
78
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodGet, u, nil) |
79
|
1 |
|
if err != nil { |
80
|
1 |
|
return |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
res, err := ss.client.Do(req) |
84
|
1 |
|
if err != nil { |
85
|
1 |
|
return |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
89
|
1 |
|
return |
90
|
|
|
} |
91
|
1 |
|
return |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Create stores a new subscription for a given customer |
95
|
|
|
// |
96
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/create-subscription |
97
|
|
|
func (ss *SubscriptionsService) Create(cID string, sc *Subscription) (s *Subscription, err error) { |
98
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions", cID) |
99
|
|
|
|
100
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodPost, u, sc) |
101
|
1 |
|
if err != nil { |
102
|
1 |
|
return |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
res, err := ss.client.Do(req) |
106
|
1 |
|
if err != nil { |
107
|
1 |
|
return |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
111
|
1 |
|
return |
112
|
|
|
} |
113
|
1 |
|
return |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Update changes fields on a subscription object |
117
|
|
|
// |
118
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/update-subscription |
119
|
|
|
func (ss *SubscriptionsService) Update(cID, sID string, sc *Subscription) (s *Subscription, err error) { |
120
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s", cID, sID) |
121
|
|
|
|
122
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodPatch, u, sc) |
123
|
1 |
|
if err != nil { |
124
|
1 |
|
return |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
res, err := ss.client.Do(req) |
128
|
1 |
|
if err != nil { |
129
|
1 |
|
return |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
133
|
1 |
|
return |
134
|
|
|
} |
135
|
1 |
|
return |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// Delete cancels a subscription |
139
|
|
|
// |
140
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/cancel-subscription |
141
|
|
|
func (ss *SubscriptionsService) Delete(cID, sID string) (s *Subscription, err error) { |
142
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s", cID, sID) |
143
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodDelete, u, nil) |
144
|
1 |
|
if err != nil { |
145
|
1 |
|
return |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
res, err := ss.client.Do(req) |
149
|
1 |
|
if err != nil { |
150
|
1 |
|
return |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
154
|
1 |
|
return |
155
|
|
|
} |
156
|
1 |
|
return |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// All retrieves all subscriptions, ordered from newest to oldest. |
160
|
|
|
// By using an API key all the subscriptions created with the current website profile will be returned. |
161
|
|
|
// In the case of an OAuth Access Token relies the website profile on the profileId field |
162
|
|
|
// |
163
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/list-all-subscriptions |
164
|
|
|
func (ss *SubscriptionsService) All(options *SubscriptionListOptions) (sl *SubscriptionList, err error) { |
165
|
1 |
|
u := fmt.Sprintf("v2/subscriptions") |
166
|
|
|
|
167
|
1 |
|
if options != nil { |
168
|
1 |
|
v, _ := query.Values(options) |
169
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
res, err := ss.list(u) |
173
|
1 |
|
if err != nil { |
174
|
1 |
|
return |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
178
|
1 |
|
return |
179
|
|
|
} |
180
|
1 |
|
return |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// List retrieves all subscriptions of a customer |
184
|
|
|
// |
185
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/list-subscriptions |
186
|
|
|
func (ss *SubscriptionsService) List(cID string, options *SubscriptionListOptions) (sl *SubscriptionList, err error) { |
187
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions", cID) |
188
|
|
|
|
189
|
1 |
|
if options != nil { |
190
|
1 |
|
v, _ := query.Values(options) |
191
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
res, err := ss.list(u) |
195
|
1 |
|
if err != nil { |
196
|
1 |
|
return |
197
|
|
|
} |
198
|
|
|
|
199
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
200
|
1 |
|
return |
201
|
|
|
} |
202
|
1 |
|
return |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// GetPayments retrieves all payments of a specific subscriptions of a customer |
206
|
|
|
// |
207
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/list-subscriptions-payments |
208
|
|
|
func (ss *SubscriptionsService) GetPayments(cID, sID string, options *SubscriptionListOptions) (sl *PaymentList, err error) { |
209
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s/payments", cID, sID) |
210
|
|
|
|
211
|
1 |
|
if options != nil { |
212
|
1 |
|
v, _ := query.Values(options) |
213
|
1 |
|
u = fmt.Sprintf("%s?%s", u, v.Encode()) |
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
res, err := ss.list(u) |
217
|
1 |
|
if err != nil { |
218
|
1 |
|
return |
219
|
|
|
} |
220
|
|
|
|
221
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
222
|
1 |
|
return |
223
|
|
|
} |
224
|
1 |
|
return |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
func (ss *SubscriptionsService) list(uri string) (r *Response, err error) { |
228
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodGet, uri, nil) |
229
|
1 |
|
if err != nil { |
230
|
1 |
|
return |
231
|
|
|
} |
232
|
|
|
|
233
|
1 |
|
r, err = ss.client.Do(req) |
234
|
1 |
|
if err != nil { |
235
|
1 |
|
return |
236
|
|
|
} |
237
|
1 |
|
return |
238
|
|
|
} |
239
|
|
|
|