gintonic

package module
v0.0.0-...-8cf1ac4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 9 Imported by: 0

README

Gin&Tonic

Glance

Documentation | 中文文档

Introduction

Gin&Tonic is a LemonSqueezy webhook library for gin framework that lets you build LemonSqueezy webhooks quickly and elegantly

Quick start

Installation

go get -u github.com/YianAndCode/gintonic

Import

import "github.com/YianAndCode/gintonic"

Write your bussines code

import (
	"context"
	"fmt"

	"github.com/YianAndCode/gintonic"
	"github.com/YianAndCode/gintonic/lmdata"
)

var _ gintonic.OrderCreatedHandler = OrderCreated

func OrderCreated(ctx context.Context, meta lmdata.Meta, data lmdata.Order) error {
	fmt.Printf("You made a sale! Total amount is %s\n", data.Attributes.TotalFormatted)
	return nil
}

Register routes to gin and run

func main() {
	r := gin.New()
	r.Use(gin.Recovery())

	gt := gintonic.New(
		"[Load secret from somewhere]",
		gintonic.WithOrderCreatedHandler(OrderCreated),
	)
	r.POST("/lemonsqueezy/webhook", gt.LemonSqueezyWebhook)

	r.Run()
}

Definition

Instantiate

You can get an instantce of gintonic just by the New function, and the first parameter is fixed as secret, then you can set any handlers as needed:

gt := gintonic.New(
    "[YOUR_SECRET]",
    gintonic.WithOrderCreatedHandler(OrderCreated), // The handler for orcer_created event
    gintonic.WithDefaultHandler(DefaultHandler),    // The default handler
)

Setting handler is done using the gintonic.With[EventName]Handler() option functions, the specific functions can be found in option.go

Handler

Handler definitions are in handler.go, divided into two types: Event Handler and DefaultHandler.

Their relationship is like case and default in a switch statement. When instantiating a gintonic instance with New, the Handler registered using gintonic.WithXXXHandler(XXX) can be considered a case. When receiving a webhook event, gintonic will judge which case it belongs to and then call the corresponding handler. If it cannot find one, it will look for a DefaultHandler. If it cannot find the corresponding Event Handler and no DefaultHandler is set, then it will return an error.

DefaultHandler is defined as:

type DefaultHandler func(ctx context.Context, eventName string, meta lmdata.Meta, data interface{}) error

Event Handler is like:

type OrderCreatedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Order) error

The only diffrence is the type of data.

Core

gintonic interacts with gin using the GinTonic.LemonSqueezyWebhook method. This method will read HTTP Headers and POST body from gin, then verify the signature. After passing verification, it dispatches events to the registered handler.

So we just need to register the LemonSqueezyWebhook method of the GinTonic instance to a route in gin.

Documentation

Index

Constants

View Source
const (
	Event_OrderCreated                 string = "order_created"
	Event_OrderRefunded                string = "order_refunded"
	Event_SubscriptionCreated          string = "subscription_created"
	Event_SubscriptionUpdated          string = "subscription_updated"
	Event_SubscriptionCancelled        string = "subscription_cancelled"
	Event_SubscriptionResumed          string = "subscription_resumed"
	Event_SubscriptionExpired          string = "subscription_expired"
	Event_SubscriptionPaused           string = "subscription_paused"
	Event_SubscriptionUnpaused         string = "subscription_unpaused"
	Event_SubscriptionPaymentSuccess   string = "subscription_payment_success"
	Event_SubscriptionPaymentFailed    string = "subscription_payment_failed"
	Event_SubscriptionPaymentRecovered string = "subscription_payment_recovered"
	Event_LicenseKeyCreated            string = "license_key_created"
	Event_LicenseKeyUpdated            string = "license_key_updated"
)

Variables

This section is empty.

Functions

func ParseEventPayload

func ParseEventPayload(eventName string, payload []byte) (any, error)

func WithDefaultHandler

func WithDefaultHandler(handler DefaultHandler) ginTonicOption

