The multi-tenant edition's plans (Free and Paid as decided, or a plans file) and each team's billing account, with a 14-day trial of Paid. A team's plan caps what its people and agents may change: modules outside it stay readable, a lapsed team changes nothing, and agents, Market entries, crawls and background jobs keep to it. Without a Stripe key, no billing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
4.7 KiB
Go
151 lines
4.7 KiB
Go
// Package billing is the multi-tenant edition's plans and each team's
|
|
// subscription to one, through the platform's own Stripe account (Stripe
|
|
// Billing): Checkout to subscribe, the Customer Portal to pay and see
|
|
// invoices, webhooks to follow the subscription. Separate from a team's own
|
|
// Stripe connection, which the Metrics module reads.
|
|
package billing
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Plan is what a team may do.
|
|
type Plan struct {
|
|
ID string `yaml:"-"`
|
|
Name string `yaml:"name"`
|
|
Modules []string `yaml:"modules"` // "all", or module IDs
|
|
// Seats: how many people; 0 on a paid plan means as many as are paid
|
|
// for (Stripe's quantity).
|
|
Seats int `yaml:"seats"`
|
|
Agents int `yaml:"agents"` // not counted as people
|
|
// Storage: StorageGB for the team, or StorageGBPerSeat for each paid
|
|
// person, pooled; more in blocks of StorageBlockGB.
|
|
StorageGB int `yaml:"storage_gb"`
|
|
StorageGBPerSeat int `yaml:"storage_gb_per_seat"`
|
|
StorageBlockGB int `yaml:"storage_block_gb"`
|
|
CrawlPages int `yaml:"crawl_pages"`
|
|
CrawlEvery string `yaml:"crawl_every"` // daily or weekly
|
|
Sources int `yaml:"sources"` // Market entries watched; 0: no limit beyond fair use
|
|
|
|
// Stripe's prices: per person a month, and (if offered) a year; and
|
|
// per extra storage block a month.
|
|
PriceMonth string `yaml:"price_month"`
|
|
PriceYear string `yaml:"price_year"`
|
|
PriceStorageBlock string `yaml:"price_storage_block"`
|
|
PerSeatCents int `yaml:"per_seat_cents"` // to show: 1500 is $15
|
|
}
|
|
|
|
// Paid says whether the plan is paid for.
|
|
func (p *Plan) Paid() bool { return p.PriceMonth != "" || p.PriceYear != "" || p.PerSeatCents > 0 }
|
|
|
|
// Includes says whether a module is in the plan.
|
|
func (p *Plan) Includes(module string) bool {
|
|
for _, m := range p.Modules {
|
|
if m == "all" || m == module {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// CrawlInterval is how often a site is audited.
|
|
func (p *Plan) CrawlInterval() time.Duration {
|
|
if p.CrawlEvery == "daily" {
|
|
return 24 * time.Hour
|
|
}
|
|
return 7 * 24 * time.Hour
|
|
}
|
|
|
|
// StorageBytes is the plan's storage for a team with seats paid people
|
|
// and blocks extra blocks.
|
|
func (p *Plan) StorageBytes(seats, blocks int) int64 {
|
|
gb := p.StorageGB + p.StorageGBPerSeat*seats + p.StorageBlockGB*blocks
|
|
return int64(gb) << 30
|
|
}
|
|
|
|
// Plans are the platform's.
|
|
type Plans struct {
|
|
Plans map[string]*Plan `yaml:"plans"`
|
|
Free string `yaml:"free"` // the plan a team drops to
|
|
Paid string `yaml:"paid"` // the plan a trial is of
|
|
TrialDays int `yaml:"trial_days"` // a new team's trial of Paid
|
|
Tax bool `yaml:"tax"` // Stripe Tax on Checkout (automatic_tax)
|
|
}
|
|
|
|
// Defaults are the plans decided on 2026-11-25 (PLAN.md, Phase 14), without
|
|
// Stripe's price IDs, which come from SUPERMARKET_PLANS or the environment.
|
|
func Defaults() *Plans {
|
|
return &Plans{
|
|
Free: "free", Paid: "paid", TrialDays: 14,
|
|
Plans: map[string]*Plan{
|
|
"free": {ID: "free", Name: "Free", Modules: []string{"content", "seo", "market"}, Seats: 2, Agents: 1,
|
|
StorageGB: 1, CrawlPages: 500, CrawlEvery: "weekly", Sources: 10},
|
|
"paid": {ID: "paid", Name: "Paid", Modules: []string{"all"}, Agents: 5,
|
|
StorageGBPerSeat: 5, StorageBlockGB: 25, CrawlPages: 10000, CrawlEvery: "daily", PerSeatCents: 1500},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Load reads the plans: a YAML file (SUPERMARKET_PLANS), or the defaults.
|
|
// priceMonth, priceYear and priceBlock fill the paid plan's Stripe prices
|
|
// when the file doesn't.
|
|
func Load(path, priceMonth, priceYear, priceBlock string) (*Plans, error) {
|
|
p := Defaults()
|
|
if path != "" {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var f Plans
|
|
if err := yaml.Unmarshal(raw, &f); err != nil {
|
|
return nil, fmt.Errorf("the plans file: %w", err)
|
|
}
|
|
if len(f.Plans) > 0 {
|
|
p.Plans = f.Plans
|
|
}
|
|
if f.Free != "" {
|
|
p.Free = f.Free
|
|
}
|
|
if f.Paid != "" {
|
|
p.Paid = f.Paid
|
|
}
|
|
if f.TrialDays != 0 {
|
|
p.TrialDays = f.TrialDays
|
|
}
|
|
p.Tax = f.Tax
|
|
}
|
|
for id, pl := range p.Plans {
|
|
pl.ID = id
|
|
if len(pl.Modules) == 0 {
|
|
return nil, fmt.Errorf("plan %s has no modules", id)
|
|
}
|
|
}
|
|
paid, free := p.Plans[p.Paid], p.Plans[p.Free]
|
|
if paid == nil || free == nil {
|
|
return nil, errors.New("the plans need a free plan and a paid one (free:, paid:)")
|
|
}
|
|
if paid.PriceMonth == "" {
|
|
paid.PriceMonth = priceMonth
|
|
}
|
|
if paid.PriceYear == "" {
|
|
paid.PriceYear = priceYear
|
|
}
|
|
if paid.PriceStorageBlock == "" {
|
|
paid.PriceStorageBlock = priceBlock
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// Get is a plan by ID, or the free one.
|
|
func (p *Plans) Get(id string) *Plan {
|
|
if pl, ok := p.Plans[id]; ok {
|
|
return pl
|
|
}
|
|
return p.Plans[p.Free]
|
|
}
|