|
1
|
|
|
package mollie |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"encoding/json" |
|
6
|
|
|
"fmt" |
|
7
|
|
|
"time" |
|
8
|
|
|
) |
|
9
|
|
|
|
|
10
|
|
|
// WebhookEventType represents the type of event that triggers a webhook. |
|
11
|
|
|
type WebhookEventType string |
|
12
|
|
|
|
|
13
|
|
|
// List of possible webhook event types. |
|
14
|
|
|
const ( |
|
15
|
|
|
PaymentLinkPaidWebhookEvent WebhookEventType = "payment-link.paid" |
|
16
|
|
|
BalanceTransactionCreatedWebhookEvent WebhookEventType = "balance-transaction.created" |
|
17
|
|
|
SalesInvoiceCreatedWebhookEvent WebhookEventType = "sales-invoice.created" |
|
18
|
|
|
SalesInvoiceIssuedWebhookEvent WebhookEventType = "sales-invoice.issued" |
|
19
|
|
|
SalesInvoiceCanceledWebhookEvent WebhookEventType = "sales-invoice.canceled" |
|
20
|
|
|
SalesInvoicePaidWebhookEvent WebhookEventType = "sales-invoice.paid" |
|
21
|
|
|
AllWebhookEvents WebhookEventType = "*" |
|
22
|
|
|
) |
|
23
|
|
|
|
|
24
|
|
|
// WebhookStatus represents the status of a webhook. |
|
25
|
|
|
type WebhookStatus string |
|
26
|
|
|
|
|
27
|
|
|
// List of possible webhook statuses. |
|
28
|
|
|
const ( |
|
29
|
|
|
WebhookStatusEnabled WebhookStatus = "enabled" |
|
30
|
|
|
WebhookStatusDisabled WebhookStatus = "disabled" |
|
31
|
|
|
WebhookStatusBlocked WebhookStatus = "blocked" |
|
32
|
|
|
WebhookStatusDeleted WebhookStatus = "deleted" |
|
33
|
|
|
) |
|
34
|
|
|
|
|
35
|
|
|
// CreateWebhook represents the payload to create a new webhook. |
|
36
|
|
|
type CreateWebhook struct { |
|
37
|
|
|
TestMode bool `json:"testmode,omitempty"` |
|
38
|
|
|
Name string `json:"name,omitempty"` |
|
39
|
|
|
URL string `json:"url,omitempty"` |
|
40
|
|
|
EventTypes []WebhookEventType `json:"eventTypes,omitempty"` |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// UpdateWebhook represents the payload to update an existing webhook. |
|
44
|
|
|
type UpdateWebhook struct { |
|
45
|
|
|
TestMode bool `json:"testmode,omitempty"` |
|
46
|
|
|
Name string `json:"name,omitempty"` |
|
47
|
|
|
URL string `json:"url,omitempty"` |
|
48
|
|
|
EventTypes []WebhookEventType `json:"eventTypes,omitempty"` |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// DeleteWebhook represents the payload to delete a webhook. |
|
52
|
|
|
type DeleteWebhook struct { |
|
53
|
|
|
TestMode bool `json:"testmode,omitempty"` |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// TestWebhook represents the payload to ping a webhook. |
|
57
|
|
|
type TestWebhook struct { |
|
58
|
|
|
TestMode bool `json:"testmode,omitempty"` |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Webhook describes a webhook object registered in the Mollie system. |
|
62
|
|
|
type Webhook struct { |
|
63
|
|
|
Resource string `json:"resource,omitempty"` |
|
64
|
|
|
ID string `json:"id,omitempty"` |
|
65
|
|
|
URL string `json:"url,omitempty"` |
|
66
|
|
|
ProfileID string `json:"profileId,omitempty"` |
|
67
|
|
|
Name string `json:"name,omitempty"` |
|
68
|
|
|
WebhookSecret string `json:"webhookSecret,omitempty"` |
|
69
|
|
|
Mode Mode `json:"mode,omitempty"` |
|
70
|
|
|
Status WebhookStatus `json:"status,omitempty"` |
|
71
|
|
|
Links WebhookLinks `json:"_links,omitempty"` |
|
72
|
|
|
EventTypes []WebhookEventType `json:"eventTypes,omitempty"` |
|
73
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// WebhookLinks represents the links related to a webhook. |
|
77
|
|
|
type WebhookLinks struct { |
|
78
|
|
|
Self *URL `json:"self,omitempty"` |
|
79
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// WebhooksListOptions represents the query parameters for listing webhooks. |
|
83
|
|
|
type WebhooksListOptions struct { |
|
84
|
|
|
From string `url:"from,omitempty"` |
|
85
|
|
|
Limit int `url:"limit,omitempty"` |
|
86
|
|
|
Sort SortDirection `url:"sort,omitempty"` |
|
87
|
|
|
EventType []WebhookEventType `url:"eventType,omitempty"` |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// WebhookList represents a paginated list of webhooks. |
|
91
|
|
|
type WebhookList struct { |
|
92
|
|
|
Count int `json:"count,omitempty"` |
|
93
|
|
|
Embedded struct { |
|
94
|
|
|
Webhooks []*Webhook `json:"webhooks,omitempty"` |
|
95
|
|
|
} `json:"_embedded,omitempty"` |
|
96
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// WebhookService handles webhook-related operations. |
|
100
|
|
|
type WebhookService service |
|
101
|
|
|
|
|
102
|
|
|
// Create creates a new webhook. |
|
103
|
|
|
// |
|
104
|
|
|
// See: https://docs.mollie.com/reference/create-webhook |
|
105
|
|
|
func (s *WebhookService) Create(ctx context.Context, wh CreateWebhook) ( |
|
106
|
|
|
res *Response, |
|
107
|
|
|
w *Webhook, |
|
108
|
|
|
err error, |
|
109
|
|
|
) { |
|
110
|
|
|
if s.client.HasAccessToken() && s.client.config.testing { |
|
111
|
|
|
wh.TestMode = true |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
res, err = s.client.post(ctx, "/v2/webhooks", wh, nil) |
|
115
|
|
|
if err != nil { |
|
116
|
|
|
return |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if err = json.Unmarshal(res.content, &w); err != nil { |
|
120
|
|
|
return |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Get retrieves a webhook by its ID. |
|
127
|
|
|
// |
|
128
|
|
|
// See: https://docs.mollie.com/reference/get-webhook |
|
129
|
|
|
func (s *WebhookService) Get(ctx context.Context, webhook string) ( |
|
130
|
|
|
res *Response, |
|
131
|
|
|
w *Webhook, |
|
132
|
|
|
err error, |
|
133
|
|
|
) { |
|
134
|
|
|
res, err = s.client.get(ctx, fmt.Sprintf("/v2/webhooks/%s", webhook), nil) |
|
135
|
|
|
if err != nil { |
|
136
|
|
|
return |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if err = json.Unmarshal(res.content, &w); err != nil { |
|
140
|
|
|
return |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// Update modifies an existing webhook. |
|
147
|
|
|
// |
|
148
|
|
|
// See: https://docs.mollie.com/reference/update-webhook |
|
149
|
|
|
func (s *WebhookService) Update(ctx context.Context, webhook string, uw UpdateWebhook) ( |
|
150
|
|
|
res *Response, |
|
151
|
|
|
w *Webhook, |
|
152
|
|
|
err error, |
|
153
|
|
|
) { |
|
154
|
|
|
if s.client.HasAccessToken() && s.client.config.testing { |
|
155
|
|
|
uw.TestMode = true |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
res, err = s.client.patch(ctx, fmt.Sprintf("/v2/webhooks/%s", webhook), uw) |
|
159
|
|
|
if err != nil { |
|
160
|
|
|
return |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if err = json.Unmarshal(res.content, &w); err != nil { |
|
164
|
|
|
return |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
// List retrieves a list of webhooks with optional filtering. |
|
171
|
|
|
// |
|
172
|
|
|
// See: https://docs.mollie.com/reference/list-webhooks |
|
173
|
|
|
func (s *WebhookService) List(ctx context.Context, options *WebhooksListOptions) ( |
|
174
|
|
|
res *Response, |
|
175
|
|
|
wl *WebhookList, |
|
176
|
|
|
err error, |
|
177
|
|
|
) { |
|
178
|
|
|
res, err = s.client.get(ctx, "/v2/webhooks", options) |
|
179
|
|
|
if err != nil { |
|
180
|
|
|
return |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if err = json.Unmarshal(res.content, &wl); err != nil { |
|
184
|
|
|
return |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
// Delete removes a webhook by its ID. |
|
191
|
|
|
// |
|
192
|
|
|
// See: https://docs.mollie.com/reference/delete-webhook |
|
193
|
|
|
func (s *WebhookService) Delete(ctx context.Context, webhook string) ( |
|
194
|
|
|
res *Response, |
|
195
|
|
|
err error, |
|
196
|
|
|
) { |
|
197
|
|
|
var dw DeleteWebhook |
|
198
|
|
|
if s.client.HasAccessToken() && s.client.config.testing { |
|
199
|
|
|
dw = DeleteWebhook{ |
|
200
|
|
|
TestMode: true, |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
res, err = s.client.delete(ctx, fmt.Sprintf("/v2/webhooks/%s", webhook), dw) |
|
205
|
|
|
if err != nil { |
|
206
|
|
|
return |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
// Test sends a test ping to the specified webhook. |
|
213
|
|
|
// |
|
214
|
|
|
// See: https://docs.mollie.com/reference/test-webhook |
|
215
|
|
|
func (s *WebhookService) Test(ctx context.Context, webhook string) ( |
|
216
|
|
|
res *Response, |
|
217
|
|
|
err error, |
|
218
|
|
|
) { |
|
219
|
|
|
var tw TestWebhook |
|
220
|
|
|
if s.client.HasAccessToken() && s.client.config.testing { |
|
221
|
|
|
tw = TestWebhook{ |
|
222
|
|
|
TestMode: true, |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
res, err = s.client.post(ctx, fmt.Sprintf("/v2/webhooks/%s/ping", webhook), tw, nil) |
|
227
|
|
|
if err != nil { |
|
228
|
|
|
return |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return |
|
232
|
|
|
} |
|
233
|
|
|
|