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
Push — master ( 7c1d1b...1170d7 )
by Victor Hugo
57s queued 12s
created

mollie/payments.go   A

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
cc 24
eloc 160
dl 0
loc 243
c 0
b 0
f 0
ccs 60
cts 60
cp 1
crap 24
rs 10

5 Methods

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