Skip to content

Commit cf03008

Browse files
authored
Merge pull request #19 from NdoleStudio/15/add-example-usage-with-webhooks-#15
Added example webhook usage. Closes #15
2 parents a4fd5be + 401ebec commit cf03008

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,43 @@ if err != nil {
120120
}
121121
```
122122

123+
### WebHooks
124+
125+
Webhooks allow Lemon Squeezy to send new data to your application when certain events occur inside your store.
126+
You can use the sample code below as inspiration for a basic `http.HandlerFunc` which processes webhook events on your server.
127+
128+
```go
129+
func WebhookHandler(_ http.ResponseWriter, req *http.Request) {
130+
131+
// 1. Authenticate the webhook request from Lemon Squeezy using the `X-Signature` header
132+
133+
// 2. Process the payload if the request is authenticated
134+
eventName := req.Header.Get("X-Event-Name")
135+
payload, err := io.ReadAll(req.Body)
136+
if err != nil {
137+
log.Fatal(err)
138+
}
139+
140+
switch eventName {
141+
case lemonsqueezy.WebhookEventSubscriptionCreated:
142+
var request lemonsqueezy.WebhookRequestSubscription
143+
if err = json.Unmarshal(payload, &request); err != nil {
144+
log.Fatal(err)
145+
}
146+
// handle subscription_created request
147+
case lemonsqueezy.WebhookEventOrderCreated:
148+
var request lemonsqueezy.WebhookRequestOrder
149+
if err = json.Unmarshal(payload, &request); err != nil {
150+
log.Fatal(err)
151+
}
152+
// handle order_created request
153+
default:
154+
log.Fatal(fmt.Sprintf("invalid event [%s] received with request [%s]", eventName, string(payload)))
155+
}
156+
}
157+
```
158+
159+
123160
## Testing
124161

125162
You can run the unit tests for this client from the root directory using the command below:

webhook_event.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package lemonsqueezy
2+
3+
const (
4+
// WebhookEventOrderCreated occurs when a new order is successfully placed.
5+
WebhookEventOrderCreated = "order_created"
6+
7+
// WebhookEventOrderRefunded occurs when a full or partial refund is made on an order.
8+
WebhookEventOrderRefunded = "order_refunded"
9+
10+
// WebhookEventSubscriptionCreated occurs when a new subscription is successfully created.
11+
WebhookEventSubscriptionCreated = "subscription_created"
12+
13+
// WebhookEventSubscriptionUpdated occurs when a subscription's data is changed or updated.
14+
WebhookEventSubscriptionUpdated = "subscription_updated"
15+
16+
// WebhookEventSubscriptionCancelled occurs when a subscription is cancelled manually by the customer or store owner.
17+
WebhookEventSubscriptionCancelled = "subscription_cancelled"
18+
19+
// WebhookEventSubscriptionResumed occurs when a subscription is resumed after being previously cancelled.
20+
WebhookEventSubscriptionResumed = "subscription_resumed"
21+
22+
// WebhookEventSubscriptionExpired occurs when a subscription has ended after being previously cancelled, or once dunning has been completed for past_due subscriptions.
23+
WebhookEventSubscriptionExpired = "subscription_expired"
24+
25+
// WebhookEventSubscriptionPaused occurs when a subscription's payment collection is paused.
26+
WebhookEventSubscriptionPaused = "subscription_paused"
27+
28+
// WebhookEventSubscriptionUnpaused occurs when a subscription's payment collection is resumed after being previously paused.
29+
WebhookEventSubscriptionUnpaused = "subscription_unpaused"
30+
31+
// WebhookEventSubscriptionPaymentSuccess occurs when a subscription payment is successful.
32+
WebhookEventSubscriptionPaymentSuccess = "subscription_payment_success"
33+
34+
// WebhookEventSubscriptionPaymentFailed occurs when a subscription renewal payment fails.
35+
WebhookEventSubscriptionPaymentFailed = "subscription_payment_failed"
36+
37+
// WebhookEventSubscriptionPaymentRecovered occurs when a subscription has a successful payment after a failed payment.
38+
WebhookEventSubscriptionPaymentRecovered = "subscription_payment_recovered"
39+
40+
// WebhookEventSubscriptionPaymentRefunded occurs when a subscription payment is refunded.
41+
WebhookEventSubscriptionPaymentRefunded = "subscription_payment_refunded"
42+
43+
// WebhookEventLicenseKeyCreated occurs when a license key is created from a new order.
44+
WebhookEventLicenseKeyCreated = "license_key_created"
45+
46+
// WebhookEventLicenseKeyUpdated occurs when a license key is updated.
47+
WebhookEventLicenseKeyUpdated = "license_key_updated"
48+
)

0 commit comments

Comments
 (0)