billing

package
v1.104.5 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TransactionStatusPending indicates that status of this transaction is pending.
	TransactionStatusPending = "pending"
	// TransactionStatusCompleted indicates that status of this transaction is complete.
	TransactionStatusCompleted = "complete"
	// TransactionStatusFailed indicates that status of this transaction is failed.
	TransactionStatusFailed = "failed"
)
View Source
const (
	// TransactionTypeCredit indicates that type of this transaction is credit.
	TransactionTypeCredit = "credit"
	// TransactionTypeDebit indicates that type of this transaction is debit.
	TransactionTypeDebit = "debit"
	// TransactionTypeUnknown indicates that type of this transaction is unknown.
	TransactionTypeUnknown = "unknown"
)
View Source
const (
	StripeSource            = "stripe"
	StorjScanEthereumSource = "ethereum"
	StorjScanZkSyncSource   = "zkSync"
	StorjScanBonusSource    = "storjscanbonus"
)

Well-known PaymentType sources.

Variables

View Source
var ChoreErr = errs.Class("billing chore")

ChoreErr is billing chore err class.

View Source
var ErrInsufficientFunds = errs.New("Insufficient funds for this transaction")

ErrInsufficientFunds represents err when a user balance is too low for some transaction.

View Source
var ErrNoTransactions = errs.New("no transactions in the database")

ErrNoTransactions represents err when there is no billing transactions in the DB.

View Source
var ErrNoWallet = errs.New("wallet does not exists")

ErrNoWallet represents err when there is no wallet in the DB.

View Source
var SourceChainIDs = map[string][]int64{
	StorjScanEthereumSource: {1, 4, 5, 1337, 11155111},
	StorjScanZkSyncSource:   {300, 324},
}

SourceChainIDs are some well known chain IDs for the above sources.

Functions

func CalculateBonusAmount added in v1.84.1

func CalculateBonusAmount(amount currency.Amount, bonusRate int64) currency.Amount

CalculateBonusAmount calculates bonus for given currency amount and bonus rate.

Types

type Chore added in v1.62.1

type Chore struct {
	TransactionCycle *sync2.Cycle
	// contains filtered or unexported fields
}

Chore periodically queries for new billing transactions from payment type.

architecture: Chore

func NewChore added in v1.62.1

func NewChore(log *zap.Logger, paymentTypes []PaymentType, transactionsDB TransactionsDB, interval time.Duration, disableLoop bool, bonusRate int64, observers ChoreObservers) *Chore

NewChore creates new chore.

func (*Chore) Close added in v1.62.1

func (chore *Chore) Close() (err error)

Close closes all underlying resources.

func (*Chore) Run added in v1.62.1

func (chore *Chore) Run(ctx context.Context) (err error)

Run runs billing transaction loop.

func (*Chore) TestSetPaymentTypes added in v1.87.2

func (chore *Chore) TestSetPaymentTypes(types []PaymentType)

TestSetPaymentTypes is used in tests to change the payment types this chore tracks.

type ChoreObservers added in v1.85.1

type ChoreObservers struct {
	UpgradeUser Observer
	PayInvoices Observer
}

ChoreObservers holds functionality to process confirmed transactions using different types of observers.

type Config added in v1.62.1

type Config struct {
	Interval    time.Duration `help:"billing chore interval to query for new transactions from all payment types" default:"15s"`
	DisableLoop bool          `help:"flag to disable querying for new billing transactions by billing chore" default:"true"`
}

Config stores needed information for billing service initialization.

type Observer added in v1.84.1

type Observer interface {
	// Process is called repeatedly for each transaction.
	Process(context.Context, Transaction) error
	// TestSetNow allows tests to have the observer act as if the current time is whatever they want.
	TestSetNow(nowFn func() time.Time)
}

Observer processes a billing transaction.

type PaymentType added in v1.62.1

type PaymentType interface {
	// Sources the supported sources of the payment type
	Sources() []string
	// Type the type of the payment
	Type() TransactionType
	// GetNewTransactions returns new transactions for a given source that occurred after the provided last transaction received.
	GetNewTransactions(ctx context.Context, source string, lastTransactionTime time.Time, metadata []byte) ([]Transaction, error)
}

PaymentType is an interface which defines functionality required for all billing payment types. Payment types can include but are not limited to Bitcoin, Ether, credit or debit card, ACH transfer, or even physical transfer of live goats. In each case, a source, type, and method to get new transactions must be defined by the service, though metadata specific to each payment type is also supported (i.e. goat hair type).

type Transaction

type Transaction struct {
	ID          int64
	UserID      uuid.UUID
	Amount      currency.Amount
	Description string
	Source      string
	Status      TransactionStatus
	Type        TransactionType
	Metadata    []byte
	Timestamp   time.Time
	CreatedAt   time.Time
}

Transaction defines billing related transaction info that is stored in the DB.

type TransactionStatus added in v1.62.1

type TransactionStatus string

TransactionStatus indicates transaction status.

type TransactionType added in v1.62.1

type TransactionType string

TransactionType indicates transaction type.

type TransactionsDB

type TransactionsDB interface {
	// Insert inserts the provided primary transaction along with zero or more
	// supplemental transactions that. This is NOT intended for bulk insertion,
	// but rather to provide an atomic commit of one or more _related_
	// transactions.
	Insert(ctx context.Context, primaryTx Transaction, supplementalTx ...Transaction) (txIDs []int64, err error)
	// FailPendingInvoiceTokenPayments marks all specified pending invoice token payments as failed, and refunds the pending charges.
	FailPendingInvoiceTokenPayments(ctx context.Context, txIDs ...int64) error
	// CompletePendingInvoiceTokenPayments updates the status of the pending invoice token payment to complete.
	CompletePendingInvoiceTokenPayments(ctx context.Context, txIDs ...int64) error
	// UpdateMetadata updates the metadata of the transaction.
	UpdateMetadata(ctx context.Context, txID int64, metadata []byte) error
	// LastTransaction returns the timestamp and metadata of the last known transaction for given source and type.
	LastTransaction(ctx context.Context, txSource string, txType TransactionType) (time.Time, []byte, error)
	// List returns all transactions for the specified user.
	List(ctx context.Context, userID uuid.UUID) ([]Transaction, error)
	// ListSource returns all transactions for the specified user and source.
	ListSource(ctx context.Context, userID uuid.UUID, txSource string) ([]Transaction, error)
	// GetBalance returns the current usable balance for the specified user.
	GetBalance(ctx context.Context, userID uuid.UUID) (currency.Amount, error)
}

TransactionsDB is an interface which defines functionality of DB which stores billing transactions.

architecture: Database

Jump to

Keyboard shortcuts

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