1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"fmt" |
7
|
|
|
"time" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// CaptureMode describes the mode of a capture. |
11
|
|
|
type CaptureMode string |
12
|
|
|
|
13
|
|
|
// CaptureMode possible values. |
14
|
|
|
const ( |
15
|
|
|
AutomaticCapture CaptureMode = "automatic" |
16
|
|
|
ManualCapture CaptureMode = "manual" |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
// CaptureStatus describes the status of a capture. |
20
|
|
|
type CaptureStatus string |
21
|
|
|
|
22
|
|
|
// CaptureStatus possible values. |
23
|
|
|
const ( |
24
|
|
|
CaptureStatusPending CaptureStatus = "pending" |
25
|
|
|
CaptureStatusSucceeded CaptureStatus = "succeeded" |
26
|
|
|
CaptureStatusFailed CaptureStatus = "failed" |
27
|
|
|
) |
28
|
|
|
|
29
|
|
|
// CreateCapture describes the payload for creating a capture. |
30
|
|
|
type CreateCapture struct { |
31
|
|
|
Description string `json:"description,omitempty"` |
32
|
|
|
Metadata any `json:"metadata,omitempty"` |
33
|
|
|
Amount *Amount `json:"amount,omitempty"` |
34
|
|
|
CaptureAccessTokenFields |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// CaptureAccessTokenFields describes the payload for creating a capture with an access token. |
38
|
|
|
type CaptureAccessTokenFields struct { |
39
|
|
|
Testmode bool `json:"testmode,omitempty"` |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Capture describes a single capture. |
43
|
|
|
// Captures are used for payments that have the authorize-then-capture flow. |
44
|
|
|
type Capture struct { |
45
|
|
|
Resource string `json:"resource,omitempty"` |
46
|
|
|
ID string `json:"id,omitempty"` |
47
|
|
|
Mode Mode `json:"mode,omitempty"` |
48
|
|
|
Amount *Amount `json:"amount,omitempty"` |
49
|
|
|
Status CaptureStatus `json:"status,omitempty"` |
50
|
|
|
SettlementAmount *Amount `json:"settlementAmount,omitempty"` |
51
|
1 |
|
PaymentID string `json:"paymentId,omitempty"` |
52
|
|
|
ShipmentID string `json:"shipmentId,omitempty"` |
53
|
1 |
|
SettlementID string `json:"settlementId,omitempty"` |
54
|
1 |
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
55
|
1 |
|
Metadata any `json:"metadata,omitempty"` |
56
|
|
|
Links CaptureLinks `json:"_links,omitempty"` |
57
|
|
|
CaptureAccessTokenFields |
58
|
1 |
|
} |
59
|
1 |
|
|
60
|
|
|
// CaptureLinks contains relevant links for a capture object. |
61
|
|
|
type CaptureLinks struct { |
62
|
1 |
|
Self *URL `json:"self,omitempty"` |
63
|
|
|
Payment *URL `json:"payment,omitempty"` |
64
|
|
|
Shipment *URL `json:"shipment,omitempty"` |
65
|
|
|
Settlement *URL `json:"settlement,omitempty"` |
66
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
// CaptureOptions describes the query params available to use when retrieving captures. |
70
|
|
|
// |
71
|
1 |
|
// See: https://docs.mollie.com/reference/get-capture#embedding-of-related-resources |
72
|
1 |
|
type CaptureOptions struct { |
73
|
1 |
|
Embed []EmbedValue `url:"embed,omitempty"` |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
// CapturesList describes a list of captures. |
77
|
1 |
|
type CapturesList struct { |
78
|
|
|
Count int `json:"count,omitempty"` |
79
|
|
|
Embedded struct { |
80
|
1 |
|
Captures []*Capture |
81
|
|
|
} `json:"_embedded,omitempty"` |
82
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// CapturesService operates over captures resource. |
86
|
|
|
type CapturesService service |
87
|
|
|
|
88
|
|
|
// Get retrieves a single capture by its ID. |
89
|
|
|
// Note the original payment’s ID is needed as well. |
90
|
|
|
// |
91
|
|
|
// See: https://docs.mollie.com/reference/get-capture |
92
|
|
|
func (cs *CapturesService) Get(ctx context.Context, payment, capture string, options *CaptureOptions) ( |
93
|
|
|
res *Response, |
94
|
|
|
c *Capture, |
95
|
|
|
err error, |
96
|
|
|
) { |
97
|
|
|
u := fmt.Sprintf("v2/payments/%s/captures/%s", payment, capture) |
98
|
|
|
|
99
|
|
|
res, err = cs.client.get(ctx, u, options) |
100
|
|
|
if err != nil { |
101
|
|
|
return |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if err = json.Unmarshal(res.content, &c); err != nil { |
105
|
|
|
return |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Create creates a new capture for a payment. |
112
|
|
|
// |
113
|
|
|
// See: https://docs.mollie.com/reference/create-capture |
114
|
|
|
func (cs *CapturesService) Create(ctx context.Context, payment string, capture CreateCapture) ( |
115
|
|
|
res *Response, |
116
|
|
|
c *Capture, |
117
|
|
|
err error, |
118
|
|
|
) { |
119
|
|
|
u := fmt.Sprintf("v2/payments/%s/captures", payment) |
120
|
|
|
|
121
|
|
|
if cs.client.HasAccessToken() && cs.client.config.testing { |
122
|
|
|
capture.Testmode = true |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
res, err = cs.client.post(ctx, u, capture, nil) |
126
|
|
|
if err != nil { |
127
|
|
|
return |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if err = json.Unmarshal(res.content, &c); err != nil { |
131
|
|
|
return |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// List retrieves all captures for a certain payment. |
138
|
|
|
// |
139
|
|
|
// See: https://docs.mollie.com/reference/list-captures |
140
|
|
|
func (cs *CapturesService) List(ctx context.Context, payment string, options *CaptureOptions) ( |
141
|
|
|
res *Response, |
142
|
|
|
cl *CapturesList, |
143
|
|
|
err error, |
144
|
|
|
) { |
145
|
|
|
u := fmt.Sprintf("v2/payments/%s/captures", payment) |
146
|
|
|
|
147
|
|
|
res, err = cs.client.get(ctx, u, options) |
148
|
|
|
if err != nil { |
149
|
|
|
return |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if err = json.Unmarshal(res.content, &cl); err != nil { |
153
|
|
|
return |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return |
157
|
|
|
} |
158
|
|
|
|