feature

package
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFeatureNotFound = errors.New("feature not found")
	ErrPriceNotFound   = errors.New("price not found")
	ErrInvalidDetail   = errors.New("invalid plan detail")
)

Functions

This section is empty.

Types

type BillingScheme

type BillingScheme string
const (
	BillingSchemeFlat   BillingScheme = "flat"
	BillingSchemeTiered BillingScheme = "tiered"
)

func BuildBillingScheme

func BuildBillingScheme(s string) BillingScheme

func (BillingScheme) ToStripe

func (b BillingScheme) ToStripe() string

type Feature

type Feature struct {
	ID         string   `json:"id" yaml:"id"`
	ProviderID string   `json:"provider_id" yaml:"provider_id"` // in case of stripe, provider id and id are same
	PlanIDs    []string // plans this feature belongs to, this is optional and can be empty

	Name        string `json:"name" yaml:"name"`   // a machine friendly name for the feature
	Title       string `json:"title" yaml:"title"` // a human friendly title for the feature
	Description string `json:"description" yaml:"description"`

	// CreditAmount is amount of credits that are awarded/consumed when buying/using this feature
	CreditAmount int64 `json:"credit_amount" yaml:"credit_amount"`

	// Prices for the feature, return only, should not be set when creating a feature
	Prices []Price `json:"prices" yaml:"prices"`

	State    string            `json:"state" yaml:"state"`
	Metadata metadata.Metadata `json:"metadata" yaml:"metadata"`

	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time
}

Feature is a product feature and has a corresponding product in the billing engine

type Filter

type Filter struct {
	PlanID     string
	FeatureIDs []string
}

type Price

type Price struct {
	ID         string `json:"id" yaml:"id"`
	FeatureID  string `json:"feature_id" yaml:"feature_id"`
	ProviderID string `json:"provider_id" yaml:"provider_id"`

	Name string `json:"name" yaml:"name"` // a machine friendly name for the price

	// BillingScheme specifies the billing scheme for the price
	// known schemes are "tiered" and "flat". Default is "flat"
	BillingScheme BillingScheme `json:"billing_scheme" yaml:"billing_scheme" default:"flat"`

	// Currency Three-letter ISO 4217 currency code in lower case
	// like "usd", "eur", "gbp"
	// https://www.six-group.com/en/products-services/financial-information/data-standards.html
	Currency string `json:"currency" yaml:"currency" default:"usd"`

	// Amount price in the minor currency unit
	// Minor unit is the smallest unit of a currency, e.g. 1 dollar equals 100 cents (with 2 decimals).
	Amount int64 `json:"amount" yaml:"amount"`

	// UsageType specifies the usage type for the price
	// known types are "licensed" and "metered". Default is "licensed"
	UsageType PriceUsageType `json:"usage_type" yaml:"usage_type" default:"licensed"`

	// MeteredAggregate specifies the aggregation method for the price
	// known aggregations are "sum", "last_during_period" and "max". Default is "sum"
	MeteredAggregate string `json:"metered_aggregate" yaml:"metered_aggregate" default:"sum"`

	// Interval is the interval at which the plan is billed
	// e.g. day, week, month, year
	Interval string `json:"interval" yaml:"interval"`

	Metadata metadata.Metadata `json:"metadata" yaml:"metadata"`

	// TierMode specifies the tier mode for the price
	// known modes are "graduated" and "volume". Default is "graduated"
	// In volume-based, the maximum quantity within a period determines the per-unit price
	// In graduated, pricing changes as the quantity increases to specific thresholds
	TierMode string `json:"tier_mode" yaml:"tier_mode" default:"graduated"`

	// Tiers specifies the optional tiers for the price
	// only applicable when BillingScheme is "tiered"
	Tiers []Tier `json:"tiers" yaml:"tiers"`

	State     string `json:"state" yaml:"state"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time
}

Price is a product price and has a corresponding price in the billing engine when creating a price, the feature must already exist when subscribing to a plan, the price must already exist

type PriceRepository

type PriceRepository interface {
	GetByID(ctx context.Context, id string) (Price, error)
	GetByName(ctx context.Context, name string) (Price, error)
	Create(ctx context.Context, price Price) (Price, error)
	UpdateByID(ctx context.Context, price Price) (Price, error)
	List(ctx context.Context, flt Filter) ([]Price, error)
}

type PriceTierMode

type PriceTierMode string
const (
	PriceTierModeGraduated PriceTierMode = "graduated"
	PriceTierModeVolume    PriceTierMode = "volume"
)

type PriceUsageType

type PriceUsageType string
const (
	PriceUsageTypeLicensed PriceUsageType = "licensed"
	PriceUsageTypeMetered  PriceUsageType = "metered"
)

func BuildPriceUsageType

func BuildPriceUsageType(s string) PriceUsageType

func (PriceUsageType) ToStripe

func (p PriceUsageType) ToStripe() string

type Repository

type Repository interface {
	GetByID(ctx context.Context, id string) (Feature, error)
	GetByName(ctx context.Context, name string) (Feature, error)
	Create(ctx context.Context, feature Feature) (Feature, error)
	UpdateByName(ctx context.Context, feature Feature) (Feature, error)
	List(ctx context.Context, flt Filter) ([]Feature, error)
}

type Service

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

func NewService

func NewService(stripeClient *client.API, repository Repository,
	priceRepository PriceRepository) *Service

func (*Service) AddPlan

func (s *Service) AddPlan(ctx context.Context, planID string, featureOb Feature) error

func (*Service) Create

func (s *Service) Create(ctx context.Context, feature Feature) (Feature, error)

func (*Service) CreatePrice

func (s *Service) CreatePrice(ctx context.Context, price Price) (Price, error)

func (*Service) GetByID

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

func (*Service) GetPriceByFeatureID

func (s *Service) GetPriceByFeatureID(ctx context.Context, id string) ([]Price, error)

func (*Service) GetPriceByID

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

func (*Service) List

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

func (*Service) Update

func (s *Service) Update(ctx context.Context, feature Feature) (Feature, error)

Update updates a feature, but it doesn't update all fields ideally we should keep it immutable and create a new feature

func (*Service) UpdatePrice

func (s *Service) UpdatePrice(ctx context.Context, price Price) (Price, error)

UpdatePrice updates a price, but it doesn't update all fields ideally we should keep it immutable and create a new price

type Tier

type Tier struct {
	FlatAmount int64
	UpTo       int64
}

Jump to

Keyboard shortcuts

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