1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"fmt" |
7
|
|
|
"time" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// SubscriptionsService operates over subscriptions resource. |
11
|
|
|
type SubscriptionsService service |
12
|
|
|
|
13
|
|
|
// SubscriptionStatus contains references to valid subscription statuses. |
14
|
|
|
type SubscriptionStatus string |
15
|
|
|
|
16
|
|
|
// Available subscription statuses. |
17
|
|
|
const ( |
18
|
|
|
SubscriptionStatusPending SubscriptionStatus = "pending" |
19
|
|
|
SubscriptionStatusActive SubscriptionStatus = "active" |
20
|
|
|
SubscriptionStatusCanceled SubscriptionStatus = "canceled" |
21
|
|
|
SubscriptionStatusSuspended SubscriptionStatus = "suspended" |
22
|
|
|
SubscriptionStatusCompleted SubscriptionStatus = "completed" |
23
|
|
|
) |
24
|
|
|
|
25
|
|
|
// SubscriptionLinks contains several URL objects relevant to the subscription. |
26
|
|
|
type SubscriptionLinks struct { |
27
|
|
|
Self *URL `json:"self,omitempty"` |
28
|
|
|
Customer *URL `json:"customer,omitempty"` |
29
|
|
|
Payments *URL `json:"payments,omitempty"` |
30
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Subscription contains information about a customer subscription. |
34
|
|
|
type Subscription struct { |
35
|
|
|
Resource string `json:"resource,omitempty"` |
36
|
|
|
ID string `json:"id,omitempty"` |
37
|
|
|
MandateID string `json:"mandateId,omitempty"` |
38
|
|
|
Mode Mode `json:"mode,omitempty"` |
39
|
|
|
CreatedAT *time.Time `json:"createdAt,omitempty"` |
40
|
|
|
Status SubscriptionStatus `json:"status,omitempty"` |
41
|
|
|
Amount *Amount `json:"amount,omitempty"` |
42
|
|
|
Times int `json:"times,omitempty"` |
43
|
|
|
TimesRemaining int `json:"timesRemaining,omitempty"` |
44
|
|
|
Interval string `json:"interval,omitempty"` |
45
|
|
|
StartDate *ShortDate `json:"startDate,omitempty"` |
46
|
|
|
NextPaymentDate *ShortDate `json:"nextPaymentDate,omitempty"` |
47
|
|
|
Description string `json:"description,omitempty"` |
48
|
|
|
Method PaymentMethod `json:"method,omitempty"` |
49
|
|
|
CanceledAt *time.Time `json:"canceledAt,omitempty"` |
50
|
|
|
WebhookURL string `json:"webhookUrl,omitempty"` |
51
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"` |
52
|
|
|
ApplicationFee *ApplicationFee `json:"applicationFee,omitempty"` |
53
|
|
|
TestMode bool `json:"testmode,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(ctx context.Context, cID, sID string) (res *Response, s *Subscription, err error) { |
77
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s", cID, sID) |
78
|
|
|
|
79
|
1 |
|
res, err = ss.client.get(ctx, u, nil) |
80
|
1 |
|
if err != nil { |
81
|
1 |
|
return |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
85
|
1 |
|
return |
86
|
|
|
} |
87
|
1 |
|
return |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Create stores a new subscription for a given customer |
91
|
|
|
// |
92
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/create-subscription |
93
|
|
|
func (ss *SubscriptionsService) Create(ctx context.Context, cID string, sc *Subscription) (res *Response, s *Subscription, err error) { |
94
|
1 |
|
uri := fmt.Sprintf("v2/customers/%s/subscriptions", cID) |
95
|
|
|
|
96
|
1 |
|
if ss.client.HasAccessToken() && ss.client.config.testing { |
97
|
1 |
|
sc.TestMode = true |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
res, err = ss.client.post(ctx, uri, sc, nil) |
101
|
1 |
|
if err != nil { |
102
|
1 |
|
return |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
106
|
1 |
|
return |
107
|
|
|
} |
108
|
1 |
|
return |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Update changes fields on a subscription object |
112
|
|
|
// |
113
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/update-subscription |
114
|
|
|
func (ss *SubscriptionsService) Update(ctx context.Context, cID, sID string, sc *Subscription) (res *Response, s *Subscription, err error) { |
115
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s", cID, sID) |
116
|
|
|
|
117
|
1 |
|
res, err = ss.client.patch(ctx, u, sc, nil) |
118
|
1 |
|
if err != nil { |
119
|
1 |
|
return |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
123
|
1 |
|
return |
124
|
|
|
} |
125
|
1 |
|
return |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Delete cancels a subscription |
129
|
|
|
// |
130
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/cancel-subscription |
131
|
|
|
func (ss *SubscriptionsService) Delete(ctx context.Context, cID, sID string) (res *Response, s *Subscription, err error) { |
132
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s", cID, sID) |
133
|
|
|
|
134
|
1 |
|
res, err = ss.client.delete(ctx, u, nil) |
135
|
1 |
|
if err != nil { |
136
|
1 |
|
return |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
140
|
1 |
|
return |
141
|
|
|
} |
142
|
1 |
|
return |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// All retrieves all subscriptions, ordered from newest to oldest. |
146
|
|
|
// By using an API key all the subscriptions created with the current website profile will be returned. |
147
|
|
|
// In the case of an OAuth Access Token relies the website profile on the profileId field |
148
|
|
|
// |
149
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/list-all-subscriptions |
150
|
|
|
func (ss *SubscriptionsService) All(ctx context.Context, opts *SubscriptionListOptions) (res *Response, sl *SubscriptionList, err error) { |
151
|
1 |
|
u := "v2/subscriptions" |
152
|
|
|
|
153
|
1 |
|
res, err = ss.list(ctx, u, opts) |
154
|
1 |
|
if err != nil { |
155
|
1 |
|
return |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
159
|
1 |
|
return |
160
|
|
|
} |
161
|
1 |
|
return |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// List retrieves all subscriptions of a customer |
165
|
|
|
// |
166
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/list-subscriptions |
167
|
|
|
func (ss *SubscriptionsService) List(ctx context.Context, cID string, opts *SubscriptionListOptions) (res *Response, sl *SubscriptionList, err error) { |
168
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions", cID) |
169
|
|
|
|
170
|
1 |
|
res, err = ss.list(ctx, u, opts) |
171
|
1 |
|
if err != nil { |
172
|
1 |
|
return |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
176
|
1 |
|
return |
177
|
|
|
} |
178
|
1 |
|
return |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// GetPayments retrieves all payments of a specific subscriptions of a customer |
182
|
|
|
// |
183
|
|
|
// See: https://docs.mollie.com/reference/v2/subscriptions-api/list-subscriptions-payments |
184
|
|
|
func (ss *SubscriptionsService) GetPayments(ctx context.Context, cID, sID string, opts *SubscriptionListOptions) (res *Response, sl *PaymentList, err error) { |
185
|
1 |
|
u := fmt.Sprintf("v2/customers/%s/subscriptions/%s/payments", cID, sID) |
186
|
|
|
|
187
|
1 |
|
res, err = ss.list(ctx, u, opts) |
188
|
1 |
|
if err != nil { |
189
|
1 |
|
return |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
193
|
1 |
|
return |
194
|
|
|
} |
195
|
1 |
|
return |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
func (ss *SubscriptionsService) list(ctx context.Context, uri string, opts interface{}) (r *Response, err error) { |
199
|
1 |
|
r, err = ss.client.get(ctx, uri, opts) |
200
|
1 |
|
if err != nil { |
201
|
1 |
|
return |
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
return |
205
|
|
|
} |
206
|
|
|
|