commerce

package
v1.5.13 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package commerce is a thin, typed Go client for the Hanzo Commerce HTTP API (Square-backed billing at commerce.hanzo.ai).

Hanzo Commerce is an external service — this package never embeds it. It owns customer records, subscriptions, checkout/charge, usage metering and invoices. Downstream Base plugins (e.g. plugins/bootnode) depend on the Client interface, not on the concrete HTTPClient, so the transport can be mocked at the boundary in tests without a live Commerce instance.

This is the Go port of the Python bootnode core/billing/commerce.py + unified.py (the IAM↔Commerce linking lives in Client.GetOrCreateCustomer).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// GetOrCreateCustomer idempotently resolves a Commerce customer for an IAM
	// user, keyed by the user's IAM id stored in customer metadata. This is the
	// unified IAM↔Commerce link from the Python unified.py.
	GetOrCreateCustomer(ctx context.Context, u User) (*Customer, error)

	// GetCustomer fetches a customer by Commerce id.
	GetCustomer(ctx context.Context, customerID string) (*Customer, error)

	// CreateSubscription subscribes a customer to a plan tier.
	CreateSubscription(ctx context.Context, customerID, tier string) (*Subscription, error)

	// GetSubscription fetches a subscription by id.
	GetSubscription(ctx context.Context, subscriptionID string) (*Subscription, error)

	// CancelSubscription cancels a subscription. When immediate is false the
	// subscription is scheduled to end at the current period boundary.
	CancelSubscription(ctx context.Context, subscriptionID string, immediate bool) error

	// ReportUsage records metered usage (compute units) against a subscription.
	ReportUsage(ctx context.Context, subscriptionID string, quantity int64, idempotencyKey string) error

	// ListInvoices returns a customer's invoices, most recent first.
	ListInvoices(ctx context.Context, customerID string) ([]Invoice, error)

	// Enabled reports whether a usable API key is configured. When false the
	// caller should treat billing as a no-op rather than erroring.
	Enabled() bool
}

Client is the billing surface the rest of Base depends on. It is small on purpose: customer lifecycle, subscriptions, usage metering and invoices. Implementations must be safe for concurrent use.

type Config

type Config struct {
	// BaseURL is the Commerce API base (default https://commerce.hanzo.ai).
	BaseURL string
	// APIKey is the Commerce bearer token. When empty the client is disabled
	// and all mutating calls return an [Error] with status 503.
	APIKey string
	// Timeout bounds each request (default 30s).
	Timeout time.Duration
}

Config configures an HTTPClient.

type Customer

type Customer struct {
	ID    string `json:"id"`
	Email string `json:"email"`
	Name  string `json:"name"`
	Org   string `json:"org"`
}

Customer is a Commerce customer record.

type Error

type Error struct {
	Status  int
	Message string
}

Error is a typed error from the Commerce API. It carries the upstream HTTP status so callers can map it to their own response codes.

func (*Error) Error

func (e *Error) Error() string

type HTTPClient

type HTTPClient struct {
	// contains filtered or unexported fields
}

HTTPClient is the concrete Client backed by the Hanzo Commerce HTTP API.

func New

func New(cfg Config) *HTTPClient

New constructs an HTTPClient. It never returns an error: a missing API key yields a disabled client (see HTTPClient.Enabled).

func (*HTTPClient) CancelSubscription

func (c *HTTPClient) CancelSubscription(ctx context.Context, subscriptionID string, immediate bool) error

CancelSubscription cancels a subscription.

func (*HTTPClient) CreateSubscription

func (c *HTTPClient) CreateSubscription(ctx context.Context, customerID, tier string) (*Subscription, error)

CreateSubscription subscribes a customer to a tier.

func (*HTTPClient) Enabled

func (c *HTTPClient) Enabled() bool

Enabled reports whether an API key is configured.

func (*HTTPClient) GetCustomer

func (c *HTTPClient) GetCustomer(ctx context.Context, customerID string) (*Customer, error)

GetCustomer fetches a customer by id.

func (*HTTPClient) GetOrCreateCustomer

func (c *HTTPClient) GetOrCreateCustomer(ctx context.Context, u User) (*Customer, error)

GetOrCreateCustomer resolves a customer by the IAM user's email, creating one when absent. The IAM id is persisted in customer metadata so the link is stable across email changes.

func (*HTTPClient) GetSubscription

func (c *HTTPClient) GetSubscription(ctx context.Context, subscriptionID string) (*Subscription, error)

GetSubscription fetches a subscription by id.

func (*HTTPClient) ListInvoices

func (c *HTTPClient) ListInvoices(ctx context.Context, customerID string) ([]Invoice, error)

ListInvoices returns a customer's invoices.

func (*HTTPClient) ReportUsage

func (c *HTTPClient) ReportUsage(ctx context.Context, subscriptionID string, quantity int64, idempotencyKey string) error

ReportUsage records metered usage against a subscription. idempotencyKey dedupes retries on the Commerce side.

type Invoice

type Invoice struct {
	ID        string `json:"id"`
	Status    string `json:"status"`
	AmountUSD int64  `json:"amountUsd"`
	Created   string `json:"created"`
}

Invoice is a Commerce invoice/order record.

type Subscription

type Subscription struct {
	ID         string `json:"id"`
	CustomerID string `json:"customerId"`
	Tier       string `json:"tier"`
	Status     string `json:"status"`
}

Subscription is a Commerce subscription record.

type User

type User struct {
	ID    string
	Email string
	Name  string
	Org   string
}

User is the minimal IAM identity the billing client needs. It mirrors the fields of iam.User without importing it, keeping commerce free of any platform/IAM dependency (one-directional: bootnode depends on commerce, not the reverse).

Jump to

Keyboard shortcuts

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