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
46 lines
739 B
Go
46 lines
739 B
Go
package wallet
|
|
|
|
import (
|
|
"github.com/hanzoai/commerce/datastore"
|
|
)
|
|
|
|
type WalletHolder struct {
|
|
WalletId string `json:"walletId"`
|
|
Wallet *Wallet `json:"wallet,omitempty" datastore:"-"`
|
|
}
|
|
|
|
func (w *WalletHolder) GetOrCreateWallet(db *datastore.Datastore) (*Wallet, error) {
|
|
wal := New(db)
|
|
|
|
// create
|
|
if w.WalletId == "" {
|
|
if err := wal.Create(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
w.WalletId = wal.Id()
|
|
w.Wallet = wal
|
|
return wal, nil
|
|
}
|
|
|
|
// get
|
|
err := wal.GetById(w.WalletId)
|
|
w.Wallet = wal
|
|
return wal, err
|
|
}
|
|
|
|
func (w *WalletHolder) LoadWallet(db *datastore.Datastore) error {
|
|
if w.Wallet != nil {
|
|
return nil
|
|
}
|
|
|
|
wal, err := w.GetOrCreateWallet(db)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
w.Wallet = wal
|
|
|
|
return nil
|
|
}
|