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

mollie.*RefundsService.Get   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
nop 3
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 5
rs 9.2333
c 0
b 0
f 0
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
// Refund describe a refund for a certain payment.
13
type Refund struct {
14
	Resource         string        `json:"resource,omitempty"`
15
	ID               string        `json:"id,omitempty"`
16
	Amount           *Amount       `json:"amount,omitempty"`
17
	SettlementID     string        `json:"settlementId,omitempty"`
18
	SettlementAmount *Amount       `json:"settlementAmount,omitempty"`
19
	Description      string        `json:"description,omitempty"`
20
	Metadata         interface{}   `json:"metadata,omitempty"`
21
	Status           *RefundStatus `json:"status,omitempty"`
22
	Lines            *OrderLines   `json:"lines,omitempty"` //TODO: this property should be from order.go
23
	PaymentID        string        `json:"paymentId,omitempty"`
24
	OrderID          string        `json:"orderId,omitempty"`
25
	CreatedAt        *time.Time    `json:"createdAt,omitempty"`
26
	Links            *RefundLinks  `json:"_links,omitempty"`
27
}
28
29
// RefundList describes how a list of refunds will be retrieved by Mollie.
30
type RefundList struct {
31
	Count    int `json:"count,omitempty"`
32
	Embedded struct {
33
		Refunds []Refund
34
	} `json:"_embedded,omitempty"`
35
	Links PaginationLinks `json:"_links,omitempty"`
36
}
37
38
// RefundStatus describes the status of the refund.
39
type RefundStatus string
40
41
// Valid refund status.
42
const (
43
	Queued     RefundStatus = "queued"
44
	Pending    RefundStatus = "pending"
45
	Processing RefundStatus = "processing"
46
	Refunded   RefundStatus = "refunded"
47
	Failed     RefundStatus = "failed"
48
)
49
50
// OrderLines describes an array of order line objects.
51
type OrderLines struct {
52
	Quantity       int     `json:"quantity,omitempty"`
53
	DiscountAmount *Amount `json:"discountAmount,omitempty"`
54
	VatAmount      *Amount `json:"vatAmount,omitempty"`
55
	TotalAmount    *Amount `json:"totalAmount,omitempty"`
56
}
57
58
// RefundLinks describes all the possible links to be returned with
59
// a Refund object.
60
type RefundLinks struct {
61
	Self       *URL `json:"self,omitempty"`
62
	Payment    *URL `json:"payment,omitempty"`
63
	Settlement *URL `json:"settlement,omitempty"`
64
	Order      *URL `json:"order,omitempty"`
65
}
66
67
// RefundOptions describes refund endpoint valid query string parameters.
68
//
69
// See: https://docs.mollie.com/reference/v2/refunds-api/get-refund.
70
type RefundOptions struct {
71
	Embed    EmbedValue `url:"embed,omitempty"`
72
	TestMode bool       `url:"testmode,omitempty"`
73
}
74
75
// EmbedValue describes the valid value of embed query string.
76
type EmbedValue string
77
78
// Valid Embed query string value.
79
const (
80
	EmbedPayment EmbedValue = "payment"
81
)
82
83
// ListRefundOptions describes list refund endpoint valid query string parameters.
84
//
85
// See: https://docs.mollie.com/reference/v2/refunds-api/list-refunds.
86
type ListRefundOptions struct {
87
	From      string     `url:"from,omitempty"`
88
	Limit     string     `url:"limit,omitempty"`
89
	ProfileID string     `url:"profileId,omitempty"`
90
	TestMode  bool       `url:"testmode,omitempty"`
91
	Embed     EmbedValue `url:"embed,omitempty"`
92
}
93
94
// RefundsService instance operates over refund resources.
95
type RefundsService service
96
97
// Get retrieve a single refund by its ID.
98
//
99
// If you do not know the original payment’s ID, you can use the List payment refunds endpoint.
100
func (rs *RefundsService) Get(paymentID, refundID string, options *RefundOptions) (refund Refund, err error) {
101 1
	u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID)
102 1
	if options != nil {
103 1
		v, _ := query.Values(options)
104 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
105
	}
106
107 1
	req, err := rs.client.NewAPIRequest(http.MethodGet, u, options)
108 1
	if err != nil {
109 1
		return
110
	}
111
112 1
	res, err := rs.client.Do(req)
