webhooks_service.go   A
last analyzed

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 68
dl 0
loc 128
rs 10
c 0
b 0
f 0

6 Methods

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