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.*RefundsService.ListRefund   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
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
// 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            []*OrderLine `json:"lines,omitempty"`
23
	PaymentID        string       `json:"paymentId,omitempty"`
24
	OrderID          string       `json:"orderId,omitempty"`
25
	CreatedAt        *time.Time   `json:"createdAt,omitempty"`
26
	TestMode         bool         `json:"testmode,omitempty"`
27
	Links            RefundLinks  `json:"_links,omitempty"`
28
}
29
30
// RefundList describes how a list of refunds will be retrieved by Mollie.
31
type RefundList struct {
32
	Count    int `json:"count,omitempty"`
33
	Embedded struct {
34
		Refunds []*Refund
35
	} `json:"_embedded,omitempty"`
36
	Links PaginationLinks `json:"_links,omitempty"`
37
}
38
39
// RefundStatus describes the status of the refund.
40
type RefundStatus string
41
42
// Valid refund status.
43
const (
44
	Queued     RefundStatus = "queued"
45
	Pending    RefundStatus = "pending"
46
	Processing RefundStatus = "processing"
47
	Refunded   RefundStatus = "refunded"
48
	Failed     RefundStatus = "failed"
49
)
50
51
// RefundLinks describes all the possible links to be returned with
52
// a Refund object.
53
type RefundLinks struct {
54
	Self          *URL `json:"self,omitempty"`
55
	Payment       *URL `json:"payment,omitempty"`
56
	Settlement    *URL `json:"settlement,omitempty"`
57
	Order         *URL `json:"order,omitempty"`
58
	Documentation *URL `json:"documentation,omitempty"`
59
}
60
61
// RefundOptions describes refund endpoint valid query string parameters.
62
//
63
// See: https://docs.mollie.com/reference/v2/refunds-api/get-refund.
64
type RefundOptions struct {
65
	Embed EmbedValue `url:"embed,omitempty"`
66
}
67
68
// ListRefundOptions describes list refund endpoint valid query string parameters.
69
//
70
// See: https://docs.mollie.com/reference/v2/refunds-api/list-refunds.
71
type ListRefundOptions struct {
72
	From      string     `url:"from,omitempty"`
73
	Limit     string     `url:"limit,omitempty"`
74
	ProfileID string     `url:"profileId,omitempty"`
75
	Embed     EmbedValue `url:"embed,omitempty"`
76
}
77
78
// RefundsService instance operates over refund resources.
79
type RefundsService service
80
81
// Get retrieve a single refund by its ID.
82
//
83
// If you do not know the original payment’s ID, you can use the List payment refunds endpoint.
84
func (rs *RefundsService) Get(paymentID, refundID string, options *RefundOptions) (refund Refund, err error) {
85 1
	u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID)
86 1
	if options != nil {
87 1
		v, _ := query.Values(options)
88 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
89
	}
90
91 1
	req, err := rs.client.NewAPIRequest(http.MethodGet, u, options)
92 1
	if err != nil {
93 1
		return
94
	}
95
96 1
	res, err := rs.client.Do(req)
97 1
	if err != nil {
98 1
		return
99
	}
100
101 1
	if err = json.Unmarshal(res.content, &refund); err != nil {
102 1
		return
103
	}
104
105 1
	return
106
}
107
108
// Create a refund payment request.
109
//
110
// See https://docs.mollie.com/reference/v2/refunds-api/create-refund.
111
func (rs *RefundsService) Create(paymentID string, re Refund, options *RefundOptions) (rf Refund, err error) {
112 1
	u := fmt.Sprintf("v2/payments/%s/refunds", paymentID)
113 1
	if options != nil {
114 1
		v, _ := query.Values(options)
115 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
116
	}
117
118 1
	if rs.client.HasAccessToken() && rs.client.config.testing {
119 1
		re.TestMode = true
120
	}
121
122 1
	req, err := rs.client.NewAPIRequest(http.MethodPost, u, re)
123 1
	if err != nil {
124 1
		return
125
	}
126
127 1
	res, err := rs.client.Do(req)
128 1
	if err != nil {
129 1
		return
130
	}
131
132 1
	if err = json.Unmarshal(res.content, &rf); err != nil {
133 1
		return
134
	}
135
136 1
	return
137
}
138
139
// Cancel try to cancel the refund request.
140
// The refund can only be canceled while the refund’s status is either queued or pending.
141
// See https://docs.mollie.com/reference/v2/refunds-api/cancel-refund
142
func (rs *RefundsService) Cancel(paymentID, refundID string, options *RefundOptions) (err error) {
143 1
	u := fmt.Sprintf("v2/payments/%s/refunds/%s", paymentID, refundID)
144 1
	if options != nil {
145 1
		v, _ := query.Values(options)
146 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
147
	}
148
149 1
	req, err := rs.client.NewAPIRequest(http.MethodDelete, u, nil)
150 1
	if err != nil {
151 1
		return
152
	}
153
154 1
	_, err = rs.client.Do(req)
155 1
	if err != nil {
156 1
		return
157
	}
158
159 1
	return
160
}
161
162
// ListRefund calls the top level https://api.mollie.com/v2/refunds.
163
//
164
// See https://docs.mollie.com/reference/v2/refunds-api/list-refunds.
165
func (rs *RefundsService) ListRefund(options *ListRefundOptions) (rl *RefundList, err error) {
166 1
	u := "v2/refunds"
167 1
	if options != nil {
168 1
		v, _ := query.Values(options)
169 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
170
	}
171 1
	return rs.list(u)
172
}
173
174
// ListRefundPayment calls the payment-specific
175
// https://api.mollie.com/v2/payments/*paymentId*/refunds.
176
// Only refunds for that specific payment are returned.
177
// See https://docs.mollie.com/reference/v2/refunds-api/list-refunds
178
func (rs *RefundsService) ListRefundPayment(paymentID string, options *ListRefundOptions) (rl *RefundList, err error) {
179 1
	u := fmt.Sprintf("v2/payments/%s/refunds", paymentID)
180 1
	if options != nil {
181 1
		v, _ := query.Values(options)
182 1
		u = fmt.Sprintf("%s?%s", u, v.Encode())
183
	}
184 1
	return rs.list(u)
185
}
186
187
func (rs *RefundsService) list(uri string) (rl *RefundList, err error) {
188 1
	req, err := rs.client.NewAPIRequest(http.MethodGet, uri, nil)
189 1
	if err != nil {
190 1
		return
191
	}
192 1
	res, err := rs.client.Do(req)
193 1
	if err != nil {
194 1
		return
195
	}
196 1
	if err = json.Unmarshal(res.content, &rl); err != nil {
197 1
		return
198
	}
199 1
	return
200
}
201