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
1.5 KiB
Go
46 lines
1.5 KiB
Go
package autorecharge
|
|
|
|
import (
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/models/mixin"
|
|
"github.com/hanzoai/commerce/util/val"
|
|
"github.com/hanzoai/orm"
|
|
)
|
|
|
|
func init() { orm.Register[AutoRecharge]("auto-recharge") }
|
|
|
|
// AutoRecharge is a per-org automatic top-up rule. When the org's available
|
|
// balance (balance minus holds) drops below ThresholdCents, the recharge cron
|
|
// charges the org's default payment method AmountCents and credits the balance.
|
|
// Mirrors the Claude/OpenAI "auto-reload" setting. One record per org (keyed by
|
|
// UserId = the org slug, which is the billing key used by topup/balance).
|
|
type AutoRecharge struct {
|
|
mixin.Model[AutoRecharge]
|
|
|
|
UserId string `json:"userId"` // org slug (billing key)
|
|
Enabled bool `json:"enabled"`
|
|
ThresholdCents int64 `json:"thresholdCents"`
|
|
AmountCents int64 `json:"amountCents"`
|
|
Currency string `json:"currency"`
|
|
|
|
// LastRechargedAt stores the ISO timestamp of the last successful auto
|
|
// top-up — used for observability and as a light guard against a runaway
|
|
// recharge loop within a single cron window.
|
|
LastRechargedAt string `json:"lastRechargedAt,omitempty"`
|
|
}
|
|
|
|
func (a *AutoRecharge) Validator() *val.Validator {
|
|
return val.New()
|
|
}
|
|
|
|
func New(db *datastore.Datastore) *AutoRecharge {
|
|
a := new(AutoRecharge)
|
|
a.Init(db)
|
|
a.Parent = db.NewKey("synckey", "", 1, nil)
|
|
return a
|
|
}
|
|
|
|
func Query(db *datastore.Datastore) datastore.Query {
|
|
return db.Query("auto-recharge")
|
|
}
|