func WithLicenseKeyCreatedHandler

func WithLicenseKeyCreatedHandler(handler LicenseKeyCreatedHandler) ginTonicOption

func WithLicenseKeyUpdatedHandler

func WithLicenseKeyUpdatedHandler(handler LicenseKeyUpdatedHandler) ginTonicOption

func WithOrderCreatedHandler

func WithOrderCreatedHandler(handler OrderCreatedHandler) ginTonicOption

func WithOrderRefundedHandler

func WithOrderRefundedHandler(handler OrderRefundedHandler) ginTonicOption

func WithSubscriptionCancelledHandler

func WithSubscriptionCancelledHandler(handler SubscriptionCancelledHandler) ginTonicOption

func WithSubscriptionCreatedHandler

func WithSubscriptionCreatedHandler(handler SubscriptionCreatedHandler) ginTonicOption

func WithSubscriptionExpiredHandler

func WithSubscriptionExpiredHandler(handler SubscriptionExpiredHandler) ginTonicOption

func WithSubscriptionPausedHandler

func WithSubscriptionPausedHandler(handler SubscriptionPausedHandler) ginTonicOption

func WithSubscriptionPaymentFailedHandler

func WithSubscriptionPaymentFailedHandler(handler SubscriptionPaymentFailedHandler) ginTonicOption

func WithSubscriptionPaymentRecoveredHandler

func WithSubscriptionPaymentRecoveredHandler(handler SubscriptionPaymentRecoveredHandler) ginTonicOption

func WithSubscriptionPaymentSuccessHandler

func WithSubscriptionPaymentSuccessHandler(handler SubscriptionPaymentSuccessHandler) ginTonicOption

func WithSubscriptionResumedHandler

func WithSubscriptionResumedHandler(handler SubscriptionResumedHandler) ginTonicOption

func WithSubscriptionUnpausedHandler

func WithSubscriptionUnpausedHandler(handler SubscriptionUnpausedHandler) ginTonicOption

func WithSubscriptionUpdatedHandler

func WithSubscriptionUpdatedHandler(handler SubscriptionUpdatedHandler) ginTonicOption

Types

type DefaultHandler

type DefaultHandler func(ctx context.Context, eventName string, meta lmdata.Meta, data interface{}) error

type GinTonic

type GinTonic struct {
	// contains filtered or unexported fields
}

func New

func New(secret string, opts ...ginTonicOption) *GinTonic

func (*GinTonic) LemonSqueezyWebhook

func (g *GinTonic) LemonSqueezyWebhook(c *gin.Context)

type LicenseKeyCreatedHandler

type LicenseKeyCreatedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.LicenseKey) error

type LicenseKeyUpdatedHandler

type LicenseKeyUpdatedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.LicenseKey) error

type OrderCreatedHandler

type OrderCreatedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Order) error

type OrderRefundedHandler

type OrderRefundedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Order) error

type SubscriptionCancelledHandler

type SubscriptionCancelledHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Subscription) error

type SubscriptionCreatedHandler

type SubscriptionCreatedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Subscription) error

type SubscriptionExpiredHandler

type SubscriptionExpiredHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Subscription) error

type SubscriptionPausedHandler

type SubscriptionPausedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Subscription) error

type SubscriptionPaymentFailedHandler

type SubscriptionPaymentFailedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.SubscriptionInvoice) error

type SubscriptionPaymentRecoveredHandler

type SubscriptionPaymentRecoveredHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.SubscriptionInvoice) error

type SubscriptionPaymentSuccessHandler

type SubscriptionPaymentSuccessHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.SubscriptionInvoice) error

type SubscriptionResumedHandler

type SubscriptionResumedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Subscription) error

type SubscriptionUnpausedHandler

type SubscriptionUnpausedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Subscription) error

type SubscriptionUpdatedHandler

type SubscriptionUpdatedHandler func(ctx context.Context, meta lmdata.Meta, data lmdata.Subscription) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL