1
|
|
|
package lemonsqueezy |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"crypto/hmac" |
6
|
|
|
"crypto/sha256" |
7
|
|
|
"encoding/hex" |
8
|
|
|
"encoding/json" |
9
|
|
|
"net/http" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
// WebhooksService is the used to verify the signature in webhook requests |
13
|
|
|
type WebhooksService service |
14
|
|
|
|
15
|
|
|
// Verify the signature in webhook requests |
16
|
|
|
// |
17
|
|
|
// https://docs.lemonsqueezy.com/api/webhooks#signing-requests |
18
|
|
|
func (service *WebhooksService) Verify(_ context.Context, signature string, body []byte) bool { |
19
|
|
|
key := []byte(service.client.signingSecret) |
20
|
|
|
h := hmac.New(sha256.New, key) |
21
|
|
|
h.Write(body) |
22
|
|
|
return hex.EncodeToString(h.Sum(nil)) == signature |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Create a webhook. |
26
|
|
|
// |
27
|
|
|
// https://docs.lemonsqueezy.com/api/webhooks#create-a-webhook |
28
|
|
|
func (service *WebhooksService) Create(ctx context.Context, params *WebhookCreateParams) (*WebhookApiResponse, *Response, error) { |
29
|
|
|
payload := map[string]any{ |
30
|
|
|
"data": map[string]any{ |
31
|
|
|
"type": "webhooks", |
32
|
|
|
"attributes": map[string]any{ |
33
|
|
|
"url": params.URL, |
34
|
|
|
"events": params.Events, |
35
|
|
|
"secret": params.Secret, |
36
|
|
|
}, |
37
|
|
|
"relationships": map[string]any{ |
38
|
|
|
"store": map[string]any{ |
39
|
|
|
"data": map[string]any{ |
40
|
|
|
"type": "stores", |
41
|
|
|
"id": params.StoreID, |
42
|
|
|
}, |
43
|
|
|
}, |
44
|
|
|
}, |
45
|
|
|
}, |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
response, err := service.client.do(ctx, http.MethodPost, "/v1/webhooks/", payload) |
49
|
|
|
if err != nil { |
50
|
|
|
return nil, response, err |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
webhook := new(WebhookApiResponse) |
54
|
|
|
if err = json.Unmarshal(*response.Body, webhook); err != nil { |
55
|
|
|
return nil, response, err |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return webhook, response, nil |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Get the webhook with the given ID. |
62
|
|
|
// |
63
|
|
|
// https://docs.lemonsqueezy.com/api/webhooks#retrieve-a-webhook |
64
|
|
|
func (service *WebhooksService) Get(ctx context.Context, webhookID string) (*WebhookApiResponse, *Response, error) { |
65
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/webhooks/"+webhookID) |
66
|
|
|
if err != nil { |
67
|
|
|
return nil, response, err |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
webhook := new(WebhookApiResponse) |
71
|
|
|
if err = json.Unmarshal(*response.Body, webhook); err != nil { |
72
|
|
|
return nil, response, err |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return webhook, response, nil |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Update an existing webhook |
79
|
|
|
// |
80
|
|
|
// https://docs.lemonsqueezy.com/api/subscriptions#update-a-webhook |
81
|
|
|
func (service *WebhooksService) Update(ctx context.Context, params *WebhookUpdateParams) (*WebhookApiResponse, *Response, error) { |
82
|
|
|
payload := map[string]any{ |
83
|
|
|
"data": map[string]any{ |
84
|
|
|
"type": "webhooks", |
85
|
|
|
"id": params.ID, |
86
|
|
|
"attributes": map[string]any{ |
87
|
|
|
"events": params.Events, |
88
|
|
|
"secret": params.Secret, |
89
|
|
|
}, |
90
|
|
|
}, |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
response, err := service.client.do(ctx, http.MethodPatch, "/v1/webhooks/"+params.ID, payload) |
94
|
|
|
if err != nil { |
95
|
|
|
return nil, response, err |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
subscription := new(WebhookApiResponse) |
99
|
|
|
if err = json.Unmarshal(*response.Body, subscription); err != nil { |
100
|
|
|
return nil, response, err |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return subscription, response, nil |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Delete a webhook with the given ID. |
107
|
|
|
// |
108
|
|
|
// https://docs.lemonsqueezy.com/api/webhooks#delete-a-webhook |
109
|
|
|
func (service *WebhooksService) Delete(ctx context.Context, webhookID string) (*Response, error) { |
110
|
|
|
return service.client.do(ctx, http.MethodDelete, "/v1/webhooks/"+webhookID) |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// List returns a paginated list of webhooks. |
114
|
|
|
// |
115
|
|
|
// https://docs.lemonsqueezy.com/api/webhooks#list-all-webhooks |
116
|
|
|
func (service *WebhooksService) List(ctx context.Context) (*WebhooksApiResponse, *Response, error) { |
117
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/webhooks") |
118
|
|
|
if err != nil { |
119
|
|
|
return nil, response, err |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
webhooks := new(WebhooksApiResponse) |
123
|
|
|
if err = json.Unmarshal(*response.Body, webhooks); err != nil { |
124
|
|
|
return nil, response, err |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return webhooks, response, nil |
128
|
|
|
} |
129
|
|
|
|