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
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package subscriptionitem
|
|
|
|
import (
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/models/mixin"
|
|
"github.com/hanzoai/commerce/util/json"
|
|
"github.com/hanzoai/commerce/util/val"
|
|
"github.com/hanzoai/orm"
|
|
|
|
. "github.com/hanzoai/commerce/types"
|
|
)
|
|
|
|
var kind = "subscription-item"
|
|
|
|
// SubscriptionItem represents a single price/product on a subscription.
|
|
// Each subscription has one or more items. For metered items, a MeterId
|
|
// links usage events to this line. For licensed items, Quantity tracks seats.
|
|
|
|
func init() { orm.Register[SubscriptionItem](kind) }
|
|
|
|
type SubscriptionItem struct {
|
|
mixin.Model[SubscriptionItem]
|
|
|
|
SubscriptionId string `json:"subscriptionId"`
|
|
PriceId string `json:"priceId,omitempty"`
|
|
PlanId string `json:"planId,omitempty"`
|
|
MeterId string `json:"meterId,omitempty"`
|
|
|
|
// Quantity for licensed/per-seat items (0 for metered)
|
|
Quantity int64 `json:"quantity"`
|
|
|
|
// "licensed" or "metered"
|
|
BillingMode string `json:"billingMode"`
|
|
|
|
Metadata Map `json:"metadata,omitempty" datastore:"-"`
|
|
Metadata_ string `json:"-" datastore:",noindex"`
|
|
}
|
|
|
|
func (si *SubscriptionItem) Load(ps []datastore.Property) (err error) {
|
|
if err = datastore.LoadStruct(si, ps); err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(si.Metadata_) > 0 {
|
|
err = json.DecodeBytes([]byte(si.Metadata_), &si.Metadata)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (si *SubscriptionItem) Save() (ps []datastore.Property, err error) {
|
|
si.Metadata_ = string(json.EncodeBytes(&si.Metadata))
|
|
return datastore.SaveStruct(si)
|
|
}
|
|
|
|
func (si *SubscriptionItem) Validator() *val.Validator {
|
|
return nil
|
|
}
|
|
|
|
func New(db *datastore.Datastore) *SubscriptionItem {
|
|
si := new(SubscriptionItem)
|
|
si.Init(db)
|
|
si.Parent = db.NewKey("synckey", "", 1, nil)
|
|
if si.BillingMode == "" {
|
|
si.BillingMode = "licensed"
|
|
}
|
|
return si
|
|
}
|
|
|
|
func Query(db *datastore.Datastore) datastore.Query {
|
|
return db.Query(kind)
|
|
}
|