GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#124)
by Victor Hugo
02:36 queued 01:14
created

mollie.*SubscriptionsService.Create   B

Complexity

Conditions 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

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