Documentation
¶
Index ¶
- Constants
- Variables
- type Behavior
- type BehaviorConfig
- type BillingScheme
- type Feature
- type FeatureRepository
- type Filter
- type Price
- type PriceRepository
- type PriceTierMode
- type PriceUsageType
- type Product
- type Repository
- type Service
- func (s *Service) AddFeatureToProduct(ctx context.Context, feature Feature, productID string) error
- func (s *Service) AddPlan(ctx context.Context, productOb Product, planID string) error
- func (s *Service) Create(ctx context.Context, product Product) (Product, error)
- func (s *Service) CreatePrice(ctx context.Context, price Price) (Price, error)
- func (s *Service) GetByID(ctx context.Context, id string) (Product, error)
- func (s *Service) GetByProviderID(ctx context.Context, id string) (Product, error)
- func (s *Service) GetFeatureByID(ctx context.Context, id string) (Feature, error)
- func (s *Service) GetFeatureByProductID(ctx context.Context, id string) ([]Feature, error)
- func (s *Service) GetPriceByID(ctx context.Context, id string) (Price, error)
- func (s *Service) GetPriceByProductID(ctx context.Context, id string) ([]Price, error)
- func (s *Service) List(ctx context.Context, flt Filter) ([]Product, error)
- func (s *Service) ListFeatures(ctx context.Context, flt Filter) ([]Feature, error)
- func (s *Service) RemoveFeatureFromProduct(ctx context.Context, featureID, productID string) error
- func (s *Service) Update(ctx context.Context, product Product) (Product, error)
- func (s *Service) UpdatePrice(ctx context.Context, price Price) (Price, error)
- func (s *Service) UpsertFeature(ctx context.Context, feature Feature) (Feature, error)
- type Tier
Constants ¶
const ( PriceStateActive = "active" PriceStateInactive = "inactive" )
Price states. A new price is active. A price is deactivated by setting it inactive rather than deleting it, since provider prices cannot be deleted.
Variables ¶
var ( ErrProductNotFound = errors.New("product not found") ErrPriceNotFound = errors.New("price not found") ErrInvalidDetail = errors.New("invalid product detail") ErrPerSeatLimitReached = errors.New("per seat limit reached") ErrInvalidFeatureDetail = errors.New("invalid feature detail") ErrFeatureNotFound = errors.New("feature not found") )
Functions ¶
This section is empty.
Types ¶
type BehaviorConfig ¶ added in v0.8.18
type BehaviorConfig struct {
// CreditAmount is amount of credits that are awarded/consumed when buying/using this feature
CreditAmount int64 `json:"credit_amount" yaml:"credit_amount"`
// SeatLimit is the maximum number of seats that can be added to the subscription
SeatLimit int64 `json:"seat_limit" yaml:"seat_limit"`
// MinQuantity is the minimum quantity that can be bought
MinQuantity int64 `json:"min_quantity" yaml:"min_quantity"`
// MaxQuantity is the maximum quantity that can be bought
MaxQuantity int64 `json:"max_quantity" yaml:"max_quantity"`
}
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"`
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
// products this feature belongs to, this is optional and can be empty
// a product will have at least one feature with the same name as the product
ProductIDs []string `json:"product_ids" yaml:"product_ids"`
Metadata metadata.Metadata `json:"metadata" yaml:"metadata"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
Feature are part of a product which allows for a more granular control on what is packed with the product. It is a platform specific concept and doesn't have a corresponding billing engine entity
type FeatureRepository ¶
type FeatureRepository 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 Price ¶
type Price struct {
ID string `json:"id" yaml:"id"`
ProductID 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", "last_ever" 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 PriceTierMode `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
func (Price) IsActive ¶ added in v0.112.0
IsActive reports whether the price can be used for new purchases. An inactive price must be left out of checkout line items, plan matching, and invoicing. Only the active state counts as usable, along with an empty state, which an in-memory price carries before it is stored (the stored default is active). Any other value is treated as not active, so an unexpected state fails closed rather than being billed.
func (Price) IsLicensed ¶ added in v0.19.0
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" )
func (PriceTierMode) String ¶ added in v0.19.0
func (t PriceTierMode) String() string
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 Product ¶
type Product 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"`
// Type is the type of the feature
// known types are "credits" and "per_seat". Default is "basic"
Behavior Behavior `json:"behavior" yaml:"behavior" default:"basic"`
// Config is the configuration for the behavior
Config BehaviorConfig `json:"config" yaml:"config"`
// Prices for the product, return only, shouldn't be set while updating a product
Prices []Price `json:"prices" yaml:"prices"`
// Features for the product, return only, shouldn't be set while updating a product
Features []Feature `json:"features" yaml:"features"`
State string `json:"state" yaml:"state"`
Metadata metadata.Metadata `json:"metadata" yaml:"metadata"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
Product is an item being sold by the platform and has a corresponding reference in the billing engine
func (Product) HasPerSeatBehavior ¶ added in v0.19.0
func (Product) IsSeatLimitBreached ¶ added in v0.19.0
type Repository ¶
type Repository interface {
GetByID(ctx context.Context, id string) (Product, error)
GetByName(ctx context.Context, name string) (Product, error)
Create(ctx context.Context, product Product) (Product, error)
UpdateByName(ctx context.Context, product Product) (Product, error)
List(ctx context.Context, flt Filter) ([]Product, error)
}
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
func NewService ¶
func NewService(stripeClient *client.API, productRepository Repository, priceRepository PriceRepository, featureRepository FeatureRepository) *Service
func (*Service) AddFeatureToProduct ¶ added in v0.8.15
func (*Service) CreatePrice ¶
func (*Service) GetByProviderID ¶ added in v0.8.19
func (*Service) GetFeatureByID ¶
func (*Service) GetFeatureByProductID ¶
func (*Service) GetPriceByID ¶
func (*Service) GetPriceByProductID ¶
func (*Service) ListFeatures ¶
func (*Service) RemoveFeatureFromProduct ¶ added in v0.8.15
func (*Service) Update ¶
Update updates a product, but it doesn't update all fields ideally we should keep it immutable and create a new product
func (*Service) UpdatePrice ¶
UpdatePrice updates a price, but it doesn't update all fields ideally we should keep it immutable and create a new price