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 (#36)
by
unknown
01:26
created

mollie.*OrdersService.Get   A

Complexity

Conditions 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.3073

Importance

Changes 0
Metric Value
cc 5
eloc 14
dl 0
loc 22
c 0
b 0
f 0
ccs 10
cts 13
cp 0.7692
crap 5.3073
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
// Orders explain the items that customers need to pay for.
13
type Orders struct {
14
	Resource            string         `json:"resource,omitempty"`
15
	ID                  string         `json:"id,omitempty"`
16
	ProfileID           string         `json:"profileId,omitempty"`
17
	Method              *PaymentMethod `json:"method,omitempty"`
18
	Mode                *Mode          `json:"mode,omitempty"`
19
	Amount              *Amount        `json:"amount,omitempty"`
20
	AmountCaptured      *Amount        `json:"amountCaptured,omitempty"`
21
	AmountRefunded      *Amount        `json:"amountRefunded,omitempty"`
22
	Status              *OrderStatus   `json:"status,omitempty"`
23
	IsCancelable        bool           `json:"isCancelable,omitempty"`
24
	BillingAddress      *OrderAddress  `json:"billingAddress,omitempty"`
25
	ConsumerDateOfBirth *ShortDate     `json:"consumerDateOfBirth,omitempty"`
26
	OrderNumber         string         `json:"orderNumber,omitempty"`
27
	ShippingAddress     *OrderAddress  `json:"shippingAddress,omitempty"`
28
	Locale              *Locale        `json:"locale,omitempty"`
29
	Metadata            interface{}    `json:"metadata,omitempty"`
30
	RedirectURL         string         `json:"redirectUrl,omitempty"`
31
	Lines               []*OrderLines  `json:"lines,omitempty"`
32
	WebhookURL          string         `json:"webhookUrl,omitempty"`
33
	CreatedAt           *time.Time     `json:"createdAt,omitempty"`
34
	ExpiresAt           *time.Time     `json:"expiresAt,omitempty"`
35
	ExpiredAt           *time.Time     `json:"expiredAt,omitempty"`
36
	PaidAt              *time.Time     `json:"paidAt,omitempty"`
37
	AuthorizedAt        *time.Time     `json:"authorizedAt,omitempty"`
38
	CanceledAt          *time.Time     `json:"canceledAt,omitempty"`
39
	CompletedAt         *time.Time     `json:"completedAt,omitempty"`
40
	Embedded            struct {
41
		Payments []Payment `json:"payments,omitempty"`
42
		Refunds  []Refund  `json:"refunds,omitempty"`
43
	} `json:"_embedded,omitempty"`
44
	Links        *OrderLinks   `json:"_links,omitempty"`
45
	OrderPayment *OrderPayment `json:"payment,omitempty"`
46
	Description  string        `json:"description,omitempty"`
47
}
48
49
var requiredCreateParamOrder = "parameter required for creating an order: %+v"
50
51
// OrderPayment describes payment specific parameters that can be passed during order creation
52
type OrderPayment struct {
53
	ConsumerAccount   string          `json:"consumerAccount,omitempty"`
54
	CustomerID        string          `json:"customerId,omitempty"`
55
	CustomerReference string          `json:"customerReference,omitempty"`
56
	Issuer            string          `json:"issuer,omitempty"`
57
	MandateID         string          `json:"mandateId,omitempty"`
58
	SequenceType      *SequenceType   `json:"sequenceType,omitempty"`
59
	VoucherNumber     string          `json:"voucherNumber,omitempty"`
60
	VoucherPin        string          `json:"voucherPin,omitempty"`
61
	WebhookURL        string          `json:"webhookUrl,omitempty"`
62
	ApplicationFee    *ApplicationFee `json:"applicationFee,omitempty"`
63
	Method            *PaymentMethod  `json:"method,omitempty"`
64
}
65
66
// OrderStatus describes the valid order status.
67
type OrderStatus string
68
69
// Valid order status.
70
const (
71
	Created    OrderStatus = "created"
72
	Paid       OrderStatus = "paid"
73
	Authorized OrderStatus = "authorized"
74
	Canceled   OrderStatus = "canceled"
75
	Shipping   OrderStatus = "shipping"
76
	Completed  OrderStatus = "completed"
77
	Expired    OrderStatus = "expired"
78
)
79
80
// OrderAddress identify both the address and the person the order is billed or shipped to.
81
type OrderAddress struct {
82
	OrganizationName string       `json:"organizationName,omitempty"`
83
	Title            string       `json:"title,omitempty"`
84
	GivenName        string       `json:"givenName,omitempty"`
85
	FamilyName       string       `json:"familyName,omitempty"`
86
	Email            string       `json:"email,omitempty"`
87
	Phone            *PhoneNumber `json:"phone,omitempty"`
88
	StreetAndNumber  string       `json:"streetAndNumber,omitempty"`
89
	StreetAdditional string       `json:"streetAdditional,omitempty"`
90
	PostalCode       string       `json:"postalCode,omitempty"`
91
	City             string       `json:"city,omitempty"`
92
	Region           string       `json:"region,omitempty"`
93
	Country          string       `json:"country,omitempty"`
94
}
95
96
// OrderLinks describes an object with several URL objects
97
// relevant to the order.
98
// Every URL object will contain an href and a type field.
99
type OrderLinks struct {
100
	Self          *URL `json:"self,omitempty"`
101
	Checkout      *URL `json:"checkout,omitempty"`
102
	Documentation *URL `json:"documentation,omitempty"`
103
}
104
105
// OrderListLinks describes an object with several URL objects
106
// relevant to the order.
107
// Every URL object will contain an href and a type field.
108
type OrderListLinks struct {
109
	Self          *URL `json:"self,omitempty"`
110
	Previous      *URL `json:"previous,omitempty"`
111
	Next          *URL `json:"next,omitempty"`
112
	Documentation *URL `json:"documentation,omitempty"`
113
}
114
115
// OrderLines contain the actual things the customer bought.
116
type OrderLines struct {
117
	Resource           string           `json:"resource,omitempty"`
118
	ID                 string           `json:"id,omitempty"`
119
	OrderID            string           `json:"orderId,omitempty"`
120
	ProductType        *ProductType     `json:"type,omitempty"`
121
	Name               string           `json:"name,omitempty"`
122
	Amount             *Amount          `json:"amount,omitempty"`
123
	Status             *OrderLineStatus `json:"status,omitempty"`
124
	IsCancelable       bool             `json:"isCancelable,omitempty"`
125
	Quantity           int              `json:"quantity,omitempty"`
126
	QuantityShipped    int              `json:"quantityShipped,omitempty"`
127
	AmountShipped      *Amount          `json:"amountShipped,omitempty"`
128
	QuantityRefunded   int              `json:"quantityRefunded,omitempty"`
129
	AmountRefunded     *Amount          `json:"amountRefunded,omitempty"`
130
	QuantityCanceled   int              `json:"quantityCanceled,omitempty"`
131
	AmountCanceled     *Amount          `json:"amountCanceled,omitempty"`
132
	ShippableQuantity  int              `json:"shippableQuantity,omitempty"`
133
	RefundableQuantity int              `json:"refundableQuantity,omitempty"`
134
	CancelableQuantity int              `json:"cancelableQuantity,omitempty"`
135
	UnitPrice          *Amount          `json:"unitPrice,omitempty"`
136
	DiscountAmount     *Amount          `json:"discountAmount,omitempty"`
137
	TotalAmount        *Amount          `json:"totalAmount,omitempty"`
138
	VatRate            string           `json:"vatRate,omitempty"`
139
	VatAmount          *Amount          `json:"vatAmount,omitempty"`
140
	SKU                string           `json:"sku,omitempty"`
141
	CreatedAt          *time.Time       `json:"createdAt,omitempty"`
142
	Links              *OrderLineLinks  `json:"_links,omitempty"`
143
	ImageURL           string           `json:"imageUrl,omitempty"`
144
	ProductURL         string           `json:"productUrl,omitempty"`
145
}
146
147
// OrderList for containing the response of list orders
148
type OrderList struct {
149
	Count    int `json:"count,omitempty"`
150
	Embedded struct {
151
		Orders []Orders `json:"orders,omitempty"`
152
	} `json:"_embedded,omitempty"`
153
	Links OrderListLinks `json:"links,omitempty"`
154
}
155
156
// OrderListRefund for containing the response of list orders
157
type OrderListRefund struct {
158
	Count    int `json:"count,omitempty"`
159
	Embedded struct {
160
		Refunds []Refund `json:"refund,omitempty"`
161
	} `json:"_embedded,omitempty"`
162
	Links OrderListLinks `json:"links,omitempty"`
163
}
164
165
// ProductType describes the type of product bought, for example, a physical or a digital product.
166
type ProductType string
167
168
// Valid product type.
169
const (
170
	Physical        ProductType = "physical"
171
	Discount        ProductType = "discount"
172
	Digital         ProductType = "digital"
173
	ShippingFee     ProductType = "shipping_fee"
174
	StoreCredit     ProductType = "store_credit"
175
	GiftCardProduct ProductType = "gift_card"
176
	Surcharge       ProductType = "surcharge"
177
)
178
179
// OrderLineStatus describes status of the order line.
180
type OrderLineStatus string
181
182
// Valid order line status.
183
const (
184
	OrderLineCreated    OrderLineStatus = "created"
185
	OrderLineAuthorized OrderLineStatus = "authorized"
186
	OrderLinePaid       OrderLineStatus = "paid"
187
	OrderLineShipping   OrderLineStatus = "shipping"
188
	OrderLineCanceled   OrderLineStatus = "canceled"
189
	OrderLineCompleted  OrderLineStatus = "completed"
190
)
191
192
// OrderLineLinks describes object with several URL objects relevant to the order line.
193
type OrderLineLinks struct {
194
	ProductURL *URL `json:"productUrl,omitempty"`
195
	ImageURL   *URL `json:"imageUrl,omitempty"`
196
}
197
198
// OrderOptions describes order endpoint valid query string parameters.
199
// See: https://docs.mollie.com/reference/v2/orders-api/get-order.
200
type OrderOptions struct {
201
	Embed     []EmbedValue `url:"embed,omitempty"`
202
	ProfileID string       `url:"profileId,omitempty"`
203
}
204
205
// OrderListOptions describes order endpoint valid query string parameters.
206
// See: https://docs.mollie.com/reference/v2/orders-api/list-orders.
207
type OrderListOptions struct {
208
	ProfileID string `url:"profileId,omitempty"`
209
	From      string `url:"from,omitempty"`
210
	Limit     int    `url:"limit,omitempty"`
211
}
212
213
// OrderListRefundOptions describes order endpoint valid query string parameters.
214
// See: https://docs.mollie.com/reference/v2/orders-api/list-orders.
215
type OrderListRefundOptions struct {
216
	From  string     `url:"from,omitempty"`
217
	Limit int        `url:"limit,omitempty"`
218
	Embed EmbedValue `url:"embed,omitempty"`
219
}
220
221
// OrdersService instance operates over refund resources.
222
type OrdersService service
223
224
// Get retrieve a single order by its ID.
225
// See https://docs.mollie.com/reference/v2/orders-api/get-order
226
func (ors *OrdersService) Get(orID string, opt *OrderOptions) (order Orders, err error) {
227 1
	u := fmt.Sprintf("v2/orders/%s", orID)
228 1
	if opt != nil {
229 1
		v, _ := query.Values(opt)
230 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
231
	}
232
233 1
	req, err := ors.client.NewAPIRequest(http.MethodGet, u, nil)
234 1
	if err != nil {
235
		return
236
	}
237
238 1
	res, err := ors.client.Do(req)
239 1
	if err != nil {
240
		return
241
	}
242
243 1
	if err = json.Unmarshal(res.content, &order); err != nil {
244
		return
245
	}
246
247 1
	return
248
}
249
250
// Create an order will automatically create the required payment to allow your customer to pay for the order.
251
// See https://docs.mollie.com/reference/v2/orders-api/create-order
252
func (ors *OrdersService) Create(ord Orders, opt *OrderOptions) (order Orders, err error) {
253 1
	u := fmt.Sprintf("v2/orders")
254 1
	if opt != nil {
255 1
		v, _ := query.Values(opt)
256 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
257
	}
258
259 1
	req, err := ors.client.NewAPIRequest(http.MethodPost, u, ord)
260 1
	if err != nil {
261
		return
262
	}
263
264 1
	res, err := ors.client.Do(req)
265 1
	if err != nil {
266
		return
267
	}
268
269 1
	if err = json.Unmarshal(res.content, &order); err != nil {
270
		return
271
	}
272
273 1
	return
274
}
275
276
// Update is used to update the billing and/or shipping address of an order.
277
// See https://docs.mollie.com/reference/v2/orders-api/update-order
278
func (ors *OrdersService) Update(orderID string, ord Orders) (order Orders, err error) {
279 1
	u := fmt.Sprintf("v2/orders/%s", orderID)
280
281 1
	req, err := ors.client.NewAPIRequest(http.MethodPatch, u, ord)
282 1
	if err != nil {
283
		return
284
	}
285
286 1
	res, err := ors.client.Do(req)
287 1
	if err != nil {
288
		return
289
	}
290
291 1
	if err = json.Unmarshal(res.content, &order); err != nil {
292
		return
293
	}
294
295 1
	return
296
}
297
298
// Cancel try to cancel the order that fulfill certain requirements
299
// See https://docs.mollie.com/reference/v2/orders-api/cancel-order
300
func (ors *OrdersService) Cancel(orderID string) (order Orders, err error) {
301 1
	u := fmt.Sprintf("v2/orders/" + orderID)
302
303 1
	req, err := ors.client.NewAPIRequest(http.MethodDelete, u, nil)
304 1
	if err != nil {
305
		return
306
	}
307
308 1
	res, err := ors.client.Do(req)
309 1
	if err != nil {
310
		return
311
	}
312
313 1
	if err = json.Unmarshal(res.content, &order); err != nil {
314
		return
315
	}
316
317 1
	return
318
}
319
320
// List is to retrieve all orders.
321
// See https://docs.mollie.com/reference/v2/orders-api/list-orders
322
func (ors *OrdersService) List(opt *OrderListOptions) (ordList OrderList, err error) {
323 1
	u := fmt.Sprintf("v2/orders")
324 1
	if opt != nil {
325 1
		v, _ := query.Values(opt)
326 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
327
	}
328
329 1
	req, err := ors.client.NewAPIRequest(http.MethodGet, u, nil)
330 1
	if err != nil {
331
		return
332
	}
333
334 1
	res, err := ors.client.Do(req)
335 1
	if err != nil {
336
		return
337
	}
338
339 1
	if err = json.Unmarshal(res.content, &ordList); err != nil {
340
		return
341
	}
342
343 1
	return
344
}
345
346
// UpdateOrderline can be used to update an order line.
347
// See https://docs.mollie.com/reference/v2/orders-api/update-orderline
348
func (ors *OrdersService) UpdateOrderline(orderID string, orderlineID string, orderline OrderLines) (order Orders, err error) {
349 1
	u := fmt.Sprintf("v2/orders/%s/lines/%s", orderID, orderlineID)
350
351 1
	req, err := ors.client.NewAPIRequest(http.MethodPatch, u, nil)
352 1
	if err != nil {
353
		return
354
	}
355
356 1
	res, err := ors.client.Do(req)
357 1
	if err != nil {
358
		return
359
	}
360
361 1
	if err = json.Unmarshal(res.content, &order); err != nil {
362
		return
363
	}
364
365 1
	return
366
}
367
368
// CancelOrderLine can be used to cancel one or more order lines
369
// that were previously authorized using a pay after delivery payment method.
370
// Use the Cancel Order API if you want to cancel the entire order or the remainder of the order.
371
// See https://docs.mollie.com/reference/v2/orders-api/cancel-order-lines
372
func (ors *OrdersService) CancelOrderLine(orderID string, orderlines *Orders) (errorResponse *ErrorResponse, err error) {
373 1
	u := fmt.Sprintf("v2/orders/%s/lines", orderID)
374
375 1
	req, err := ors.client.NewAPIRequest(http.MethodDelete, u, orderlines)
376 1
	if err != nil {
377
		return
378
	}
379
380 1
	res, err := ors.client.Do(req)
381 1
	if err != nil {
382 1
		json.Unmarshal(res.content, &errorResponse)
383 1
		return
384
	}
385
386
	if res.StatusCode == http.StatusNoContent {
387
		return nil, nil
388
	}
389
390
	return
391
}
392
393
// CreateOrderPayment can only be created while the status of the order is created,
394
// and when the status of the existing payment is either expired, canceled or failed.
395
// See https://docs.mollie.com/reference/v2/orders-api/create-order-payment
396
func (ors *OrdersService) CreateOrderPayment(orderID string, ordPay *OrderPayment) (payment *Payment, errorResponse *ErrorResponse, err error) {
397 1
	u := fmt.Sprintf("v2/orders/%s/payments", orderID)
398
399 1
	req, err := ors.client.NewAPIRequest(http.MethodPost, u, ordPay)
400 1
	if err != nil {
401
		return
402
	}
403
404 1
	res, err := ors.client.Do(req)
405 1
	if err != nil {
406 1
		err = json.Unmarshal(res.content, &errorResponse)
407 1
		if err != nil {
408 1
			return
409
		}
410
		return
411
	}
412
413 1
	if err = json.Unmarshal(res.content, &payment); err != nil {
414
		return
415
	}
416
417 1
	return
418
}
419
420
// CreateOrderRefund using the Orders API, refunds should be made against the order.
421
// See https://docs.mollie.com/reference/v2/orders-api/create-order-refund
422
func (ors *OrdersService) CreateOrderRefund(orderID string, order *Orders) (refund Refund, errorResponse *ErrorResponse, err error) {
423 1
	u := fmt.Sprintf("v2/orders/%s/refunds", orderID)
424
425 1
	req, err := ors.client.NewAPIRequest(http.MethodPost, u, order)
426 1
	if err != nil {
427
		return
428
	}
429
430 1
	res, err := ors.client.Do(req)
431 1
	if err != nil {
432 1
		err = json.Unmarshal(res.content, &errorResponse)
433 1
		if err != nil {
434 1
			return
435
		}
436
		return
437
	}
438
439 1
	if err = json.Unmarshal(res.content, &refund); err != nil {
440
		return
441
	}
442
443 1
	return
444
}
445
446
// ListOrderRefunds retrieve all order refunds.
447
// See https://docs.mollie.com/reference/v2/orders-api/list-order-refunds
448
func (ors *OrdersService) ListOrderRefunds(orderID string, opt *OrderListRefundOptions) (orderListRefund OrderListRefund, err error) {
449 1
	u := fmt.Sprintf("v2/orders/%s/refunds", orderID)
450 1
	if opt != nil {
451 1
		v, _ := query.Values(opt)
452 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
453
	}
454
455 1
	req, err := ors.client.NewAPIRequest(http.MethodGet, u, nil)
456 1
	if err != nil {
457
		return
458
	}
459
460 1
	res, err := ors.client.Do(req)
461 1
	if err != nil {
462
		return
463
	}
464
465 1
	if err = json.Unmarshal(res.content, &orderListRefund); err != nil {
466
		return
467
	}
468
469 1
	return
470
}
471