mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
32 lines
761 B
Go
32 lines
761 B
Go
package balance
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/hanzoai/commerce/models/order"
|
|
"github.com/hanzoai/commerce/models/organization"
|
|
"github.com/hanzoai/commerce/models/payment"
|
|
"github.com/hanzoai/commerce/models/types/accounts"
|
|
"github.com/hanzoai/commerce/models/user"
|
|
)
|
|
|
|
var InsufficientCredit = errors.New("Insufficient credit")
|
|
|
|
func Authorize(org *organization.Organization, ord *order.Order, usr *user.User, pay *payment.Payment) error {
|
|
// Deprecated
|
|
pay.Type = accounts.BalanceType
|
|
|
|
pay.Account.Type = accounts.BalanceType
|
|
pay.Live = org.Live
|
|
|
|
if err := usr.CalculateBalances(!org.Live); err != nil {
|
|
return err
|
|
}
|
|
|
|
if val, ok := usr.Transactions[ord.Currency]; !ok || val.Balance < ord.Total {
|
|
return InsufficientCredit
|
|
}
|
|
|
|
return nil
|
|
}
|