Phase 14, part 4: what happens when it lapses
An hourly platform job: a trial over without a subscription goes to Free; past due, the owner is told and every page has a banner; at 14 days read-only; at 53 days everyone is warned; at 60 days the team is deleted with 30 days to undo. Each email once. Undoing a deletion for not paying gives a week more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/appmail"
|
||||
"supermarket/internal/audit"
|
||||
"supermarket/internal/billing"
|
||||
"supermarket/internal/users"
|
||||
@@ -245,3 +246,136 @@ func (s *Server) stripeWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
// Lapses: an hourly look at every team's account (the platform's job).
|
||||
// - a trial ended without a subscription: the Free plan, and the owner told
|
||||
// - past due: the owner told (and a banner, lapseBanner)
|
||||
// - past due 14 days: read-only (billing.Effective), and the owner told
|
||||
// - past due 53 days: everyone told the team goes in a week
|
||||
// - past due 60 days: deleted as an owner's deletion is (30 days to undo)
|
||||
|
||||
const warnBeforeDelete = 7 * 24 * time.Hour
|
||||
|
||||
func (s *Server) billingTurn(ctx context.Context, now time.Time) {
|
||||
if !s.billingOn() {
|
||||
return
|
||||
}
|
||||
accts, err := billing.All(s.platform())
|
||||
if err != nil {
|
||||
s.Log.Error("billing: listing accounts", "err", err)
|
||||
return
|
||||
}
|
||||
for _, a := range accts {
|
||||
t, err := users.GetTeam(s.platform(), a.Team)
|
||||
if err != nil || t.Status == users.TeamDeleting {
|
||||
continue
|
||||
}
|
||||
ts, err := s.forTeam(a.Team)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
note := func(action, detail string) {
|
||||
ts.record(audit.Entry{Kind: audit.System, Action: action, Target: t.Name, Detail: detail})
|
||||
s.Log.Info("billing: "+action, "team", t.ID)
|
||||
}
|
||||
save := func() {
|
||||
if err := billing.Save(s.platform(), a, now); err != nil {
|
||||
s.Log.Error("billing: saving", "team", a.Team, "err", err)
|
||||
}
|
||||
}
|
||||
owner := ts.ownerOf()
|
||||
switch {
|
||||
case a.Status == billing.Trialing && a.Subscription == "" && !now.Before(a.TrialEnds):
|
||||
a.Status = billing.Free
|
||||
save()
|
||||
note("trial-ended", "now on the Free plan")
|
||||
ts.mailOwner(owner, "Your Supermarket trial has ended", "The trial of "+s.Plans.Get(s.Plans.Paid).Name+" for "+t.Name+
|
||||
" has ended, so the team is on the Free plan now: what's outside it stays, to read. To keep everything, subscribe:\n"+s.PublicURL+"/settings/billing")
|
||||
case a.Status == billing.PastDue && !a.PastDueSince.IsZero():
|
||||
late := now.Sub(a.PastDueSince)
|
||||
switch {
|
||||
case late >= billing.DeleteAfter && a.Warned != "deleting":
|
||||
a.Warned = "deleting"
|
||||
save()
|
||||
when := now.Add(DeleteAfter)
|
||||
if err := users.SetTeamStatus(s.platform(), t.ID, users.TeamDeleting, when); err != nil {
|
||||
s.Log.Error("billing: deleting a lapsed team", "team", t.ID, "err", err)
|
||||
continue
|
||||
}
|
||||
note("team-deleting", "the payment has been due 60 days")
|
||||
s.tellTeam(t, t.Name+" is being deleted from Supermarket", "The payment for "+t.Name+" has been due for 60 days, so the team is being deleted. Its data goes for good on "+
|
||||
when.Format("2 January 2006")+". Until then, its owner can undo it when signing in, then pay under Settings → Billing.")
|
||||
case late >= billing.DeleteAfter-warnBeforeDelete && late < billing.DeleteAfter && a.Warned != "deleting-soon" && a.Warned != "deleting":
|
||||
a.Warned = "deleting-soon"
|
||||
save()
|
||||
note("lapse-warned", "deleted in a week")
|
||||
s.tellTeam(t, t.Name+" will be deleted in a week", "The payment for "+t.Name+" has been due since "+a.PastDueSince.Format("2 January")+
|
||||
". Unless it's paid, the team will be deleted in a week. Its owner can pay under Settings → Billing:\n"+s.PublicURL+"/settings/billing")
|
||||
case late >= billing.ReadOnlyAfter && late < billing.DeleteAfter-warnBeforeDelete && a.Warned != "read-only":
|
||||
a.Warned = "read-only"
|
||||
save()
|
||||
note("lapse-read-only", "the payment has been due 14 days")
|
||||
ts.mailOwner(owner, t.Name+" is read-only until it's paid", "The payment for "+t.Name+" has been due for 14 days, so the team is read-only: nothing is fetched, sent or published, and agents are refused. Pay under Settings → Billing to carry on:\n"+s.PublicURL+"/settings/billing")
|
||||
case late < billing.ReadOnlyAfter && a.Warned == "":
|
||||
a.Warned = "past-due"
|
||||
save()
|
||||
note("lapse-due", "a payment failed")
|
||||
ts.mailOwner(owner, "A payment for "+t.Name+" failed", "Stripe couldn't take the payment for "+t.Name+
|
||||
". Update the card under Settings → Billing within 14 days, or the team becomes read-only:\n"+s.PublicURL+"/settings/billing")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ownerOf is the team's owner.
|
||||
func (s *Server) ownerOf() *users.User {
|
||||
list, err := users.Members(s.platform(), s.team())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
for _, m := range list {
|
||||
if m.Role == users.Owner {
|
||||
return m.User
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) mailOwner(u *users.User, subject, body string) {
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
s.sendMail(appmail.Message{To: u.Email, ToName: u.Name, Subject: subject, Body: "Hello" + nameSuffix(u.Name) + ",\n\n" + body + "\n"})
|
||||
}
|
||||
|
||||
// lapseBanner is what every page of a team behind on its payment says.
|
||||
func (s *Server) lapseBanner() string {
|
||||
if !s.billingOn() || s.team() == 0 {
|
||||
return ""
|
||||
}
|
||||
a, err := billing.Get(s.platform(), s.team())
|
||||
if err != nil || a.Status != billing.PastDue || a.PastDueSince.IsZero() {
|
||||
return ""
|
||||
}
|
||||
if _, ro := a.Effective(s.Plans, time.Now()); ro {
|
||||
return "The team's payment has been due since " + a.PastDueSince.Local().Format("2 Jan") + ", so it's read-only: nothing is fetched, sent or published. Its owner can pay under Settings → Billing."
|
||||
}
|
||||
return "The team's payment is due since " + a.PastDueSince.Local().Format("2 Jan") + ". Its owner can pay under Settings → Billing; after 14 days the team becomes read-only."
|
||||
}
|
||||
|
||||
// lapseUndone: a team deleted for not paying, brought back by its owner,
|
||||
// has a week to pay before it's deleted again.
|
||||
func (s *Server) lapseUndone(team int64, now time.Time) {
|
||||
if !s.billingOn() {
|
||||
return
|
||||
}
|
||||
a, err := billing.Get(s.platform(), team)
|
||||
if err != nil || a.Status != billing.PastDue || a.Warned != "deleting" {
|
||||
return
|
||||
}
|
||||
a.PastDueSince = now.Add(-billing.DeleteAfter + warnBeforeDelete)
|
||||
a.Warned = "read-only" // the next turn warns of the week
|
||||
if err := billing.Save(s.platform(), a, now); err != nil {
|
||||
s.Log.Error("billing: saving", "team", team, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user