Skip to content

Commit

Permalink
Merge branch 'main' of github.com:NdoleStudio/lemonsqueezy-go
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Jan 25, 2024
2 parents f3419f4 + cf03008 commit 2d689f9
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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:
Expand Down
48 changes: 48 additions & 0 deletions webhook_event.go
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 2d689f9

Please sign in to comment.