Documentation
¶
Overview ¶
Package ledger provides a composable usage-based billing engine for Go applications.
Ledger is designed as a library, not a service. Import it directly into your Go application and run it against your own database. It provides:
- Entitlement checks against a plan's limits, cached in the store
- Usage metering with buffered, batched ingestion
- Flexible pricing models (graduated, volume, flat tiers)
- Invoice generation with base, overage, seat, discount and tax lines
- Pluggable payment provider integration through provider.Provider (no provider implementation ships with the module)
- An audit trail bridge (package audithook) for Chronicle or any recorder
- Metrics through a small MetricFactory interface (package observability)
Quick Start ¶
Create a ledger instance with your preferred store:
import (
"github.com/xraph/grove"
"github.com/xraph/grove/drivers/pgdriver"
"github.com/xraph/ledger"
"github.com/xraph/ledger/store/postgres"
)
// Open a Grove database
pg := pgdriver.New()
if err := pg.Open(ctx, databaseURL); err != nil {
log.Fatal(err)
}
db, err := grove.Open(pg)
if err != nil {
log.Fatal(err)
}
// Create ledger
l := ledger.New(postgres.New(db))
// Start the ledger (migrates the store, then starts background workers)
if err := l.Start(ctx); err != nil {
log.Fatal(err)
}
defer l.Stop()
Core Concepts ¶
Plans define what features are available and at what limits:
p := &plan.Plan{
Name: "Pro",
Slug: "pro",
Currency: "usd",
Status: plan.StatusActive,
Features: []plan.Feature{
{Key: "api_calls", Limit: 10000, Type: plan.FeatureMetered, Period: plan.PeriodMonthly},
{Key: "seats", Limit: 5, Type: plan.FeatureSeat},
{Key: "sso", Limit: 1, Type: plan.FeatureBoolean},
},
}
err := l.CreatePlan(ctx, p)
Subscriptions connect tenants to plans:
sub := &subscription.Subscription{TenantID: tenantID, PlanID: p.ID}
err := l.CreateSubscription(ctx, sub)
Entitlements check if a tenant can use a feature. Entitled, Check, Meter and MeterWithKey read the tenant and app from the context. Set them with WithTenant; the plain string keys "tenant_id" and "app_id" still work:
ctx = ledger.WithTenant(ctx, tenantID, appID)
result, err := l.Entitled(ctx, "api_calls")
if result.Allowed {
// Process API call
l.Meter(ctx, "api_calls", 1)
}
Entitled reads a store outage as no access. Check returns the same answer but reports an outage as an error wrapping ErrUnavailable. MeterWithKey is Meter for an event with an idempotency key: the store counts a key once, and a failed write is retried instead of dropped. The key is global across tenants and apps, so namespace it, for example "<tenant>/<app>/<message id>".
All monetary calculations use integer arithmetic to avoid floating-point precision issues. The Money type represents amounts in the smallest currency unit (cents for USD, pence for GBP, etc).
Integration ¶
Ledger works with the rest of the Forgery ecosystem:
- Forge: the extension package registers the engine as a Forge extension
- Grove: the PostgreSQL, SQLite and MongoDB stores run on a *grove.DB
- Chronicle: package audithook turns billing events into audit records
- go-utils: logging, and metrics through package observability
TypeID ¶
All entities use TypeID for globally unique, type-safe identifiers:
plan_01h2xcejqtf2nbrexx3vqjhp41 // Plan ID sub_01h2xcejqtf2nbrexx3vqjhp41 // Subscription ID inv_01h455vb4pex5vsknk084sn02q // Invoice ID
TypeIDs are K-sortable, making them ideal for database indexes and providing natural time-ordering of entities.
Index ¶
- Variables
- func IsNotFound(err error) bool
- func IsQuotaError(err error) bool
- func IsRetryable(err error) bool
- func ValidateFeature(f *feature.Feature) error
- func WithTenant(ctx context.Context, tenantID, appID string) context.Context
- type Entity
- type ID
- type ImportOption
- type InvoiceOption
- type Ledger
- func (l *Ledger) ActivatePlan(ctx context.Context, planID id.PlanID) error
- func (l *Ledger) Advance(ctx context.Context, now time.Time) (LifecycleReport, error)
- func (l *Ledger) AdvancePeriods(ctx context.Context, now time.Time) ([]id.SubscriptionID, error)
- func (l *Ledger) ApplyCoupon(ctx context.Context, subID id.SubscriptionID, code string) (*coupon.Coupon, error)
- func (l *Ledger) ArchiveFeature(ctx context.Context, featureID id.FeatureID) error
- func (l *Ledger) ArchivePlan(ctx context.Context, planID id.PlanID) error
- func (l *Ledger) CancelSubscription(ctx context.Context, subID id.SubscriptionID, immediately bool) error
- func (l *Ledger) ChangePlan(ctx context.Context, subID id.SubscriptionID, planID id.PlanID, ...) (*subscription.Subscription, error)
- func (l *Ledger) Check(ctx context.Context, featureKey string) (*entitlement.Result, error)
- func (l *Ledger) CreateCoupon(ctx context.Context, c *coupon.Coupon) error
- func (l *Ledger) CreateFeature(ctx context.Context, f *feature.Feature) error
- func (l *Ledger) CreatePlan(ctx context.Context, p *plan.Plan) error
- func (l *Ledger) CreateSubscription(ctx context.Context, sub *subscription.Subscription) error
- func (l *Ledger) DeleteCoupon(ctx context.Context, couponID id.CouponID) error
- func (l *Ledger) DeleteFeature(ctx context.Context, featureID id.FeatureID) error
- func (l *Ledger) DeletePlan(ctx context.Context, planID id.PlanID) error
- func (l *Ledger) EnactCancels(ctx context.Context, now time.Time) ([]id.SubscriptionID, error)
- func (l *Ledger) EndTrials(ctx context.Context, now time.Time) ([]id.SubscriptionID, error)
- func (l *Ledger) Entitled(ctx context.Context, featureKey string) (*entitlement.Result, error)
- func (l *Ledger) ExportInvoice(ctx context.Context, invID id.InvoiceID, format string) ([]byte, error)
- func (l *Ledger) FinalizeInvoice(ctx context.Context, invID id.InvoiceID) error
- func (l *Ledger) GenerateInvoice(ctx context.Context, subID id.SubscriptionID, opts ...InvoiceOption) (inv *invoice.Invoice, err error)
- func (l *Ledger) GetActiveSubscription(ctx context.Context, tenantID, appID string) (*subscription.Subscription, error)
- func (l *Ledger) GetFeature(ctx context.Context, featureID id.FeatureID) (*feature.Feature, error)
- func (l *Ledger) GetFeatureByKey(ctx context.Context, key, appID string) (*feature.Feature, error)
- func (l *Ledger) GetPlan(ctx context.Context, planID id.PlanID) (*plan.Plan, error)
- func (l *Ledger) GetPlanBySlug(ctx context.Context, slug, appID string) (*plan.Plan, error)
- func (l *Ledger) GetSubscription(ctx context.Context, subID id.SubscriptionID) (*subscription.Subscription, error)
- func (l *Ledger) HandleWebhook(ctx context.Context, providerName string, payload []byte) (res *provider.WebhookResult, err error)
- func (l *Ledger) HasProviders() bool
- func (l *Ledger) Health(ctx context.Context) error
- func (l *Ledger) ImportFeatureFromProvider(ctx context.Context, providerName, providerID string, opts ...ImportOption) (_ *feature.Feature, err error)
- func (l *Ledger) ImportInvoiceFromProvider(ctx context.Context, providerName, providerID string, opts ...ImportOption) (_ *invoice.Invoice, err error)
- func (l *Ledger) ImportPlanFromProvider(ctx context.Context, providerName, providerID string, opts ...ImportOption) (_ *plan.Plan, err error)
- func (l *Ledger) ImportSubscriptionFromProvider(ctx context.Context, providerName, providerID string, opts ...ImportOption) (_ *subscription.Subscription, err error)
- func (l *Ledger) InspectEntitlement(ctx context.Context, tenantID, appID, featureKey string) (*entitlement.Result, error)
- func (l *Ledger) InvoiceFormats() []string
- func (l *Ledger) LifecycleInterval() time.Duration
- func (l *Ledger) ListAppliedCoupons(ctx context.Context, subID id.SubscriptionID) ([]*coupon.Coupon, error)
- func (l *Ledger) ListFeatures(ctx context.Context, appID string, opts feature.ListOpts) ([]*feature.Feature, error)
- func (l *Ledger) ListGlobalFeatures(ctx context.Context, opts feature.ListOpts) ([]*feature.Feature, error)
- func (l *Ledger) ListPaymentMethods(ctx context.Context, tenantID string) (methods []provider.PaymentMethod, err error)
- func (l *Ledger) MarkInvoicePaid(ctx context.Context, invID id.InvoiceID, paidAt time.Time, paymentRef string) error
- func (l *Ledger) MarkInvoiceVoided(ctx context.Context, invID id.InvoiceID, reason string) error
- func (l *Ledger) MarkInvoicesPastDue(ctx context.Context, now time.Time) ([]id.InvoiceID, error)
- func (l *Ledger) Meter(ctx context.Context, featureKey string, quantity int64) error
- func (l *Ledger) MeterWithKey(ctx context.Context, featureKey string, quantity int64, idempotencyKey string) error
- func (l *Ledger) PauseSubscription(ctx context.Context, subID id.SubscriptionID) (*subscription.Subscription, error)
- func (l *Ledger) ProviderNames() []string
- func (l *Ledger) Remaining(ctx context.Context, featureKey string) (int64, error)
- func (l *Ledger) ResumeSubscription(ctx context.Context, subID id.SubscriptionID) (*subscription.Subscription, error)
- func (l *Ledger) Start(ctx context.Context) error
- func (l *Ledger) Stop() error
- func (l *Ledger) Store() store.Store
- func (l *Ledger) SyncFeatureToProvider(ctx context.Context, featureID id.FeatureID) (res *provider.SyncResult, err error)
- func (l *Ledger) SyncInvoiceToProvider(ctx context.Context, invID id.InvoiceID) (res *provider.SyncResult, err error)
- func (l *Ledger) SyncPlanToProvider(ctx context.Context, planID id.PlanID) (res *provider.SyncResult, err error)
- func (l *Ledger) SyncSubscriptionToProvider(ctx context.Context, subID id.SubscriptionID) (res *provider.SyncResult, err error)
- func (l *Ledger) UpdateCoupon(ctx context.Context, c *coupon.Coupon) error
- func (l *Ledger) UpdateFeature(ctx context.Context, f *feature.Feature) error
- func (l *Ledger) UpdatePlan(ctx context.Context, p *plan.Plan) error
- type LifecycleReport
- type Money
- type MultiError
- type Option
- func WithClock(now func() time.Time) Option
- func WithEntitlementCacheTTL(ttl time.Duration) Option
- func WithLifecycleInterval(d time.Duration) Option
- func WithLogger(logger log.Logger) Option
- func WithMeterConfig(batchSize int, flushInterval time.Duration) Option
- func WithMetrics(m gometrics.Metrics) Option
- func WithPlugin(p plugin.Plugin) Option
- func WithoutMigrate() Option
- type Prefix
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
var ( // General errors ErrNotFound = errors.New("ledger: not found") ErrAlreadyExists = errors.New("ledger: already exists") ErrInvalidInput = errors.New("ledger: invalid input") ErrForbidden = errors.New("ledger: forbidden") // Plan errors ErrPlanNotFound = errors.New("ledger: plan not found") ErrPlanArchived = errors.New("ledger: plan is archived") ErrPlanInUse = errors.New("ledger: plan is in use by subscriptions") ErrFeatureNotFound = errors.New("ledger: feature not found") ErrFeatureArchived = errors.New("ledger: feature is archived") ErrInvalidPricing = errors.New("ledger: invalid pricing configuration") ErrDuplicateFeature = errors.New("ledger: duplicate feature key") // Subscription errors ErrSubscriptionNotFound = errors.New("ledger: subscription not found") ErrSubscriptionExists = errors.New("ledger: subscription already exists") ErrSubscriptionCanceled = errors.New("ledger: subscription is canceled") ErrSubscriptionExpired = errors.New("ledger: subscription is expired") ErrInvalidUpgrade = errors.New("ledger: invalid plan upgrade") ErrInvalidDowngrade = errors.New("ledger: invalid plan downgrade") ErrTrialExpired = errors.New("ledger: trial period has expired") ErrNoActiveSubscription = errors.New("ledger: no active subscription") // Metering errors ErrMeterBufferFull = errors.New("ledger: meter buffer full") ErrInvalidQuantity = errors.New("ledger: invalid usage quantity") ErrDuplicateEvent = errors.New("ledger: duplicate usage event") ErrEventTooOld = errors.New("ledger: usage event too old") // Entitlement errors ErrQuotaExceeded = errors.New("ledger: quota exceeded") ErrFeatureDisabled = errors.New("ledger: feature disabled") ErrHardLimitReached = errors.New("ledger: hard limit reached") ErrSoftLimitReached = errors.New("ledger: soft limit reached (warning)") ErrNoEntitlement = errors.New("ledger: no entitlement for feature") // Invoice errors ErrInvoiceNotFound = errors.New("ledger: invoice not found") ErrInvoiceFinalized = errors.New("ledger: invoice already finalized") ErrInvoicePaid = errors.New("ledger: invoice already paid") ErrInvoiceVoided = errors.New("ledger: invoice is voided") ErrInvoiceIncomplete = errors.New("ledger: invoice incomplete") ErrInvalidDiscount = errors.New("ledger: invalid discount") // Coupon errors ErrCouponNotFound = errors.New("ledger: coupon not found") ErrCouponExpired = errors.New("ledger: coupon expired") ErrCouponInvalid = errors.New("ledger: coupon invalid") ErrCouponExhausted = errors.New("ledger: coupon redemptions exhausted") ErrCouponNotStarted = errors.New("ledger: coupon not yet valid") ErrCouponAlreadyApplied = errors.New("ledger: coupon already applied to this subscription") // Provider errors ErrProviderNotFound = errors.New("ledger: provider not found") ErrProviderSync = errors.New("ledger: provider sync failed") ErrProviderWebhook = errors.New("ledger: webhook validation failed") ErrProviderNotConfigured = errors.New("ledger: provider not configured") // Store errors ErrStoreNotReady = errors.New("ledger: store not ready") ErrStoreClosed = errors.New("ledger: store is closed") ErrTransactionFailed = errors.New("ledger: transaction failed") ErrMigrationFailed = errors.New("ledger: migration failed") // answer being no. Check returns it so a caller can tell an outage from a // tenant with no access. ErrUnavailable = errors.New("ledger: unavailable") // Cache errors ErrCacheMiss = errors.New("ledger: cache miss") ErrCacheInvalidate = errors.New("ledger: cache invalidation failed") )
Sentinel errors for common failure scenarios.
var ( USD = types.USD EUR = types.EUR GBP = types.GBP JPY = types.JPY CAD = types.CAD AUD = types.AUD Zero = types.Zero Sum = types.Sum )
Re-export Money constructors
var NewEntity = types.NewEntity
Re-export Entity constructor
Functions ¶
func IsNotFound ¶
IsNotFound returns true if the error is a not found error.
func IsQuotaError ¶
IsQuotaError returns true if the error is related to quota/limits.
func IsRetryable ¶
IsRetryable returns true if the error is temporary and the operation can be retried.
func ValidateFeature ¶
ValidateFeature checks the shape of a catalog feature: a key, a known type and period, and a default limit of -1 (unlimited) or more. The feature writes themselves store whatever they are given, so the dashboard contract and the provider imports call this first. A problem wraps ErrInvalidInput, and the text after its prefix says what is wrong. It does not look at the status: a draft feature is a valid shape, and each caller decides which statuses it accepts.
func WithTenant ¶
WithTenant returns a context that scopes Meter, MeterWithKey, Entitled, Check and Remaining to a tenant and an app. It takes precedence over the plain "tenant_id" and "app_id" string keys older callers set, which still work when WithTenant is not used.
Types ¶
type ImportOption ¶
type ImportOption func(*importConfig)
ImportOption configures an import from a payment provider.
func ImportInto ¶
func ImportInto(appID string) ImportOption
ImportInto files the imported record under appID. The provider interface has no per-app scoping and several apps can share one provider account, so the caller names the app. A record the provider itself files under a different app is refused with that entity's not-found error, and nothing is stored. An empty appID is the shared catalog for a feature, and the no-app scope for a plan, a subscription or an invoice.
type InvoiceOption ¶
type InvoiceOption func(*invoiceConfig)
InvoiceOption configures GenerateInvoice.
func ForPeriod ¶
func ForPeriod(start, end time.Time) InvoiceOption
ForPeriod makes GenerateInvoice bill a period the subscription has already had, not its current one: the period the lifecycle clock just rolled over, say, or one a catch-up skipped (OnSubscriptionRenewed lists them). The period must be the current one or one before it on the subscription's cadence, must have started, and must end after the subscription was created; anything else is refused with ErrInvalidInput. A period a resume stretched counts with its stretched end, and the walk crosses it (see periodBelongsTo); a period from before an older stretch, which Ledger no longer remembers, is refused. A paused subscription's current period is not finished, so it cannot be invoiced until it is resumed and its end passes. A second live invoice for the same period is refused with ErrAlreadyExists, as for the current period. Nothing calls this on its own: Ledger never bills a period automatically.
Only the usage is the period's own. The base fee, the seats, the coupons and the prices come from the subscription and its plan as they are now, because ChangePlan leaves the period alone and Ledger keeps no history of any of them. A plan whose billing period changed no longer lines up with the periods before the change, and those are refused.
type Ledger ¶
type Ledger struct {
// contains filtered or unexported fields
}
Ledger is the main billing engine.
func (*Ledger) ActivatePlan ¶
ActivatePlan makes a plan available for new subscriptions. The plan must validate first.
func (*Ledger) Advance ¶
Advance runs every lifecycle step once, as of now, in this order: it moves ended periods forward, enacts due cancellations, ends due trials, and marks overdue invoices past due. Periods go first so that a cancellation dated inside a missed period ends that period; the period step never moves past a cancellation. Cancellations go before trials so that a trial canceled before it ended is never announced as converted.
The background worker calls it on every tick. Call it yourself from your own scheduler with the worker turned off, or from a test with any now. It is safe to run on several replicas at once: every change is a conditional store write, and only the run whose write lands reports it and fires its hook. Each step keeps going past a row that fails; the error joins them.
A run that moves something, or fails, is traced as "run lifecycle" with a child span per step that wrote or failed. A run that moves nothing leaves no span, so a clock ticking every minute over an idle store adds no traces.
func (*Ledger) AdvancePeriods ¶
AdvancePeriods moves every active, trialing or past-due subscription whose period has ended into the period that contains now, in one write however many periods it missed, and never past a scheduled cancellation. Periods follow the plan's billing period (see billingPeriod); a plan billed "none" never rolls. OnSubscriptionRenewed fires once per subscription moved, with every period that ended in the move.
One catch-up walks at most maxCatchUpPeriods periods, a hundred years of months. A subscription further behind than that, whose period ended before any date this engine could have set, is left where it is and reported in the error on every run. It needs its period set by hand in the store; the clock never guesses how it got there.
func (*Ledger) ApplyCoupon ¶
func (l *Ledger) ApplyCoupon(ctx context.Context, subID id.SubscriptionID, code string) (*coupon.Coupon, error)
ApplyCoupon attaches a coupon to a subscription by code.
Every check that can fail runs before any write, so a rejected coupon leaves no application row and no incremented redemption count. The application row and the redemption count are written together through l.store.RedeemCoupon, which the store commits as one unit: a review of the previous two-write version found both of its assumptions false. A coupon read as under its cap could still be over its cap by the time the write landed, because the check and the write were two separate steps with no lock between them - a barrier test redeemed a coupon capped at 1 five times this way. And a row without a count turned out not to be recoverable either: a retry after a failure between the two writes saw the application row already there and returned ErrCouponAlreadyApplied, so the count never caught up. RedeemCoupon closes both gaps by enforcing the cap with a conditional increment inside the same unit that records the application, rather than a read followed by a write.
func (*Ledger) ArchiveFeature ¶
ArchiveFeature archives a catalog feature.
func (*Ledger) ArchivePlan ¶
ArchivePlan stops new subscriptions to a plan. Existing subscriptions keep billing. Archiving an archived plan does nothing.
func (*Ledger) CancelSubscription ¶
func (l *Ledger) CancelSubscription(ctx context.Context, subID id.SubscriptionID, immediately bool) error
CancelSubscription cancels a subscription, now or at the end of its current period. A subscription that is already canceled or expired is refused, so a cancel on it can neither move its cancel_at nor announce the cancellation again. One that is still running can be canceled again while a scheduled cancel waits: a scheduled cancel sets cancel_at to the current period end once more, and an immediate one stops it now.
OnSubscriptionCanceled fires only when the subscription stopped now. A cancel dated later fires OnSubscriptionCancelScheduled, and the lifecycle clock fires OnSubscriptionCanceled when it enacts it. That holds even when the period it ends is already over: the subscription keeps running until the clock's next run cancels it as of that period's end.
func (*Ledger) ChangePlan ¶
func (l *Ledger) ChangePlan(ctx context.Context, subID id.SubscriptionID, planID id.PlanID, quantity map[string]int64) (*subscription.Subscription, error)
ChangePlan moves a subscription to another active plan in the same app. The change takes effect from the next invoice; nothing is prorated. A nil quantity keeps the current seat counts.
func (*Ledger) Check ¶
Check answers the same question as Entitled, from the same cache, plans and quotas, but it does not turn a store failure into "no access". A subscription lookup that fails with anything other than ErrNoActiveSubscription or ErrSubscriptionNotFound, and a usage aggregate that fails, return a nil result and an error that wraps ErrUnavailable (and the store's own error), so a caller can fail open or closed on purpose. A failure caused by the caller's own cancelled or expired context comes back as that context error, not as ErrUnavailable.
func (*Ledger) CreateCoupon ¶
CreateCoupon validates and stores a new coupon. A code is unique within an app. A new coupon's redemption count is always zero.
func (*Ledger) CreateFeature ¶
CreateFeature creates a new catalog feature.
func (*Ledger) CreatePlan ¶
CreatePlan validates and stores a new billing plan. It normalises the plan first (see normalisePlan), and refuses a slug the app already uses with ErrAlreadyExists.
func (*Ledger) CreateSubscription ¶
func (l *Ledger) CreateSubscription(ctx context.Context, sub *subscription.Subscription) error
CreateSubscription creates a new subscription.
A subscription must name its tenant. Every store reads an empty tenant id as "every tenant", so one without a tenant would later be billed for other customers' usage. It is refused with an error wrapping ErrInvalidInput and nothing is written. The app id may stay empty: deployments that never set one still bill their base fee.
func (*Ledger) DeleteCoupon ¶
DeleteCoupon removes a coupon. Subscriptions it was applied to stop receiving its discount on their next invoice.
func (*Ledger) DeleteFeature ¶
DeleteFeature deletes a catalog feature.
func (*Ledger) DeletePlan ¶
DeletePlan removes a plan no subscription references, in any status. A plan with subscriptions is archived instead, because invoicing reads the plan.
func (*Ledger) EnactCancels ¶
EnactCancels cancels every subscription whose cancel_at is at or before now and that is not already canceled or expired, paused ones included, and sets canceled_at to cancel_at. OnSubscriptionCanceled fires for each, with the row read back after the write: an operator's scheduled cancel landing between the list and the write moves cancel_at, and the stored canceled_at is the one that counts.
A running subscription whose period ended before its cancel_at is caught up first, as AdvancePeriods would, so the cancel ends the period it falls in and the periods before it are announced. Inside one Advance the period step has already done that; this covers another replica's cancel step landing between this replica's period list and its write, and a caller running EnactCancels on its own. The advance is conditional like any other, so a row another replica already moved is left alone.
func (*Ledger) EndTrials ¶
EndTrials makes every trialing subscription whose trial_end is at or before now active. A paused subscription's trial is left alone. OnSubscriptionTrialEnded fires for each.
func (*Ledger) Entitled ¶
Entitled checks if the current tenant can use a feature.
It runs on most requests, so its span is only started under a caller's span: a check made outside any trace does not begin one.
A store that cannot answer reads as no access here: a failed subscription lookup comes back as "no active subscription" with a nil error. Use Check when you need to tell an outage from a real no.
func (*Ledger) ExportInvoice ¶
func (l *Ledger) ExportInvoice(ctx context.Context, invID id.InvoiceID, format string) ([]byte, error)
ExportInvoice renders an invoice through the formatter registered for format.
func (*Ledger) FinalizeInvoice ¶
FinalizeInvoice transitions an invoice from draft to pending.
func (*Ledger) GenerateInvoice ¶
func (l *Ledger) GenerateInvoice(ctx context.Context, subID id.SubscriptionID, opts ...InvoiceOption) (inv *invoice.Invoice, err error)
GenerateInvoice generates an invoice for a subscription period.
It assembles line items in a fixed order: the base fee, then metered usage overage per feature in plan order, then seat charges per seat feature in plan order, then one discount line per applied coupon, then one tax line. Every Money the function builds is normalised to one lowercased currency taken from the plan, so two values built from different casings of the same currency (e.g. "USD" and "usd") never trip Money.Add/Subtract's case-sensitive mismatch panic. Tax is computed on the net amount (subtotal less discounts, clamped at zero), not the gross subtotal, so a discounted invoice is never taxed on money the customer was never charged.
Every sum it builds (subtotal, discount, net, tax and total) is checked. An amount that overflows an int64 fails generation with an error wrapping types.ErrOverflow that names the stage, and nothing is stored: a wrapped figure must never become an invoice.
A subscription with an empty tenant id is refused with an error wrapping ErrInvalidInput before anything else is read for it. Every store reads an empty tenant as "every tenant", so billing one would total other customers' usage. An empty app id is allowed and still bills the base fee; only a feature totalled by a plugin aggregator refuses it (see aggregateUsage).
Usage is always the billed period's own: the events from its start to its end, or to now while it is still running (see usageWindow). That holds for the current period and for one named with ForPeriod, so two invoices for consecutive periods never count the same event. A feature's reset period (plan.Feature.Period) plays no part in billing; it shapes quota checks alone.
With ForPeriod it bills a period the subscription has already had instead of its current one. Only usage is historical: the plan, its prices, the seat counts and the coupons are all read as they are now, because Ledger keeps no history of them and ChangePlan leaves the period alone.
func (*Ledger) GetActiveSubscription ¶
func (l *Ledger) GetActiveSubscription(ctx context.Context, tenantID, appID string) (*subscription.Subscription, error)
GetActiveSubscription retrieves the active subscription for a tenant.
func (*Ledger) GetFeature ¶
GetFeature retrieves a catalog feature by ID.
func (*Ledger) GetFeatureByKey ¶
GetFeatureByKey retrieves a catalog feature by key and app scope.
func (*Ledger) GetPlanBySlug ¶
GetPlanBySlug retrieves a plan by slug.
func (*Ledger) GetSubscription ¶
func (l *Ledger) GetSubscription(ctx context.Context, subID id.SubscriptionID) (*subscription.Subscription, error)
GetSubscription retrieves a subscription by ID.
func (*Ledger) HandleWebhook ¶
func (l *Ledger) HandleWebhook(ctx context.Context, providerName string, payload []byte) (res *provider.WebhookResult, err error)
HandleWebhook routes an incoming webhook payload to the correct provider.
func (*Ledger) HasProviders ¶
HasProviders returns true if any payment provider plugins are registered.
func (*Ledger) ImportFeatureFromProvider ¶
func (l *Ledger) ImportFeatureFromProvider(ctx context.Context, providerName, providerID string, opts ...ImportOption) (_ *feature.Feature, err error)
ImportFeatureFromProvider pulls a catalog feature from the provider and creates it. A key the catalog already uses is refused with ErrAlreadyExists, which also refuses importing the same feature twice.
func (*Ledger) ImportInvoiceFromProvider ¶
func (l *Ledger) ImportInvoiceFromProvider(ctx context.Context, providerName, providerID string, opts ...ImportOption) (_ *invoice.Invoice, err error)
ImportInvoiceFromProvider pulls an invoice from the provider and stores it. Its subscription must already be this app's subscription for the invoice's tenant. It is refused with ErrAlreadyExists when the provider id is already stored, or when the subscription already has a live invoice for the same period: the rule GenerateInvoice keeps, because two would bill twice.
func (*Ledger) ImportPlanFromProvider ¶
func (l *Ledger) ImportPlanFromProvider(ctx context.Context, providerName, providerID string, opts ...ImportOption) (_ *plan.Plan, err error)
ImportPlanFromProvider pulls a plan from the provider and creates it through CreatePlan, so it is validated like any other plan and a slug the app already uses is refused with ErrAlreadyExists. That is also what refuses importing the same plan twice.
func (*Ledger) ImportSubscriptionFromProvider ¶
func (l *Ledger) ImportSubscriptionFromProvider(ctx context.Context, providerName, providerID string, opts ...ImportOption) (_ *subscription.Subscription, err error)
ImportSubscriptionFromProvider pulls a subscription from the provider and creates it through CreateSubscription. Its plan must already be a plan in the same app, so the plan is imported first. A provider id the tenant already has in the app is refused with ErrAlreadyExists.
func (*Ledger) InspectEntitlement ¶
func (l *Ledger) InspectEntitlement(ctx context.Context, tenantID, appID, featureKey string) (*entitlement.Result, error)
InspectEntitlement answers an entitlement question for a named tenant and app, fresh from the store, without touching the cache or firing plugin events. It is for operators looking at a customer, not for enforcement.
func (*Ledger) InvoiceFormats ¶
InvoiceFormats names every registered invoice formatter, sorted.
func (*Ledger) LifecycleInterval ¶
LifecycleInterval reports how often the lifecycle clock runs. Zero means it is off.
func (*Ledger) ListAppliedCoupons ¶
func (l *Ledger) ListAppliedCoupons(ctx context.Context, subID id.SubscriptionID) ([]*coupon.Coupon, error)
ListAppliedCoupons returns the coupons attached to a subscription, oldest application first.
func (*Ledger) ListFeatures ¶
func (l *Ledger) ListFeatures(ctx context.Context, appID string, opts feature.ListOpts) ([]*feature.Feature, error)
ListFeatures lists catalog features for an app.
func (*Ledger) ListGlobalFeatures ¶
func (l *Ledger) ListGlobalFeatures(ctx context.Context, opts feature.ListOpts) ([]*feature.Feature, error)
ListGlobalFeatures lists catalog features with no app scope.
func (*Ledger) ListPaymentMethods ¶
func (l *Ledger) ListPaymentMethods(ctx context.Context, tenantID string) (methods []provider.PaymentMethod, err error)
ListPaymentMethods lists payment methods from the provider for a tenant. Payment methods are always fetched live and never stored locally.
func (*Ledger) MarkInvoicePaid ¶
func (l *Ledger) MarkInvoicePaid(ctx context.Context, invID id.InvoiceID, paidAt time.Time, paymentRef string) error
MarkInvoicePaid marks an invoice as paid.
func (*Ledger) MarkInvoiceVoided ¶
MarkInvoiceVoided marks an invoice as voided with a reason.
func (*Ledger) MarkInvoicesPastDue ¶
MarkInvoicesPastDue moves every pending invoice whose due date is before now to past_due. The subscription is left as it is: its entitlements carry on. OnInvoicePastDue fires for each.
func (*Ledger) Meter ¶
Meter records a usage event (non-blocking).
The event is buffered and written by a background worker. If that write fails the event is dropped and counted. Use MeterWithKey when the event must survive a store outage and must not count twice. After Stop it returns an error wrapping ErrStoreClosed.
func (*Ledger) MeterWithKey ¶
func (l *Ledger) MeterWithKey(ctx context.Context, featureKey string, quantity int64, idempotencyKey string) error
MeterWithKey is Meter for an event that carries an idempotency key. The store counts a key once, so sending the same key again, from a retry of your own or a replay, adds nothing.
The key is unique across every tenant and every app, not within the tenant in the context. Every store dedupes on the key alone, so two tenants that send the same key have the second event dropped without an error. Namespace it yourself, for example "<tenant>/<app>/<message id>".
A key must be non-empty, valid UTF-8, free of NUL bytes and at most 255 bytes long, or the call returns ErrInvalidInput. A key a store would always reject would otherwise sit in the retry queue until Stop.
A keyed event whose write fails is not dropped. The worker keeps it and tries again with a growing wait (one second, doubling to thirty), ahead of newer events, until the store takes it or Stop makes a last attempt. While events are held they count against the buffer together with the events waiting in it, so a long outage ends in ErrMeterBufferFull from this method, not in unbounded memory.
func (*Ledger) PauseSubscription ¶
func (l *Ledger) PauseSubscription(ctx context.Context, subID id.SubscriptionID) (*subscription.Subscription, error)
PauseSubscription pauses an active or trialing subscription and records when, in paused_at. While it is paused the lifecycle clock leaves its period and its trial alone, so the billing cycle stands still until ResumeSubscription.
func (*Ledger) ProviderNames ¶
ProviderNames names every registered payment provider, sorted.
func (*Ledger) ResumeSubscription ¶
func (l *Ledger) ResumeSubscription(ctx context.Context, subID id.SubscriptionID) (*subscription.Subscription, error)
ResumeSubscription resumes a paused subscription. A subscription the lifecycle clock canceled while it was paused stays canceled.
A pause stops the billing clock, so the resume stretches the period by the length of the pause: current_period_end moves on by now minus paused_at, and current_period_start stays. The stretched period carries one base fee and is billed once its new end passes, like any other; the lifecycle clock lists it in Renewal.Ended then, and the periods after it follow the clock's anchor from its new end, usually that end's day of the month. A cancel scheduled for the period end moves with it. Ledger remembers the most recent stretch (Subscription.Stretch), so ForPeriod can still prove the periods on both sides of it.
A trial still running when the subscription was paused resumes as a trial, and its end moves on by the same length, so the customer gets the trial days they had left; the clock ends it, and fires OnSubscriptionTrialEnded, once that later date passes.
A subscription paused before Ledger recorded paused_at has no pause start. Its period and its trial end stay where they were, and it resumes as a trial only if that end is still ahead.
Everything is one conditional store write that lands only while the subscription is still paused and still carries the paused_at read here.
func (*Ledger) SyncFeatureToProvider ¶
func (l *Ledger) SyncFeatureToProvider(ctx context.Context, featureID id.FeatureID) (res *provider.SyncResult, err error)
SyncFeatureToProvider pushes a local feature to the payment provider.
func (*Ledger) SyncInvoiceToProvider ¶
func (l *Ledger) SyncInvoiceToProvider(ctx context.Context, invID id.InvoiceID) (res *provider.SyncResult, err error)
SyncInvoiceToProvider pushes a local invoice to the payment provider.
func (*Ledger) SyncPlanToProvider ¶
func (l *Ledger) SyncPlanToProvider(ctx context.Context, planID id.PlanID) (res *provider.SyncResult, err error)
SyncPlanToProvider pushes a local plan to the payment provider.
func (*Ledger) SyncSubscriptionToProvider ¶
func (l *Ledger) SyncSubscriptionToProvider(ctx context.Context, subID id.SubscriptionID) (res *provider.SyncResult, err error)
SyncSubscriptionToProvider pushes a local subscription to the payment provider.
func (*Ledger) UpdateCoupon ¶
UpdateCoupon saves the mutable fields of an existing coupon: name, redemption cap, validity window and metadata. The code, type, value, currency and app are fixed once created, because applied coupons are priced from them on every future invoice. The redemption count is never written here.
func (*Ledger) UpdateFeature ¶
UpdateFeature updates a catalog feature.
type LifecycleReport ¶
type LifecycleReport struct {
PeriodsAdvanced []id.SubscriptionID
CancelsEnacted []id.SubscriptionID
TrialsEnded []id.SubscriptionID
InvoicesPastDue []id.InvoiceID
}
LifecycleReport lists what one Advance run changed.
func (LifecycleReport) Empty ¶
func (r LifecycleReport) Empty() bool
Empty reports whether the run changed nothing.
type MultiError ¶
type MultiError struct {
Errors []error
}
MultiError represents multiple errors that occurred.
func (MultiError) Error ¶
func (e MultiError) Error() string
func (MultiError) HasErrors ¶
func (e MultiError) HasErrors() bool
HasErrors returns true if there are any errors.
type Option ¶
type Option func(*Ledger)
Option configures a Ledger instance.
func WithClock ¶
WithClock sets where the engine reads the time for its decisions. Tests use it. A nil clock keeps time.Now.
These follow the clock: the lifecycle worker's Advance, a new subscription's creation time, first period and trial, the periods GenerateInvoice will bill, coupon validity, and an invoice's due date.
These stay on the wall clock: UpdatedAt and other change stamps, the timestamp on a metered usage event, the moment an immediate cancel or a void is recorded, the store's own usage windows, and the expiry of the entitlement cache.
func WithEntitlementCacheTTL ¶
WithEntitlementCacheTTL sets the entitlement cache TTL.
func WithLifecycleInterval ¶
WithLifecycleInterval sets how often the lifecycle clock runs Advance in the background (default: one minute). Zero or less turns the clock off, for a deployment that calls Advance from its own scheduler.
func WithMeterConfig ¶
WithMeterConfig configures metering parameters.
func WithMetrics ¶
WithMetrics records the engine's metrics on m: lifecycle runs and transitions, metered events, entitlement checks, invoice generation, payment provider calls and plugin hook failures. In a Forge app pass app.Metrics(); the Forge extension does that for you. A nil m, the default, records nothing.
Spans need no option. The engine starts them on the global OpenTelemetry tracer provider, which forge.New installs.
func WithoutMigrate ¶
func WithoutMigrate() Option
WithoutMigrate makes Start skip the store migration and start only the background workers, for a deployment that migrates its schema separately.
type ValidationError ¶
ValidationError represents a validation failure with details.
func (ValidationError) Error ¶
func (e ValidationError) Error() string
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package audithook bridges Ledger lifecycle events to an audit trail backend.
|
Package audithook bridges Ledger lifecycle events to an audit trail backend. |
|
Package extension provides the Forge extension adapter for Ledger.
|
Package extension provides the Forge extension adapter for Ledger. |
|
contract
Package contract wires Ledger into the Forge dashboard's contract path.
|
Package contract wires Ledger into the Forge dashboard's contract path. |
|
Package feature provides standalone, reusable feature definitions for the Ledger billing engine.
|
Package feature provides standalone, reusable feature definitions for the Ledger billing engine. |
|
Package id defines TypeID-based identity types for all Ledger entities.
|
Package id defines TypeID-based identity types for all Ledger entities. |
|
internal
|
|
|
telemetry
Package telemetry is the tracing and metrics plumbing the engine and the plugin registry share.
|
Package telemetry is the tracing and metrics plumbing the engine and the plugin registry share. |
|
telemetrytest
Package telemetrytest holds what ledger's tests use to see its telemetry: a span recorder installed as the global tracer provider, and a metrics sink that keeps every series it hands out.
|
Package telemetrytest holds what ledger's tests use to see its telemetry: a span recorder installed as the global tracer provider, and a metrics sink that keeps every series it hands out. |
|
Package observability provides a metrics extension for Ledger that records lifecycle event counts via go-utils MetricFactory.
|
Package observability provides a metrics extension for Ledger that records lifecycle event counts via go-utils MetricFactory. |
|
Package plugin provides an extensible plugin system for Ledger.
|
Package plugin provides an extensible plugin system for Ledger. |
|
Package provider defines the contract that payment provider plugins (Stripe, Braintree, etc.) must implement.
|
Package provider defines the contract that payment provider plugins (Stripe, Braintree, etc.) must implement. |
|
mongo
Package mongo provides a MongoDB Store implementation for Ledger using Grove ORM.
|
Package mongo provides a MongoDB Store implementation for Ledger using Grove ORM. |
|
postgres
Package postgres provides a PostgreSQL Store implementation for Ledger using Grove ORM.
|
Package postgres provides a PostgreSQL Store implementation for Ledger using Grove ORM. |
|
sqlite
Package sqlite provides a SQLite Store implementation for Ledger using Grove ORM.
|
Package sqlite provides a SQLite Store implementation for Ledger using Grove ORM. |
|
storetest
Package storetest provides a conformance suite every store.Store implementation must pass, plus constructors for each backend.
|
Package storetest provides a conformance suite every store.Store implementation must pass, plus constructors for each backend. |
|
Package types provides common types used across Ledger.
|
Package types provides common types used across Ledger. |