Documentation
¶
Overview ¶
Package subscription manages tenant plan subscriptions, renewals, expiration, grace periods, and billing hooks.
Index ¶
- Variables
- type BillingEvent
- type BillingHook
- type LifecycleService
- type MemoryService
- func (service *MemoryService) Downgrade(ctx context.Context, tenantID types.TenantID, planID string) (Subscription, error)
- func (service *MemoryService) Expire(ctx context.Context, tenantID types.TenantID) (Subscription, error)
- func (service *MemoryService) ExpireDue(ctx context.Context) ([]Subscription, error)
- func (service *MemoryService) Get(ctx context.Context, tenantID types.TenantID) (Subscription, error)
- func (service *MemoryService) Renew(ctx context.Context, tenantID types.TenantID, currentPeriodEnd time.Time) (Subscription, error)
- func (service *MemoryService) Subscribe(ctx context.Context, tenantID types.TenantID, planID string) (Subscription, error)
- func (service *MemoryService) SubscribeWithPeriod(ctx context.Context, tenantID types.TenantID, planID string, ...) (Subscription, error)
- func (service *MemoryService) Unsubscribe(ctx context.Context, tenantID types.TenantID) (Subscription, error)
- func (service *MemoryService) Upgrade(ctx context.Context, tenantID types.TenantID, planID string) (Subscription, error)
- type Option
- type Service
- type Status
- type Subscription
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSubscriptionNotFound reports that a subscription does not exist. ErrSubscriptionNotFound = errors.New("gotenancy/subscription: subscription not found") // ErrSubscriptionAlreadyExists reports that a tenant already has a subscription. ErrSubscriptionAlreadyExists = errors.New("gotenancy/subscription: subscription already exists") // ErrInvalidSubscription reports invalid subscription metadata. ErrInvalidSubscription = errors.New("gotenancy/subscription: invalid subscription") // ErrInvalidTransition reports an invalid subscription lifecycle transition. ErrInvalidTransition = errors.New("gotenancy/subscription: invalid transition") )
Functions ¶
This section is empty.
Types ¶
type BillingEvent ¶
type BillingEvent struct {
TenantID types.TenantID
Action string
FromPlan string
ToPlan string
Status Status
CurrentPeriodEnd *time.Time
}
BillingEvent describes a subscription change for external billing systems.
type BillingHook ¶
type BillingHook func(context.Context, BillingEvent) error
BillingHook receives subscription lifecycle changes.
type LifecycleService ¶ added in v0.1.5
type LifecycleService interface {
Service
SubscribeWithPeriod(ctx context.Context, tenantID types.TenantID, planID string, currentPeriodEnd time.Time) (Subscription, error)
Renew(ctx context.Context, tenantID types.TenantID, currentPeriodEnd time.Time) (Subscription, error)
Expire(ctx context.Context, tenantID types.TenantID) (Subscription, error)
ExpireDue(ctx context.Context) ([]Subscription, error)
}
LifecycleService extends Service with billing-period renewal and expiration operations.
type MemoryService ¶
type MemoryService struct {
// contains filtered or unexported fields
}
MemoryService is a thread-safe in-memory subscription service.
func NewMemoryService ¶
func NewMemoryService(opts ...Option) *MemoryService
NewMemoryService creates an empty subscription service.
func (*MemoryService) Downgrade ¶
func (service *MemoryService) Downgrade(ctx context.Context, tenantID types.TenantID, planID string) (Subscription, error)
Downgrade changes an active subscription to a lower plan.
func (*MemoryService) Expire ¶ added in v0.1.5
func (service *MemoryService) Expire(ctx context.Context, tenantID types.TenantID) (Subscription, error)
Expire marks an active subscription expired immediately.
func (*MemoryService) ExpireDue ¶ added in v0.1.5
func (service *MemoryService) ExpireDue(ctx context.Context) ([]Subscription, error)
ExpireDue marks active subscriptions expired after their current period and grace window end.
func (*MemoryService) Get ¶
func (service *MemoryService) Get(ctx context.Context, tenantID types.TenantID) (Subscription, error)
Get returns a subscription by tenant ID.
func (*MemoryService) Renew ¶ added in v0.1.5
func (service *MemoryService) Renew(ctx context.Context, tenantID types.TenantID, currentPeriodEnd time.Time) (Subscription, error)
Renew reactivates an active or expired subscription with a new current period end.
func (*MemoryService) Subscribe ¶
func (service *MemoryService) Subscribe(ctx context.Context, tenantID types.TenantID, planID string) (Subscription, error)
Subscribe creates an active subscription for a tenant.
func (*MemoryService) SubscribeWithPeriod ¶ added in v0.1.5
func (service *MemoryService) SubscribeWithPeriod(ctx context.Context, tenantID types.TenantID, planID string, currentPeriodEnd time.Time) (Subscription, error)
SubscribeWithPeriod creates an active subscription with a current billing period end.
func (*MemoryService) Unsubscribe ¶
func (service *MemoryService) Unsubscribe(ctx context.Context, tenantID types.TenantID) (Subscription, error)
Unsubscribe cancels an active subscription.
func (*MemoryService) Upgrade ¶
func (service *MemoryService) Upgrade(ctx context.Context, tenantID types.TenantID, planID string) (Subscription, error)
Upgrade changes an active subscription to a higher plan.
type Option ¶
type Option func(*MemoryService)
Option configures MemoryService.
func WithBillingHook ¶
func WithBillingHook(hook BillingHook) Option
WithBillingHook sets the billing hook.
func WithGracePeriod ¶ added in v0.1.5
WithGracePeriod sets the post-period grace window before ExpireDue marks a subscription expired.
type Service ¶
type Service interface {
Subscribe(ctx context.Context, tenantID types.TenantID, planID string) (Subscription, error)
Unsubscribe(ctx context.Context, tenantID types.TenantID) (Subscription, error)
Upgrade(ctx context.Context, tenantID types.TenantID, planID string) (Subscription, error)
Downgrade(ctx context.Context, tenantID types.TenantID, planID string) (Subscription, error)
Get(ctx context.Context, tenantID types.TenantID) (Subscription, error)
}
Service manages tenant subscriptions.