billing

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	USAGE_BASED uint8 = iota + 1
	MONTHLY
	QUARTERLY
	ANNUALLY
)

BillingCycle ENUMS

View Source
const (
	PRICE_SUM uint8 = iota + 1
	PRICE_MAX
	PRICE_MIN
)

PricingPolicy ENUMS

Variables

View Source
var (
	ErrBadAmount         error = errors.New("billing: bad amount input")
	ErrInsufficientFunds error = errors.New("billing: insufficient funds") // I can hear it...
)
View Source
var (
	ErrInvalidSerialNumber = errors.New("billing: invalid serial number")
	ErrInvalidOwnerID      = errors.New("billing: invalid owner ID, need OwnerUserID or OwnerAffiliationID")
	ErrInvalidProductID    = errors.New("billing: invalid product ID")
	ErrInvalidWalletID     = errors.New("billing: invalid wallet ID")
)
View Source
var (
	ErrProductListingNotFound = errors.New("billing: product listing not found")
)

Functions

func AddBillingRecord added in v0.1.0

func AddBillingRecord(record BillingRecord) (uint64, error)

func AddProduct

func AddProduct(product *Product) (uint64, error)

Add entry to database

func AddProductListing added in v0.1.0

func AddProductListing(pl *ProductListing) (uint64, error)

func BeginningOfDay added in v0.1.0

func BeginningOfDay(t time.Time) time.Time

func BillingOptionsToJSON added in v0.1.0

func BillingOptionsToJSON(billingOptions []BillingOption) (string, error)

func CollectRecurringPayment added in v0.1.0

func CollectRecurringPayment(product *Product) error

func CollectUsageBasedPayment added in v0.1.0

func CollectUsageBasedPayment(product *Product, total float64) error

func DailyRecurringBilling added in v0.1.0

func DailyRecurringBilling() []error

DailyRecurringBilling() should be called at (at least) a daily basis. It will bill all active recurring billing products that is due.

func DeleteProductListingByID added in v0.1.0

func DeleteProductListingByID(productID uint64) error

func DeleteProductListingGroupByID added in v0.1.0

func DeleteProductListingGroupByID(id uint64) error

func ForceCollectRecurringPayment added in v0.1.0

func ForceCollectRecurringPayment(product *Product) error

THIS FUNCTION IS NOT EXPECTED TO BE USED IN ANYWHERE FOR Pre-V2 builds. IT DOES NOT CHECK IF IT IS DUE OR NOT.

func ForceCollectUsageBasedPayment added in v0.1.0

func ForceCollectUsageBasedPayment(product *Product, total float64) error

Be sure to terminate this product after this.

func HourlyProductTermination added in v0.1.0

func HourlyProductTermination() []error

HourlyProductTermination() should be called at an hourly basis. It will pickup all products that is due to be terminated: - If usage-based, charge the final amount - Terminate Account - Terminate Product

func HourlyUsageBilling added in v0.1.0

func HourlyUsageBilling() []error

HourlyUsageBilling() should be called at an hourly basis. It will bill all active usage-based billing products.

func ListProductListingGroupIDs added in v0.1.10

func ListProductListingGroupIDs() ([]uint64, error)

func NewProductListingGroup added in v0.1.0

func NewProductListingGroup(plg ProductListingGroup) (uint64, error)

func Setup

func Setup(d *sql.DB, sqlTblPrefix string)

Setup() of billing package requires: - Previous Setup() of auth package - *sql.DB's dsn has `parseTime=true`

func TerminateProductBySerialNumber added in v0.0.17

func TerminateProductBySerialNumber(serialNumber uint64) error

func ToTerminateProductOn added in v0.1.0

func ToTerminateProductOn(product *Product, terminationDate time.Time) error

func UpdateProduct

func UpdateProduct(product *Product) error

func UpdateProductListing added in v0.1.0

func UpdateProductListing(pl *ProductListing) error

Types

type BillingOption added in v0.1.0

type BillingOption struct {
	BillingCycle         uint8                  `json:"billing_cycle"` // As defined in billing.go
	Price                float64                `json:"price"`
	MonthlySpendingCap   float64                `json:"monthly_spending_cap"`   // Only for Usage-based billing
	CurrentMonthSpending float64                `json:"current_month_spending"` // Only for Usage-based billing
	AccountConfiguration map[string]interface{} `json:"account_configuration"`
}

func BillingOptionsFromJSON added in v0.1.0

func BillingOptionsFromJSON(jsonStr string) ([]BillingOption, error)

type BillingRecord added in v0.1.0

type BillingRecord struct {
	SerialNumber        uint64
	WalletID            uint64
	UserID              uint64
	ProductID           uint64
	ProductSerialNumber uint64
	BillingCycle        uint8
	BilledAmount        float64
	BilledAt            time.Time
}

func ListAllBillingRecords added in v0.1.0

