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 (#170)
by Victor Hugo
02:26 queued 11s
created

mollie/orders.go   A

Size/Duplication

Total Lines 382
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
cc 31
eloc 228
dl 0
loc 382
ccs 65
cts 65
cp 1
crap 31
rs 9.92
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A mollie.*OrdersService.Get 0 11 3
A mollie.*OrdersService.CreateOrderPayment 0 13 3
A mollie.*OrdersService.CreateOrderRefund 0 13 3
A mollie.*OrdersService.ListOrderRefunds 0 13 3
A mollie.*OrdersService.UpdateOrderLine 0 13 3
A mollie.*OrdersService.CancelOrderLines 0 9 2
A mollie.*OrdersService.Update 0 11 3
A mollie.*OrdersService.Cancel 0 11 3
A mollie.*OrdersService.List 0 11 3
A mollie.*OrdersService.Create 0 15 5
1
package mollie
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"fmt"
7
	"time"
8
)
9
10
// Order explain the items that customers need to pay for.
11
type Order struct {
12
	TestMode                                 bool          `json:"testmode,omitempty"`
13
	IsCancelable                             bool          `json:"isCancelable,omitempty"`
14
	ShopperCountryMustMatchTheBillingCountry bool          `json:"shopperCountryMustMatchTheBillingCountry,omitempty"`
15
	Resource                                 string        `json:"resource,omitempty"`
16
	ID                                       string        `json:"id,omitempty"`
17
	ProfileID                                string        `json:"profileId,omitempty"`
18
	OrderNumber                              string        `json:"orderNumber,omitempty"`
19
	RedirectURL                              string        `json:"redirectUrl,omitempty"`
20
	WebhookURL                               string        `json:"webhookUrl,omitempty"`
21
	Description                              string        `json:"description,omitempty"`
22
	Sku                                      string        `json:"sku,omitempty"`
23
	Metadata                                 interface{}   `json:"metadata,omitempty"`
24
	Mode                                     Mode          `json:"mode,omitempty"`
25
	Method                                   PaymentMethod `json:"method,omitempty"`
26
	Status                                   OrderStatus   `json:"status,omitempty"`
27
	Locale                                   Locale        `json:"locale,omitempty"`
28
	ShippingAddress                          OrderAddress  `json:"shippingAddress,omitempty"`
29
	Links                                    OrderLinks    `json:"_links,omitempty"`
30
	Amount                                   *Amount       `json:"amount,omitempty"`
31
	AmountCaptured                           *Amount       `json:"amountCaptured,omitempty"`
32
	AmountRefunded                           *Amount       `json:"amountRefunded,omitempty"`
33
	BillingAddress                           *OrderAddress `json:"billingAddress,omitempty"`
34
	ConsumerDateOfBirth                      *ShortDate    `json:"consumerDateOfBirth,omitempty"`
35
	CreatedAt                                *time.Time    `json:"createdAt,omitempty"`
36
	ExpiresAt                                *time.Time    `json:"expiresAt,omitempty"`
37
	ExpiredAt                                *time.Time    `json:"expiredAt,omitempty"`
38
	PaidAt                                   *time.Time    `json:"paidAt,omitempty"`
39
	AuthorizedAt                             *time.Time    `json:"authorizedAt,omitempty"`
40
	CanceledAt                               *time.Time    `json:"canceledAt,omitempty"`
41
	CompletedAt                              *time.Time    `json:"completedAt,omitempty"`
42
	OrderPayment                             *OrderPayment `json:"payment,omitempty"`
43
	Lines                                    []*OrderLine  `json:"lines,omitempty"`
44
	Embedded                                 struct {
45
		Payments []*Payment `json:"payments,omitempty"`
46
		Refunds  []*Refund  `json:"refunds,omitempty"`
47
	} `json:"_embedded,omitempty"`
48
}
49
50
// OrderPayment describes payment specific parameters that can be passed during order creation.
51
type OrderPayment struct {
52
	ConsumerAccount   string          `json:"consumerAccount,omitempty"`
53
	CustomerID        string          `json:"customerId,omitempty"`
54
	CustomerReference string          `json:"customerReference,omitempty"`
55
	Issuer            string          `json:"issuer,omitempty"`
56
	MandateID         string          `json:"mandateId,omitempty"`
57
	SequenceType      SequenceType    `json:"sequenceType,omitempty"`
58
	VoucherNumber     string          `json:"voucherNumber,omitempty"`
59
	VoucherPin        string          `json:"voucherPin,omitempty"`
60
	WebhookURL        string          `json:"webhookUrl,omitempty"`
61
	ApplicationFee    *ApplicationFee `json:"applicationFee,omitempty"`
62
	Method            PaymentMethod   `json:"method,omitempty"`
63
}
64
65
// OrderStatus describes the valid order status.
66
type OrderStatus string
67
68
// Valid order status.
69
const (
70
	Created    OrderStatus = "created"
71
	Paid       OrderStatus = "paid"
72
	Authorized OrderStatus = "authorized"
73
	Canceled   OrderStatus = "canceled"
74
	Shipping   OrderStatus = "shipping"
75
	Completed  OrderStatus = "completed"
76
	Expired    OrderStatus = "expired"
77
)
78
79
// OrderAddress identify both the address and the person the order is billed or shipped to.
80
type OrderAddress struct {
81
	OrganizationName string      `json:"organizationName,omitempty"`
82
	Title            string      `json:"title,omitempty"`
83
	GivenName        string      `json:"givenName,omitempty"`
84
	FamilyName       string      `json:"familyName,omitempty"`
85
	Email            string      `json:"email,omitempty"`
86
	Phone            PhoneNumber `json:"phone,omitempty"`
87
	StreetAndNumber  string      `json:"streetAndNumber,omitempty"`
88
	StreetAdditional string      `json:"streetAdditional,omitempty"`
89
	PostalCode       string      `json:"postalCode,omitempty"`
90
	City             string      `json:"city,omitempty"`
91
	Region           string      `json:"region,omitempty"`
92
	Country          string      `json:"country,omitempty"`
93
}
94
95
// OrderLinks describes an object with several URL objects
96
// relevant to the order.
97
// Every URL object will contain an href and a type field.
98
type OrderLinks struct {
99
	Self          *URL `json:"self,omitempty"`
100
	Checkout      *URL `json:"checkout,omitempty"`
101
	Documentation *URL `json:"documentation,omitempty"`
102
	Dashboard     *URL `json:"dashboard,omitempty"`
103
}
104
105
// OrderLine contain the actual things the customer bought.
106
type OrderLine struct {
107
	Resource           string          `json:"resource,omitempty"`
108
	ID                 string          `json:"id,omitempty"`
109
	OrderID            string          `json:"orderId,omitempty"`
110
	ProductType        ProductType     `json:"type,omitempty"`
111
	Name               string          `json:"name,omitempty"`
112
	Amount             *Amount         `json:"amount,omitempty"`
113
	Status             OrderLineStatus `json:"status,omitempty"`
114
	IsCancelable       bool            `json:"isCancelable,omitempty"`
115
	Quantity           int             `json:"quantity,omitempty"`
116
	QuantityShipped    int             `json:"quantityShipped,omitempty"`
117
	AmountShipped      *Amount         `json:"amountShipped,omitempty"`
118
	QuantityRefunded   int             `json:"quantityRefunded,omitempty"`
119
	AmountRefunded     *Amount         `json:"amountRefunded,omitempty"`
120
	QuantityCanceled   int             `json:"quantityCanceled,omitempty"`
121
	AmountCanceled     *Amount         `json:"amountCanceled,omitempty"`
122
	ShippableQuantity  int             `json:"shippableQuantity,omitempty"`
123
	RefundableQuantity int             `json:"refundableQuantity,omitempty"`
124
	CancelableQuantity int             `json:"cancelableQuantity,omitempty"`
125
	UnitPrice          *Amount         `json:"unitPrice,omitempty"`
126
	DiscountAmount     *Amount         `json:"discountAmount,omitempty"`
127
	TotalAmount        *Amount         `json:"totalAmount,omitempty"`
128
	VatRate            string          `json:"vatRate,omitempty"`
129
	VatAmount          *Amount         `json:"vatAmount,omitempty"`
130
	SKU                string          `json:"sku,omitempty"`
131
	CreatedAt          *time.Time      `json:"createdAt,omitempty"`
132
	Links              OrderLineLinks  `json:"_links,omitempty"`
133
	ImageURL           string          `json:"imageUrl,omitempty"`
134
	ProductURL         string          `json:"productUrl,omitempty"`
135
	Metadata           interface{}     `json:"metadata,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(ctx context.Context, orID string, opts *OrderOptions) (res *Response, order *Order, err error) {
216 1
	res, err = ors.client.get(ctx, fmt.Sprintf("v2/orders/%s", orID), opts)
217 1
	if err != nil {
218 1
		return
219
	}
220
221 1
	if err = json.Unmarshal(res.content, &order); err != nil {
222 1
		return
223
	}
224
225 1
	return
226
}
227
228
// Create an order will automatically create the required payment to allow your customer to pay for the order.
229
//
230
// See https://docs.mollie.com/reference/v2/orders-api/create-order
231
func (ors *OrdersService) Create(ctx context.Context, ord Order, opts *OrderOptions) (res *Response, order *Order, err error) {
232 1
	if ors.client.HasAccessToken() && ors.client.config.testing {
233 1
		ord.TestMode = true
234
	}
235
236 1
	res, err = ors.client.post(ctx, "v2/orders", ord, opts)
237 1
	if err != nil {
238 1
		return
239
	}
240
241 1
	if err = json.Unmarshal(res.content, &order); err != nil {
242 1
		return
243
	}
244
245 1
	return
246
}
247
248
// Update is used to update the billing and/or shipping address of an order.
249
//
250
// See https://docs.mollie.com/reference/v2/orders-api/update-order
251
func (ors *OrdersService) Update(ctx context.Context, orderID string, ord Order) (res *Response, order *Order, err error) {
252 1
	res, err = ors.client.patch(ctx, fmt.Sprintf("v2/orders/%s", orderID), ord, nil)
253 1
	if err != nil {
254 1
		return
255
	}
256
257 1
	if err = json.Unmarshal(res.content, &order); err != nil {
258 1
		return
259
	}
260
261 1
	return
262
}
263
264
// Cancel try to cancel the order that fulfill certain requirements.
265
//
266
// See https://docs.mollie.com/reference/v2/orders-api/cancel-order
267
func (ors *OrdersService) Cancel(ctx context.Context, orderID string) (res *Response, order *Order, err error) {
268 1
	res, err = ors.client.delete(ctx, fmt.Sprintf("v2/orders/%s", orderID), nil)
269 1
	if err != nil {
270 1
		return
271
	}
272
273 1
	if err = json.Unmarshal(res.content, &order); err != nil {
274 1
		return
275
	}
276
277 1
	return
278
}
279
280
// List is to retrieve all orders.
281
//
282
// See https://docs.mollie.com/reference/v2/orders-api/list-orders
283
func (ors *OrdersService) List(ctx context.Context, opts *OrderListOptions) (res *Response, ordList *OrderList, err error) {
284 1
	res, err = ors.client.get(ctx, "v2/orders", opts)
285 1
	if err != nil {
286 1
		return
287
	}
288
289 1
	if err = json.Unmarshal(res.content, &ordList); err != nil {
290 1
		return
291
	}
292
293 1
	return
294
}
295
296
// UpdateOrderLine can be used to update an order line.
297
//
298
// See https://docs.mollie.com/reference/v2/orders-api/update-orderline
299
func (ors *OrdersService) UpdateOrderLine(ctx context.Context, orderID string, orderLineID string, orderLine OrderLine) (res *Response, order *Order, err error) {
300 1
	u := fmt.Sprintf("v2/orders/%s/lines/%s", orderID, orderLineID)
301
302 1
	res, err = ors.client.patch(ctx, u, orderLine, nil)
303 1
	if err != nil {
304 1
		return
305
	}
306
307 1
	if err = json.Unmarshal(res.content, &order); err != nil {
308 1
		return
309
	}
310
311 1
	return
312
}
313
314
// CancelOrderLines can be used to cancel one or more order lines
315
// that were previously authorized using a pay after delivery payment method.
316
// Use the Cancel Order API if you want to cancel the entire order or the remainder of the order.
317
//
318
// See https://docs.mollie.com/reference/v2/orders-api/cancel-order-lines
319
func (ors *OrdersService) CancelOrderLines(ctx context.Context, orderID string, orderLines []OrderLine) (res *Response, err error) {
320 1
	u := fmt.Sprintf("v2/orders/%s/lines", orderID)
321
322 1
	res, err = ors.client.delete(ctx, u, nil)
323 1
	if err != nil {
324 1
		return
325
	}
326
327 1
	return
328
}
329
330
// CreateOrderPayment can only be created while the status of the order is created,
331
// and when the status of the existing payment is either expired, canceled or failed.
332
//
333
// See https://docs.mollie.com/reference/v2/orders-api/create-order-payment
334
func (ors *OrdersService) CreateOrderPayment(ctx context.Context, orderID string, ordPay *OrderPayment) (res *Response, payment *Payment, err error) {
335 1
	u := fmt.Sprintf("v2/orders/%s/payments", orderID)
336
337 1
	res, err = ors.client.post(ctx, u, ordPay, nil)
338 1
	if err != nil {
339 1
		return
340
	}
341
342 1
	if err = json.Unmarshal(res.content, &payment); err != nil {
343 1
		return
344
	}
345
346 1
	return
347
}
348
349
// CreateOrderRefund using the Orders API, refunds should be made against the order.
350
//
351
// See https://docs.mollie.com/reference/v2/orders-api/create-order-refund
352
func (ors *OrdersService) CreateOrderRefund(ctx context.Context, orderID string, order *Order) (res *Response, refund *Refund, err error) {
353 1
	u := fmt.Sprintf("v2/orders/%s/refunds", orderID)
354
355 1
	res, err = ors.client.post(ctx, u, order, nil)
356 1
	if err != nil {
357 1
		return
358
	}
359
360 1
	if err = json.Unmarshal(res.content, &refund); err != nil {
361 1
		return
362
	}
363
364 1
	return
365
}
366
367
// ListOrderRefunds retrieve all order refunds.
368
//
369
// See https://docs.mollie.com/reference/v2/orders-api/list-order-refunds
370
func (ors *OrdersService) ListOrderRefunds(ctx context.Context, orderID string, opts *OrderListRefundOptions) (res *Response, orderListRefund *OrderListRefund, err error) {
371 1
	u := fmt.Sprintf("v2/orders/%s/refunds", orderID)
372
373 1
	res, err = ors.client.get(ctx, u, opts)
374 1
	if err != nil {
375 1
		return
376
	}
377
378 1
	if err = json.Unmarshal(res.content, &orderListRefund); err != nil {
379 1
		return
380
	}
381
382 1
	return
383
}
384