generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 17
/
webhooks_service.go
129 lines (111 loc) · 3.5 KB
/
webhooks_service.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
package lemonsqueezy
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"strconv"
)
// WebhooksService is the used to verify the signature in webhook requests
type WebhooksService service
// Verify the signature in webhook requests
//
// https://docs.lemonsqueezy.com/api/webhooks#signing-requests
func (service *WebhooksService) Verify(_ context.Context, signature string, body []byte) bool {
key := []byte(service.client.signingSecret)
h := hmac.New(sha256.New, key)
h.Write(body)
return hex.EncodeToString(h.Sum(nil)) == signature
}
// Create a webhook.
//
// https://docs.lemonsqueezy.com/api/webhooks#create-a-webhook
func (service *WebhooksService) Create(ctx context.Context, storeId int, params *WebhookCreateParams) (*WebhookApiResponse, *Response, error) {
payload := map[string]any{
"data": map[string]any{
"type": "webhooks",
"attributes": map[string]any{
"url": params.URL,
"events": params.Events,
"secret": params.Secret,
},
"relationships": map[string]any{
"store": map[string]any{
"data": map[string]any{
"type": "stores",
"id": strconv.Itoa(storeId),
},
},
},
},
}
response, err := service.client.do(ctx, http.MethodPost, "/v1/webhooks/", payload)
if err != nil {
return nil, response, err
}
webhook := new(WebhookApiResponse)
if err = json.Unmarshal(*response.Body, webhook); err != nil {
return nil, response, err
}
return webhook, response, nil
}
// Get the webhook with the given ID.
//
// https://docs.lemonsqueezy.com/api/webhooks#retrieve-a-webhook
func (service *WebhooksService) Get(ctx context.Context, webhookID string) (*WebhookApiResponse, *Response, error) {
response, err := service.client.do(ctx, http.MethodGet, "/v1/webhooks/"+webhookID)
if err != nil {
return nil, response, err
}
webhook := new(WebhookApiResponse)
if err = json.Unmarshal(*response.Body, webhook); err != nil {
return nil, response, err
}
return webhook, response, nil
}
// Update an existing webhook
//
// https://docs.lemonsqueezy.com/api/subscriptions#update-a-webhook
func (service *WebhooksService) Update(ctx context.Context, params *WebhookUpdateParams) (*WebhookApiResponse, *Response, error) {
payload := map[string]any{
"data": map[string]any{
"type": "webhooks",
"id": params.ID,
"attributes": map[string]any{
"events": params.Events,
"secret": params.Secret,
},
},
}
response, err := service.client.do(ctx, http.MethodPatch, "/v1/webhooks/"+params.ID, payload)
if err != nil {
return nil, response, err
}
subscription := new(WebhookApiResponse)
if err = json.Unmarshal(*response.Body, subscription); err != nil {
return nil, response, err
}
return subscription, response, nil
}
// Delete a webhook with the given ID.
//
// https://docs.lemonsqueezy.com/api/webhooks#delete-a-webhook
func (service *WebhooksService) Delete(ctx context.Context, webhookID string) (*Response, error) {
return service.client.do(ctx, http.MethodDelete, "/v1/webhooks/"+webhookID)
}
// List returns a paginated list of webhooks.
//
// https://docs.lemonsqueezy.com/api/webhooks#list-all-webhooks
func (service *WebhooksService) List(ctx context.Context) (*WebhooksApiResponse, *Response, error) {
response, err := service.client.do(ctx, http.MethodGet, "/v1/webhooks")
if err != nil {
return nil, response, err
}
webhooks := new(WebhooksApiResponse)
if err = json.Unmarshal(*response.Body, webhooks); err != nil {
return nil, response, err
}
return webhooks, response, nil
}