113 1
	if err != nil {
114 1
		return
115
	}
116
117 1
	if err = json.Unmarshal(res.content, &refund); err != nil {
118 1
		return
119
	}
120
121 1
	return
122
}
123
124
var (
125
	requiredCreateParamRefund = "parameter required for creating a refund: %+v"
126
)
127
128
// Create a refund payment request.
129
//
130
// See https://docs.mollie.com/reference/v2/refunds-api/create-refund.
131
func (rs *RefundsService) Create(paymentID string, re Refund, options *RefundOptions) (rf Refund, err error) {
132 1
	if re.Amount == nil {
133
		return re, fmt.Errorf(requiredCreateParamRefund, re.Amount)
134
	}
135
136 1
	if re.Amount.Currency == "" {
137 1
		return re, fmt.Errorf(requiredCreateParamRefund, re.Amount.Currency)
138
	}
139
140 1
	if re.Amount.Value == "" {
141 1
		return re, fmt.Errorf(requiredCreateParamRefund, re.Amount.Value)
142
	}
143
144 1
	u := fmt.Sprintf("v2/payments/%s/refunds", paymentID)
145 1
	if options != nil {
146 1
		v, _ := query.Values(options)
147 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
148
	}
149
150 1
	req, err := rs.client.NewAPIRequest(http.MethodPost, u, re)
151 1
	if err != nil {
152 1
		return
153
	}
154
155 1
	res, err := rs.client.Do(req)
156 1
	if err != nil {
157 1
		return
158
	}
159
160 1
	if err = json.Unmarshal(res.content, &rf); err != nil {
161 1
		return
162
	}
163
164 1
	return
165
}
166
167
// Cancel try to cancel the refund request.
168
// The refund can only be canceled while the refund’s status is either queued or pending.
169
// See https://docs.mollie.com/reference/v2/refunds-api/cancel-refund
170
func (rs *RefundsService) Cancel(paymentID, refundID string, options *RefundOptions) (err error) {
171 1
	u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID)
172 1
	if options != nil {
173 1
		v, _ := query.Values(options)
174 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
175
	}
176
177 1
	req, err := rs.client.NewAPIRequest(http.MethodDelete, u, nil)
178 1
	if err != nil {
179 1
		return
180
	}
181
182 1
	_, err = rs.client.Do(req)
183 1
	if err != nil {
184 1
		return
185
	}
186
187 1
	return
188
}
189
190
// ListRefund calls the top level https://api.mollie.com/v2/refunds.
191
//
192
// See https://docs.mollie.com/reference/v2/refunds-api/list-refunds.
193
func (rs *RefundsService) ListRefund(options *ListRefundOptions) (rl RefundList, err error) {
194 1
	u := fmt.Sprintf("v2/refunds")
195 1
	if options != nil {
196 1
		v, _ := query.Values(options)
197 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
198
	}
199
200 1
	req, err := rs.client.NewAPIRequest(http.MethodGet, u, nil)
201 1
	if err != nil {
202 1
		return
203
	}
204
205 1
	res, err := rs.client.Do(req)
206 1
	if err != nil {
207 1
		return
208
	}
209
210 1
	if err = json.Unmarshal(res.content, &rl); err != nil {
211 1
		return
212
	}
213
214 1
	return
215
}
216
217
// ListRefundPayment calls the payment-specific
218
// https://api.mollie.com/v2/payments/*paymentId*/refunds.
219
// Only refunds for that specific payment are returned.
220
// See https://docs.mollie.com/reference/v2/refunds-api/list-refunds
221
func (rs *RefundsService) ListRefundPayment(paymentID string, options *ListRefundOptions) (rl RefundList, err error) {
222 1
	u := fmt.Sprintf("v2/payments/%s/refunds", paymentID)
223 1
	if options != nil {
224 1
		v, _ := query.Values(options)
225 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
226
	}
227
228 1
	req, err := rs.client.NewAPIRequest(http.MethodGet, u, nil)
229 1
	if err != nil {
230 1
		return
231
	}
232
233 1
	res, err := rs.client.Do(req)
234 1
	if err != nil {
235 1
		return
236
	}
237
238 1
	if err = json.Unmarshal(res.content, &rl); err != nil {
239 1
		return
240
	}
241
242 1
	return
243
}
244