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
Push — master ( 76d75a...85f6e4 )
by Victor Hugo
01:21
created

mollie.*RefundsService.Create   B

Complexity

Conditions 6

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 17
dl 0
loc 27
ccs 16
cts 16
cp 1
crap 6
rs 8.6166
c 0
b 0
f 0
nop 3
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"`
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
// RefundLinks describes all the possible links to be returned with
51
// a Refund object.
52
type RefundLinks struct {
53
	Self       *URL `json:"self,omitempty"`
54
	Payment    *URL `json:"payment,omitempty"`
55
	Settlement *URL `json:"settlement,omitempty"`
56
	Order      *URL `json:"order,omitempty"`
57
}
58
59
// RefundOptions describes refund endpoint valid query string parameters.
60
//
61
// See: https://docs.mollie.com/reference/v2/refunds-api/get-refund.
62
type RefundOptions struct {
63
	Embed EmbedValue `url:"embed,omitempty"`
64
}
65
66
// EmbedValue describes the valid value of embed query string.
67
type EmbedValue string
68
69
// Valid Embed query string value.
70
const (
71
	EmbedPayment EmbedValue = "payment"
72
)
73
74
// ListRefundOptions describes list refund endpoint valid query string parameters.
75
//
76
// See: https://docs.mollie.com/reference/v2/refunds-api/list-refunds.
77
type ListRefundOptions struct {
78
	From      string     `url:"from,omitempty"`
79
	Limit     string     `url:"limit,omitempty"`
80
	ProfileID string     `url:"profileId,omitempty"`
81
	Embed     EmbedValue `url:"embed,omitempty"`
82
}
83
84
// RefundsService instance operates over refund resources.
85
type RefundsService service
86
87
// Get retrieve a single refund by its ID.
88
//
89
// If you do not know the original payment’s ID, you can use the List payment refunds endpoint.
90
func (rs *RefundsService) Get(paymentID, refundID string, options *RefundOptions) (refund Refund, err error) {
91 1
	u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID)
92 1
	if options != nil {
93 1
		v, _ := query.Values(options)
94 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
95
	}
96
97 1
	req, err := rs.client.NewAPIRequest(http.MethodGet, u, options)
98 1
	if err != nil {
99 1
		return
100
	}
101
102 1
	res, err := rs.client.Do(req)
103 1
	if err != nil {
104 1
		return
105
	}
106
107 1
	if err = json.Unmarshal(res.content, &refund); err != nil {
108 1
		return
109
	}
110
111 1
	return
112
}
113
114
var (
115
	requiredCreateParamRefund = "parameter required for creating a refund: %+v"
116
)
117
118
// Create a refund payment request.
119
//
120
// See https://docs.mollie.com/reference/v2/refunds-api/create-refund.
121
func (rs *RefundsService) Create(paymentID string, re Refund, options *RefundOptions) (rf Refund, err error) {
122 1
	err = re.checkMandatoryParam()
123 1
	if err != nil {
124 1
		return re, err
125
	}
126
127 1
	u := fmt.Sprintf("v2/payments/%s/refunds", paymentID)
128 1
	if options != nil {
129 1
		v, _ := query.Values(options)
130 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
131
	}
132
133 1
	req, err := rs.client.NewAPIRequest(http.MethodPost, u, re)
134 1
	if err != nil {
135 1
		return
136
	}
137
138 1
	res, err := rs.client.Do(req)
139 1
	if err != nil {
140 1
		return
141
	}
142
143 1
	if err = json.Unmarshal(res.content, &rf); err != nil {
144 1
		return
145
	}
146
147 1
	return
148
}
149
150
// Cancel try to cancel the refund request.
151
// The refund can only be canceled while the refund’s status is either queued or pending.
152
// See https://docs.mollie.com/reference/v2/refunds-api/cancel-refund
153
func (rs *RefundsService) Cancel(paymentID, refundID string, options *RefundOptions) (err error) {
154 1
	u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID)
155 1
	if options != nil {
156 1
		v, _ := query.Values(options)
157 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
158
	}
159
160 1
	req, err := rs.client.NewAPIRequest(http.MethodDelete, u, nil)
161 1
	if err != nil {
162 1
		return
163
	}
164
165 1
	_, err = rs.client.Do(req)
166 1
	if err != nil {
167 1
		return
168
	}
169
170 1
	return
171
}
172
173
// ListRefund calls the top level https://api.mollie.com/v2/refunds.
174
//
175
// See https://docs.mollie.com/reference/v2/refunds-api/list-refunds.
176
func (rs *RefundsService) ListRefund(options *ListRefundOptions) (rl RefundList, err error) {
177 1
	u := fmt.Sprintf("v2/refunds")
178 1
	if options != nil {
179 1
		v, _ := query.Values(options)
180 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
181
	}
182
183 1
	req, err := rs.client.NewAPIRequest(http.MethodGet, u, nil)
184 1
	if err != nil {
185 1
		return
186
	}
187
188 1
	res, err := rs.client.Do(req)
189 1
	if err != nil {
190 1
		return
191
	}
192
193 1
	if err = json.Unmarshal(res.content, &rl); err != nil {
194 1
		return
195
	}
196
197 1
	return
198
}
199
200
// ListRefundPayment calls the payment-specific
201
// https://api.mollie.com/v2/payments/*paymentId*/refunds.
202
// Only refunds for that specific payment are returned.
203
// See https://docs.mollie.com/reference/v2/refunds-api/list-refunds
204
func (rs *RefundsService) ListRefundPayment(paymentID string, options *ListRefundOptions) (rl RefundList, err error) {
205 1
	u := fmt.Sprintf("v2/payments/%s/refunds", paymentID)
206 1
	if options != nil {
207 1
		v, _ := query.Values(options)
208 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
209
	}
210
211 1
	req, err := rs.client.NewAPIRequest(http.MethodGet, u, nil)
212 1
	if err != nil {
213 1
		return
214
	}
215
216 1
	res, err := rs.client.Do(req)
217 1
	if err != nil {
218 1
		return
219
	}
220
221 1
	if err = json.Unmarshal(res.content, &rl); err != nil {
222 1
		return
223
	}
224
225 1
	return
226
}
227
228
func (re *Refund) checkMandatoryParam() (err error) {
229 1
	if re.Amount == nil {
230 1
		return fmt.Errorf(requiredCreateParamRefund, re.Amount)
231
	}
232
233 1
	if re.Amount.Currency == "" {
234 1
		return fmt.Errorf(requiredCreateParamRefund, re.Amount.Currency)
235
	}
236
237 1
	if re.Amount.Value == "" {
238 1
		return fmt.Errorf(requiredCreateParamRefund, re.Amount.Value)
239
	}
240
241 1
	return nil
242
}
243