2022-04-26 21:23:27 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package billing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-28 02:42:26 +01:00
|
|
|
"fmt"
|
2022-04-26 21:23:27 +01:00
|
|
|
"time"
|
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
"storj.io/common/currency"
|
2022-04-26 21:23:27 +01:00
|
|
|
"storj.io/common/uuid"
|
|
|
|
)
|
|
|
|
|
2022-06-16 17:48:07 +01:00
|
|
|
// TransactionStatus indicates transaction status.
|
|
|
|
type TransactionStatus string
|
2022-04-26 21:23:27 +01:00
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
// ErrInsufficientFunds represents err when a user balance is too low for some transaction.
|
|
|
|
var ErrInsufficientFunds = errs.New("Insufficient funds for this transaction")
|
|
|
|
|
2022-08-15 15:41:19 +01:00
|
|
|
// ErrNoWallet represents err when there is no wallet in the DB.
|
2023-01-20 21:40:23 +00:00
|
|
|
var ErrNoWallet = errs.New("wallet does not exists")
|
2022-08-15 15:41:19 +01:00
|
|
|
|
|
|
|
// ErrNoTransactions represents err when there is no billing transactions in the DB.
|
|
|
|
var ErrNoTransactions = errs.New("no transactions in the database")
|
|
|
|
|
2022-04-26 21:23:27 +01:00
|
|
|
const (
|
2022-06-16 17:48:07 +01:00
|
|
|
// TransactionStatusPending indicates that status of this transaction is pending.
|
|
|
|
TransactionStatusPending = "pending"
|
|
|
|
// TransactionStatusCancelled indicates that status of this transaction is cancelled.
|
|
|
|
TransactionStatusCancelled = "cancelled"
|
2022-05-10 20:19:53 +01:00
|
|
|
// TransactionStatusCompleted indicates that status of this transaction is complete.
|
|
|
|
TransactionStatusCompleted = "complete"
|
2022-04-26 21:23:27 +01:00
|
|
|
)
|
|
|
|
|
2022-06-16 17:48:07 +01:00
|
|
|
// TransactionType indicates transaction type.
|
|
|
|
type TransactionType string
|
2022-04-26 21:23:27 +01:00
|
|
|
|
2022-06-16 17:48:07 +01:00
|
|
|
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"
|
|
|
|
)
|
2022-04-26 21:23:27 +01:00
|
|
|
|
|
|
|
// TransactionsDB is an interface which defines functionality
|
|
|
|
// of DB which stores billing transactions.
|
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type TransactionsDB interface {
|
2023-03-24 12:08:40 +00:00
|
|
|
// 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)
|
2022-06-16 17:48:07 +01:00
|
|
|
// UpdateStatus updates the status of the transaction.
|
|
|
|
UpdateStatus(ctx context.Context, txID int64, status TransactionStatus) error
|
|
|
|
// UpdateMetadata updates the metadata of the transaction.
|
|
|
|
UpdateMetadata(ctx context.Context, txID int64, metadata []byte) error
|
2022-06-17 00:29:31 +01:00
|
|
|
// 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)
|
2022-04-26 21:23:27 +01:00
|
|
|
// List returns all transactions for the specified user.
|
|
|
|
List(ctx context.Context, userID uuid.UUID) ([]Transaction, error)
|
2023-05-15 16:14:30 +01:00
|
|
|
// ListSource returns all transactions for the specified user and source.
|
|
|
|
ListSource(ctx context.Context, userID uuid.UUID, txSource string) ([]Transaction, error)
|
2022-06-16 17:48:07 +01:00
|
|
|
// GetBalance returns the current usable balance for the specified user.
|
2022-09-06 13:43:09 +01:00
|
|
|
GetBalance(ctx context.Context, userID uuid.UUID) (currency.Amount, error)
|
2022-04-26 21:23:27 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 00:29:31 +01:00
|
|
|
// 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 PaymentType interface {
|
|
|
|
// Source the source of the payment
|
|
|
|
Source() string
|
|
|
|
// Type the type of the payment
|
|
|
|
Type() TransactionType
|
|
|
|
// GetNewTransactions returns new transactions that occurred after the provided last transaction received.
|
|
|
|
GetNewTransactions(ctx context.Context, lastTransactionTime time.Time, metadata []byte) ([]Transaction, error)
|
|
|
|
}
|
|
|
|
|
2023-03-28 02:42:26 +01:00
|
|
|
// Well-known PaymentType sources.
|
|
|
|
const (
|
|
|
|
StripeSource = "stripe"
|
|
|
|
StorjScanSource = "storjscan"
|
|
|
|
StorjScanBonusSource = "storjscanbonus"
|
|
|
|
)
|
|
|
|
|
2022-04-26 21:23:27 +01:00
|
|
|
// Transaction defines billing related transaction info that is stored in the DB.
|
|
|
|
type Transaction struct {
|
2022-06-16 17:48:07 +01:00
|
|
|
ID int64
|
|
|
|
UserID uuid.UUID
|
2022-09-06 13:43:09 +01:00
|
|
|
Amount currency.Amount
|
2022-04-26 21:23:27 +01:00
|
|
|
Description string
|
2022-06-16 17:48:07 +01:00
|
|
|
Source string
|
|
|
|
Status TransactionStatus
|
|
|
|
Type TransactionType
|
|
|
|
Metadata []byte
|
2022-04-26 21:23:27 +01:00
|
|
|
Timestamp time.Time
|
|
|
|
CreatedAt time.Time
|
|
|
|
}
|
2023-03-28 02:42:26 +01:00
|
|
|
|
|
|
|
func prepareBonusTransaction(bonusRate int64, source string, transaction Transaction) (Transaction, bool) {
|
|
|
|
// Bonus transactions only apply when enabled (i.e. positive rate) and
|
|
|
|
// for StorjScan transactions.
|
|
|
|
switch {
|
|
|
|
case bonusRate <= 0:
|
|
|
|
return Transaction{}, false
|
|
|
|
case source != StorjScanSource:
|
|
|
|
return Transaction{}, false
|
|
|
|
case transaction.Type != TransactionTypeCredit:
|
|
|
|
// This is defensive. Storjscan shouldn't provide "debit" transactions.
|
|
|
|
return Transaction{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return Transaction{
|
|
|
|
UserID: transaction.UserID,
|
|
|
|
Amount: calculateBonusAmount(transaction.Amount, bonusRate),
|
|
|
|
Description: fmt.Sprintf("STORJ Token Bonus (%d%%)", bonusRate),
|
|
|
|
Source: StorjScanBonusSource,
|
|
|
|
Status: TransactionStatusCompleted,
|
|
|
|
Type: TransactionTypeCredit,
|
|
|
|
Timestamp: transaction.Timestamp,
|
|
|
|
Metadata: append([]byte(nil), transaction.Metadata...),
|
|
|
|
}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func calculateBonusAmount(amount currency.Amount, bonusRate int64) currency.Amount {
|
|
|
|
bonusUnits := amount.BaseUnits() * bonusRate / 100
|
|
|
|
return currency.AmountFromBaseUnits(bonusUnits, amount.Currency())
|
|
|
|
}
|