func ListAllBillingRecords() ([]BillingRecord, error)

func ListBillingRecordsByWalletID added in v0.1.0

func ListBillingRecordsByWalletID(walletID uint64) ([]BillingRecord, error)

type Product

type Product struct {

	// Ownership. At least one must be set.
	// if both are set, the product is treated
	// as a private owned product.
	OwnerUserID        uint64
	OwnerAffiliationID uint64

	// billing/payment
	ProductID uint64 // identifier for product type, description, pricing, etc. productID is actually the Serial Number of Product Listing

	WalletID      uint64
	BillingOption BillingOption
	// contains filtered or unexported fields
}

func GetProductBySerialNumber added in v0.0.17

func GetProductBySerialNumber(serialNumber uint64) (*Product, error)

func ListActiveProductsByBillingCycle

func ListActiveProductsByBillingCycle(billingCycle uint8) ([]*Product, error)

For automated-billing purposes. Designed for Pay-As-You-Go billing. But may be used for other purposes later?

func ListAffiliationProducts added in v0.0.17

func ListAffiliationProducts(ownerAffiliationID uint64) ([]*Product, error)

For affliation user viewing. Lists all products owned by the affiliation, including both shared and private owned products. Client should implement local-pagination to reduce need of repeated query Client should mark private products (ownerUserID != 0) as private API Server should hide dateTermination in response, if dateTermination is earlier than dateCreation

func ListAllProducts

func ListAllProducts() ([]*Product, error)

For admin viewing. Client should implement local-pagination

func ListProductsByProductID added in v0.0.17

func ListProductsByProductID(productID uint64) ([]*Product, error)

For admin viewing.

func ListProductsToTerminate added in v0.1.0

func ListProductsToTerminate() ([]*Product, error)

For auto-billing.

func ListSystemProducts added in v0.1.10

func ListSystemProducts() ([]*Product, error)

func ListUserProducts

func ListUserProducts(ownerUserID uint64) ([]*Product, error)

For user viewing. Lists all products owned by the user. Client should implement local-pagination to reduce need of repeated query API Server should hide dateTermination in reponse, if dateTermination is earlier than dateCreation

func (*Product) Add added in v0.0.17

func (p *Product) Add() (uint64, error)

Note that Add() does not charge the wallet.

func (*Product) CollectPayment added in v0.1.0

func (p *Product) CollectPayment(v ...interface{}) error

CollectPayment() expects

func (*Product) DateCreation

func (p *Product) DateCreation() time.Time

func (*Product) DateLastBill added in v0.1.0

func (p *Product) DateLastBill() time.Time

func (*Product) DateTermination

func (p *Product) DateTermination() time.Time

func (*Product) ForceCollectPayment added in v0.1.0

func (p *Product) ForceCollectPayment(v ...interface{}) error

func (*Product) Save added in v0.1.0

func (p *Product) Save() error

func (*Product) SerialNumber added in v0.0.17

func (p *Product) SerialNumber() uint64

func (*Product) Terminate added in v0.0.17

func (p *Product) Terminate() error

func (*Product) Terminated added in v0.1.0

func (p *Product) Terminated() bool

func (*Product) ToTerminateOn added in v0.1.0

func (p *Product) ToTerminateOn(terminationDate time.Time) error

type ProductListing added in v0.1.0

type ProductListing struct {
	ProductGroupID     uint64 `json:"product_group_id"`
	ProductName        string `json:"product_name"`
	ProductDescription string `json:"product_description"`

	// Internal BizLogic Related
	ServerType          string                 `json:"server_type"`
	ServerInstanceID    string                 `json:"server_instance_id"`
	ServerConfiguration map[string]interface{} `json:"server_configuration"`  // a big chunk of JSON
	BillingOptions      []BillingOption        `json:"billing_options"`       // Stored as JSON Arr
	UsageBillingFactors UsageBillingFactors    `json:"usage_billing_factors"` // Store as JSON Obj
	// contains filtered or unexported fields
}

func GetProductListingByID added in v0.1.0

func GetProductListingByID(productID uint64) (*ProductListing, error)

For Customer Purchasing. Throwing error for discontinued product

func ListProductListingsByGroupID added in v0.1.10

func ListProductListingsByGroupID(productGroupID uint64) ([]*ProductListing, error)

For Customer. Not showing hidden/disconiued products

func SudoGetProductListingByID added in v0.1.0

func SudoGetProductListingByID(productID uint64) (*ProductListing, error)

For Admin Editing/Customer Viewing Existing. Will show discontinued correctly

func SudoListProductListingsByGroupID added in v0.1.10

func SudoListProductListingsByGroupID(productGroupID uint64) ([]*ProductListing, error)

For Admin. Include hidden/discontinued products

func (*ProductListing) Add added in v0.1.0

