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
01:19
created

mollie.*PaymentsService.Get   A

Complexity

Conditions 5

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

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