stripe

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 14 Imported by: 0

README

GoModularAuth Stripe Billing Plugin

gomodularauth-stripe provides enterprise subscription billing, customer provisioning, checkout sessions, customer portal sessions, and webhook synchronization with Stripe for GoModularAuth.

Installation

go get github.com/BladiCreator/go-modular-auth/plugins/stripe

Quick Start


Detailed guide: doc/plugins/stripe.md.

Documentation

Overview

Package stripe implements Stripe billing, customer portal access, and subscription lifecycle management.

Index

Constants

View Source
const (
	// ModelStripeCustomers is the registered model name for Stripe customers.
	ModelStripeCustomers = "stripe_customers"

	// ModelStripeSubscriptions is the registered model name for Stripe subscriptions.
	ModelStripeSubscriptions = "stripe_subscriptions"
)
View Source
const PluginID = "stripe"

PluginID identifies this plugin in the GoModularAuth runtime.

Variables

View Source
var (
	// ErrCustomerNotFound indicates that no matching customer was found for the target reference.
	ErrCustomerNotFound = errors.New("stripe: customer not found")

	// ErrSubscriptionNotFound indicates that no active subscription was found.
	ErrSubscriptionNotFound = errors.New("stripe: subscription not found")

	// ErrInvalidWebhookSignature indicates the webhook signature header is invalid or failed verification.
	ErrInvalidWebhookSignature = errors.New("stripe: invalid webhook signature")

	// ErrInvalidPayload indicates the webhook payload could not be parsed or decoded.
	ErrInvalidPayload = errors.New("stripe: invalid webhook payload")

	// ErrInvalidReference indicates an empty or invalid target reference (ID or type).
	ErrInvalidReference = errors.New("stripe: invalid target reference")

	// ErrMissingAPIKey indicates the Stripe secret API key is missing or not configured.
	ErrMissingAPIKey = errors.New("stripe: missing required stripe api key")

	// ErrMissingWebhookSecret indicates the Stripe webhook signing secret is not configured.
	ErrMissingWebhookSecret = errors.New("stripe: missing required stripe webhook secret")
)

Functions

func RegisterSchema added in v1.3.0

func RegisterSchema(s *schema.Schema) error

RegisterSchema contributes the Stripe models to the compiled engine schema.

Types

type BillingPortalSession added in v1.3.0

type BillingPortalSession struct {
	URL string `json:"url"`
}

BillingPortalSession represents a created Stripe Billing Customer Portal session.

type CheckoutSession added in v1.3.0

type CheckoutSession struct {
	SessionID string `json:"session_id"`
	URL       string `json:"url"`
}

CheckoutSession represents the created Stripe Checkout Session result.

type Config

type Config struct {
	// APIKey is the secret Stripe API key (e.g. sk_test_... or sk_live_...).
	APIKey string

	// WebhookSecret is the secret used to verify webhook signatures (whsec_...).
	WebhookSecret string

	// AutoCreateCustomerOnSignUp automatically provisions a Stripe Customer when a new user registers.
	AutoCreateCustomerOnSignUp bool

	// AutoCreateCustomerOnOrgCreation automatically provisions a Stripe Customer when a new organization is created.
	AutoCreateCustomerOnOrgCreation bool
}

Config holds the configuration settings for the Stripe plugin module.

type CreateCheckoutInput added in v1.3.0

type CreateCheckoutInput struct {
	Target     TargetReference `json:"target"`
	PriceID    string          `json:"price_id"`
	SuccessURL string          `json:"success_url"`
	CancelURL  string          `json:"cancel_url"`
	Mode       string          `json:"mode,omitempty"` // "subscription" or "payment"
	Seats      int64           `json:"seats,omitempty"`
	Metadata   map[string]any  `json:"metadata,omitempty"`
}

CreateCheckoutInput contains parameters for creating a Stripe Checkout Session.

type Customer added in v1.3.0

type Customer struct {
	ID               string         `json:"id"`
	ReferenceID      string         `json:"reference_id"`
	ReferenceType    ReferenceType  `json:"reference_type"`
	StripeCustomerID string         `json:"stripe_customer_id"`
	Email            string         `json:"email"`
	Metadata         map[string]any `json:"metadata,omitempty"`
	CreatedAt        time.Time      `json:"created_at"`
	UpdatedAt        time.Time      `json:"updated_at"`
}

Customer models a synchronized Stripe customer mapped to a user or organization.

type Module added in v1.3.0

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

Module implements auth.PluginModule using plugin-sdk.

func New

func New(opts ...Option) *Module

New creates an installable Stripe PluginModule with optional configurations.

func (*Module) ID added in v1.3.0

func (m *Module) ID() string

ID returns the module's unique identifier.

func (*Module) Init added in v1.3.0

func (m *Module) Init(h *auth.Host) error

Init configures infrastructure, registers hooks, and provides the typed Capability into GoModularAuth.

func (*Module) RegisterSchema added in v1.3.0

