subscription

package
v0.32.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProviderTestResource   = "test_resource"
	InitiatorIDMetadataKey = "initiated_by"
)

Variables

View Source
var (
	ErrNotFound                       = fmt.Errorf("subscription not found")
	ErrInvalidUUID                    = fmt.Errorf("invalid syntax of uuid")
	ErrInvalidID                      = fmt.Errorf("invalid subscription id")
	ErrInvalidDetail                  = fmt.Errorf("invalid subscription detail")
	ErrAlreadyOnSamePlan              = fmt.Errorf("already on the same plan")
	ErrNoPhaseActive                  = fmt.Errorf("no phase active")
	ErrPhaseIsUpdating                = fmt.Errorf("phase is in the middle of a change, please try again later")
	ErrSubscriptionOnProviderNotFound = fmt.Errorf("failed to get subscription from billing provider")
)

Functions

This section is empty.

Types

type ChangeRequest added in v0.8.29

type ChangeRequest struct {
	PlanID    string
	Immediate bool

	CancelUpcoming bool
}

type CreditService added in v0.8.32

type CreditService interface {
	Add(ctx context.Context, cred credit.Credit) error
	GetByID(ctx context.Context, id string) (credit.Transaction, error)
}

type CustomerService

type CustomerService interface {
	GetByID(ctx context.Context, id string) (customer.Customer, error)
	List(ctx context.Context, filter customer.Filter) ([]customer.Customer, error)
}

type Filter

type Filter struct {
	CustomerID string
	ProviderID string
	PlanID     string
	State      string
}

type OrganizationService added in v0.8.13

type OrganizationService interface {
	MemberCount(ctx context.Context, orgID string) (int64, error)
}

type Phase added in v0.8.19

type Phase struct {
	EffectiveAt time.Time
	EndsAt      time.Time
	PlanID      string
	Reason      string
}

type PhaseReason added in v0.24.0

type PhaseReason string
const (
	SubscriptionCancel PhaseReason = "cancel"
	SubscriptionChange PhaseReason = "change"
)

func (PhaseReason) String added in v0.24.0

func (s PhaseReason) String() string

type PlanService

type PlanService interface {
	List(ctx context.Context, filter plan.Filter) ([]plan.Plan, error)
	GetByID(ctx context.Context, id string) (plan.Plan, error)
}

type ProductService added in v0.8.19

type ProductService interface {
	GetByProviderID(ctx context.Context, id string) (product.Product, error)
}

type Repository

type Repository interface {
	GetByID(ctx context.Context, id string) (Subscription, error)
	Create(ctx context.Context, subs Subscription) (Subscription, error)
	UpdateByID(ctx context.Context, subs Subscription) (Subscription, error)
	List(ctx context.Context, filter Filter) ([]Subscription, error)
	GetByProviderID(ctx context.Context, id string) (Subscription, error)
	Delete(ctx context.Context, id string) error
}

type Service

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

func NewService

func NewService(stripeClient *client.API, config billing.Config, repository Repository,
	customerService CustomerService, planService PlanService,
	orgService OrganizationService, productService ProductService,
	creditService CreditService) *Service

func (*Service) Cancel

func (s *Service) Cancel(ctx context.Context, id string, immediate bool) (Subscription, error)

func (*Service) CancelUpcomingPhase added in v0.8.29

func (s *Service) CancelUpcomingPhase(ctx context.Context, sub Subscription) error

CancelUpcomingPhase cancels the scheduled phase of the subscription

func (*Service) ChangePlan added in v0.8.19

func (s *Service) ChangePlan(ctx context.Context, id string, changeRequest ChangeRequest) (Phase, error)

ChangePlan changes the plan of the subscription by creating a subscription schedule it first checks if the schedule is already created, if not it creates a new schedule using the current subscription as the base and the new plan as the target in upcoming phase. Phases can be immediately changed or at the end of the current period.

func (*Service) Close

func (s *Service) Close() error

func (*Service) Create

func (s *Service) Create(ctx context.Context, sub Subscription) (Subscription, error)

func (*Service) DeleteByCustomer added in v0.8.35

func (s *Service) DeleteByCustomer(ctx context.Context, customr customer.Customer) error

func (*Service) GetByID

func (s *Service) GetByID(ctx context.Context, id string) (Subscription, error)

func (*Service) GetByProviderID

func (s *Service) GetByProviderID(ctx context.Context, id string) (Subscription, error)

func (*Service) HasUserSubscribedBefore added in v0.8.36

func (s *Service) HasUserSubscribedBefore(ctx context.Context, customerID string, planID string) (bool, error)

func (*Service) Init

func (s *Service) Init(ctx context.Context) error

func (*Service) List

func (s *Service) List(ctx context.Context, filter Filter) ([]Subscription, error)

func (*Service) SyncWithProvider

func (s *Service) SyncWithProvider(ctx context.Context, customr customer.Customer) error

SyncWithProvider syncs the subscription state with the billing provider

func (*Service) TriggerSyncByProviderID added in v0.13.0

func (s *Service) TriggerSyncByProviderID(ctx context.Context, id string) error

func (*Service) UpdateProductQuantity added in v0.8.19

func (s *Service) UpdateProductQuantity(ctx context.Context, orgID string, currentPlan plan.Plan,
	stripeSubscription *stripe.Subscription, stripeSchedule *stripe.SubscriptionSchedule) error

UpdateProductQuantity updates the quantity of the product in the subscription Note: check if we need to handle subscription schedule

type State

type State string
const (
	StateActive   State = "active"
	StateTrialing State = "trialing"
	StatePastDue  State = "past_due"
	StateCanceled State = "canceled"
	StateEnded    State = "ended"
)

func (State) String added in v0.8.18

func (s State) String() string

type Subscription

type Subscription struct {
	ID         string
	ProviderID string // identifier set by the billing engine provider
	CustomerID string
	PlanID     string

	State string

	Metadata metadata.Metadata

	Phase       Phase
	PlanHistory []Phase

	CreatedAt            time.Time
	UpdatedAt            time.Time
	CanceledAt           time.Time
	DeletedAt            time.Time
	EndedAt              time.Time
	TrialEndsAt          time.Time
	CurrentPeriodStartAt time.Time
	CurrentPeriodEndAt   time.Time
	BillingCycleAnchorAt time.Time
}

func (Subscription) IsActive added in v0.8.30

func (s Subscription) IsActive() bool

func (Subscription) IsCanceled added in v0.25.0

func (s Subscription) IsCanceled() bool

Jump to

Keyboard shortcuts

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