console: a billing-free account deletes while billing isn't ready (plans 6b branch review)
While billing isn't ready the retirement gate refuses (delete.unverified) only an account with billing state it would act on or judge: a bound customer or customer-create attempt, a recorded subscription, a non-terminal Checkout intent, or an outstanding op (hasBillingState). An account with none deletes as it does with billing off, with no provider call.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+70
−10
internal/console/billingready_test.go+31 −6internal/console/billingretire.go+39 −4
diff --git a/internal/console/billingready_test.go b/internal/console/billingready_test.go| index e4e4abe30..734a5861a 100644 |
| --- a/internal/console/billingready_test.go |
| +++ b/internal/console/billingready_test.go |
| @@ -150,17 +150,42 @@ func TestRemovalWhileNotReadyRemovesAndSendsNothing(t *testing.T) { |
| } |
| } |
| +// TestDeleteRefusedWhileNotReady: an account with billing state the gate |
| +// would act on or judge is refused with no provider request. |
| func TestDeleteRefusedWhileNotReady(t *testing.T) { |
| - o := newDeleteFixture(t, nil, false) |
| + t.Run("recorded subscription", func(t *testing.T) { |
| + o := newDeleteFixture(t, nil, false) |
| + o.stepUp(t) |
| + o.notReady() |
| + o.wantDeleteRefused(t, o.deleteAccount(t), billingCopy["delete.unverified"]) |
| + wantCounts(t, o.counts(), map[string]int{}) |
| + // Ready again, the same deletion goes through. |
| + o.srv.billing.ready.Store(true) |
| + o.stepUp(t) |
| + r := o.deleteAccount(t) |
| + o.wantDeleted(t, r.Status, r.FlashMsg) |
| + }) |
| + t.Run("bound customer only", func(t *testing.T) { |
| + o := newCheckoutFixture(t, true) |
| + o.deletable(t) |
| + o.stepUp(t) |
| + o.notReady() |
| + o.wantDeleteRefused(t, o.deleteAccount(t), billingCopy["delete.unverified"]) |
| + wantCounts(t, o.counts(), map[string]int{}) |
| + }) |
| +} |
| + |
| +// TestBillingFreeAccountDeletesWhileNotReady: an account with no billing |
| +// state has nothing at the provider, so it deletes as with billing off, with |
| +// no provider request. |
| +func TestBillingFreeAccountDeletesWhileNotReady(t *testing.T) { |
| + o := newCheckoutFixture(t, false) |
| + o.deletable(t) |
| o.stepUp(t) |
| o.notReady() |
| - o.wantDeleteRefused(t, o.deleteAccount(t), billingCopy["delete.unverified"]) |
| - o.wantNoSend(t) |
| - // Ready again, the same deletion goes through. |
| - o.srv.billing.ready.Store(true) |
| - o.stepUp(t) |
| r := o.deleteAccount(t) |
| o.wantDeleted(t, r.Status, r.FlashMsg) |
| + wantCounts(t, o.counts(), map[string]int{}) |
| } |
| // TestCheckoutSendersSendNothingWhileNotReady: the customer create, the |
diff --git a/internal/console/billingretire.go b/internal/console/billingretire.go| index 9302bd66c..e22ae4985 100644 |
| --- a/internal/console/billingretire.go |
| +++ b/internal/console/billingretire.go |
| @@ -59,8 +59,8 @@ func (s *Store) OutstandingOpsForAccount(accountID int64) ([]billingOp, error) { |
| } |
| // retireGateLocked decides, under the hold, whether the account may be |
| -// deleted. While billing is not ready it makes no request and refuses |
| -// (delete.unverified). The order is load-bearing: the only provider writes |
| +// deleted. While billing is not ready it makes no request, and refuses |
| +// (delete.unverified) an account with billing state (hasBillingState). The order is load-bearing: the only provider writes |
| // are steps 1 and 2, and step 3 always runs after them, so what is judged is |
| // what those writes left. |
| // |
| @@ -80,9 +80,18 @@ func (s *Store) OutstandingOpsForAccount(accountID int64) ([]billingOp, error) { |
| // collected and no change waiting for payment (delete.collecting). |
| func (s *Server) retireGateLocked(h billingHold, ctx context.Context, acct *Account, now time.Time) error { |
| // Not ready: the gate can neither finish work at the provider nor trust |
| - // what it would judge, so nothing is checked and deletion is refused. |
| + // what it would judge. An account with billing state to act on or judge |
| + // is refused with no request; one with none has nothing at the provider |
| + // and deletes as it would with billing off. |
| if err := s.requireBillingReady(); err != nil { |
| - return errDeleteUnverified |
| + state, err := s.hasBillingState(acct) |
| + if err != nil { |
| + return err |
| + } |
| + if state { |
| + return errDeleteUnverified |
| + } |
| + return nil |
| } |
| // 1. Outstanding work. |
| ops, err := s.store.OutstandingOpsForAccount(acct.ID) |
| @@ -160,6 +169,32 @@ func (s *Server) retireGateLocked(h billingHold, ctx context.Context, acct *Acco |
| return nil |
| } |
| +// hasBillingState is whether the account has anything the retirement gate |
| +// would act on or judge at the provider: a bound customer or a customer |
| +// create attempt, a recorded subscription, a Checkout intent that is not |
| +// history, or an outstanding operation. |
| +func (s *Server) hasBillingState(acct *Account) (bool, error) { |
| + b, err := s.store.BillingAccount(acct.ID) |
| + if err != nil { |
| + return false, err |
| + } |
| + if b != nil && (b.Customer != "" || b.CustomerPrepared != nil || b.Subscription != "") { |
| + return true, nil |
| + } |
| + ci, err := s.store.CheckoutIntent(acct.ID) |
| + if err != nil { |
| + return false, err |
| + } |
| + if ci != nil && !intentTerminal(ci) { |
| + return true, nil |
| + } |
| + ops, err := s.store.OutstandingOpsForAccount(acct.ID) |
| + if err != nil { |
| + return false, err |
| + } |
| + return len(ops) > 0, nil |
| +} |
| + |
| // retireForDeletion is account deletion's billing step, after the owner, |
| // CSRF and name checks; it stands in for the step-up gate. On true the |
| // caller holds the account's billing hold, the account is retiring and the |