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.*OrdersService.List   A

Complexity

Conditions 5

Size

Total Lines 22
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 22
ccs 13
cts 13
cp 1
crap 5
rs 9.2333
c 0
b 0
f 0
nop 1
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
// Order explain the items that customers need to pay for.
13
type Order struct {
14
	Resource            string        `json:"resource,omitempty"`
15
	ID                  string        `json:"id,omitempty"`
16
	ProfileID           string        `json:"profileId,omitempty"`
17
	TestMode            bool          `json:"testmode,omitempty"`
18
	Method              PaymentMethod `json:"method,omitempty"`
19
	Mode                Mode          `json:"mode,omitempty"`
20
	Amount              *Amount       `json:"amount,omitempty"`
21
	AmountCaptured      *Amount       `json:"amountCaptured,omitempty"`
22
	AmountRefunded      *Amount       `json:"amountRefunded,omitempty"`
23
	Status              OrderStatus   `json:"status,omitempty"`
24
	IsCancelable        bool          `json:"isCancelable,omitempty"`
25
	BillingAddress      *OrderAddress `json:"billingAddress,omitempty"`
26
	ConsumerDateOfBirth *ShortDate    `json:"consumerDateOfBirth,omitempty"`
27
	OrderNumber         string        `json:"orderNumber,omitempty"`
28
	ShippingAddress     OrderAddress  `json:"shippingAddress,omitempty"`
29
	Locale              Locale        `json:"locale,omitempty"`
30
	Metadata            interface{}   `json:"metadata,omitempty"`
31
	RedirectURL         string        `json:"redirectUrl,omitempty"`
32
	Lines               []*OrderLine  `json:"lines,omitempty"`
33
	WebhookURL          string        `json:"webhookUrl,omitempty"`
34
	CreatedAt           *time.Time    `json:"createdAt,omitempty"`
35
	ExpiresAt           *time.Time    `json:"expiresAt,omitempty"`
36
	ExpiredAt           *time.Time    `json:"expiredAt,omitempty"`
37
	PaidAt              *time.Time    `json:"paidAt,omitempty"`
38
	AuthorizedAt        *time.Time    `json:"authorizedAt,omitempty"`
39
	CanceledAt          *time.Time    `json:"canceledAt,omitempty"`
40
	CompletedAt         *time.Time    `json:"completedAt,omitempty"`
41
	Embedded            struct {
42
		Payments []*Payment `json:"payments,omitempty"`
43
		Refunds  []*Refund  `json:"refunds,omitempty"`
44
	} `json:"_embedded,omitempty"`
45
	Links                                    OrderLinks    `json:"_links,omitempty"`
46
	OrderPayment                             *OrderPayment `json:"payment,omitempty"`
47
	Description                              string        `json:"description,omitempty"`
48
	ShopperCountryMustMatchTheBillingCountry bool          `json:"shopperCountryMustMatchTheBillingCountry,omitempty"`
49
}
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
	Dashboard     *URL `json:"dashboard,omitempty"`
