AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): send trial-ending notice for an early trial_will_end event (SECRT-2827) (#15529)
Krzysztof Czerwinskif05d2d73930a
diff --git a/autogpt_platform/backend/backend/notifications/trial.py b/autogpt_platform/backend/backend/notifications/trial.pyindex 836d5242bb..1be190d6b0 100644--- a/autogpt_platform/backend/backend/notifications/trial.py+++ b/autogpt_platform/backend/backend/notifications/trial.py@@ -33,7 +33,10 @@ from backend.notifications.trial_plan import notice_planfrom backend.util.posthog_events import PostHogEventlogger = logging.getLogger(__name__)-TRIAL_REMINDER_WINDOW = timedelta(days=3)+# Stripe sends customer.subscription.trial_will_end three days before+# trial_end. The extra day absorbs an event created or handled a little early;+# a delayed or replayed event for a trial since extended further still drops.+TRIAL_REMINDER_WINDOW = timedelta(days=4)TrialNoticeKind = Literal["started", "ending", "canceled", "resumed", "ended", "converted", "payment_failed"@@ -81,7 +84,8 @@ async def notify_trial(subscription: dict, kind: TrialNoticeKind) -> bool:"payment_failed",):return False- if not _notice_applies(trial, kind, current):+ if skip := _notice_skip_reason(trial, kind, current):+ logger.info(f"Dropped trial {kind} notice for user {user_id}: {skip}")return Trueuser = await user_db().get_user_by_id(user_id)if kind == "ended" and await _another_plan_is_live(trial, current):@@ -287,33 +291,48 @@ def trial_notice_data(def _notice_applies(trial: TrialState, kind: TrialNoticeKind, current: dict) -> bool:- status = current.get("status")+ return _notice_skip_reason(trial, kind, current) is None+++def _notice_skip_reason(+ trial: TrialState, kind: TrialNoticeKind, current: dict+) -> str | None:+ """Why the notice no longer describes the subscription, or None."""+ status = current.get("status") or "unknown"+ if kind == "converted":+ if status == "active" and trial.converted_at is not None:+ return None+ return f"subscription is {status}, not converted"+ if kind == "payment_failed":+ if status in ("past_due", "unpaid", "incomplete") and not trial.converted_at:+ return None+ return f"subscription is {status}, not a failed conversion"+ if kind == "ended":+ if status in ("canceled", "unpaid", "paused") and not trial.converted_at:+ return None+ return f"subscription is {status}, not an ended trial"+ return _running_trial_skip_reason(trial, kind, current)+++def _running_trial_skip_reason(+ trial: TrialState, kind: TrialNoticeKind, current: dict+) -> str | None:trial_end = current.get("trial_end") or 0now = datetime.now(UTC).timestamp()- in_trial = status == "trialing" and trial_end > now+ if current.get("status") != "trialing" or trial_end <= now:+ return "trial is no longer running"if kind == "ending" and trial_end > now + TRIAL_REMINDER_WINDOW.total_seconds():- return False- if kind in ("started", "ending", "resumed"):- return (- in_trial- and trial.card_verified_at is not None- and not current.get("cancel_at_period_end")- )- if kind == "canceled":- # It promises access until trial_end, which only a verified card gives.- return (- in_trial- and trial.card_verified_at is not None- and bool(current.get("cancel_at_period_end"))- )- if kind == "converted":- return status == "active" and trial.converted_at is not None- if kind == "payment_failed":- return (- status in ("past_due", "unpaid", "incomplete")- and trial.converted_at is None- )- return status in ("canceled", "unpaid", "paused") and trial.converted_at is None+ return "trial ends after the reminder window"+ # A canceled notice promises access until trial_end, which only a verified+ # card gives.+ if trial.card_verified_at is None:+ return "card is not verified"+ cancel_pending = bool(current.get("cancel_at_period_end"))+ if cancel_pending and kind != "canceled":+ return "cancellation is scheduled"+ if not cancel_pending and kind == "canceled":+ return "cancellation is not scheduled"+ return Noneasync def _another_plan_is_live(trial: TrialState, current: dict) -> bool:diff --git a/autogpt_platform/backend/backend/notifications/trial_test.py b/autogpt_platform/backend/backend/notifications/trial_test.pyindex 1194acae15..dfc51d2b14 100644--- a/autogpt_platform/backend/backend/notifications/trial_test.py+++ b/autogpt_platform/backend/backend/notifications/trial_test.py@@ -236,6 +236,103 @@ def test_ending_reminder_waits_for_current_reminder_window(trial):assert not notices._notice_applies(trial, "ending", raw)+def test_ending_reminder_sent_just_before_three_day_mark(trial):+ """Stripe's trial_will_end lead time is three days. An event created or+ processed a little before that mark is still the reminder."""+ raw = {+ "status": "trialing",+ "trial_end": int((datetime.now(UTC) + timedelta(days=3, hours=1)).timestamp()),+ "cancel_at_period_end": False,+ }+ assert notices._notice_applies(trial, "ending", raw)+++def test_ending_reminder_dropped_just_past_reminder_window(trial):+ trial_end = datetime.now(UTC) + notices.TRIAL_REMINDER_WINDOW + timedelta(hours=1)+ raw = {+ "status": "trialing",+ "trial_end": int(trial_end.timestamp()),+ "cancel_at_period_end": False,+ }+ assert notices._notice_skip_reason(trial, "ending", raw) == (+ "trial ends after the reminder window"+ )+++def test_replayed_ending_reminder_for_ended_trial_names_the_real_reason(trial):+ raw = {+ "status": "canceled",+ "trial_end": int((datetime.now(UTC) + timedelta(days=7)).timestamp()),+ "cancel_at_period_end": False,+ }+ assert notices._notice_skip_reason(trial, "ending", raw) == (+ "trial is no longer running"+ )+++ "kind, current, reason",+ [+ ("converted", {}, "subscription is unknown, not converted"),+ (+ "payment_failed",+ {"status": "active"},+ "subscription is active, not a failed conversion",+ ),+ (+ "ended",+ {"status": "trialing"},+ "subscription is trialing, not an ended trial",+ ),+ ("canceled", {"status": "trialing"}, "cancellation is not scheduled"),+ ],+)+def test_skip_reason_names_why_the_notice_no_longer_applies(+ trial, kind, current, reason+):+ current = {**current, "trial_end": int(trial.ends_at.timestamp())}+ assert notices._notice_skip_reason(trial, kind, current) == reason+++async def test_dropped_notice_is_logged_without_sending(trial, caplog):+ raw = {+ "id": trial.subscription_id,+ "customer": trial.customer_id,+ "status": "trialing",+ "trial_end": int((datetime.now(UTC) + timedelta(days=7)).timestamp()),+ "cancel_at_period_end": False,+ "metadata": {+ "trial_enrollment_id": trial.id,+ "user_id": trial.user_id,+ "trial_checkout_attempt": str(trial.checkout_attempt),+ },+ }+ queue = AsyncMock()+ with (+ patch.object(notices, "stripe_call", AsyncMock(return_value=raw)),+ patch.object(+ notices,+ "credit_db",+ return_value=MagicMock(+ get_subscription_trial=AsyncMock(return_value=trial)+ ),+ ),+ patch.object(notices, "queue_notification_async", queue),+ patch.object(notices, "_track_billing_event") as track,+ caplog.at_level("INFO", logger=notices.logger.name),+ ):+ assert await notices.notify_trial(raw, "ending")+ queue.assert_not_awaited()+ track.assert_not_called()+ [record] = [r for r in caplog.records if r.name == notices.logger.name]+ assert record.levelname == "INFO"+ message = record.getMessage()+ assert "ending" in message+ assert trial.user_id in message+ assert "reminder window" in message++def test_failed_conversion_suppresses_paid_confirmation(trial):assert not notices._notice_applies(trial, "converted", {"status": "past_due"})diff --git a/docs/engineering/transactional-email-design.md b/docs/engineering/transactional-email-design.mdindex 701ee7c519..8b384291e2 100644--- a/docs/engineering/transactional-email-design.md+++ b/docs/engineering/transactional-email-design.md@@ -118,8 +118,12 @@ To verify the trial-ending reminder, follow the delivery path in order:subscribes to `customer.subscription.trial_will_end`.2. Check the Stripe Dashboard's **Trial ending events** setting is three days.Compare a recent event's creation time with its subscription's `trial_end`.- The code only sends an ending notice inside the final three days, while the- trial is active, the card is verified and cancellation is not scheduled.+ The code only sends an ending notice inside the final four days (Stripe's+ three plus a day of slack for an early event), while the trial is active,+ the card is verified and cancellation is not scheduled. A notice dropped by+ these checks is logged at info as `Dropped trial <kind> notice for user+ <user id>: <reason>`. Events for an earlier checkout attempt or an unstarted+ trial, and a notice already sent once, return without this line.3. Check the endpoint's delivery result and backend processing. A successfulStripe webhook response is not proof that an email was delivered.4. Check PostHog for `trial_ending`, the current event name, and