billing

package
v0.0.0-...-dac86b4 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PlanFree = Plan{
		ID:                  "free",
		Name:                "Free",
		PriceMonthly:        0,
		ExecutionsPerMonth:  1000,
		MaxPipelines:        5,
		MaxStepsPerPipeline: 20,
		RetentionDays:       7,
		MaxWorkers:          2,
	}

	PlanStarter = Plan{
		ID:                  "starter",
		Name:                "Starter",
		PriceMonthly:        4900,
		ExecutionsPerMonth:  50_000,
		MaxPipelines:        25,
		MaxStepsPerPipeline: 50,
		RetentionDays:       30,
		MaxWorkers:          8,
		Features:            []string{"email-support", "custom-domains"},
	}

	PlanProfessional = Plan{
		ID:                  "professional",
		Name:                "Professional",
		PriceMonthly:        19900,
		ExecutionsPerMonth:  500_000,
		MaxPipelines:        0,
		MaxStepsPerPipeline: 0,
		RetentionDays:       90,
		MaxWorkers:          32,
		Features:            []string{"email-support", "custom-domains", "priority-builds", "advanced-analytics"},
	}

	PlanEnterprise = Plan{
		ID:                  "enterprise",
		Name:                "Enterprise",
		PriceMonthly:        0,
		ExecutionsPerMonth:  0,
		MaxPipelines:        0,
		MaxStepsPerPipeline: 0,
		RetentionDays:       365,
		MaxWorkers:          0,
		Features: []string{
			"sso",
			"multi-region",
			"dedicated-infrastructure",
			"sla-guarantee",
			"priority-support",
			"custom-domains",
			"advanced-analytics",
			"audit-log-export",
		},
	}

	// AllPlans is the ordered list of available plans.
	AllPlans = []Plan{PlanFree, PlanStarter, PlanProfessional, PlanEnterprise}
)

Predefined billing plans.

Functions

This section is empty.

Types

type BillingProvider

type BillingProvider interface {
	// CreateCustomer registers a new billing customer for the given tenant.
	CreateCustomer(ctx context.Context, tenantID, email string) (customerID string, err error)
	// CreateSubscription starts a subscription for the customer on the given plan.
	CreateSubscription(ctx context.Context, customerID, planID string) (subscriptionID string, err error)
	// CancelSubscription cancels an active subscription.
	CancelSubscription(ctx context.Context, subscriptionID string) error
	// ReportUsage reports metered usage for a subscription.
	ReportUsage(ctx context.Context, subscriptionID string, quantity int64) error
	// HandleWebhook processes an incoming webhook payload from the payment provider.
	HandleWebhook(ctx context.Context, payload []byte, signature string) error
}

BillingProvider abstracts payment and subscription management.

type Handler

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

Handler exposes billing endpoints over HTTP.

func NewHandler

func NewHandler(meter UsageMeter, provider BillingProvider) *Handler

NewHandler creates a new billing HTTP handler.

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers billing endpoints on the given mux.

type InMemoryMeter

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

InMemoryMeter is a thread-safe in-memory UsageMeter suitable for tests.

func NewInMemoryMeter

func NewInMemoryMeter() *InMemoryMeter

NewInMemoryMeter creates an InMemoryMeter.

func (*InMemoryMeter) CheckLimit

func (m *InMemoryMeter) CheckLimit(_ context.Context, tenantID string) (bool, int64, error)

CheckLimit checks whether the tenant may run another execution.

func (*InMemoryMeter) GetUsage

func (m *InMemoryMeter) GetUsage(_ context.Context, tenantID string, period time.Time) (*UsageReport, error)

GetUsage returns the usage report for the given period.

func (*InMemoryMeter) RecordExecution

func (m *InMemoryMeter) RecordExecution(_ context.Context, tenantID, pipelineName string) error

RecordExecution records a single execution for the tenant.

func (*InMemoryMeter) SetPlan

func (m *InMemoryMeter) SetPlan(tenantID, planID string)

SetPlan associates a tenant with a billing plan.

type MockBillingProvider

type MockBillingProvider struct {

	// Customers maps tenantID -> customerID.
	Customers map[string]string
	// Subscriptions maps subscriptionID -> planID.
	Subscriptions map[string]string
	// UsageReports collects (subscriptionID, quantity) pairs.
	UsageReports []UsageEntry
	// WebhookPayloads collects raw webhook bodies.
	WebhookPayloads [][]byte

	// Error fields allow tests to inject failures.
	CreateCustomerErr     error
	CreateSubscriptionErr error
	CancelSubscriptionErr error
	ReportUsageErr        error
	HandleWebhookErr      error
	// contains filtered or unexported fields
}

