fix(alerts): announce a subscriber when the payment lands, not when Checkout opens

Stripe creates the subscription in 'incomplete' seconds after the Checkout
page opens, before the user types anything, and the 'New subscriber' alert
fired on that row. In the 30 days to 16-sep-2026, 41 of 68 subscriptions
expired unpaid that way (none a card decline) and each one sent an email.
The alert now fires only for active/trialing rows; an incomplete one that
later pays is announced by the 'Payment received' alert on invoice.paid.
This commit is contained in:
juancarlos.cavero 2026-09-16 21:02:38 +02:00
parent 5c0e01d1c3
commit afd29e1718
2 changed files with 34 additions and 2 deletions

View file

@ -490,6 +490,19 @@ def _sub_period(sub_obj: dict):
return start, end
def new_subscriber_label(status: str) -> str | None:
"""Alert text for a brand-new subscription row, or None to stay silent.
Only states where a payment method actually went through count: an
'incomplete' row is a checkout that has not been paid (yet), not a sale.
"""
if status == "trialing":
return "trial started — card on file"
if status == "active":
return status
return None
async def _upsert_subscription(sub_obj: dict, event_created: datetime):
price_id = _sub_price_id(sub_obj)
info = plan_info_for_price(price_id)
@ -554,10 +567,16 @@ async def _upsert_subscription(sub_obj: dict, event_created: datetime):
setattr(row, k, v)
# Purchase alert: someone just subscribed (trial started or paid outright).
# Not for 'incomplete': Checkout creates the subscription the moment the
# user hits pay, before 3DS / the card answer, so that status only says
# someone reached the button. About half of them expire unpaid (24 of 53
# in the 30 days to 16-sep-2026), and the ones that do pay are announced
# by the 'Payment received' alert on invoice.paid, which carries the
# amount; a second message here would just be noise.
now_status = sub_obj["status"]
if is_new_sub:
label = new_subscriber_label(now_status) if is_new_sub else None
if label:
from .alerts import send_admin_alert
label = "trial started — card on file" if now_status == "trialing" else now_status
await send_admin_alert(
"🎉 New subscriber",
f"{buyer_email or 'A user'} started the {plan} ({interval}) plan.\nStatus: {label}.",

View file

@ -80,3 +80,16 @@ class TestAttentionStates:
# the dead end, so the two sets must stay reconciled.
for status in config.CHECKOUT_BLOCKING_STATES:
assert status in ("active", "trialing") or status in config.BILLING_ATTENTION_STATES
class TestNewSubscriberAlert:
"""The 'New subscriber' admin alert must mean money, not a click on pay."""
def test_incomplete_is_silent(self):
from cloud.billing import new_subscriber_label
assert new_subscriber_label("incomplete") is None
@pytest.mark.parametrize("status", ["active", "trialing"])
def test_paid_states_alert(self, status):
from cloud.billing import new_subscriber_label
assert new_subscriber_label(status)