diff --git a/README.md b/README.md index 829f493..43772d8 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,43 @@ if err != nil { } ``` +### WebHooks + +Webhooks allow Lemon Squeezy to send new data to your application when certain events occur inside your store. +You can use the sample code below as inspiration for a basic `http.HandlerFunc` which processes webhook events on your server. + +```go +func WebhookHandler(_ http.ResponseWriter, req *http.Request) { + + // 1. Authenticate the webhook request from Lemon Squeezy using the `X-Signature` header + + // 2. Process the payload if the request is authenticated + eventName := req.Header.Get("X-Event-Name") + payload, err := io.ReadAll(req.Body) + if err != nil { + log.Fatal(err) + } + + switch eventName { + case lemonsqueezy.WebhookEventSubscriptionCreated: + var request lemonsqueezy.WebhookRequestSubscription + if err = json.Unmarshal(payload, &request); err != nil { + log.Fatal(err) + } + // handle subscription_created request + case lemonsqueezy.WebhookEventOrderCreated: + var request lemonsqueezy.WebhookRequestOrder + if err = json.Unmarshal(payload, &request); err != nil { + log.Fatal(err) + } + // handle order_created request + default: + log.Fatal(fmt.Sprintf("invalid event [%s] received with request [%s]", eventName, string(payload))) + } +} +``` + + ## Testing You can run the unit tests for this client from the root directory using the command below: diff --git a/webhook_event.go b/webhook_event.go new file mode 100644 index 0000000..052f00f --- /dev/null +++ b/webhook_event.go @@ -0,0 +1,48 @@ +package lemonsqueezy + +const ( + // WebhookEventOrderCreated occurs when a new order is successfully placed. + WebhookEventOrderCreated = "order_created" + + // WebhookEventOrderRefunded occurs when a full or partial refund is made on an order. + WebhookEventOrderRefunded = "order_refunded" + + // WebhookEventSubscriptionCreated occurs when a new subscription is successfully created. + WebhookEventSubscriptionCreated = "subscription_created" + + // WebhookEventSubscriptionUpdated occurs when a subscription's data is changed or updated. + WebhookEventSubscriptionUpdated = "subscription_updated" + + // WebhookEventSubscriptionCancelled occurs when a subscription is cancelled manually by the customer or store owner. + WebhookEventSubscriptionCancelled = "subscription_cancelled" + + // WebhookEventSubscriptionResumed occurs when a subscription is resumed after being previously cancelled. + WebhookEventSubscriptionResumed = "subscription_resumed" + + // WebhookEventSubscriptionExpired occurs when a subscription has ended after being previously cancelled, or once dunning has been completed for past_due subscriptions. + WebhookEventSubscriptionExpired = "subscription_expired" + + // WebhookEventSubscriptionPaused occurs when a subscription's payment collection is paused. + WebhookEventSubscriptionPaused = "subscription_paused" + + // WebhookEventSubscriptionUnpaused occurs when a subscription's payment collection is resumed after being previously paused. + WebhookEventSubscriptionUnpaused = "subscription_unpaused" + + // WebhookEventSubscriptionPaymentSuccess occurs when a subscription payment is successful. + WebhookEventSubscriptionPaymentSuccess = "subscription_payment_success" + + // WebhookEventSubscriptionPaymentFailed occurs when a subscription renewal payment fails. + WebhookEventSubscriptionPaymentFailed = "subscription_payment_failed" + + // WebhookEventSubscriptionPaymentRecovered occurs when a subscription has a successful payment after a failed payment. + WebhookEventSubscriptionPaymentRecovered = "subscription_payment_recovered" + + // WebhookEventSubscriptionPaymentRefunded occurs when a subscription payment is refunded. + WebhookEventSubscriptionPaymentRefunded = "subscription_payment_refunded" + + // WebhookEventLicenseKeyCreated occurs when a license key is created from a new order. + WebhookEventLicenseKeyCreated = "license_key_created" + + // WebhookEventLicenseKeyUpdated occurs when a license key is updated. + WebhookEventLicenseKeyUpdated = "license_key_updated" +)