func (m *Module) RegisterSchema(s *schema.Schema) error

RegisterSchema contributes Stripe models to the compiled schema.

type Option

type Option func(*Config)

Option configures a Stripe Config.

func WithAPIKey added in v1.3.0

func WithAPIKey(key string) Option

WithAPIKey sets the Stripe API secret key.

func WithAutoCreateCustomerOnOrgCreation added in v1.3.0

func WithAutoCreateCustomerOnOrgCreation(enabled bool) Option

WithAutoCreateCustomerOnOrgCreation controls whether customers are automatically provisioned on organization creation.

func WithAutoCreateCustomerOnSignUp added in v1.3.0

func WithAutoCreateCustomerOnSignUp(enabled bool) Option

WithAutoCreateCustomerOnSignUp controls whether customers are automatically provisioned on user registration.

func WithWebhookSecret

func WithWebhookSecret(secret string) Option

WithWebhookSecret sets the Stripe webhook signature verification secret.

type ReferenceType added in v1.3.0

type ReferenceType string

ReferenceType specifies the entity type bound to a billing resource.

const (
	// ReferenceUser binds the resource to an individual core User.
	ReferenceUser ReferenceType = "user"

	// ReferenceOrganization binds the resource to a multi-tenant Organization.
	ReferenceOrganization ReferenceType = "organization"
)

type Stripe added in v1.3.0

type Stripe interface {
	// GetCustomer retrieves the customer mapped to the target reference (user or organization).
	GetCustomer(ctx context.Context, target TargetReference) (*Customer, error)

	// CreateCustomer explicitly provisions a customer on Stripe and stores the local mapping.
	CreateCustomer(ctx context.Context, target TargetReference) (*Customer, error)

	// CreateCheckoutSession generates a Stripe Checkout Session for subscription or payment.
	CreateCheckoutSession(ctx context.Context, in CreateCheckoutInput) (*CheckoutSession, error)

	// CreateBillingPortalSession generates a customer portal URL for self-service subscription management.
	CreateBillingPortalSession(ctx context.Context, target TargetReference, returnURL string) (*BillingPortalSession, error)

	// GetActiveSubscription retrieves the current active subscription for the given target reference.
	GetActiveSubscription(ctx context.Context, target TargetReference) (*Subscription, error)

	// HasActiveSubscription reports whether the target identity has an active subscription matching any of the given price IDs.
	// If no price IDs are specified, any active subscription is accepted.
	HasActiveSubscription(ctx context.Context, target TargetReference, priceIDs ...string) (bool, error)

	// HandleWebhook processes an incoming Stripe webhook payload and verifies its signature in a transport-agnostic manner.
	HandleWebhook(ctx context.Context, payload []byte, signature string) (*WebhookEvent, error)
}

Stripe defines the typed capability interface exported by the Stripe plugin module. Applications retrieve this via auth.Get[stripe.Stripe](engine).

type Subscription

type Subscription struct {
	ID                   string             `json:"id"`
	CustomerID           string             `json:"customer_id"`
	ReferenceID          string             `json:"reference_id"`
	ReferenceType        ReferenceType      `json:"reference_type"`
	StripeSubscriptionID string             `json:"stripe_subscription_id"`
	StripePriceID        string             `json:"stripe_price_id"`
	Status               SubscriptionStatus `json:"status"`
	Seats                int64              `json:"seats,omitempty"`
	CancelAtPeriodEnd    bool               `json:"cancel_at_period_end"`
	CurrentPeriodStart   time.Time          `json:"current_period_start"`
	CurrentPeriodEnd     time.Time          `json:"current_period_end"`
	Metadata             map[string]any     `json:"metadata,omitempty"`
	CreatedAt            time.Time          `json:"created_at"`
	UpdatedAt            time.Time          `json:"updated_at"`
}

Subscription models a synchronized Stripe subscription.

func (*Subscription) Active added in v1.3.0

func (s *Subscription) Active() bool

Active reports whether the subscription grants current access.

type SubscriptionStatus

type SubscriptionStatus string

SubscriptionStatus represents the lifecycle state of a Stripe subscription.

const (
	StatusActive     SubscriptionStatus = "active"
	StatusPastDue    SubscriptionStatus = "past_due"
	StatusCanceled   SubscriptionStatus = "canceled"
	StatusIncomplete SubscriptionStatus = "incomplete"
	StatusTrialing   SubscriptionStatus = "trialing"
)

type TargetReference added in v1.3.0

type TargetReference struct {
	Type ReferenceType `json:"type"`
	ID   string        `json:"id"`
}

TargetReference represents a polymorphic target identity (user or organization).

type WebhookEvent added in v1.3.0

type WebhookEvent struct {
	ID        string         `json:"id"`
	Type      string         `json:"type"`
	Data      map[string]any `json:"data"`
	HandledAt time.Time      `json:"handled_at"`
}

WebhookEvent represents a processed webhook event.

Jump to

Keyboard shortcuts

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