Files
zeekay 1943f1c093 Hanzo Commerce
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
2026-09-23 21:52:16 -07:00

42 lines
1.5 KiB
Go

package blockchain
import (
"context"
"errors"
"fmt"
"github.com/hanzoai/commerce/models/blockchains"
"github.com/hanzoai/commerce/models/types/currency"
"github.com/hanzoai/commerce/models/wallet"
)
// PaymentFn is the per-chain payment dispatcher. EVM payment support ships in
// github.com/hanzoai/commerce/luxfi/cevm and registers itself on
// import; consumers who don't need EVM never link geth.
type PaymentFn func(ctx context.Context, from wallet.Account, to string, amount, fee currency.Cents, password []byte) (string, error)
var paymentFns = map[blockchains.Type]PaymentFn{}
// RegisterPayment wires a payment function for a chain type. Later
// registrations override earlier ones; callers should register once at init
// time.
func RegisterPayment(typ blockchains.Type, fn PaymentFn) {
paymentFns[typ] = fn
}
// ErrNoPayment is returned when MakePayment is called for a chain type whose
// payment function hasn't been registered (e.g. an EVM chain without the
// luxfi/cevm sub-module wired in).
var ErrNoPayment = errors.New("blockchain: no payment function registered for chain type")
func MakePayment(ctx context.Context, from wallet.Account, to string, amount, fee currency.Cents, password []byte) (string, error) {
switch from.Type {
case blockchains.BitcoinType, blockchains.BitcoinTestnetType:
return MakeBitcoinPayment(ctx, from, to, amount, fee, password)
}
if fn, ok := paymentFns[from.Type]; ok {
return fn(ctx, from, to, amount, fee, password)
}
return "", fmt.Errorf("%w: %v", ErrNoPayment, from.Type)
}