Versions in this module Expand all Collapse all v1 v1.1.0 Dec 2, 2025 Changes in this version + var ErrAlreadyExists = errors.New("subscription already exists") + var ErrInvalidSubscription = errors.New("invalid subscription data") + var ErrNotFound = errors.New("subscription not found") + func CalculatePeriodEnd(start time.Time, period BillingPeriod, interval int) time.Time + type BillingPeriod string + const PeriodDay + const PeriodMonth + const PeriodWeek + const PeriodYear + type ChangeSubscriptionRequest struct + Metadata map[string]string + NewBillingInterval int + NewBillingPeriod BillingPeriod + NewPriceID string + NewProductID string + ProrationBehavior string + SubscriptionID string + type ChangeSubscriptionResult struct + EffectiveDate time.Time + NewProduct string + PreviousProduct string + ProrationAmount int64 + Subscription Subscription + type CreateStripeSubscriptionRequest struct + BillingInterval int + BillingPeriod BillingPeriod + CurrentPeriodEnd time.Time + Metadata map[string]string + ProductID string + StripeCustomerID string + StripeSubscriptionID string + TrialEnd *time.Time + type CreateX402SubscriptionRequest struct + BillingInterval int + BillingPeriod BillingPeriod + Metadata map[string]string + ProductID string + Wallet string + type MemoryRepository struct + func NewMemoryRepository() *MemoryRepository + func (r *MemoryRepository) Close() error + func (r *MemoryRepository) Create(_ context.Context, sub Subscription) error + func (r *MemoryRepository) Delete(_ context.Context, id string) error + func (r *MemoryRepository) ExtendPeriod(_ context.Context, id string, newStart, newEnd time.Time) error + func (r *MemoryRepository) Get(_ context.Context, id string) (Subscription, error) + func (r *MemoryRepository) GetByStripeCustomerID(_ context.Context, customerID string) ([]Subscription, error) + func (r *MemoryRepository) GetByStripeSubscriptionID(_ context.Context, stripeSubID string) (Subscription, error) + func (r *MemoryRepository) GetByWallet(_ context.Context, wallet, productID string) (Subscription, error) + func (r *MemoryRepository) ListActive(_ context.Context, productID string) ([]Subscription, error) + func (r *MemoryRepository) ListByProduct(_ context.Context, productID string) ([]Subscription, error) + func (r *MemoryRepository) ListExpiring(_ context.Context, before time.Time) ([]Subscription, error) + func (r *MemoryRepository) Update(_ context.Context, sub Subscription) error + func (r *MemoryRepository) UpdateStatus(_ context.Context, id string, status Status) error + type PaymentMethod string + const PaymentMethodStripe + const PaymentMethodX402 + type PostgresRepository struct + func NewPostgresRepository(connStr string) (*PostgresRepository, error) + func NewPostgresRepositoryWithDB(db *sql.DB) *PostgresRepository + func (r *PostgresRepository) Close() error + func (r *PostgresRepository) Create(ctx context.Context, sub Subscription) error + func (r *PostgresRepository) Delete(ctx context.Context, id string) error + func (r *PostgresRepository) ExtendPeriod(ctx context.Context, id string, newStart, newEnd time.Time) error + func (r *PostgresRepository) Get(ctx context.Context, id string) (Subscription, error) + func (r *PostgresRepository) GetByStripeCustomerID(ctx context.Context, customerID string) ([]Subscription, error) + func (r *PostgresRepository) GetByStripeSubscriptionID(ctx context.Context, stripeSubID string) (Subscription, error) + func (r *PostgresRepository) GetByWallet(ctx context.Context, wallet, productID string) (Subscription, error) + func (r *PostgresRepository) ListActive(ctx context.Context, productID string) ([]Subscription, error) + func (r *PostgresRepository) ListByProduct(ctx context.Context, productID string) ([]Subscription, error) + func (r *PostgresRepository) ListExpiring(ctx context.Context, before time.Time) ([]Subscription, error) + func (r *PostgresRepository) Update(ctx context.Context, sub Subscription) error + func (r *PostgresRepository) UpdateStatus(ctx context.Context, id string, status Status) error + func (r *PostgresRepository) WithTableName(name string) *PostgresRepository + type ProductConfig struct + AllowX402 bool + BillingInterval int + BillingPeriod BillingPeriod + GracePeriodHours int + StripePriceID string + TrialDays int + func (c ProductConfig) IsValid() bool + type Repository interface + Close func() error + Create func(ctx context.Context, sub Subscription) error + Delete func(ctx context.Context, id string) error + ExtendPeriod func(ctx context.Context, id string, newStart, newEnd time.Time) error + Get func(ctx context.Context, id string) (Subscription, error) + GetByStripeCustomerID func(ctx context.Context, customerID string) ([]Subscription, error) + GetByStripeSubscriptionID func(ctx context.Context, stripeSubID string) (Subscription, error) + GetByWallet func(ctx context.Context, wallet, productID string) (Subscription, error) + ListActive func(ctx context.Context, productID string) ([]Subscription, error) + ListByProduct func(ctx context.Context, productID string) ([]Subscription, error) + ListExpiring func(ctx context.Context, before time.Time) ([]Subscription, error) + Update func(ctx context.Context, sub Subscription) error + UpdateStatus func(ctx context.Context, id string, status Status) error + func NewRepository(cfg RepositoryConfig) (Repository, error) + func NewRepositoryWithDB(cfg RepositoryConfig, sharedDB *sql.DB) (Repository, error) + type RepositoryConfig struct + Backend string + GracePeriodHours int + PostgresDB *sql.DB + PostgresURL string + TableName string + type Service struct + func NewService(repo Repository, gracePeriodHours int) *Service + func (s *Service) Cancel(ctx context.Context, id string, atPeriodEnd bool) error + func (s *Service) ChangeSubscription(ctx context.Context, req ChangeSubscriptionRequest) (*ChangeSubscriptionResult, error) + func (s *Service) CreateStripeSubscription(ctx context.Context, req CreateStripeSubscriptionRequest) (Subscription, error) + func (s *Service) CreateX402Subscription(ctx context.Context, req CreateX402SubscriptionRequest) (Subscription, error) + func (s *Service) ExpireOverdue(ctx context.Context) (int, error) + func (s *Service) ExtendX402Subscription(ctx context.Context, id string, period BillingPeriod, interval int) (Subscription, error) + func (s *Service) Get(ctx context.Context, id string) (Subscription, error) + func (s *Service) GetByStripeSubscriptionID(ctx context.Context, stripeSubID string) (Subscription, error) + func (s *Service) GetByWallet(ctx context.Context, wallet, productID string) (Subscription, error) + func (s *Service) HandleStripeCancelled(ctx context.Context, stripeSubID string) error + func (s *Service) HandleStripePaymentFailed(ctx context.Context, stripeSubID string) error + func (s *Service) HandleStripeRenewal(ctx context.Context, stripeSubID string, periodStart, periodEnd time.Time) error + func (s *Service) HandleStripeSubscriptionUpdated(ctx context.Context, stripeSubID string, update StripeSubscriptionUpdate) error + func (s *Service) HasAccess(ctx context.Context, wallet, productID string) (bool, *Subscription, error) + func (s *Service) HasStripeAccess(ctx context.Context, stripeSubID string) (bool, *Subscription, error) + func (s *Service) ListExpiring(ctx context.Context, within time.Duration) ([]Subscription, error) + func (s *Service) ReactivateSubscription(ctx context.Context, id string) (Subscription, error) + type Status string + const StatusActive + const StatusCancelled + const StatusExpired + const StatusPastDue + const StatusTrialing + type StripeSubscriptionUpdate struct + BillingInterval int + BillingPeriod BillingPeriod + CancelAtPeriodEnd bool + CancelledAt *time.Time + CurrentPeriodEnd time.Time + CurrentPeriodStart time.Time + NewProductID string + Status Status + type Subscription struct + BillingInterval int + BillingPeriod BillingPeriod + CancelAtPeriodEnd bool + CancelledAt *time.Time + CreatedAt time.Time + CurrentPeriodEnd time.Time + CurrentPeriodStart time.Time + ID string + Metadata map[string]string + PaymentMethod PaymentMethod + ProductID string + Status Status + StripeCustomerID string + StripeSubscriptionID string + TrialEnd *time.Time + UpdatedAt time.Time + Wallet string + func (s Subscription) DaysUntilExpiration() int + func (s Subscription) IsActive() bool + func (s Subscription) IsActiveAt(t time.Time) bool + func (s Subscription) IsTrialing() bool + func (s Subscription) NextPeriodEnd() time.Time