fix(billing): block new checkout for past_due/unpaid/incomplete subs

Guard only blocked active/trialing, so a user whose first charge failed
(sub in past_due) could create a second subscription. Real case: annual
trial -> end-trial -> card declined -> past_due, then a fresh monthly sub,
leaving two live subs on one customer. Now any non-terminal status blocks.
This commit is contained in:
victormanuel.caverogracia 2026-07-18 23:34:58 +02:00
parent d2febde078
commit 9e5aa7431d

View file

@ -101,7 +101,15 @@ async def _active_sub_exists(user_id) -> bool:
sub = (await s.execute( sub = (await s.execute(
select(Subscription).where(Subscription.user_id == user_id) select(Subscription).where(Subscription.user_id == user_id)
)).scalar_one_or_none() )).scalar_one_or_none()
return bool(sub and sub.status in ("active", "trialing")) # Block a second checkout for ANY non-terminal subscription, not just
# active/trialing. A first charge that fails leaves the sub in past_due /
# unpaid / incomplete; those must still block, otherwise the user can spawn a
# second subscription while the first one keeps dunning (real case: annual
# trial -> end-trial -> card declined -> past_due, then a fresh monthly sub).
# Terminal states (canceled, incomplete_expired) do NOT block.
return bool(sub and sub.status in (
"active", "trialing", "past_due", "unpaid", "incomplete", "paused",
))
# --------------------------------------------------------------------------- # # --------------------------------------------------------------------------- #