MockBillingProvider is a test double that records calls and returns configurable results.

func NewMockBillingProvider

func NewMockBillingProvider() *MockBillingProvider

NewMockBillingProvider creates a MockBillingProvider ready for use.

func (*MockBillingProvider) CancelSubscription

func (m *MockBillingProvider) CancelSubscription(_ context.Context, subscriptionID string) error

CancelSubscription cancels a mock subscription.

func (*MockBillingProvider) CreateCustomer

func (m *MockBillingProvider) CreateCustomer(_ context.Context, tenantID, _ string) (string, error)

CreateCustomer creates a mock customer.

func (*MockBillingProvider) CreateSubscription

func (m *MockBillingProvider) CreateSubscription(_ context.Context, customerID, planID string) (string, error)

CreateSubscription creates a mock subscription.

func (*MockBillingProvider) HandleWebhook

func (m *MockBillingProvider) HandleWebhook(_ context.Context, payload []byte, _ string) error

HandleWebhook records the webhook payload.

func (*MockBillingProvider) ReportUsage

func (m *MockBillingProvider) ReportUsage(_ context.Context, subscriptionID string, quantity int64) error

ReportUsage records a usage report.

type Plan

type Plan struct {
	ID                  string   `json:"id"`
	Name                string   `json:"name"`
	PriceMonthly        int      `json:"price_monthly"`          // cents
	ExecutionsPerMonth  int64    `json:"executions_per_month"`   // 0 = unlimited
	MaxPipelines        int      `json:"max_pipelines"`          // 0 = unlimited
	MaxStepsPerPipeline int      `json:"max_steps_per_pipeline"` // 0 = unlimited
	RetentionDays       int      `json:"retention_days"`
	MaxWorkers          int      `json:"max_workers"`
	Features            []string `json:"features,omitempty"`
}

Plan represents a billing plan with usage limits.

func PlanByID

func PlanByID(id string) *Plan

PlanByID looks up a plan by its identifier. Returns nil if not found.

func (Plan) IsUnlimited

func (p Plan) IsUnlimited() bool

IsUnlimited reports whether the plan has no execution limit.

type SQLiteMeter

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

SQLiteMeter is a UsageMeter backed by a SQLite database.

func NewSQLiteMeter

func NewSQLiteMeter(db *sql.DB) (*SQLiteMeter, error)

NewSQLiteMeter creates a new SQLiteMeter and initialises the schema.

func (*SQLiteMeter) CheckLimit

func (m *SQLiteMeter) CheckLimit(ctx context.Context, tenantID string) (bool, int64, error)

CheckLimit checks whether the tenant may run another execution.

func (*SQLiteMeter) GetUsage

func (m *SQLiteMeter) GetUsage(ctx context.Context, tenantID string, period time.Time) (*UsageReport, error)

GetUsage returns the usage report for the given period from SQLite.

func (*SQLiteMeter) RecordExecution

func (m *SQLiteMeter) RecordExecution(ctx context.Context, tenantID, pipelineName string) error

RecordExecution records an execution in SQLite.

func (*SQLiteMeter) SetPlan

func (m *SQLiteMeter) SetPlan(tenantID, planID string)

SetPlan associates a tenant with a billing plan.

type UsageEntry

type UsageEntry struct {
	SubscriptionID string
	Quantity       int64
}

UsageEntry records a single usage report.

type UsageMeter

type UsageMeter interface {
	// RecordExecution records a single pipeline execution for the tenant.
	RecordExecution(ctx context.Context, tenantID, pipelineName string) error
	// GetUsage returns the usage report for the given billing period.
	GetUsage(ctx context.Context, tenantID string, period time.Time) (*UsageReport, error)
	// CheckLimit checks whether the tenant is allowed to run another execution
	// and returns the remaining executions in the current period.
	CheckLimit(ctx context.Context, tenantID string) (allowed bool, remaining int64, err error)
}

UsageMeter tracks and queries resource consumption per tenant.

type UsageReport

type UsageReport struct {
	TenantID       string    `json:"tenant_id"`
	Period         time.Time `json:"period"` // first day of billing period
	ExecutionCount int64     `json:"execution_count"`
	PipelineCount  int       `json:"pipeline_count"`
	StepCount      int       `json:"step_count"`
	WorkerPeak     int       `json:"worker_peak"`
}

UsageReport summarizes resource usage for a tenant during a billing period.

Jump to

Keyboard shortcuts

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