-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
163 lines (127 loc) · 3.77 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package mobilepay
import (
"context"
"fmt"
"net/http"
)
// subscribe/create webhook
const webhooksBasePath = "v1/webhooks"
type GetWebhookRequest struct {
}
type WebhookEventEnum int
type WebhookEvent string
const (
Unknown WebhookEventEnum = iota
PaymentReserved
PaymentExpired
PaymentPointActivated
)
func (webhookEvent WebhookEventEnum) Name() WebhookEvent {
return [...]WebhookEvent{"Unknown", "payment.reserved", "payment.expired", "paymentpoint.activated"}[webhookEvent]
}
type WebhookService interface {
Get(context.Context) (*WebhooksRoot, error)
Create(context.Context, *WebhookCreateParams) (*Webhook, error)
Find(context.Context, string) (*Webhook, error)
Update(context.Context, string, *WebhookUpdateParams) (*Webhook, error)
Delete(context.Context, string) error
}
type WebhookServiceOp struct {
client *Client
}
var _ WebhookService = &WebhookServiceOp{}
type GetWebhookResponse struct {
Webhooks []Webhook `json:"webhooks"`
}
// WebhookCreateParams represents a request to create a payment.
type WebhookCreateParams struct {
// Find of subscribed events.
Events []WebhookEvent `json:"events"`
// URL to where webhook requests will be sent. Must be HTTPS. Scheme and host will be converted to lower case. Result can be seen in the response.
Url string `json:"url"`
}
// WebhookUpdateParams represents a request to update a webhook record.
type WebhookUpdateParams struct {
Url string `json:"url"`
Events []WebhookEvent `json:"events"`
}
type Webhook struct {
WebhookId string `json:"webhookId"`
SignatureKey string `json:"signatureKey"`
Url string `json:"url"`
Events []WebhookEvent `json:"events"`
}
// webhooksRoot represents a response from the MobilePay App Payment API
type WebhooksRoot struct {
Webhooks []Webhook `json:"webhooks"`
}
// List all webhooks.
func (s WebhookServiceOp) Get(ctx context.Context) (*WebhooksRoot, error) {
path := webhooksBasePath
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
root := new(WebhooksRoot)
_, err = s.client.Do(ctx, req, root)
if err != nil {
return nil, err
}
return root, nil
}
// Create webhook
func (s *WebhookServiceOp) Create(ctx context.Context, createRequest *WebhookCreateParams) (*Webhook, error) {
if createRequest == nil {
return nil, newArgError("createRequest", "cannot be nil")
}
path := webhooksBasePath
req, err := s.client.NewRequest(ctx, http.MethodPost, path, createRequest)
if err != nil {
return nil, err
}
root := new(Webhook)
_, err = s.client.Do(ctx, req, root)
if err != nil {
return nil, err
}
return root, err
}
// Get individual webhook. It requires a non-empty webhook id.
func (s *WebhookServiceOp) Find(ctx context.Context, webhookId string) (*Webhook, error) {
path := fmt.Sprintf("%s/%s", webhooksBasePath, webhookId)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
root := new(Webhook)
_, err = s.client.Do(ctx, req, root)
if err != nil {
return nil, err
}
return root, err
}
func (s WebhookServiceOp) Update(ctx context.Context, webhookId string, request *WebhookUpdateParams) (*Webhook, error) {
path := fmt.Sprintf("%s/%s", webhooksBasePath, webhookId)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, request)
if err != nil {
return nil, err
}
root := new(Webhook)
_, err = s.client.Do(ctx, req, root)
if err != nil {
return nil, err
}
return root, err
}
func (s *WebhookServiceOp) Delete(ctx context.Context, webhookId string) error {
path := fmt.Sprintf("%s/%s", webhooksBasePath, webhookId)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}