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 (#46)
by Victor Hugo
01:28
created

mollie.*OrdersService.CreateOrderRefund   A

Complexity

Conditions 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

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