func (pl *ProductListing) Add() (uint64, error)

func (*ProductListing) CreateProduct added in v0.1.0

func (pl *ProductListing) CreateProduct(ownerUserID, ownerAffiliationID uint64, billingCycle uint8, walletID uint64) (*Product, error)

func (*ProductListing) Delete added in v0.1.0

func (pl *ProductListing) Delete() error

func (*ProductListing) Discontinue added in v0.1.0

func (pl *ProductListing) Discontinue() error

Discontinue() prevents the product from being purchased

func (*ProductListing) Discontinued added in v0.1.0

func (pl *ProductListing) Discontinued() bool

func (*ProductListing) Hidden added in v0.1.0

func (pl *ProductListing) Hidden() bool

func (*ProductListing) Hide added in v0.1.0

func (pl *ProductListing) Hide() error

Hide() prevents the product from being shown by listing

func (*ProductListing) ProductID added in v0.1.0

func (pl *ProductListing) ProductID() uint64

func (*ProductListing) Reactivate added in v0.1.0

func (pl *ProductListing) Reactivate() error

Reactivate() undo the Discontinue()

func (*ProductListing) Save added in v0.1.0

func (pl *ProductListing) Save() error

func (*ProductListing) Unhide added in v0.1.0

func (pl *ProductListing) Unhide() error

Unhide() undo the Hide()

type ProductListingGroup added in v0.1.0

type ProductListingGroup struct {
	ProductGroupID          uint64 `json:"product_group_id"`
	ProductGroupName        string `json:"product_group_name"`
	ProductGroupDescription string `json:"product_group_description"`
	Hidden                  bool   `json:"hidden"`
}

func GetProductListingGroupByID added in v0.1.0

func GetProductListingGroupByID(id uint64) (ProductListingGroup, error)

func (ProductListingGroup) Delete added in v0.1.0

func (plg ProductListingGroup) Delete() error

func (ProductListingGroup) Save added in v0.1.0

func (plg ProductListingGroup) Save() error

type UsageBillingFactor added in v0.1.0

type UsageBillingFactor struct {
	BillingGroupID uint8   `json:"billing_group_id"`
	ResourceID     uint64  `json:"resource_id"` // see: Ulysses.Lib/server/resource.go
	UnitPrice      float64 `json:"unit_price"`  // UnitPrice * Used = Price
}

func (*UsageBillingFactor) CalculatePrice added in v0.1.0

func (ubf *UsageBillingFactor) CalculatePrice(used float64) float64

type UsageBillingFactors added in v0.1.0

type UsageBillingFactors struct {
	PricingPolicy uint8                           `json:"pricing_policy"` // 0 - SUM(GroupPrice...), 1 - MAX(GroupPrice...), 2 - MIN(GroupPrice...)
	Factors       map[uint8][]*UsageBillingFactor `json:"factors"`        // key: BillingGroupID
}

func UsageBillingFactorsFromJSON added in v0.1.0

func UsageBillingFactorsFromJSON(jsonStr string) (*UsageBillingFactors, error)

func (*UsageBillingFactors) CalculateTotalPrice added in v0.1.0

func (ubfs *UsageBillingFactors) CalculateTotalPrice(resUsageMap map[uint64]float64) (float64, error)

for resUsageMap, uint64 is the ResourceID, float64 is the usage

func (*UsageBillingFactors) ToJSON added in v0.1.0

func (ubfs *UsageBillingFactors) ToJSON() (string, error)

type Wallet

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

func GetWalletByID added in v0.0.17

func GetWalletByID(walletID uint64) (*Wallet, error)

GetWalletByID() build a Wallet struct reflecting an entry in the database.

func UserWallet

func UserWallet(ownerID uint64) (*Wallet, error)

func (*Wallet) Balance

func (w *Wallet) Balance() float64

func (*Wallet) Deposit

func (w *Wallet) Deposit(amount float64) error

func (*Wallet) Disable added in v0.0.17

func (w *Wallet) Disable() error

func (*Wallet) Disabled added in v0.1.11

func (w *Wallet) Disabled() bool

func (*Wallet) Enable added in v0.1.10

func (w *Wallet) Enable() error

func (*Wallet) ID added in v0.1.10

func (w *Wallet) ID() uint64

func (*Wallet) OwnerUserID added in v0.1.11

func (w *Wallet) OwnerUserID() uint64

func (*Wallet) Secured

func (w *Wallet) Secured() float64

func (*Wallet) Spend

func (w *Wallet) Spend(amount float64) error

Spend() tries to consumes balance from the wallet, without throwing error even if balance is insufficient. May result in negative balance.

func (*Wallet) TrySpend added in v0.0.17

func (w *Wallet) TrySpend(amount float64) error

Spend() is safe. i.e., it does not leave a negative balance will return error if not enough balance to spend

Jump to

Keyboard shortcuts

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