104
}
105
106
// OrderLine contain the actual things the customer bought.
107
type OrderLine struct {
108
	Resource           string          `json:"resource,omitempty"`
109
	ID                 string          `json:"id,omitempty"`
110
	OrderID            string          `json:"orderId,omitempty"`
111
	ProductType        ProductType     `json:"type,omitempty"`
112
	Name               string          `json:"name,omitempty"`
113
	Amount             *Amount         `json:"amount,omitempty"`
114
	Status             OrderLineStatus `json:"status,omitempty"`
115
	IsCancelable       bool            `json:"isCancelable,omitempty"`
116
	Quantity           int             `json:"quantity,omitempty"`
117
	QuantityShipped    int             `json:"quantityShipped,omitempty"`
118
	AmountShipped      *Amount         `json:"amountShipped,omitempty"`
119
	QuantityRefunded   int             `json:"quantityRefunded,omitempty"`
120
	AmountRefunded     *Amount         `json:"amountRefunded,omitempty"`
121
	QuantityCanceled   int             `json:"quantityCanceled,omitempty"`
122
	AmountCanceled     *Amount         `json:"amountCanceled,omitempty"`
123
	ShippableQuantity  int             `json:"shippableQuantity,omitempty"`
124
	RefundableQuantity int             `json:"refundableQuantity,omitempty"`
125
	CancelableQuantity int             `json:"cancelableQuantity,omitempty"`
126
	UnitPrice          *Amount         `json:"unitPrice,omitempty"`
127
	DiscountAmount     *Amount         `json:"discountAmount,omitempty"`
128
	TotalAmount        *Amount         `json:"totalAmount,omitempty"`
129
	VatRate            string          `json:"vatRate,omitempty"`
130
	VatAmount          *Amount         `json:"vatAmount,omitempty"`
131
	SKU                string          `json:"sku,omitempty"`
132
	CreatedAt          *time.Time      `json:"createdAt,omitempty"`
133
	Links              OrderLineLinks  `json:"_links,omitempty"`
134
	ImageURL           string          `json:"imageUrl,omitempty"`
135
	ProductURL         string          `json:"productUrl,omitempty"`
136
}
137
138
// OrderList for containing the response of list orders
139
type OrderList struct {
140
	Count    int `json:"count,omitempty"`
141
	Embedded struct {
142
		Orders []*Order `json:"orders,omitempty"`
143
	} `json:"_embedded,omitempty"`
144
	Links PaginationLinks `json:"links,omitempty"`
145
}
146
147
// OrderListRefund for containing the response of list orders
148
type OrderListRefund struct {
149
	Count    int `json:"count,omitempty"`
150
	Embedded struct {
151
		Refunds []*Refund `json:"refund,omitempty"`
152
	} `json:"_embedded,omitempty"`
153
	Links PaginationLinks `json:"links,omitempty"`
154
}
155
156
// ProductType describes the type of product bought, for example, a physical or a digital product.
157
type ProductType string
158
159
// Valid product type.
160
const (
161
	Physical        ProductType = "physical"
162
	Discount        ProductType = "discount"
163
	Digital         ProductType = "digital"
164
	ShippingFee     ProductType = "shipping_fee"
165
	StoreCredit     ProductType = "store_credit"
166
	GiftCardProduct ProductType = "gift_card"
167
	Surcharge       ProductType = "surcharge"
168
)
169
170
// OrderLineStatus describes status of the order line.
171
type OrderLineStatus string
172
173
// Valid order line status.
174
const (
175
	OrderLineCreated    OrderLineStatus = "created"
176
	OrderLineAuthorized OrderLineStatus = "authorized"
177
	OrderLinePaid       OrderLineStatus = "paid"
178
	OrderLineShipping   OrderLineStatus = "shipping"
179
	OrderLineCanceled   OrderLineStatus = "canceled"
180
	OrderLineCompleted  OrderLineStatus = "completed"
181
)
182
183
// OrderLineLinks describes object with several URL objects relevant to the order line.
184
type OrderLineLinks struct {
185
	ProductURL *URL `json:"productUrl,omitempty"`
186
	ImageURL   *URL `json:"imageUrl,omitempty"`
187
}
188
189
// OrderOptions describes order endpoint valid query string parameters.
190
type OrderOptions struct {
191
	Embed     []EmbedValue `url:"embed,omitempty"`
192
	ProfileID string       `url:"profileId,omitempty"`
193
}
194
195
// OrderListOptions describes order endpoint valid query string parameters.
196
type OrderListOptions struct {
197
	ProfileID string `url:"profileId,omitempty"`
198
	From      string `url:"from,omitempty"`
199
	Limit     int    `url:"limit,omitempty"`
200
}
201
202
// OrderListRefundOptions describes order endpoint valid query string parameters.
203
type OrderListRefundOptions struct {
204
	From  string     `url:"from,omitempty"`
205
	Limit int        `url:"limit,omitempty"`
206
	Embed EmbedValue `url:"embed,omitempty"`
207
}
208
209
// OrdersService instance operates over refund resources.
210
type OrdersService service
211
212
// Get retrieve a single order by its ID.
213
//
214
// See https://docs.mollie.com/reference/v2/orders-api/get-order
215
func (ors *OrdersService) Get(orID string, opt *OrderOptions) (order *Order, err error) {
216 1
	u := fmt.Sprintf("v2/orders/%s", orID)
217 1
	if opt != nil {
218 1
		v, _ := query.Values(opt)
219 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
220
	}
221
222 1
	req, err := ors.client.NewAPIRequest(http.MethodGet, u, nil)
223 1
	if err != nil {
224 1
		return
225
	}
226
227 1
	res, err := ors.client.Do(req)
228 1
	if err != nil {
229 1
		return
230
	}
231
232 1
	if err = json.Unmarshal(res.content, &order); err != nil {
233 1
		return
234
	}
235
236 1
	return
237
}
238
239
// Create an order will automatically create the required payment to allow your customer to pay for the order.
240
//
241
// See https://docs.mollie.com/reference/v2/orders-api/create-order
242
func (ors *OrdersService) Create(ord Order, opt *OrderOptions) (order *Order, err error) {
243 1
	u := "v2/orders"
244 1
	if opt != nil {
245 1
		v, _ := query.Values(opt)
246 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
247
	}
248
249 1
	if ors.client.HasAccessToken() && ors.client.config.testing {
250 1
		ord.TestMode = true
251
	}
252
253 1
	req, err := ors.client.NewAPIRequest(http.MethodPost, u, ord)
254 1
	if err != nil {
255 1
		return
256
	}
257
258 1
	res, err := ors.client.Do(req)
259 1
	if err != nil {
260 1
		return
261
	}
262
263 1
	if err = json.Unmarshal(res.content, &order); err != nil {
264 1
		return
265
	}
266
267 1
	return
268
}
269
270
// Update is used to update the billing and/or shipping address of an order.
271
//
272
// See https://docs.mollie.com/reference/v2/orders-api/update-order
273
func (ors *OrdersService) Update(orderID string, ord Order) (order *Order, err error) {
274 1
	u := fmt.Sprintf("v2/orders/%s", orderID)
275
276 1
	req, err := ors.client.NewAPIRequest(http.MethodPatch, u, ord)
277 1
	if err != nil {
278 1
		return
279
	}
280
281 1
	res, err := ors.client.Do(req)
282 1
	if err != nil {
283 1
		return
284
	}
285
286 1
	if err = json.Unmarshal(res.content, &order); err != nil {
287 1
		return
288
	}
289
290 1
	return
291
}
292
293
// Cancel try to cancel the order that fulfill certain requirements.
294
//
295
// See https://docs.mollie.com/reference/v2/orders-api/cancel-order
296
func (ors *OrdersService) Cancel(orderID string) (order *Order, err error) {
297 1
	u := fmt.Sprintf("v2/orders/%s", orderID)
298
299 1
	req, err := ors.client.NewAPIRequest(http.MethodDelete, u, nil)
300 1
	if err != nil {
301 1
		return
302
	}
303
304 1
	res, err := ors.client.Do(req)
305 1
	if err != nil {
306 1
		return
307
	}
308
309 1
	if err = json.Unmarshal(res.content, &order); err != nil {
310
		return
311
	}
312
313 1
	return
314
}
315
316
// List is to retrieve all orders.
317
//
318
// See https://docs.mollie.com/reference/v2/orders-api/list-orders
319
func (ors *OrdersService) List(opt *OrderListOptions) (ordList *OrderList, err error) {
320 1
	u := "v2/orders"
321 1
	if opt != nil {
322 1
		v, _ := query.Values(opt)
323 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
324
	}
325
326 1
	req, err := ors.client.NewAPIRequest(http.MethodGet, u, nil)
327 1
	if err != nil {
328 1
		return
329
	}
330
331 1
	res, err := ors.client.Do(req)
332 1
	if err != nil {
333 1
		return
334
	}
335
336 1
	if err = json.Unmarshal(res.content, &ordList); err != nil {
337 1
		return
338
	}
339
340 1
	return
341
}
342
343
// UpdateOrderLine can be used to update an order line.
344
//
345
// See https://docs.mollie.com/reference/v2/orders-api/update-orderline
346
func (ors *OrdersService) UpdateOrderLine(orderID string, orderLineID string, orderLine OrderLine) (order *Order, err error) {
347 1
	u := fmt.Sprintf("v2/orders/%s/lines/%s", orderID, orderLineID)
348
349 1
	req, err := ors.client.NewAPIRequest(http.MethodPatch, u, orderLine)
350 1
	if err != nil {
351 1
		return
352
	}
353
354 1
	res, err := ors.client.Do(req)
355 1
	if err != nil {
356 1
		return
357
	}
358
359 1
	if err = json.Unmarshal(res.content, &order); err != nil {
360 1
		return
361
	}
362
363 1
	return
364
}
365
366
// CancelOrderLines can be used to cancel one or more order lines
367
// that were previously authorized using a pay after delivery payment method.
368
// Use the Cancel Order API if you want to cancel the entire order or the remainder of the order.
369
//
370
// See https://docs.mollie.com/reference/v2/orders-api/cancel-order-lines
371
func (ors *OrdersService) CancelOrderLines(orderID string, orderLines []OrderLine) (err error) {
372 1
	u := fmt.Sprintf("v2/orders/%s/lines", orderID)
373
374 1
	req, err := ors.client.NewAPIRequest(http.MethodDelete, u, orderLines)
375 1
	if err != nil {
376 1
		return
377
	}
378
379 1
	_, err = ors.client.Do(req)
380 1
	if err != nil {
381 1
		return
382
	}
383
384 1
	return
385
}
386
387
// CreateOrderPayment can only be created while the status of the order is created,
388
// and when the status of the existing payment is either expired, canceled or failed.
389
// See https://docs.mollie.com/reference/v2/orders-api/create-order-payment
390
func (ors *OrdersService) CreateOrderPayment(orderID string, ordPay *OrderPayment) (payment *Payment, err error) {
391 1
	u := fmt.Sprintf("v2/orders/%s/payments", orderID)
392
393 1
	req, err := ors.client.NewAPIRequest(http.MethodPost, u, ordPay)
394 1
	if err != nil {
395 1
		return
396
	}
397
398 1
	res, err := ors.client.Do(req)
399 1
	if err != nil {
400 1
		return
401
	}
402
403 1
	if err = json.Unmarshal(res.content, &payment); err != nil {
404 1
		return
405
	}
406
407 1
	return
408
}
409
410
// CreateOrderRefund using the Orders API, refunds should be made against the order.
411
// See https://docs.mollie.com/reference/v2/orders-api/create-order-refund
412
func (ors *OrdersService) CreateOrderRefund(orderID string, order *Order) (refund Refund, err error) {
413 1
	u := fmt.Sprintf("v2/orders/%s/refunds", orderID)
414
415 1
	req, err := ors.client.NewAPIRequest(http.MethodPost, u, order)
416 1
	if err != nil {
417 1
		return
418
	}
419
420 1
	res, err := ors.client.Do(req)
421 1
	if err != nil {
422 1
		return
423
	}
424
425 1
	if err = json.Unmarshal(res.content, &refund); err != nil {
426 1
		return
427
	}
428
429 1
	return
430
}
431
432
// ListOrderRefunds retrieve all order refunds.
433
// See https://docs.mollie.com/reference/v2/orders-api/list-order-refunds
434
func (ors *OrdersService) ListOrderRefunds(orderID string, opt *OrderListRefundOptions) (orderListRefund OrderListRefund, err error) {
435 1
	u := fmt.Sprintf("v2/orders/%s/refunds", orderID)
436 1
	if opt != nil {
437 1
		v, _ := query.Values(opt)
438 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
439
	}
440
441 1
	req, err := ors.client.NewAPIRequest(http.MethodGet, u, nil)
442 1
	if err != nil {
443 1
		return
444
	}
445
446 1
	res, err := ors.client.Do(req)
447 1
	if err != nil {
448 1
		return
449
	}
450
451 1
	if err = json.Unmarshal(res.content, &orderListRefund); err != nil {
452 1
		return
453
	}
454
455 1
	return
456
}
457