Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/PRICING_EXPERIMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Weekly billing at $1 to $2 gives away a fifth to a third of revenue before hosti

Card required at trial start (Stripe `trial_period_days`) keeps trial abuse and support load down. A no-card trial can be a fourth variant if A shows low trial starts.

The checkout code supports variant A. Set `STRIPE_TRIAL_DAYS` (1 to 90) and new Pro checkouts get that many free days, with the card collected up front. Unset or invalid means no trial, which is the control. The page copy does not mention a trial yet, so change it in the same release that sets the variable.

## Hypotheses and the numbers to record

Fill each cell from real traffic. Do not publish targets or results until they are measured.
Expand Down
8 changes: 8 additions & 0 deletions governance/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ type StripeConfig struct {
// Stripe handles indirect tax, fraud, and transaction support. The account
// must be enrolled or Stripe rejects the session.
ManagedPayments bool `json:"managed_payments"`
// TrialDays gives new Pro subscribers a free trial of this many days before
// the first charge. 0 (the default) means no trial. Checkout still collects
// a card, and Stripe bills it when the trial ends.
TrialDays int `json:"trial_days"`
}

// managedPaymentsAPIVersion is the Stripe API version required to create
Expand Down Expand Up @@ -497,6 +501,10 @@ func (s *DefaultBillingService) CreateCheckoutSession(ctx context.Context, tenan
data.Set("metadata[tenant_id]", tenantID)
data.Set("metadata[tier]", string(tier))

if s.cfg.TrialDays > 0 && tier == TierPro {
data.Set("subscription_data[trial_period_days]", strconv.Itoa(s.cfg.TrialDays))
}

if email != "" {
data.Set("customer_email", email)
}
Expand Down
24 changes: 24 additions & 0 deletions governance/billing_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,27 @@ func TestCheckoutSession_WithoutManagedPaymentsStillRestrictsToCards(t *testing.
t.Fatalf("managed payments leaked into a normal session: %v version=%q", form, version)
}
}

func TestCheckoutSession_TrialOffByDefaultAndSetForPro(t *testing.T) {
run := func(days int) url.Values {
cfg := liveConfig()
cfg.TrialDays = days
svc := NewDefaultBillingService(cfg, NewFeatureGate(TierCore))
cap := &captureTransport{}
svc.httpClient = &http.Client{Transport: cap}
if _, err := svc.CreateCheckoutSession(context.Background(), "t1", "a@example.com", TierPro); err != nil {
t.Fatalf("CreateCheckoutSession: %v", err)
}
form, err := url.ParseQuery(cap.body)
if err != nil {
t.Fatal(err)
}
return form
}
if f := run(0); f.Has("subscription_data[trial_period_days]") {
t.Fatalf("no trial expected by default: %v", f)
}
if got := run(14).Get("subscription_data[trial_period_days]"); got != "14" {
t.Fatalf("trial_period_days = %q, want 14", got)
}
}
7 changes: 7 additions & 0 deletions tools/httpserver/billing_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -112,6 +113,11 @@ func loadStripeConfig(getenv func(string) string) governance.StripeConfig {
simulateStr := strings.ToLower(getenv("STRIPE_SIMULATE"))
simulate := simulateStr == "true" || simulateStr == "1" || secretKey == ""
managedStr := strings.ToLower(getenv("STRIPE_MANAGED_PAYMENTS"))
// STRIPE_TRIAL_DAYS: free trial length for Pro, 1 to 90. Anything else means no trial.
trialDays, _ := strconv.Atoi(strings.TrimSpace(getenv("STRIPE_TRIAL_DAYS")))
if trialDays < 1 || trialDays > 90 {
trialDays = 0
}

// Stripe requires absolute return URLs. JOLTRIN_PUBLIC_URL (the
// site's public origin, no trailing slash) builds them; without it
Expand All @@ -133,6 +139,7 @@ func loadStripeConfig(getenv func(string) string) governance.StripeConfig {
CancelURL: firstNonEmpty(getenv("STRIPE_CANCEL_URL"), cancelDefault),
Simulate: simulate,
ManagedPayments: managedStr == "true" || managedStr == "1",
TrialDays: trialDays,
}
}

Expand Down
9 changes: 9 additions & 0 deletions tools/httpserver/billing_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,15 @@ func TestLoadStripeConfig_ManagedPayments(t *testing.T) {
}
}

func TestLoadStripeConfig_TrialDays(t *testing.T) {
for in, want := range map[string]int{"": 0, "14": 14, " 7 ": 7, "0": 0, "-3": 0, "91": 0, "abc": 0, "90": 90} {
got := loadStripeConfig(envFrom(map[string]string{"STRIPE_TRIAL_DAYS": in})).TrialDays
if got != want {
t.Errorf("STRIPE_TRIAL_DAYS=%q gave %d, want %d", in, got, want)
}
}
}

var inquiryClient int32

func postInquiry(t *testing.T, origin string, body []byte) *httptest.ResponseRecorder {
Expand Down
Loading