From afd29e1718663e267c275fa676925a2f36ae6d3c Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Wed, 16 Sep 2026 21:02:38 +0200 Subject: [PATCH] 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. --- cloud/billing.py | 23 +++++++++++++++++++++-- tests/test_billing_states.py | 13 +++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/cloud/billing.py b/cloud/billing.py index 3110f57..29ed7d2 100644 --- a/cloud/billing.py +++ b/cloud/billing.py @@ -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}.", diff --git a/tests/test_billing_states.py b/tests/test_billing_states.py index 100ddc6..ffa5903 100644 --- a/tests/test_billing_states.py +++ b/tests/test_billing_states.py @@ -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)