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)