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/billing): a failed renewal only cuts access on its own subscription, and wallet payments can resume (#15345)
Bentlyff3287a5ba65
diff --git a/autogpt_platform/backend/backend/api/features/billing/subscriptions/routes.py b/autogpt_platform/backend/backend/api/features/billing/subscriptions/routes.pyindex 05b138a194..c4c576483c 100644--- a/autogpt_platform/backend/backend/api/features/billing/subscriptions/routes.py+++ b/autogpt_platform/backend/backend/api/features/billing/subscriptions/routes.py@@ -37,7 +37,6 @@ from backend.data.credit import (get_proration_credit_cents,get_subscription_price_id,get_user_billing_cycle,- handle_subscription_payment_failure,handle_subscription_payment_success,modify_stripe_subscription_for_tier,release_pending_subscription_schedule,@@ -53,6 +52,9 @@ from backend.data.stripe_authentication import requires_authenticationfrom backend.data.stripe_client import stripe_callfrom backend.data.subscription_activation_events import recover_claimed_billing_eventfrom backend.data.subscription_checkout import AnotherPlanLive+from backend.data.subscription_payment_failure import (+ handle_subscription_payment_failure,+)from backend.data.subscription_trial import TrialStatefrom backend.data.subscription_trial_billing import (TRIAL_BILLING_EVENTS,@@ -74,6 +76,9 @@ from backend.data.subscription_upgrade_models import (SubscriptionUpgradePreviewRequest,UpgradePreviewUnavailable,)+from backend.data.subscription_wallet_payment import (+ reconcile_wallet_payment_on_paid_invoice,+)from backend.data.user import get_user_by_idfrom backend.notifications import lifecyclefrom backend.notifications.queue import queue_pass_work@@ -1001,6 +1006,7 @@ async def stripe_webhook(request: Request):await sync_subscription_schedule_from_stripe(data_object)if event_type in ("invoice.payment_succeeded", "invoice.paid"):+ await reconcile_wallet_payment_on_paid_invoice(data_object)await handle_subscription_payment_success(data_object)await on_trial_invoice(data_object, paid=True)@@ -1026,6 +1032,7 @@ async def stripe_webhook(request: Request):invoice = await stripe_call(stripe.Invoice.retrieve_async, invoice_id)invoice_payload = cast(dict, invoice)if event_type == "invoice_payment.paid":+ await reconcile_wallet_payment_on_paid_invoice(invoice_payload)await handle_subscription_payment_success(invoice_payload)await on_trial_invoice(invoice_payload, paid=True)else:diff --git a/autogpt_platform/backend/backend/api/features/subscription_routes_test.py b/autogpt_platform/backend/backend/api/features/subscription_routes_test.pyindex 8a588eba88..8b53a7646a 100644--- a/autogpt_platform/backend/backend/api/features/subscription_routes_test.py+++ b/autogpt_platform/backend/backend/api/features/subscription_routes_test.py@@ -1369,6 +1369,60 @@ def test_stripe_webhook_dispatches_invoice_payment_failed(failure_mock.assert_awaited_once_with(invoice_obj)+ "event_type", ["invoice.payment_succeeded", "invoice_payment.paid"]+)+def test_stripe_webhook_paid_invoice_reconciles_the_wallet_payment_first(+ client: fastapi.testclient.TestClient,+ mocker: pytest_mock.MockFixture,+ event_type: str,+) -> None:+ """Both paid events settle or refund an unfinished wallet payment before+ the success handler, whose credit grant reads the settled state."""+ invoice_obj = {"id": "in_test", "customer": "cus_test", "amount_paid": 1999}+ data_object = (+ invoice_obj+ if event_type == "invoice.payment_succeeded"+ else {"object": "invoice_payment", "invoice": "in_test"}+ )+ event = {"type": event_type, "data": {"object": data_object}}+ mocker.patch.object(+ stripe.Invoice, "retrieve_async", AsyncMock(return_value=invoice_obj)+ )++ mocker.patch(+ "backend.api.features.billing.subscriptions.routes.settings.secrets.stripe_webhook_secret",+ new="whsec_test",+ )+ mocker.patch(+ "backend.api.features.billing.subscriptions.routes.stripe.Webhook.construct_event",+ return_value=event,+ )+ calls = Mock()+ mocker.patch(+ "backend.api.features.billing.subscriptions.routes.reconcile_wallet_payment_on_paid_invoice",+ new=AsyncMock(side_effect=lambda inv: calls("refund", inv)),+ )+ mocker.patch(+ "backend.api.features.billing.subscriptions.routes.handle_subscription_payment_success",+ new=AsyncMock(side_effect=lambda inv: calls("success", inv)),+ )+ mocker.patch(+ "backend.api.features.billing.subscriptions.routes.on_trial_invoice",+ new_callable=AsyncMock,+ )++ response = client.post(+ "/credits/stripe_webhook",+ content=b"{}",+ headers={"stripe-signature": "t=1,v1=abc"},+ )++ assert response.status_code == 200+ assert [c.args[0] for c in calls.call_args_list] == ["refund", "success"]+ calls.assert_any_call("refund", invoice_obj)++def test_update_subscription_tier_paid_to_paid_modifies_subscription(client: fastapi.testclient.TestClient,mocker: pytest_mock.MockFixture,diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.pyindex 16acb91da4..75e3a471da 100644--- a/autogpt_platform/backend/backend/data/credit.py+++ b/autogpt_platform/backend/backend/data/credit.py@@ -61,6 +61,7 @@ from backend.data.subscription_upgrade_models import (UpgradePreviewUnavailable,)from backend.data.user import get_user_by_id, get_user_email_by_id+from backend.data.wallet_payment_state import invoice_paid_from_walletfrom backend.notifications.queue import queue_notification_asyncfrom backend.util import posthog_clientfrom backend.util.cache import cached@@ -1651,18 +1652,16 @@ def invalidate_subscription_caches(user_id: str) -> None:# (including the payment-failure cancel) calls invalidate_billing_lookups.-# Stripe stamps ``cancellation_details.reason = "cancellation_requested"`` on-# any cancel made through the API, including ours after a failed renewal the-# balance could not cover. This comment on that cancel is what lets the-# ``customer.subscription.deleted`` handler report it as involuntary churn.-PAYMENT_FAILURE_CANCELLATION_COMMENT = "autogpt:payment_failed"+# Set as ``cancellation_details.comment`` when a failed renewal finds that the+# customer replaced that subscription with another plan. The deletion webhook+# reads it so that cancel is not reported as the customer leaving.+REPLACED_PLAN_CANCELLATION_COMMENT = "autogpt:replaced_by_another_plan"async def _cancel_customer_subscriptions(customer_id: str,exclude_sub_id: str | None = None,at_period_end: bool = False,- cancellation_comment: str | None = None,) -> int:"""Cancel all billable Stripe subscriptions for a customer, optionally excluding one.@@ -1676,9 +1675,6 @@ async def _cancel_customer_subscriptions(Uses the async Stripe client. Raises stripe.StripeError on list/cancel failure so callersthat need strict consistency can react; cleanup callers can catch and log instead.- ``cancellation_comment`` is set as ``cancellation_details.comment`` on an- immediate cancel, where the subscription's deletion webhook can read it.-Returns the number of subscriptions cancelled/scheduled for cancellation."""# Query active and trialing separately; Stripe's list API accepts a single status@@ -1687,7 +1683,7 @@ async def _cancel_customer_subscriptions(seen_ids: set[str] = set()try:await _cancel_listed_subscriptions(- customer_id, seen_ids, exclude_sub_id, at_period_end, cancellation_comment+ customer_id, seen_ids, exclude_sub_id, at_period_end)finally:if seen_ids:@@ -1700,7 +1696,6 @@ async def _cancel_listed_subscriptions(seen_ids: set[str],exclude_sub_id: str | None,at_period_end: bool,- cancellation_comment: str | None,) -> None:for status in ("active", "trialing"):subscriptions = await stripe_call(@@ -1739,7 +1734,7 @@ async def _cancel_listed_subscriptions(canceled = await stripe_call(stripe.Subscription.cancel_async,sub_id,- **_cancel_params(cancellation_comment, trial=True),+ **_cancel_params(trial=True),)if (sub.get("metadata") or {}).get("trial_enrollment_id"):await sync_subscription_from_stripe(dict(canceled))@@ -1747,21 +1742,16 @@ async def _cancel_listed_subscriptions(await stripe_call(stripe.Subscription.cancel_async,sub_id,- **_cancel_params(cancellation_comment, trial=False),+ **_cancel_params(trial=False),)-def _cancel_params(- cancellation_comment: str | None, *, trial: bool-) -> stripe.Subscription.CancelParams:- """A trial ends without an invoice or proration; a comment, when given,- becomes ``cancellation_details.comment``."""+def _cancel_params(*, trial: bool) -> stripe.Subscription.CancelParams:+ """A trial ends without an invoice or proration."""params: stripe.Subscription.CancelParams = {}if trial:params["invoice_now"] = Falseparams["prorate"] = False- if cancellation_comment:- params["cancellation_details"] = {"comment": cancellation_comment}return params@@ -2893,6 +2883,34 @@ async def _cleanup_stale_subscriptions(customer_id: str, new_sub_id: str) -> Non)+async def replaced_by_newer_plan(customer_id: str, subscription: dict) -> bool:+ """Whether the customer has an active or trialing plan created after this one.++ An older plan never replaces a newer one, even when a failed cleanup left+ it active. A trial cancelled to end at ``trial_end`` does not count: it+ ends without an invoice and never becomes a plan. Without a ``created``,+ nothing is treated as newer.+ """+ created = subscription.get("created")+ if not created:+ return False+ for status in ("active", "trialing"):+ subscriptions = await stripe_call(+ stripe.Subscription.list_async,+ customer=customer_id,+ status=status,+ limit=10,+ )+ async for other in stripe_list_items(subscriptions):+ if other["id"] == subscription["id"]:+ continue+ if status == "trialing" and other.get("cancel_at_period_end"):+ continue+ if (other.get("created") or 0) > created:+ return True+ return False++async def sync_subscription_from_stripe(stripe_subscription: dict, *, track_lifecycle: bool = True) -> None:@@ -3011,6 +3029,17 @@ async def _sync_subscription_tier_from_stripe(stripe_subscription: dict) -> Noneinvalidate_subscription_caches(user.id)returnprice_id = ""+ if status == "active" and await replaced_by_newer_plan(+ customer_id, stripe_subscription+ ):+ # Paying an old past-due plan revives it next to the plan that+ # replaced it. Keep the newer plan and its tier; end this one.+ from backend.data.subscription_payment_failure import ( # local import avoids circular+ end_revived_plan,+ )++ await end_revived_plan(new_sub_id)+ returnif status in ("active", "trialing"):price_id = ""items = stripe_subscription.get("items", {}).get("data", [])@@ -3373,196 +3402,6 @@ def _invoice_subscription_metadata(invoice: dict) -> dict:return metadata if isinstance(metadata, dict) else {}-async def handle_subscription_payment_failure(invoice: dict) -> None:- """Handle a failed Stripe subscription payment.-- Tries to cover the invoice amount from the user's credit balance.-- - Balance sufficient → deduct from balance, then pay the Stripe invoice so- Stripe stops retrying it. The sub stays intact and the user keeps their tier.- - Balance insufficient → cancel Stripe sub immediately, downgrade to BASIC.- Cancelling here avoids further Stripe retries on an invoice we cannot cover.- """- customer_id = invoice.get("customer")- if not customer_id:- logger.warning(- "handle_subscription_payment_failure: missing customer in invoice; skipping"- )- return-- user = await User.prisma().find_first(where={"stripeCustomerId": customer_id})- if not user:- logger.warning(- "handle_subscription_payment_failure: no user found for customer %s",- customer_id,- )- return-- trial = await get_subscription_trial(user.id)- if (- trial- and trial.converted_at is None- and trial.subscription_id == _invoice_subscription_id(invoice)- ):- # Initial conversion stays on its existing invoice for authentication or- # card repair. Failure must not spend wallet credits or cancel the sub.- return- current_tier = user.subscriptionTier or SubscriptionTier.NO_TIER- if current_tier in (SubscriptionTier.NO_TIER, SubscriptionTier.TRIAL):- return- invoice_id: str = invoice.get("id") or ""- if not invoice_id:- return- invoice = dict(await stripe_call(stripe.Invoice.retrieve_async, invoice_id))- if invoice.get("status") != "open" or invoice.get("customer") != customer_id:- return- if current_tier == SubscriptionTier.ENTERPRISE:- logger.warning(- "handle_subscription_payment_failure: skipping ENTERPRISE user %s"- " (customer %s) — tier is admin-managed",- user.id,- customer_id,- )- return-- amount_due: int = invoice.get("amount_due", 0)- sub_id = _invoice_subscription_id(invoice)-- if invoice.get("billing_reason") == "subscription_create":- # A new plan's first payment failed inside Checkout, which lets the- # customer retry; no plan lapsed.- logger.info(- "handle_subscription_payment_failure: first payment of new sub %s"- " failed for user %s; leaving it to Checkout",- sub_id,- user.id,- )- return-- if amount_due <= 0:- logger.info(- "handle_subscription_payment_failure: amount_due=%d for user %s;"- " nothing to deduct",- amount_due,- user.id,- )- return-- if (- sub_id- and invoice_id- # A pending update waits on the invoice its own change made, never on- # a renewal, so the common failure skips the Stripe lookup.- and invoice.get("billing_reason") != "subscription_cycle"- and await _nothing_left_to_cover(invoice_id)- ):- # Stripe applies a pending update only once its invoice is paid, and- # voids the invoice when the update lapses: nothing has changed. A- # trial converting early waits like this for the bank to confirm.- logger.info(- "handle_subscription_payment_failure: invoice %s on sub %s for user"- " %s waits on a pending update or is already settled; nothing to"- " cover",- invoice_id,- sub_id,- user.id,- )- return-- credit_model = UserCredit()- try:- await credit_model._add_transaction(- user_id=user.id,- amount=-amount_due,- transaction_type=CreditTransactionType.SUBSCRIPTION,- fail_insufficient_credits=True,- # Use invoice_id as the idempotency key so that Stripe webhook retries- # (e.g. on a transient stripe.Invoice.pay failure) do not double-charge.- transaction_key=invoice_id or None,- metadata=SafeJson(- {- "stripe_customer_id": customer_id,- "stripe_subscription_id": sub_id,- "reason": "subscription_payment_failure_covered_by_balance",- }- ),- )- # Balance covered the invoice. Pay the Stripe invoice with- # ``paid_out_of_band=True`` so Stripe marks the invoice paid without- # retrying the card charge — the card already failed and the user is- # paying via their AutoGPT balance, so a card retry here would- # double-bill the user (card charge + balance debit). Stripe still- # fires ``invoice.payment_succeeded`` on the transition; the success- # handler reads ``paid_out_of_band`` and skips its grant path so the- # balance debit isn't reversed if the credit-grant flag is on.- if invoice_id:- try:- await stripe_call(- stripe.Invoice.pay_async, invoice_id, paid_out_of_band=True- )- except stripe.StripeError:- logger.warning(- "handle_subscription_payment_failure: balance deducted for user"- " %s but failed to mark invoice %s as paid; Stripe may retry",- user.id,- invoice_id,- )- finally:- # A past-due subscription is active again, also when the call- # timed out after Stripe applied it.- await invalidate_billing_lookups(customer_id)- logger.info(- "handle_subscription_payment_failure: deducted %d cents from balance"- " for user %s; Stripe invoice %s paid, sub %s intact, tier preserved",- amount_due,- user.id,- invoice_id,- sub_id,- )- except InsufficientBalanceError:- # Balance insufficient — cancel Stripe subscription first, then downgrade DB.- # Order matters: if we downgrade the DB first and the Stripe cancel fails, the- # user is permanently stuck on BASIC while Stripe continues billing them.- # Cancelling Stripe first is safe: if the DB write then fails, the webhook- # customer.subscription.deleted will fire and correct the tier eventually.- logger.info(- "handle_subscription_payment_failure: insufficient balance for user %s;"- " cancelling Stripe sub %s then downgrading to BASIC",- user.id,- sub_id,- )- try:- await _cancel_customer_subscriptions(- customer_id, cancellation_comment=PAYMENT_FAILURE_CANCELLATION_COMMENT- )- except stripe.StripeError:- logger.warning(- "handle_subscription_payment_failure: failed to cancel Stripe sub %s"- " for user %s (customer %s); skipping tier downgrade to avoid"- " inconsistency — Stripe may continue retrying the invoice",- sub_id,- user.id,- customer_id,- )- return- await set_subscription_tier(user.id, SubscriptionTier.NO_TIER)---async def _nothing_left_to_cover(invoice_id: str) -> bool:- """Whether a pending update still waits on this invoice, or Stripe has- settled it since: paid once the bank confirmed, or voided when the update- lapsed. The failure event can be delivered after either."""- invoice = await stripe_call(- stripe.Invoice.retrieve_async, invoice_id, expand=["subscription"]- )- if invoice.get("status") in ("paid", "void"):- return True- subscription = invoice.get("subscription") or {}- return bool(subscription.get("pending_update")) and (- subscription.get("latest_invoice") == invoice_id- )--async def handle_subscription_payment_success(invoice: dict) -> None:"""Optionally grant AutoGPT credits equal to the paid Stripe invoice amount.@@ -3586,7 +3425,7 @@ async def handle_subscription_payment_success(invoice: dict) -> None:- Non-subscription invoices (no ``subscription`` field).- Zero-amount invoices (e.g. card-validation checks, $0 trials).- ENTERPRISE users (admin-managed; they don't pay via self-service).- - Invoices already covered from balance via ``paid_out_of_band``.+ - Invoices covered from balance by the failure handler."""customer_id = invoice.get("customer")if not customer_id:@@ -3633,12 +3472,14 @@ async def handle_subscription_payment_success(invoice: dict) -> None:# invoice from the user's balance and marked it paid out of band — the# balance was debited there, granting matching credits here would reverse# the debit and give the user a free billing period.- if invoice.get("paid_out_of_band"):+ # The wallet record decides; ``paid_out_of_band`` is gone from the+ # Invoice since Stripe API 2025-03-31.basil.+ if invoice.get("paid_out_of_band") or await invoice_paid_from_wallet(+ user.id, invoice_id+ ):logger.info(- "handle_subscription_payment_success: skipping invoice %s for user %s"- " (paid_out_of_band — covered by balance in failure handler)",- invoice_id,- user.id,+ f"handle_subscription_payment_success: skipping invoice {invoice_id}"+ f" for user {user.id} (covered by balance in failure handler)")returndiff --git a/autogpt_platform/backend/backend/data/credit_pending_update_test.py b/autogpt_platform/backend/backend/data/credit_pending_update_test.pydeleted file mode 100644index 98dc467a54..0000000000--- a/autogpt_platform/backend/backend/data/credit_pending_update_test.py+++ /dev/null@@ -1,122 +0,0 @@-"""A failed payment on a change Stripe holds until it's paid changed nothing,-so it must not end the paid plans a lapsed payment otherwise ends. The failure-event can also arrive after the bank confirmed or the hold lapsed."""--from unittest.mock import AsyncMock, MagicMock, patch--import pytest-import stripe-from prisma.enums import SubscriptionTier-from prisma.models import User--from backend.data import credit---def _failed_invoice(billing_reason: str) -> dict:- return {- "id": "in_held",- "customer": "cus_1",- "subscription": "sub_trial",- "amount_due": 32000,- "status": "open",- "billing_reason": billing_reason,- }---def lapse():- user = MagicMock(spec=User)- user.id = "user-1"- user.subscriptionTier = SubscriptionTier.PRO- with (- patch(- "backend.data.credit.User.prisma",- return_value=MagicMock(find_first=AsyncMock(return_value=user)),- ),- patch.object(- credit, "_nothing_left_to_cover", AsyncMock(return_value=True)- ) as settled,- patch.object(- credit.UserCredit, "_add_transaction", new_callable=AsyncMock- ) as deduct,- patch.object(- credit, "_cancel_customer_subscriptions", new_callable=AsyncMock- ) as cancel,- patch.object(credit, "set_subscription_tier", new_callable=AsyncMock) as tier,- patch.object(stripe.Invoice, "pay_async", new_callable=AsyncMock),- patch.object(credit, "get_subscription_trial", AsyncMock(return_value=None)),- patch.object(- stripe.Invoice,- "retrieve_async",- AsyncMock(return_value=_failed_invoice("subscription_cycle")),- ) as retrieve,- ):- yield MagicMock(- settled=settled, deduct=deduct, cancel=cancel, tier=tier, retrieve=retrieve- )---async def test_a_failed_payment_on_a_held_change_cancels_nothing(lapse):- lapse.retrieve.return_value = _failed_invoice("subscription_update")- await credit.handle_subscription_payment_failure(- _failed_invoice("subscription_update")- )-- lapse.settled.assert_awaited_once_with("in_held")- lapse.deduct.assert_not_awaited()- lapse.cancel.assert_not_awaited()- lapse.tier.assert_not_awaited()---async def test_a_failed_renewal_is_never_looked_up_as_a_held_change(lapse):- """A pending update waits on the invoice its own change made, so a renewal- goes straight to the balance without a Stripe lookup."""- await credit.handle_subscription_payment_failure(- _failed_invoice("subscription_cycle")- )-- lapse.settled.assert_not_awaited()- lapse.deduct.assert_awaited_once()--- "status,subscription,nothing_left",- [- (- "open",- {"pending_update": {"expires_at": 1}, "latest_invoice": "in_held"},- True,- ),- # A later invoice on the same subscription is not the held one.- (- "open",- {"pending_update": {"expires_at": 1}, "latest_invoice": "in_next"},- False,- ),- ("open", {"pending_update": None, "latest_invoice": "in_held"}, False),- # Settled before this event was handled: paid once the bank confirmed,- # or voided when the hold lapsed or a later change replaced it.- ("paid", {"pending_update": None, "latest_invoice": "in_held"}, True),- ("void", {"pending_update": None, "latest_invoice": "in_next"}, True),- ],-)-async def test_only_a_held_or_settled_invoice_leaves_nothing_to_cover(- status, subscription, nothing_left-):- retrieve = AsyncMock(- return_value=stripe.Invoice.construct_from(- {- "id": "in_held",- "status": status,- "subscription": {"id": "sub_trial", **subscription},- },- "test-key",- )- )- with patch.object(stripe.Invoice, "retrieve_async", retrieve):- assert await credit._nothing_left_to_cover("in_held") is nothing_left-- retrieve.assert_awaited_once_with("in_held", expand=["subscription"])diff --git a/autogpt_platform/backend/backend/data/credit_subscription_test.py b/autogpt_platform/backend/backend/data/credit_subscription_test.pyindex d92c0c262e..60ac08d955 100644--- a/autogpt_platform/backend/backend/data/credit_subscription_test.py+++ b/autogpt_platform/backend/backend/data/credit_subscription_test.py@@ -13,7 +13,6 @@ from prisma.models import Userfrom backend.data import creditfrom backend.data.credit import (- PAYMENT_FAILURE_CANCELLATION_COMMENT,UserCredit,_get_active_subscription_cached,_is_stripe_reconcilable,@@ -22,7 +21,6 @@ from backend.data.credit import (create_subscription_checkout,get_pending_subscription_change,get_proration_credit_cents,- handle_subscription_payment_failure,handle_subscription_payment_success,is_tier_downgrade,is_tier_upgrade,@@ -32,7 +30,16 @@ from backend.data.credit import (sync_subscription_from_stripe,sync_subscription_schedule_from_stripe,)-from backend.util.exceptions import InsufficientBalanceError++[email protected](autouse=True)+def _no_wallet_payments():+ """No invoice in these tests was paid from the wallet unless one says so."""+ with patch(+ "backend.data.credit.invoice_paid_from_wallet",+ new=AsyncMock(return_value=False),+ ) as wallet:+ yield walletclass _CacheClearable(Protocol):@@ -48,17 +55,6 @@ def _clear_cache(fn: _CacheClearable) -> None:fn.cache_clear()[email protected](autouse=True)-def _no_pending_update():- """The payment failures here are plans that lapsed, not a change Stripe- holds until it's paid (see credit_pending_update_test.py)."""- with patch(- "backend.data.credit._nothing_left_to_cover",- AsyncMock(return_value=False),- ):- yield--@pytest.fixture(autouse=True)def billing_boundaries():"""Legacy billing tests isolate the new authoritative reconciliation boundary.@@ -1904,205 +1900,6 @@ async def test_sync_subscription_from_stripe_no_metadata_user_id_skips_check()-async def test_handle_subscription_payment_failure_balance_covers_pays_invoice(- billing_boundaries,-):- """When balance covers the invoice, Stripe Invoice.pay is called with- paid_out_of_band=True so the card isn't double-charged on top of the- balance debit (the card already failed; retrying it would let the- success-handler webhook reverse the debit via the credit grant)."""- mock_user = _make_user(user_id="user-1", tier=SubscriptionTier.PRO)- invoice = {- "id": "in_abc123",- "customer": "cus_123",- "subscription": "sub_abc123",- "amount_due": 2000,- }-- with (- patch(- "backend.data.credit.User.prisma",- return_value=MagicMock(find_first=AsyncMock(return_value=mock_user)),- ),- patch(- "backend.data.credit.UserCredit._add_transaction",- new_callable=AsyncMock,- ),- patch(- "backend.data.credit.stripe.Invoice.pay_async", new_callable=AsyncMock- ) as mock_pay,- ):- billing_boundaries.invoice.return_value = {**invoice, "status": "open"}- await handle_subscription_payment_failure(invoice)- mock_pay.assert_called_once_with("in_abc123", paid_out_of_band=True)---async def test_handle_subscription_payment_failure_ignores_a_new_plans_first_payment():- """A card declined in Checkout leaves the new plan incomplete for the- customer to retry. Treating it as a lapsed plan would cancel every live- subscription, including a cancel-pending trial that still has access."""- mock_user = _make_user(user_id="user-1", tier=SubscriptionTier.TRIAL)- invoice = {- "id": "in_first",- "customer": "cus_123",- "subscription": "sub_new_max",- "amount_due": 32000,- "billing_reason": "subscription_create",- }-- with (- patch(- "backend.data.credit.User.prisma",- return_value=MagicMock(find_first=AsyncMock(return_value=mock_user)),- ),- patch(- "backend.data.credit.UserCredit._add_transaction", new_callable=AsyncMock- ) as mock_add_tx,- patch(- "backend.data.credit._cancel_customer_subscriptions",- new_callable=AsyncMock,- ) as mock_cancel,- patch(- "backend.data.credit.set_subscription_tier", new_callable=AsyncMock- ) as mock_set,- ):- await handle_subscription_payment_failure(invoice)- mock_add_tx.assert_not_awaited()- mock_cancel.assert_not_awaited()- mock_set.assert_not_awaited()---async def test_handle_subscription_payment_failure_invoice_pay_error_does_not_raise(- billing_boundaries,-):- """Failure to mark the invoice as paid is logged but does not propagate."""- import stripe as stripe_mod-- mock_user = _make_user(user_id="user-1", tier=SubscriptionTier.PRO)- invoice = {- "id": "in_abc123",- "customer": "cus_123",- "subscription": "sub_abc123",- "amount_due": 2000,- }-- with (- patch(- "backend.data.credit.User.prisma",- return_value=MagicMock(find_first=AsyncMock(return_value=mock_user)),- ),- patch(- "backend.data.credit.UserCredit._add_transaction",- new_callable=AsyncMock,- ),- patch(- "backend.data.credit.stripe.Invoice.pay_async",- side_effect=stripe_mod.StripeError("network error"),- ),- patch(- "backend.data.credit.invalidate_billing_lookups", new_callable=AsyncMock- ) as invalidate,- ):- # Must not raise — the pay failure is only logged as a warning- billing_boundaries.invoice.return_value = {**invoice, "status": "open"}- await handle_subscription_payment_failure(invoice)- # A timed-out call may still have been applied in Stripe, so the cached- # billing lookups are dropped either way.- invalidate.assert_awaited_once_with("cus_123")---async def test_handle_subscription_payment_failure_passes_invoice_id_as_transaction_key(- billing_boundaries,-):- """invoice_id is used as the idempotency key to prevent double-charging on webhook retries."""- mock_user = _make_user(user_id="user-1", tier=SubscriptionTier.PRO)- invoice = {- "id": "in_idempotency_test",- "customer": "cus_123",- "subscription": "sub_abc123",- "amount_due": 2000,- }-- with (- patch(- "backend.data.credit.User.prisma",- return_value=MagicMock(find_first=AsyncMock(return_value=mock_user)),- ),- patch(- "backend.data.credit.UserCredit._add_transaction",- new_callable=AsyncMock,- ) as mock_add_tx,- patch("backend.data.credit.stripe.Invoice.pay_async", new_callable=AsyncMock),- ):- billing_boundaries.invoice.return_value = {**invoice, "status": "open"}- await handle_subscription_payment_failure(invoice)- mock_add_tx.assert_called_once()- _, kwargs = mock_add_tx.call_args- assert kwargs.get("transaction_key") == "in_idempotency_test"---async def test_handle_subscription_payment_failure_marks_its_cancel_as_payment_failed(- billing_boundaries,-):- """Stripe stamps our API cancel ``cancellation_requested``; the comment is- what lets the deletion webhook report it as involuntary churn."""- mock_user = _make_user(user_id="user-1", tier=SubscriptionTier.PRO)- invoice = {- "id": "in_uncovered",- "customer": "cus_123",- "subscription": "sub_abc123",- "amount_due": 2000,- }- active_subs = MagicMock()- active_subs.data = [- stripe.Subscription.construct_from(- {"id": "sub_abc123", "schedule": None}, "sk_test"- )- ]- active_subs.has_more = False- no_subs = MagicMock()- no_subs.data = []- no_subs.has_more = False-- def list_side_effect(*args, **kwargs):- return no_subs if kwargs.get("status") == "trialing" else active_subs-- with (- patch(- "backend.data.credit.User.prisma",- return_value=MagicMock(find_first=AsyncMock(return_value=mock_user)),- ),- patch(- "backend.data.credit.UserCredit._add_transaction",- new_callable=AsyncMock,- side_effect=InsufficientBalanceError(- message="no balance", user_id="user-1", balance=0, amount=2000- ),- ),- patch(- "backend.data.credit.stripe.Subscription.list_async",- side_effect=list_side_effect,- ),- patch(- "backend.data.credit.stripe.Subscription.cancel_async",- new_callable=AsyncMock,- ) as mock_cancel,- patch("backend.data.credit.set_subscription_tier", new_callable=AsyncMock),- ):- billing_boundaries.invoice.return_value = {**invoice, "status": "open"}- await handle_subscription_payment_failure(invoice)-- mock_cancel.assert_called_once_with(- "sub_abc123",- cancellation_details={"comment": PAYMENT_FAILURE_CANCELLATION_COMMENT},- )--def _patch_credit_grant_config(enabled: bool):"""Patch ``settings.config.enable_subscription_credit_grant`` for thesuccess handler's gate check.@@ -2467,6 +2264,39 @@ async def test_handle_subscription_payment_success_skips_paid_out_of_band():add_tx_mock.assert_not_called()+async def test_handle_subscription_payment_success_skips_wallet_paid_invoice(+ _no_wallet_payments,+):+ """From Stripe API 2025-03-31.basil the invoice has no ``paid_out_of_band``;+ the wallet's own record must still stop the grant (a free period)."""+ _no_wallet_payments.return_value = True+ mock_user = _make_user(user_id="user-1", tier=SubscriptionTier.PRO)+ invoice = {+ "id": "in_wallet_123",+ "customer": "cus_123",+ "subscription": "sub_abc123",+ "amount_paid": 5000,+ "billing_reason": "subscription_cycle",+ }++ add_tx_mock = AsyncMock()+ with (+ patch(+ "backend.data.credit.User.prisma",+ return_value=MagicMock(find_first=AsyncMock(return_value=mock_user)),+ ),+ patch(+ "backend.data.credit.UserCredit._add_transaction",+ new=add_tx_mock,+ ),+ _patch_credit_grant_config(True),+ ):+ await handle_subscription_payment_success(invoice)+ add_tx_mock.assert_not_called()+ _no_wallet_payments.assert_awaited_once_with("user-1", "in_wallet_123")++@pytest.mark.asyncioasync def test_handle_subscription_payment_success_skips_zero_amount():"""Zero-amount invoices (card validation, $0 trials) are no-ops."""diff --git a/autogpt_platform/backend/backend/data/posthog_lifecycle_test.py b/autogpt_platform/backend/backend/data/posthog_lifecycle_test.pyindex 2f159977d6..ebf46b9610 100644--- a/autogpt_platform/backend/backend/data/posthog_lifecycle_test.py+++ b/autogpt_platform/backend/backend/data/posthog_lifecycle_test.py@@ -3,7 +3,7 @@ from itertools import permutationsimport pytest-from backend.data.credit import PAYMENT_FAILURE_CANCELLATION_COMMENT+from backend.data.credit import REPLACED_PLAN_CANCELLATION_COMMENTfrom backend.data.posthog_lifecycle import (LifecycleSnapshot,LifecycleUser,@@ -260,7 +260,9 @@ def test_only_our_own_cancel_comments_mean_we_canceled(comment, expected):def test_every_comment_we_cancel_with_is_recognised_as_ours():- ours = [PAYMENT_FAILURE_CANCELLATION_COMMENT] + [+ # Plans cancelled for non-payment before #15345 still carry+ # ``autogpt:payment_failed``.+ ours = ["autogpt:payment_failed", REPLACED_PLAN_CANCELLATION_COMMENT] + [reason.stripe_comment for reason in TrialRejectionReason]for comment in ours:diff --git a/autogpt_platform/backend/backend/data/stripe_invoice_payments.py b/autogpt_platform/backend/backend/data/stripe_invoice_payments.pynew file mode 100644index 0000000000..c27e2ab2c0--- /dev/null+++ b/autogpt_platform/backend/backend/data/stripe_invoice_payments.py@@ -0,0 +1,106 @@+"""Read how an invoice was paid, whatever Stripe API version shaped it.++Stripe API 2025-03-31.basil removed ``paid``, ``paid_out_of_band``, ``charge``+and ``payment_intent`` from the Invoice and moved its payments to+``invoice.payments`` (InvoicePayment objects, returned only when expanded).+Webhook payloads use the endpoint's API version while our own calls use the+SDK's pinned one, so the same handler can see either shape and must never+read a missing field as "no".+"""++import logging+from typing import Any++import stripe++from backend.data.stripe_client import stripe_call++logger = logging.getLogger(__name__)++_BASIL = "2025-03-31"+# InvoicePayment types Stripe collected itself, as opposed to a payment+# recorded out of band.+_COLLECTED_PAYMENT_TYPES = ("payment_intent", "charge")+++async def paid_by_stripe_collection(invoice: dict) -> bool | None:+ """Whether Stripe collected the paid invoice itself (card, bank debit)+ rather than it being marked paid out of band.++ None when the invoice does not say, which callers must treat as unknown.+ """+ if invoice.get("status") != "paid":+ return False+ if "paid_out_of_band" not in invoice and _payments(invoice) is None:+ invoice = await _retrieve_with_payments(invoice["id"])+ if "paid_out_of_band" in invoice:+ return invoice.get("status") == "paid" and not invoice["paid_out_of_band"]+ payments = _payments(invoice)+ if payments is None:+ return None+ return any(+ payment.get("status") == "paid"+ and _payment_source(payment).get("type") in _COLLECTED_PAYMENT_TYPES+ for payment in payments+ )+++async def payment_in_progress(invoice: dict) -> bool:+ """Whether a payment on the invoice is still settling, e.g. a bank debit.++ When the invoice does not say, it answers True: the caller then only cuts+ access and leaves the subscription and invoice for Stripe to settle.+ """+ if "payment_intent" not in invoice and _payments(invoice) is None:+ invoice = await _retrieve_with_payments(invoice["id"])+ if "payment_intent" in invoice:+ payment_intent_ids = [stripe_id(invoice.get("payment_intent"))]+ elif (payments := _payments(invoice)) is not None:+ payment_intent_ids = [+ stripe_id(_payment_source(payment).get("payment_intent"))+ for payment in payments+ if payment.get("status") != "canceled"+ ]+ else:+ logger.error(+ f"Cannot tell whether a payment on invoice {invoice.get('id')} is"+ " processing; treating it as in progress"+ )+ return True+ for payment_intent_id in filter(None, payment_intent_ids):+ payment_intent = await stripe_call(+ stripe.PaymentIntent.retrieve_async, payment_intent_id+ )+ if payment_intent.get("status") == "processing":+ return True+ return False+++def stripe_id(value: Any) -> str:+ """The id of a Stripe reference, whether it arrived as an id or expanded."""+ if isinstance(value, str):+ return value+ if isinstance(value, dict):+ return value.get("id") or ""+ return ""+++async def _retrieve_with_payments(invoice_id: str) -> dict:+ """The invoice at the SDK's version, with its payments where that has them."""+ if stripe.api_version < _BASIL:+ return dict(await stripe_call(stripe.Invoice.retrieve_async, invoice_id))+ return dict(+ await stripe_call(+ stripe.Invoice.retrieve_async, invoice_id, expand=["payments"]+ )+ )+++def _payments(invoice: dict) -> list[dict] | None:+ """``invoice.payments`` when the invoice carries it (basil or later, expanded)."""+ payments = invoice.get("payments")+ return payments.get("data") if payments else None+++def _payment_source(payment: dict) -> dict:+ return payment.get("payment") or {}diff --git a/autogpt_platform/backend/backend/data/subscription_payment_failure.py b/autogpt_platform/backend/backend/data/subscription_payment_failure.pynew file mode 100644index 0000000000..0aafe7cf49--- /dev/null+++ b/autogpt_platform/backend/backend/data/subscription_payment_failure.py@@ -0,0 +1,314 @@+"""React to a failed subscription invoice from Stripe.++Stripe delivers ``invoice.payment_failed`` late, twice, and out of order, and+a customer can have an old failed subscription next to a newer paid one. So+the handler reads the invoice and its subscription fresh from Stripe, acts+only on that one subscription, and only while the invoice is still its+latest and still unpaid. Every step is safe to repeat, and any Stripe error+is raised so the webhook retries and resumes.++An unpaid subscription that is the customer's only plan is not cancelled:+access ends at once, while Stripe keeps retrying the invoice and its payment+link stays usable. Paying it later restores access through the+subscription's own update event. One the customer has replaced with a newer+active or trialing plan is cancelled and its unpaid invoices voided instead:+paying it would reactivate the old plan, whose update event would then+cancel the newer one as a duplicate.+"""++import logging+from typing import Any++import stripe+from prisma.enums import SubscriptionTier+from prisma.models import User+from pydantic import BaseModel++from backend.data.credit import (+ REPLACED_PLAN_CANCELLATION_COMMENT,+ _invoice_subscription_id,+ replaced_by_newer_plan,+ sync_subscription_from_stripe,+)+from backend.data.stripe_client import stripe_call, stripe_list_items+from backend.data.stripe_invoice_payments import payment_in_progress, stripe_id+from backend.data.subscription_trial import get_subscription_trial+from backend.data.subscription_wallet_payment import (+ pay_invoice_from_wallet,+ settle_wallet_payment,+)+from backend.data.wallet_payment_state import (+ WalletPayment,+ WalletPaymentState,+ find_wallet_payment,+)++logger = logging.getLogger(__name__)++# Subscription states in which a failed invoice means the customer stopped+# paying. ``incomplete`` is left alone: its first payment may still be+# authenticated, and Stripe expires it on its own.+_UNPAID_STATUSES = ("past_due", "unpaid")+# Invoice states still payable through a payment link or the portal.+_PAYABLE_INVOICE_STATUSES = ("open", "uncollectible")+# Tiers with no paid plan to keep: the wallet starts no payment for them.+_UNPAID_TIERS = (SubscriptionTier.NO_TIER, SubscriptionTier.TRIAL)+++class FailedInvoice(BaseModel):+ user_id: str+ user_tier: SubscriptionTier+ customer_id: str+ invoice: dict[str, Any]+ subscription: dict[str, Any]++ @property+ def invoice_id(self) -> str:+ return self.invoice["id"]++ @property+ def sub_id(self) -> str:+ return self.subscription["id"]++ @property+ def is_latest(self) -> bool:+ return stripe_id(self.subscription.get("latest_invoice")) == self.invoice_id++ @property+ def was_replaced(self) -> bool:+ """Cancelled by us because another plan replaced it."""+ details = self.subscription.get("cancellation_details") or {}+ return (+ self.subscription.get("status") == "canceled"+ and details.get("comment") == REPLACED_PLAN_CANCELLATION_COMMENT+ )++ @property+ def waits_on_pending_update(self) -> bool:+ """Stripe applies a pending update only once its invoice is paid, and+ voids that invoice when the update lapses, so a failed payment on it+ changed nothing. A trial converting early waits like this for the+ customer's bank to confirm."""+ return self.is_latest and bool(self.subscription.get("pending_update"))++ @property+ def is_unpaid(self) -> bool:+ return (+ self.is_latest+ and not self.waits_on_pending_update+ and self.invoice.get("status") == "open"+ and self.subscription.get("status") in _UNPAID_STATUSES+ )+++async def handle_subscription_payment_failure(invoice: dict) -> None:+ """Pay the failed invoice from the wallet, or cut access until it is paid.++ - A payment still processing (e.g. a bank debit) is left to settle before+ anything else: the wallet is not touched and the tier sync cuts access.+ - A subscription a newer active or trialing plan replaced is cancelled+ and its unpaid invoices voided, never paid: paying would reactivate+ it, and its update event would then cancel the newer plan as a+ duplicate. A wallet debit already started for it is refunded.+ - An invoice a pending update waits on, or one Stripe has since paid or+ voided, covers nothing: the change it holds applies only once paid.+ - A wallet payment already started for this invoice is finished first.+ - Balance covers it → debit the wallet and mark the invoice paid, unless+ the user has no paid tier left (``NO_TIER`` or ``TRIAL``) or it is an+ unconverted trial's own invoice, which waits for the card instead.+ - Otherwise → recompute the tier from that subscription, which is+ ``past_due`` or ``unpaid`` and so gives no access unless another plan+ is active. Nothing changes in Stripe: its retries and the payment link+ stay open, and a trial's first invoice takes the same path.+ """+ failed = await _load_failed_invoice(invoice)+ if failed is None:+ return+ payment = await find_wallet_payment(failed.user_id, failed.invoice_id)+ if await _left_to_settle(failed, payment):+ return+ replaced = failed.is_unpaid and await replaced_by_newer_plan(+ failed.customer_id, failed.subscription+ )+ if payment is not None:+ await settle_wallet_payment(+ failed.user_id, failed.invoice_id, may_pay=failed.is_unpaid and not replaced+ )+ if replaced:+ await _end_replaced_subscription(failed)+ return+ if failed.was_replaced:+ # An earlier delivery cancelled it but did not finish voiding. Any of+ # its invoices' events may finish it, not only the latest one's.+ await _void_unpaid_invoices(failed.sub_id)+ return+ if payment is not None:+ return+ if not failed.is_unpaid:+ logger.info(+ f"Payment failure for invoice {failed.invoice_id} is stale: invoice"+ f" {failed.invoice.get('status')}, subscription {failed.sub_id}"+ f" {failed.subscription.get('status')}, latest={failed.is_latest},"+ f" pending update={failed.waits_on_pending_update}"+ )+ return+ if failed.invoice.get("amount_due", 0) <= 0:+ return+ if await _wallet_may_start_payment(failed) and await pay_invoice_from_wallet(+ failed.user_id, failed.customer_id, failed.sub_id, failed.invoice+ ):+ return+ logger.info(+ f"Wallet did not pay invoice {failed.invoice_id} of user"+ f" {failed.user_id}; cutting access while Stripe retries subscription"+ f" {failed.sub_id}"+ )+ await sync_subscription_from_stripe(failed.subscription)+++async def _left_to_settle(failed: FailedInvoice, payment: WalletPayment | None) -> bool:+ """Leave the invoice alone while a payment on it is still processing.++ Stripe will not mark the invoice paid while it settles, so a wallet+ payment would only fail and retry; that payment's own success or failure+ event decides what happens next. Access is still cut by the tier sync.+ """+ wallet_unfinished = (+ payment is not None and payment.state == WalletPaymentState.DEBITED+ )+ if not (failed.is_unpaid or wallet_unfinished):+ return False+ if not await payment_in_progress(failed.invoice):+ return False+ logger.info(+ f"A payment on invoice {failed.invoice_id} is still processing;"+ f" cutting access but leaving subscription {failed.sub_id} to settle"+ )+ await sync_subscription_from_stripe(failed.subscription)+ return True+++async def _wallet_may_start_payment(failed: FailedInvoice) -> bool:+ """An unconverted trial's first invoice stays open for authentication or+ card repair, and a user without a paid tier has no plan to keep, so+ neither spends wallet credit. A payment already started is still+ finished by ``settle_wallet_payment``."""+ if failed.user_tier in _UNPAID_TIERS:+ return False+ trial = await get_subscription_trial(failed.user_id)+ return not (+ trial is not None+ and trial.converted_at is None+ and trial.subscription_id == failed.sub_id+ )+++async def _load_failed_invoice(invoice: dict) -> FailedInvoice | None:+ customer_id = invoice.get("customer")+ sub_id = _invoice_subscription_id(invoice)+ invoice_id = invoice.get("id") or ""+ if not customer_id or not sub_id or not invoice_id:+ return None+ user = await User.prisma().find_first(where={"stripeCustomerId": customer_id})+ if not user:+ logger.warning(f"Payment failure for unknown Stripe customer {customer_id}")+ return None+ if user.subscriptionTier == SubscriptionTier.ENTERPRISE:+ # Admin-managed; a self-service failure must not touch it.+ return None+ fresh_invoice = dict(await stripe_call(stripe.Invoice.retrieve_async, invoice_id))+ subscription = dict(await stripe_call(stripe.Subscription.retrieve_async, sub_id))+ if (+ fresh_invoice.get("customer") != customer_id+ or subscription.get("customer") != customer_id+ ):+ logger.error(+ f"Invoice {invoice_id} or subscription {sub_id} does not belong to"+ f" customer {customer_id}; ignoring the payment failure"+ )+ return None+ return FailedInvoice(+ user_id=user.id,+ user_tier=user.subscriptionTier or SubscriptionTier.NO_TIER,+ customer_id=customer_id,+ invoice=fresh_invoice,+ subscription=subscription,+ )+++async def _end_replaced_subscription(failed: FailedInvoice) -> None:+ logger.info(+ f"Subscription {failed.sub_id} of user {failed.user_id} was replaced by"+ f" another plan; cancelling it and voiding invoice {failed.invoice_id}"+ )+ # Cancel first: Stripe stops retrying invoices of a cancelled subscription.+ # If voiding then fails, the retry finishes it from the cancelled state.+ canceled = await _cancel_subscription(failed.sub_id)+ await _void_unpaid_invoices(failed.sub_id)+ await sync_subscription_from_stripe(canceled)+++async def end_revived_plan(sub_id: str) -> None:+ """End an old plan that was paid after a newer plan replaced it.++ Stripe's card retry or the payment link can pay an old ``past_due`` plan+ before any failure event reaches us. It is ended like a replaced plan, so+ its update event never cancels the newer one. The payment that revived it+ stays with Stripe and needs a manual refund.+ """+ logger.error(+ f"Subscription {sub_id} was paid after a newer plan replaced it;"+ " cancelling it. Its last payment needs a manual refund"+ )+ await _cancel_subscription(sub_id)+ await _void_unpaid_invoices(sub_id)+++async def _cancel_subscription(sub_id: str) -> dict:+ try:+ canceled = await stripe_call(+ stripe.Subscription.cancel_async,+ sub_id,+ cancellation_details={"comment": REPLACED_PLAN_CANCELLATION_COMMENT},+ )+ return dict(canceled)+ except stripe.StripeError:+ current = dict(await stripe_call(stripe.Subscription.retrieve_async, sub_id))+ if current.get("status") == "canceled":+ return current+ raise+++async def _void_unpaid_invoices(sub_id: str) -> None:+ """Close a replaced subscription's unpaid invoices.++ Cancelling only pauses automatic collection; an ``open`` or+ ``uncollectible`` invoice stays payable through its payment link and the+ portal, which would charge for a plan the customer has replaced. An+ invoice with a payment still processing is left for that payment's own+ success or failure event.+ """+ for status in _PAYABLE_INVOICE_STATUSES:+ invoices = await stripe_call(+ stripe.Invoice.list_async, subscription=sub_id, status=status, limit=100+ )+ async for invoice in stripe_list_items(invoices):+ await _void_invoice(sub_id, dict(invoice))+++async def _void_invoice(sub_id: str, invoice: dict) -> None:+ invoice_id: str = invoice["id"]+ if await payment_in_progress(invoice):+ logger.warning(f"Not voiding invoice {invoice_id}: payment processing")+ return+ try:+ await stripe_call(stripe.Invoice.void_invoice_async, invoice_id)+ except stripe.StripeError:+ current = await stripe_call(stripe.Invoice.retrieve_async, invoice_id)+ if current.get("status") in _PAYABLE_INVOICE_STATUSES:+ raise+ if current.get("status") == "paid":+ logger.error(+ f"Invoice {invoice_id} was paid after subscription {sub_id}"+ " was cancelled as replaced; needs a manual fix"+ )diff --git a/autogpt_platform/backend/backend/data/subscription_payment_failure_test.py b/autogpt_platform/backend/backend/data/subscription_payment_failure_test.pynew file mode 100644index 0000000000..25a5c192f3--- /dev/null+++ b/autogpt_platform/backend/backend/data/subscription_payment_failure_test.py@@ -0,0 +1,1108 @@+"""Failed-payment handling against a fake Stripe account and wallet ledger.++The fakes hold state, so a test can deliver the same event twice, fail a+Stripe call part way, and check what a retry does with what was left behind.+The tier sync is the real one, so a test sees the tier the customer ends up on.+Fake Stripe shapes each invoice by API version like the real one: before+2025-03-31.basil it carries ``paid_out_of_band`` and ``payment_intent``; from+basil on it has neither, and ``payments`` only when expanded.+"""++import asyncio+from collections.abc import Awaitable, Callable+from contextlib import ExitStack, asynccontextmanager+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+import stripe+from prisma.enums import CreditTransactionType, SubscriptionTier+from prisma.errors import UniqueViolationError++from backend.data.credit import (+ REPLACED_PLAN_CANCELLATION_COMMENT,+ sync_subscription_from_stripe,+)+from backend.data.subscription_payment_failure import (+ handle_subscription_payment_failure,+)+from backend.data.subscription_wallet_payment import (+ reconcile_wallet_payment_on_paid_invoice,+)+from backend.util.exceptions import InsufficientBalanceError++CUSTOMER = "cus_1"+USER = "user-1"+ACACIA = "2025-02-24.acacia"+ENDIVE = "2026-09-30.endive"+++def _is_basil(api_version: str) -> bool:+ return api_version >= "2025-03-31"+++class FakeStripe:+ def __init__(self) -> None:+ self.invoices: dict[str, dict] = {}+ self.subscriptions: dict[str, dict] = {}+ self.payment_intents: dict[str, dict] = {}+ self.cancelled: list[str] = []+ self.voided: list[str] = []+ self.last_status: dict[str, str] = {}+ self.paid_out_of_band: list[str] = []+ self.fail_next: dict[str, int] = {}+ # Runs after Stripe applied a pay, before the response returns.+ self.after_pay: Callable[[], Awaitable[None]] | None = None++ def add_subscription(+ self,+ sub_id: str,+ status: str,+ latest_invoice: str = "",+ price: str = "price_pro",+ created: int | None = None,+ ):+ self.subscriptions[sub_id] = {+ "id": sub_id,+ "customer": CUSTOMER,+ "status": status,+ "latest_invoice": latest_invoice or None,+ "metadata": {},+ "items": {"data": [{"price": {"id": price}}]},+ }+ # Stripe stamps every subscription; by default, in the order added.+ self.subscriptions[sub_id]["created"] = (+ created if created is not None else len(self.subscriptions)+ )++ def add_invoice(+ self,+ invoice_id: str,+ sub_id: str,+ status: str = "open",+ amount_due: int = 2000,+ payment_intent: str | None = None,+ ):+ self.invoices[invoice_id] = {+ "id": invoice_id,+ "customer": CUSTOMER,+ "subscription": sub_id,+ "status": status,+ "amount_due": amount_due,+ "paid_out_of_band": False,+ "payment_intent": payment_intent,+ "_payments": (+ [_invoice_payment("open", payment_intent)] if payment_intent else []+ ),+ }+ return self.view(invoice_id)++ def view(self, invoice_id: str, api_version: str | None = None, expand=()) -> dict:+ """The invoice as Stripe returns it at ``api_version`` (the SDK's by+ default; pass the endpoint's for a webhook payload)."""+ invoice = dict(self.invoices[invoice_id])+ payments = invoice.pop("_payments")+ if not _is_basil(api_version or stripe.api_version):+ return invoice+ del invoice["paid_out_of_band"], invoice["payment_intent"]+ if "payments" in expand:+ invoice["payments"] = {"data": [dict(p) for p in payments]}+ return invoice++ def card_pays(self, invoice_id: str, pi_id: str = "pi_card") -> None:+ """Stripe's own retry of the customer's card succeeds."""+ invoice = self.invoices[invoice_id]+ invoice.update(status="paid", paid_out_of_band=False, payment_intent=pi_id)+ invoice["_payments"].append(_invoice_payment("paid", pi_id))+ sub = self.subscriptions[invoice["subscription"]]+ if sub["latest_invoice"] == invoice_id:+ sub["status"] = "active"++ def card_fails(self, invoice_id: str) -> None:+ """Stripe's own retry of the customer's card fails again."""+ invoice = self.invoices[invoice_id]+ assert invoice["status"] == "open"+ self.subscriptions[invoice["subscription"]]["status"] = "past_due"++ def _maybe_fail(self, call: str) -> None:+ if self.fail_next.get(call, 0) > 0:+ self.fail_next[call] -= 1+ raise stripe.APIConnectionError(f"{call} failed")++ async def retrieve_invoice(self, invoice_id: str, expand=()):+ return self.view(invoice_id, expand=expand)++ async def retrieve_subscription(self, sub_id: str):+ return dict(self.subscriptions[sub_id])++ async def retrieve_payment_intent(self, pi_id: str):+ return dict(self.payment_intents[pi_id])++ async def pay(self, invoice_id: str, paid_out_of_band: bool = False):+ self._maybe_fail("pay")+ invoice = self.invoices[invoice_id]+ if invoice["status"] not in ("open", "uncollectible"):+ raise stripe.InvalidRequestError("Invoice is already paid", None)+ pi = self.payment_intents.get(invoice["payment_intent"] or "")+ if pi and pi["status"] == "processing":+ raise stripe.InvalidRequestError("A payment is in progress", None)+ invoice.update(status="paid", paid_out_of_band=paid_out_of_band)+ self.paid_out_of_band.append(invoice_id)+ sub = self.subscriptions[invoice["subscription"]]+ if sub["latest_invoice"] == invoice_id:+ sub["status"] = "active"+ if self.after_pay:+ await self.after_pay()+ # The request reached Stripe, but its response was lost.+ self._maybe_fail("pay_response")+ return self.view(invoice_id)++ async def cancel(self, sub_id: str, **params):+ self._maybe_fail("cancel")+ sub = self.subscriptions[sub_id]+ if sub["status"] == "canceled":+ raise stripe.InvalidRequestError("already canceled", None)+ sub.update(+ status="canceled", cancellation_details=params.get("cancellation_details")+ )+ self.cancelled.append(sub_id)+ return dict(sub)++ async def list_invoices(self, subscription: str, status: str, limit: int):+ page = MagicMock()+ page.data = [+ stripe.Invoice.construct_from(self.view(inv["id"]), "sk_test")+ for inv in self.invoices.values()+ if inv["subscription"] == subscription and inv["status"] == status+ ]+ page.has_more = False+ return page++ async def void(self, invoice_id: str):+ self._maybe_fail("void")+ invoice = self.invoices[invoice_id]+ if invoice["status"] not in ("open", "uncollectible"):+ raise stripe.InvalidRequestError("not open", None)+ invoice["status"] = "void"+ self.voided.append(invoice_id)+ return self.view(invoice_id)++ async def list_subscriptions(self, customer: str, status: str, limit: int):+ page = MagicMock()+ page.data = [+ dict(sub)+ for sub in self.subscriptions.values()+ if sub["customer"] == customer and sub["status"] == status+ ]+ page.has_more = False+ return page+++def _invoice_payment(status: str, pi_id: str) -> dict:+ return {+ "object": "invoice_payment",+ "status": status,+ "payment": {"type": "payment_intent", "payment_intent": pi_id},+ }+++class FakeLedger:+ """The wallet: a balance plus transactions unique by key, like the DB,+ which checks the balance before the key."""++ def __init__(self, balance: int) -> None:+ self.balance = balance+ self.transactions: dict[str, int] = {}+ self.metadata: dict[str, dict] = {}+ self.fail_next_update = 0++ async def add_transaction(self, *, user_id, amount, transaction_key, **kwargs):+ if kwargs.get("fail_insufficient_credits") and self.balance + amount < 0:+ raise InsufficientBalanceError(+ message="no balance",+ user_id=user_id,+ balance=self.balance,+ amount=amount,+ )+ if transaction_key in self.transactions:+ raise UniqueViolationError({"error": "duplicate key"})+ self.transactions[transaction_key] = amount+ self.metadata[transaction_key] = dict(kwargs["metadata"].data)+ self.balance += amount+ return self.balance, transaction_key++ async def find_many(self, where):+ return [+ MagicMock(+ transactionKey=key,+ amount=self.transactions[key],+ type=CreditTransactionType.SUBSCRIPTION,+ metadata=dict(self.metadata[key]),+ )+ for key in where["transactionKey"]["in"]+ if key in self.transactions+ ]++ async def update(self, where, data):+ if self.fail_next_update > 0:+ self.fail_next_update -= 1+ raise ConnectionError("database went away")+ key = where["creditTransactionIdentifier"]["transactionKey"]+ self.metadata[key] = dict(data["metadata"].data)++ def settled(self, invoice_id: str) -> bool:+ return self.metadata.get(invoice_id, {}).get("wallet_payment") == "settled"+++class World:+ def __init__(self, balance: int = 0, api_version: str = ACACIA) -> None:+ self.api_version = api_version+ self.stripe = FakeStripe()+ self.ledger = FakeLedger(balance)+ self.synced: list[dict] = []+ self.tier = SubscriptionTier.PRO+ self.tier_writes: list[SubscriptionTier] = []+ self.locks: dict[str, asyncio.Lock] = {}+ self._stack = ExitStack()++ @asynccontextmanager+ async def _lock(self, invoice_id: str):+ async with self.locks.setdefault(invoice_id, asyncio.Lock()):+ yield++ async def _sync(self, subscription: dict, **kwargs):+ self.synced.append(dict(subscription))+ await sync_subscription_from_stripe(subscription, **kwargs)++ async def _find_user(self, **kwargs):+ return MagicMock(id=USER, subscriptionTier=self.tier, stripeCustomerId=CUSTOMER)++ async def _locked_user(self, user_id: str, tx):+ return await self._find_user()++ async def _tx_update(self, where, data):+ await self._set_tier(where["id"], data["subscriptionTier"])++ @asynccontextmanager+ async def _transaction(self):+ yield MagicMock(user=MagicMock(update=self._tx_update))++ async def _set_tier(self, user_id: str, tier: SubscriptionTier, **kwargs):+ self.tier = tier+ self.tier_writes.append(tier)++ def subscription_updated(self, sub_id: str) -> Awaitable[None]:+ """Stripe's ``customer.subscription.updated`` for ``sub_id``."""+ return sync_subscription_from_stripe(dict(self.stripe.subscriptions[sub_id]))++ def __enter__(self):+ users = MagicMock(find_first=self._find_user)+ fs = self.stripe+ patches = [+ patch("backend.data.credit.User.prisma", return_value=users),+ patch("backend.data.credit.set_subscription_tier", self._set_tier),+ patch(+ "backend.data.credit.build_price_to_tier_map",+ new=AsyncMock(+ return_value={+ "price_pro": SubscriptionTier.PRO,+ "price_max": SubscriptionTier.MAX,+ }+ ),+ ),+ patch("backend.data.credit._track_billing_event"),+ # Paid-tier writes go through #15208's activation boundary; its+ # evidence and locks are tested in subscription_activation_*.+ patch("backend.data.credit.transaction", self._transaction),+ patch(+ "backend.data.credit.subscription_activation.lock_activation_user",+ self._locked_user,+ ),+ patch(+ "backend.data.credit.subscription_activation.publish_initial_pro_activation",+ new=AsyncMock(return_value=True),+ ),+ patch(+ "backend.data.credit.get_subscription_trial",+ new=AsyncMock(return_value=None),+ ),+ patch(+ "backend.data.subscription_payment_failure.get_subscription_trial",+ new=AsyncMock(return_value=None),+ ),+ patch("backend.data.credit.schedule_posthog_lifecycle_sync"),+ patch(+ "backend.data.credit.get_pending_subscription_change",+ new=MagicMock(),+ ),+ patch.object(stripe.Subscription, "list_async", fs.list_subscriptions),+ patch(+ "backend.data.subscription_payment_failure.User.prisma",+ return_value=users,+ ),+ patch(+ "backend.data.subscription_wallet_payment.User.prisma",+ return_value=users,+ ),+ patch(+ "backend.data.wallet_payment_state.CreditTransaction.prisma",+ return_value=MagicMock(+ find_many=self.ledger.find_many, update=self.ledger.update+ ),+ ),+ patch.object(stripe, "api_version", self.api_version),+ patch(+ "backend.data.subscription_wallet_payment._wallet_payment_lock",+ self._lock,+ ),+ patch(+ "backend.data.subscription_wallet_payment.UserCredit._add_transaction",+ side_effect=self.ledger.add_transaction,+ ),+ patch(+ "backend.data.subscription_payment_failure.sync_subscription_from_stripe",+ side_effect=self._sync,+ ),+ patch.object(stripe.Invoice, "retrieve_async", fs.retrieve_invoice),+ patch.object(stripe.Invoice, "pay_async", fs.pay),+ patch.object(stripe.Invoice, "list_async", fs.list_invoices),+ patch.object(stripe.Invoice, "void_invoice_async", fs.void),+ patch.object(+ stripe.Subscription, "retrieve_async", fs.retrieve_subscription+ ),+ patch.object(stripe.Subscription, "cancel_async", fs.cancel),+ patch.object(+ stripe.PaymentIntent, "retrieve_async", fs.retrieve_payment_intent+ ),+ ]+ for p in patches:+ self._stack.enter_context(p)+ return self++ def __exit__(self, *exc):+ self._stack.close()+++def _renewal_failed(world: World, sub_id="sub_1", invoice_id="in_1", **kw) -> dict:+ world.stripe.add_subscription(sub_id, "past_due", latest_invoice=invoice_id)+ return world.stripe.add_invoice(invoice_id, sub_id, **kw)+++async def test_unpaid_renewal_cuts_access_and_leaves_the_bill_payable():+ with World(balance=0) as world:+ event = _renewal_failed(world)+ await handle_subscription_payment_failure(event)++ assert world.tier == SubscriptionTier.NO_TIER+ assert world.stripe.cancelled == []+ assert world.stripe.voided == []+ assert world.stripe.subscriptions["sub_1"]["status"] == "past_due"+ assert world.stripe.invoices["in_1"]["status"] == "open"+ assert world.ledger.transactions == {}+++async def test_stripe_retry_paying_later_restores_access():+ with World(balance=0) as world:+ event = _renewal_failed(world)+ await handle_subscription_payment_failure(event)+ assert world.tier == SubscriptionTier.NO_TIER++ world.stripe.card_pays("in_1")+ await reconcile_wallet_payment_on_paid_invoice(world.stripe.view("in_1"))+ await world.subscription_updated("sub_1")+ # The original failure, delivered again after the payment.+ await handle_subscription_payment_failure(event)++ assert world.tier == SubscriptionTier.PRO+ assert world.tier_writes == [SubscriptionTier.NO_TIER, SubscriptionTier.PRO]+ assert world.ledger.transactions == {}+++async def test_each_failed_retry_keeps_access_cut_without_cancelling():+ with World(balance=0) as world:+ event = _renewal_failed(world)+ await handle_subscription_payment_failure(event)+ world.stripe.card_fails("in_1")+ await handle_subscription_payment_failure(event)++ assert world.tier == SubscriptionTier.NO_TIER+ assert world.tier_writes == [SubscriptionTier.NO_TIER]+ assert world.stripe.cancelled == []+ assert world.stripe.voided == []+++async def test_retry_after_access_was_cut_is_not_paid_from_the_wallet():+ """Once the tier is cut the user has no paid plan to keep, so a later+ retry waits for the card or payment link even after a top-up (#15208)."""+ with World(balance=0) as world:+ event = _renewal_failed(world)+ await handle_subscription_payment_failure(event)+ world.ledger.balance = 5000+ world.stripe.card_fails("in_1")+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {}+ assert world.stripe.paid_out_of_band == []+ assert world.stripe.cancelled == []+ assert world.stripe.invoices["in_1"]["status"] == "open"+ assert world.tier == SubscriptionTier.NO_TIER+++async def test_a_started_wallet_payment_is_finished_after_access_was_cut():+ """The guard only stops a new debit: one taken before the tier was cut+ still pays the invoice it was taken for."""+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.StripeError):+ await handle_subscription_payment_failure(event)+ world.tier = SubscriptionTier.NO_TIER+ await handle_subscription_payment_failure(event)+ await world.subscription_updated("sub_1")++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.stripe.paid_out_of_band == ["in_1"]+ assert world.tier == SubscriptionTier.PRO+++async def test_an_unconverted_trials_first_invoice_is_not_paid_from_the_wallet():+ """The trial's conversion invoice stays open for authentication or a card+ fix; it never spends wallet credit (#15208)."""+ with World(balance=50000) as world:+ event = _renewal_failed(world)+ trial = MagicMock(converted_at=None, subscription_id="sub_1")+ with patch(+ "backend.data.subscription_payment_failure.get_subscription_trial",+ new=AsyncMock(return_value=trial),+ ):+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {}+ assert world.stripe.paid_out_of_band == []+ assert world.stripe.cancelled == []+ assert world.stripe.invoices["in_1"]["status"] == "open"+++async def test_old_failure_never_cancels_a_newer_active_subscription():+ """The reproduced defect: an old past-due subscription's failed retry+ used to cancel the customer's newer, paid subscription instead."""+ with World(balance=0) as world:+ event = _renewal_failed(world, sub_id="sub_old", invoice_id="in_old")+ world.stripe.add_subscription("sub_new", "active", latest_invoice="in_new")+ world.stripe.add_invoice("in_new", "sub_new", status="paid")+ await handle_subscription_payment_failure(event)++ # The replaced old plan is ended so Stripe cannot revive it, and the+ # tier is recomputed from it, so the sync sees sub_new and keeps the+ # paid tier, instead of a blanket NO_TIER write.+ assert world.stripe.cancelled == ["sub_old"]+ assert world.stripe.voided == ["in_old"]+ assert world.stripe.subscriptions["sub_new"]["status"] == "active"+ assert [s["id"] for s in world.synced] == ["sub_old"]+ assert world.synced[0]["status"] == "canceled"+ assert world.tier == SubscriptionTier.PRO+ assert world.tier_writes == []+++def _superseded_by_a_newer_plan(+ world: World, balance: int, newer_price: str = "price_max"+) -> dict:+ """An old PRO subscription went past due, and the customer then bought+ MAX through Checkout, whose cleanup skips past-due subscriptions."""+ world.ledger.balance = balance+ world.tier = SubscriptionTier.MAX+ world.stripe.add_subscription(+ "sub_new", "active", latest_invoice="in_new", price=newer_price, created=2+ )+ world.stripe.add_invoice("in_new", "sub_new", status="paid")+ world.stripe.add_subscription("sub_old", "past_due", "in_old", created=1)+ return world.stripe.add_invoice("in_old", "sub_old")+++async def test_replaced_subscription_is_cancelled_and_voided_never_paid():+ """Paying the old invoice would reactivate the old plan, and its update+ event would cancel the newer one as a stale duplicate."""+ with World() as world:+ event = _superseded_by_a_newer_plan(world, balance=5000)+ await handle_subscription_payment_failure(event)+ # Stripe's events for the cancel.+ await world.subscription_updated("sub_old")++ assert world.ledger.transactions == {}+ assert world.stripe.paid_out_of_band == []+ assert world.stripe.cancelled == ["sub_old"]+ assert world.stripe.subscriptions["sub_old"]["cancellation_details"] == {+ "comment": REPLACED_PLAN_CANCELLATION_COMMENT+ }+ assert world.stripe.voided == ["in_old"]+ assert world.stripe.subscriptions["sub_new"]["status"] == "active"+ assert world.tier == SubscriptionTier.MAX+ assert world.tier_writes == []+++ "newer_price,tier",+ [("price_max", SubscriptionTier.MAX), ("price_pro", SubscriptionTier.PRO)],+)+async def test_old_plan_paid_after_a_newer_plan_replaced_it_is_ended(+ newer_price: str, tier: SubscriptionTier+):+ """Stripe's card retry can pay an old past-due plan before any failure+ event reaches us. Its update event ends the old plan, instead of+ cancelling the newer one as a stale duplicate (or, on the same tier,+ billing the customer for both)."""+ with World() as world:+ _superseded_by_a_newer_plan(world, balance=0, newer_price=newer_price)+ world.tier = tier+ world.stripe.card_pays("in_old")+ await reconcile_wallet_payment_on_paid_invoice(world.stripe.view("in_old"))+ await world.subscription_updated("sub_old")+ # Stripe's events for the cancel.+ await world.subscription_updated("sub_old")++ assert world.stripe.cancelled == ["sub_old"]+ assert world.stripe.subscriptions["sub_old"]["cancellation_details"] == {+ "comment": REPLACED_PLAN_CANCELLATION_COMMENT+ }+ assert world.stripe.subscriptions["sub_new"]["status"] == "active"+ assert world.tier == tier+ assert world.tier_writes == []+++async def test_a_newer_plans_own_update_still_ends_the_older_plan():+ """The check looks for a newer plan only: the newer plan's own update+ still cleans up an older active one, as a Checkout upgrade does."""+ with World() as world:+ world.stripe.add_subscription("sub_old", "active", "in_old", created=1)+ world.stripe.add_subscription(+ "sub_new", "active", "in_new", price="price_max", created=2+ )+ await world.subscription_updated("sub_new")++ assert world.stripe.cancelled == ["sub_old"]+ assert world.tier == SubscriptionTier.MAX+++async def test_an_older_plan_left_active_does_not_replace_a_newer_failing_one():+ """If a Checkout cleanup failed and left the older plan active, a failure+ on the newer plan must not end the newer plan as replaced."""+ with World(balance=0) as world:+ world.tier = SubscriptionTier.MAX+ world.stripe.add_subscription("sub_old", "active", "in_old", created=1)+ world.stripe.add_subscription(+ "sub_new", "past_due", "in_new", price="price_max", created=2+ )+ event = world.stripe.add_invoice("in_new", "sub_new")+ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == []+ assert world.stripe.voided == []+ assert world.stripe.subscriptions["sub_new"]["status"] == "past_due"+ assert world.stripe.invoices["in_new"]["status"] == "open"+++async def test_a_trialing_plan_also_replaces_an_old_subscription():+ with World() as world:+ event = _renewal_failed(world, sub_id="sub_old", invoice_id="in_old")+ world.stripe.add_subscription("sub_new", "trialing", price="price_max")+ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == ["sub_old"]+ assert world.stripe.voided == ["in_old"]+ assert world.stripe.subscriptions["sub_new"]["status"] == "trialing"+++async def test_a_cancel_pending_trial_does_not_replace_an_unpaid_plan():+ """A cancelled trial ends at trial_end without an invoice, so it is not a+ plan that replaces the failed one: Stripe keeps retrying the failed one."""+ with World(balance=0) as world:+ event = _renewal_failed(world, sub_id="sub_paid", invoice_id="in_paid")+ world.stripe.add_subscription("sub_trial", "trialing")+ world.stripe.subscriptions["sub_trial"]["cancel_at_period_end"] = True+ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == []+ assert world.stripe.voided == []+ assert world.stripe.subscriptions["sub_paid"]["status"] == "past_due"+ assert world.stripe.invoices["in_paid"]["status"] == "open"+ assert world.stripe.subscriptions["sub_trial"]["status"] == "trialing"+ assert [s["id"] for s in world.synced] == ["sub_paid"]+++async def test_new_plans_declined_first_payment_leaves_a_cancel_pending_trial():+ """A card declined in Checkout leaves the new plan incomplete for the+ customer to retry; no plan lapsed, so the trial and wallet are untouched."""+ with World(balance=50000) as world:+ world.tier = SubscriptionTier.TRIAL+ world.stripe.add_subscription("sub_trial", "trialing")+ world.stripe.subscriptions["sub_trial"]["cancel_at_period_end"] = True+ world.stripe.add_subscription(+ "sub_max", "incomplete", latest_invoice="in_first", price="price_max"+ )+ event = world.stripe.add_invoice("in_first", "sub_max", amount_due=32000)+ event["billing_reason"] = "subscription_create"+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {}+ assert world.stripe.cancelled == []+ assert world.synced == []+ assert world.tier == SubscriptionTier.TRIAL+++async def test_declined_in_place_trial_conversion_leaves_the_trial_alone():+ """The in-place "Subscribe now" uses error_if_incomplete; a decline+ leaves the subscription trialing, so its failed invoice is no renewal to+ pay from the wallet or cut access for."""+ with World(balance=50000) as world:+ world.tier = SubscriptionTier.TRIAL+ world.stripe.add_subscription("sub_trial", "trialing", latest_invoice="in_now")+ world.stripe.subscriptions["sub_trial"]["cancel_at_period_end"] = True+ event = world.stripe.add_invoice("in_now", "sub_trial", amount_due=5000)+ event["billing_reason"] = "subscription_update"+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {}+ assert world.stripe.paid_out_of_band == []+ assert world.stripe.cancelled == []+ assert world.synced == []+ assert world.tier == SubscriptionTier.TRIAL++[email protected]("invoice_status", ["open", "void", "paid"])+async def test_a_held_trial_conversion_covers_nothing(invoice_status: str):+ """A conversion the bank must confirm is a pending update: Stripe applies+ it only once its invoice is paid, and voids the invoice when it lapses.+ The failure event can arrive before or after either, and none of them+ is a lapsed plan to pay from the wallet or cut access for."""+ with World(balance=50000) as world:+ world.tier = SubscriptionTier.TRIAL+ world.stripe.add_subscription("sub_trial", "trialing", latest_invoice="in_held")+ world.stripe.subscriptions["sub_trial"].update(+ cancel_at_period_end=True, pending_update={"expires_at": 1}+ )+ event = world.stripe.add_invoice(+ "in_held", "sub_trial", status=invoice_status, amount_due=32000+ )+ event["billing_reason"] = "subscription_update"+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {}+ assert world.stripe.paid_out_of_band == []+ assert world.stripe.cancelled == []+ assert world.stripe.voided == []+ assert world.synced == []+ assert world.tier == SubscriptionTier.TRIAL+++async def test_a_held_change_on_a_past_due_plan_is_neither_paid_nor_cancelled():+ """Paying a pending update's invoice applies the change it holds, so the+ wallet must not pay it even on a past-due plan, and the plan is not one+ another plan replaced: only its renewal's own failure decides that."""+ with World(balance=50000) as world:+ world.stripe.add_subscription("sub_new", "active", price="price_max")+ _renewal_failed(world, sub_id="sub_old", invoice_id="in_held")+ world.stripe.subscriptions["sub_old"]["pending_update"] = {"expires_at": 1}+ event = world.stripe.view("in_held")+ event["billing_reason"] = "subscription_update"+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {}+ assert world.stripe.paid_out_of_band == []+ assert world.stripe.cancelled == []+ assert world.stripe.voided == []+ assert world.synced == []+++async def test_a_trial_converted_to_another_plan_loses_it_when_its_renewal_fails():+ """A trial ended early on Max carries Max in its conversion metadata. Once+ converted, the trial reconcile grants nothing more, so a failed renewal+ cuts access like any plan's, and the card paying later restores Max."""+ with World(balance=0) as world:+ world.tier = SubscriptionTier.MAX+ world.stripe.add_subscription(+ "sub_trial", "past_due", latest_invoice="in_renewal", price="price_max"+ )+ world.stripe.subscriptions["sub_trial"]["metadata"] = {+ "trial_enrollment_id": "t1",+ "trial_conversion_price_id": "price_max",+ "trial_conversion_tier": "MAX",+ }+ event = world.stripe.add_invoice("in_renewal", "sub_trial", amount_due=32000)+ event["billing_reason"] = "subscription_cycle"++ async def converted_trial(user_id: str, sub_id: str):+ return dict(world.stripe.subscriptions[sub_id]), None++ with patch(+ "backend.data.credit.subscription_trial_stripe.reconcile_trial_subscription",+ side_effect=converted_trial,+ ):+ await handle_subscription_payment_failure(event)+ assert world.tier == SubscriptionTier.NO_TIER+ assert world.stripe.cancelled == []+ assert world.stripe.invoices["in_renewal"]["status"] == "open"++ world.stripe.card_pays("in_renewal")+ await world.subscription_updated("sub_trial")++ assert world.tier == SubscriptionTier.MAX+++async def test_started_wallet_payment_is_refunded_once_a_newer_plan_replaced_it():+ with World(balance=5000) as world:+ event = _renewal_failed(world, sub_id="sub_old", invoice_id="in_old")+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ world.stripe.add_subscription(+ "sub_new", "active", latest_invoice="in_new", price="price_max"+ )++ await handle_subscription_payment_failure(event)+ await handle_subscription_payment_failure(event)++ assert world.stripe.paid_out_of_band == []+ assert world.ledger.balance == 5000+ assert world.ledger.transactions == {"in_old": -2000, "in_old:wallet-refund": 2000}+ assert world.stripe.cancelled == ["sub_old"]+ assert world.stripe.voided == ["in_old"]+ assert world.stripe.subscriptions["sub_new"]["status"] == "active"+++async def test_replaced_subscription_void_failure_is_resumed_by_the_retry():+ with World() as world:+ event = _superseded_by_a_newer_plan(world, balance=0)+ world.stripe.fail_next["void"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ assert world.stripe.cancelled == ["sub_old"]+ assert world.stripe.invoices["in_old"]["status"] == "open"++ await handle_subscription_payment_failure(event)+ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == ["sub_old"]+ assert world.stripe.voided == ["in_old"]+++async def test_an_older_invoice_event_also_finishes_voiding_a_replaced_subscription():+ with World() as world:+ world.stripe.add_invoice("in_older", "sub_old", status="uncollectible")+ event = _superseded_by_a_newer_plan(world, balance=0)+ world.stripe.fail_next["void"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ assert world.stripe.cancelled == ["sub_old"]++ await handle_subscription_payment_failure(world.stripe.view("in_older"))++ assert sorted(world.stripe.voided) == ["in_old", "in_older"]+ assert world.stripe.cancelled == ["sub_old"]+++async def test_replaced_subscription_cancel_failure_raises_and_is_retried():+ with World() as world:+ event = _superseded_by_a_newer_plan(world, balance=0)+ world.stripe.fail_next["cancel"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ assert world.stripe.voided == []++ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == ["sub_old"]+ assert world.stripe.voided == ["in_old"]+++async def test_replaced_subscription_also_voids_its_uncollectible_invoices():+ with World() as world:+ event = _superseded_by_a_newer_plan(world, balance=0)+ world.stripe.add_invoice("in_older", "sub_old", status="uncollectible")+ await handle_subscription_payment_failure(event)++ assert sorted(world.stripe.voided) == ["in_old", "in_older"]+++async def test_replaced_subscription_keeps_an_invoice_whose_payment_is_processing():+ with World() as world:+ event = _superseded_by_a_newer_plan(world, balance=0)+ world.stripe.add_invoice("in_older", "sub_old", payment_intent="pi_2")+ world.stripe.payment_intents["pi_2"] = {"id": "pi_2", "status": "processing"}+ await handle_subscription_payment_failure(event)++ assert world.stripe.voided == ["in_old"]+ assert world.stripe.invoices["in_older"]["status"] == "open"+++async def test_replaced_subscription_whose_payment_is_processing_is_left_alone():+ with World() as world:+ world.ledger.balance = 0+ world.stripe.add_subscription("sub_new", "active", price="price_max")+ event = _renewal_failed(+ world, sub_id="sub_old", invoice_id="in_old", payment_intent="pi_1"+ )+ world.stripe.payment_intents["pi_1"] = {"id": "pi_1", "status": "processing"}+ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == []+ assert world.stripe.voided == []+++async def test_failure_after_stripe_ended_an_only_plan_voids_nothing():+ """Only our own replaced-plan cancel is resumed into voiding; an only+ plan Stripe ended after its retries keeps its invoice for part 2."""+ with World(balance=0) as world:+ event = _renewal_failed(world)+ world.stripe.subscriptions["sub_1"].update(+ status="canceled", cancellation_details={"reason": "payment_failed"}+ )+ await handle_subscription_payment_failure(event)++ assert world.stripe.voided == []+ assert world.stripe.invoices["in_1"]["status"] == "open"+++async def test_failure_of_an_invoice_that_is_no_longer_latest_does_nothing():+ with World(balance=5000) as world:+ world.stripe.add_subscription("sub_1", "past_due", latest_invoice="in_2")+ event = world.stripe.add_invoice("in_1", "sub_1")+ world.stripe.add_invoice("in_2", "sub_1")+ await handle_subscription_payment_failure(event)++ assert world.stripe.paid_out_of_band == []+ assert world.ledger.transactions == {}+ assert world.synced == []+++async def test_delayed_failure_after_the_customer_paid_does_nothing():+ with World(balance=0) as world:+ event = _renewal_failed(world)+ world.stripe.card_pays("in_1")+ await handle_subscription_payment_failure(event)++ assert world.synced == []+ assert world.tier == SubscriptionTier.PRO+++async def test_duplicate_failure_event_writes_the_tier_once():+ with World(balance=0) as world:+ event = _renewal_failed(world)+ await handle_subscription_payment_failure(event)+ await handle_subscription_payment_failure(event)++ assert world.tier_writes == [SubscriptionTier.NO_TIER]+ assert world.stripe.cancelled == []+++async def test_failure_after_stripe_ended_the_subscription_does_nothing():+ """Stripe's own final action after its retries cancels the subscription;+ its deletion event syncs the tier, so a late failure has nothing to do."""+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.subscriptions["sub_1"]["status"] = "canceled"+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {}+ assert world.synced == []+++async def test_payment_still_processing_leaves_the_wallet_alone():+ with World(balance=5000) as world:+ event = _renewal_failed(world, payment_intent="pi_1")+ world.stripe.payment_intents["pi_1"] = {"id": "pi_1", "status": "processing"}+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {}+ assert [s["status"] for s in world.synced] == ["past_due"]+ assert world.tier == SubscriptionTier.NO_TIER+++async def test_trial_first_invoice_takes_the_same_path():+ """An unconverted trial's first invoice stays open for a card fix like any+ other; the trial reconcile maps the past-due trial to NO_TIER."""+ with World(balance=0) as world:+ event = _renewal_failed(world)+ world.stripe.subscriptions["sub_1"]["metadata"] = {"trial_enrollment_id": "t1"}++ async def reconcile_trial(user_id: str, sub_id: str):+ sub = world.stripe.subscriptions[sub_id]+ assert sub["status"] == "past_due"+ await world._set_tier(user_id, SubscriptionTier.NO_TIER)+ return dict(sub), SubscriptionTier.NO_TIER++ with patch(+ "backend.data.credit.subscription_trial_stripe.reconcile_trial_subscription",+ side_effect=reconcile_trial,+ ), patch("backend.data.credit.invalidate_subscription_caches"):+ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == []+ assert world.stripe.voided == []+ assert world.stripe.invoices["in_1"]["status"] == "open"+ assert world.tier == SubscriptionTier.NO_TIER+++async def test_incomplete_subscription_is_left_alone():+ with World(balance=0) as world:+ world.stripe.add_subscription("sub_1", "incomplete", latest_invoice="in_1")+ event = world.stripe.add_invoice("in_1", "sub_1")+ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == []+ assert world.synced == []+++async def test_enterprise_user_is_skipped():+ with World(balance=0) as world:+ event = _renewal_failed(world)+ user = MagicMock(id=USER, subscriptionTier=SubscriptionTier.ENTERPRISE)+ with patch(+ "backend.data.subscription_payment_failure.User.prisma",+ return_value=MagicMock(find_first=AsyncMock(return_value=user)),+ ):+ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == []+++async def test_invoice_of_another_customer_is_ignored():+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.subscriptions["sub_1"]["customer"] = "cus_other"+ await handle_subscription_payment_failure(event)++ assert world.stripe.cancelled == []+ assert world.ledger.transactions == {}+++async def test_wallet_covers_invoice_and_marks_it_paid_out_of_band():+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.ledger.balance == 3000+ assert world.stripe.invoices["in_1"]["paid_out_of_band"] is True+ assert world.ledger.settled("in_1")+ assert world.stripe.cancelled == []+++async def test_duplicate_failure_event_debits_the_wallet_once():+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ await handle_subscription_payment_failure(event)+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.ledger.balance == 3000+ assert world.stripe.paid_out_of_band == ["in_1"]+++async def test_wallet_pay_failure_keeps_debit_and_the_retry_finishes_it():+ """Defect 5: the debit used to stay while Stripe still saw the bill unpaid,+ and the retry crashed on the debit's unique key without paying."""+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ assert world.ledger.transactions == {"in_1": -2000}+ assert world.stripe.invoices["in_1"]["status"] == "open"++ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.ledger.balance == 3000+ assert world.stripe.invoices["in_1"]["status"] == "paid"+ assert world.stripe.cancelled == []+++async def test_card_paying_first_refunds_the_unfinished_wallet_debit():+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ # Stripe's own card retry succeeds before the webhook retry arrives.+ world.stripe.card_pays("in_1")++ await reconcile_wallet_payment_on_paid_invoice(world.stripe.view("in_1"))+ await handle_subscription_payment_failure(event)++ assert world.ledger.balance == 5000+ assert world.ledger.transactions == {"in_1": -2000, "in_1:wallet-refund": 2000}+ assert world.stripe.paid_out_of_band == []+++async def test_resumed_wallet_payment_is_refunded_once_the_plan_is_gone():+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ world.stripe.subscriptions["sub_1"]["status"] = "canceled"++ await handle_subscription_payment_failure(event)++ assert world.ledger.balance == 5000+ assert world.stripe.paid_out_of_band == []+++async def test_wallet_paid_invoice_is_not_refunded_on_its_success_event():+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ await handle_subscription_payment_failure(event)+ await reconcile_wallet_payment_on_paid_invoice(world.stripe.view("in_1"))++ assert world.ledger.balance == 3000+ assert "in_1:wallet-refund" not in world.ledger.transactions+++async def test_non_subscription_invoice_is_ignored():+ with World(balance=5000) as world:+ await handle_subscription_payment_failure(+ {"id": "in_x", "customer": CUSTOMER, "amount_due": 2000}+ )++ assert world.ledger.transactions == {}diff --git a/autogpt_platform/backend/backend/data/subscription_pending_cache_test.py b/autogpt_platform/backend/backend/data/subscription_pending_cache_test.pyindex cb33fc5557..9b94856dda 100644--- a/autogpt_platform/backend/backend/data/subscription_pending_cache_test.py+++ b/autogpt_platform/backend/backend/data/subscription_pending_cache_test.py@@ -19,6 +19,8 @@ import stripefrom prisma.enums import SubscriptionTierfrom backend.data import credit as worker_a+from backend.data import subscription_payment_failure as payment_failure+from backend.data import subscription_wallet_payment as wallet_paymentfrom backend.data import versioned_cachefrom backend.data.redis_client import get_redis_asyncfrom backend.data.subscription_pending_cache import PENDING_CHANGES@@ -203,12 +205,35 @@ async def test_a_renewal_the_balance_cant_cover_clears_the_pending_change(user_id, fake_stripe, worker_b):"""A cancellation is scheduled, then the renewal fails and the balance- can't cover it, so we end the plan in Stripe now: the "cancels on" badge- must go on every worker, not linger until it expires."""+ can't cover it. The plan stops giving access (Stripe keeps it past_due+ and retries the invoice), so the "cancels on" badge must go on every+ worker, not linger until it expires."""fake_stripe.subscription["cancel_at_period_end"] = Truepending = await worker_b.get_pending_subscription_change(user_id)assert pending is not None and pending[0] == SubscriptionTier.NO_TIER+ fake_stripe.subscription = {+ **fake_stripe.subscription,+ "status": "past_due",+ "latest_invoice": "in_renewal",+ }+ customer_id = fake_stripe.subscription["customer"]+ renewal = {+ "id": "in_renewal",+ "object": "invoice",+ "customer": customer_id,+ "status": "open",+ "amount_due": 5000,+ "billing_reason": "subscription_cycle",+ "subscription": "sub_pro",+ }++ async def retrieve_invoice(invoice_id: str, **params):+ return stripe.Invoice.construct_from(renewal, "test-key")++ async def retrieve_subscription(subscription_id: str, **params):+ return fake_stripe.current()+users = MagicMock()users.prisma.return_value.find_first = AsyncMock(return_value=MagicMock(id=user_id, subscriptionTier=SubscriptionTier.PRO)@@ -217,23 +242,29 @@ async def test_a_renewal_the_balance_cant_cover_clears_the_pending_change(balance._add_transaction = AsyncMock(side_effect=InsufficientBalanceError("no balance", user_id, 0, 5000))- renewal = {- "id": "in_renewal",- "status": "open",- "customer": fake_stripe.subscription["customer"],- "amount_due": 5000,- "billing_reason": "subscription_cycle",- "subscription": "sub_pro",- }with (- patch.object(worker_a, "User", users),- patch.object(worker_a, "UserCredit", MagicMock(return_value=balance)),- patch.object(worker_a, "set_subscription_tier", AsyncMock()) as set_tier,- patch.object(worker_a, "get_subscription_trial", AsyncMock(return_value=None)),- patch.object(stripe.Invoice, "retrieve_async", AsyncMock(return_value=renewal)),+ patch.object(stripe.Invoice, "retrieve_async", retrieve_invoice),+ patch.object(stripe.Subscription, "retrieve_async", retrieve_subscription),+ patch.object(payment_failure, "User", users),+ patch.object(+ payment_failure, "find_wallet_payment", AsyncMock(return_value=None)+ ),+ patch.object(+ payment_failure, "payment_in_progress", AsyncMock(return_value=False)+ ),+ patch.object(+ wallet_payment, "find_wallet_payment", AsyncMock(return_value=None)+ ),+ patch.object(wallet_payment, "UserCredit", MagicMock(return_value=balance)),+ patch.object(+ payment_failure, "get_subscription_trial", AsyncMock(return_value=None)+ ),):- await worker_a.handle_subscription_payment_failure(renewal)+ await payment_failure.handle_subscription_payment_failure(renewal)- assert fake_stripe.subscription["status"] == "canceled"- set_tier.assert_awaited_once_with(user_id, SubscriptionTier.NO_TIER)+ assert fake_stripe.subscription["status"] == "past_due"+ sync_tier = worker_a._sync_subscription_tier_from_stripe+ assert isinstance(sync_tier, AsyncMock)+ sync_tier.assert_awaited_once()+ assert sync_tier.await_args.args[0]["status"] == "past_due"assert await worker_b.get_pending_subscription_change(user_id) is Nonediff --git a/autogpt_platform/backend/backend/data/subscription_wallet_payment.py b/autogpt_platform/backend/backend/data/subscription_wallet_payment.pynew file mode 100644index 0000000000..af67df279f--- /dev/null+++ b/autogpt_platform/backend/backend/data/subscription_wallet_payment.py@@ -0,0 +1,237 @@+"""Pay a failed subscription invoice from the AutoGPT wallet, safely.++Paying is two writes in two systems: debit the wallet, then tell Stripe the+invoice is paid. Either can fail on its own, and Stripe can deliver the same+failure twice or late. The debit is keyed by the invoice id, so it is both+the guard against a second debit and the record that a payment was started;+every later delivery resumes from it until it is settled (the invoice is+paid out of band with it) or refunded. See ``wallet_payment_state``.++Deliveries for one invoice can run at once, so every decision on its wallet+payment is made under a per-invoice lock, from our state and the invoice+re-read inside that lock, never from a copy an earlier read left behind.+"""++import logging+from collections.abc import AsyncIterator+from contextlib import asynccontextmanager++import stripe+from autogpt_libs.utils.synchronize import AsyncRedisKeyedMutex+from prisma.enums import CreditTransactionType+from prisma.errors import UniqueViolationError+from prisma.models import User++from backend.data.credit import UserCredit, invalidate_billing_lookups+from backend.data.redis_client import get_redis_async+from backend.data.stripe_client import stripe_call+from backend.data.stripe_invoice_payments import paid_by_stripe_collection+from backend.data.wallet_payment_state import (+ WalletPayment,+ WalletPaymentState,+ find_wallet_payment,+ mark_wallet_payment_settled,+ wallet_refund_key,+)+from backend.util.exceptions import InsufficientBalanceError+from backend.util.json import SafeJson++logger = logging.getLogger(__name__)++# Invoice states ``Invoice.pay`` still accepts.+_PAYABLE_INVOICE_STATUSES = ("open", "uncollectible")+# Outlasts the few Stripe calls (30s timeout each) made while it is held.+_LOCK_TIMEOUT_SECONDS = 300+++async def pay_invoice_from_wallet(+ user_id: str, customer_id: str, sub_id: str, invoice: dict+) -> bool:+ """Debit the wallet for ``invoice`` and mark it paid out of band.++ Returns False, without changing anything, when the balance cannot cover+ the invoice. Raises when Stripe would not mark the invoice paid, leaving+ the debit for the retry to resume from.+ """+ invoice_id: str = invoice["id"]+ try:+ await UserCredit()._add_transaction(+ user_id=user_id,+ amount=-invoice.get("amount_due", 0),+ transaction_type=CreditTransactionType.SUBSCRIPTION,+ fail_insufficient_credits=True,+ transaction_key=invoice_id,+ metadata=SafeJson(+ {+ "stripe_customer_id": customer_id,+ "stripe_subscription_id": sub_id,+ "reason": "subscription_payment_failure_covered_by_balance",+ }+ ),+ )+ except InsufficientBalanceError:+ # The balance is checked before the key, so a concurrent delivery+ # that took the debit first shows up here when the wallet cannot+ # cover a second bill; that payment must be finished, not cancelled.+ if await find_wallet_payment(user_id, invoice_id) is None:+ return False+ except UniqueViolationError:+ # A concurrent delivery of the same failure took the debit first.+ pass+ if not await settle_wallet_payment(user_id, invoice_id, may_pay=True):+ raise RuntimeError(f"Wallet debit for invoice {invoice_id} is missing")+ return True+++async def settle_wallet_payment(+ user_id: str, invoice_id: str, *, may_pay: bool+) -> bool:+ """Finish a started wallet payment: mark the invoice paid, or refund the debit.++ Safe to repeat, and to run concurrently. ``may_pay`` is False once the+ invoice no longer pays for a live plan; the debit is then given back+ instead of spent on it. A settled or refunded payment is left as it is:+ once refunded, the debit must never pay the invoice, even if the invoice+ is reopened and payable again. Returns False when there is no debit.+ """+ async with _wallet_payment_lock(invoice_id):+ payment = await find_wallet_payment(user_id, invoice_id)+ if payment is None:+ return False+ if payment.state == WalletPaymentState.DEBITED:+ await _settle_locked(payment, may_pay=may_pay)+ return True+++async def _settle_locked(payment: WalletPayment, *, may_pay: bool) -> None:+ invoice_id = payment.invoice_id+ invoice = dict(await stripe_call(stripe.Invoice.retrieve_async, invoice_id))+ if may_pay and invoice.get("status") in _PAYABLE_INVOICE_STATUSES:+ try:+ # Out of band, so Stripe does not also retry the card that failed.+ await stripe_call(+ stripe.Invoice.pay_async, invoice_id, paid_out_of_band=True+ )+ except stripe.StripeError:+ invoice = dict(await stripe_call(stripe.Invoice.retrieve_async, invoice_id))+ if invoice.get("status") in _PAYABLE_INVOICE_STATUSES:+ logger.warning(+ f"Wallet debit taken for invoice {invoice_id} (user"+ f" {payment.user_id}) but Stripe would not mark it paid;"+ " the retry resumes it",+ exc_info=True,+ )+ raise+ else:+ await _drop_cached_lookup(invoice)+ await mark_wallet_payment_settled(payment)+ logger.info(+ f"Paid invoice {invoice_id} of user {payment.user_id} from wallet"+ )+ return+ await _settle_unpayable(payment, invoice)+++async def reconcile_wallet_payment_on_paid_invoice(invoice: dict) -> None:+ """On an invoice's success event, settle or refund an unfinished wallet debit.++ Stripe keeps retrying the card while a wallet payment is unfinished; if a+ retry succeeds first, the customer would otherwise pay twice. The event+ also closes the window where our out-of-band pay reached Stripe but the+ settlement was not recorded.+ """+ invoice_id = invoice.get("id") or ""+ customer_id = invoice.get("customer")+ if not invoice_id or not customer_id:+ return+ user = await User.prisma().find_first(where={"stripeCustomerId": customer_id})+ if not user:+ return+ # Almost every paid invoice has no wallet payment; checking first keeps+ # a Redis outage from failing their webhooks on the lock.+ if not await _unfinished_wallet_payment(user.id, invoice_id):+ return+ async with _wallet_payment_lock(invoice_id):+ payment = await _unfinished_wallet_payment(user.id, invoice_id)+ if payment is None:+ return+ fresh = dict(await stripe_call(stripe.Invoice.retrieve_async, invoice_id))+ await _settle_unpayable(payment, fresh)+++async def _unfinished_wallet_payment(+ user_id: str, invoice_id: str+) -> WalletPayment | None:+ payment = await find_wallet_payment(user_id, invoice_id)+ if payment is None or payment.state != WalletPaymentState.DEBITED:+ return None+ return payment+++@asynccontextmanager+async def _wallet_payment_lock(invoice_id: str) -> AsyncIterator[None]:+ mutex = AsyncRedisKeyedMutex(await get_redis_async(), _LOCK_TIMEOUT_SECONDS)+ async with mutex.locked(f"subscription-wallet-payment:{invoice_id}"):+ yield+++async def _settle_unpayable(payment: WalletPayment, invoice: dict) -> None:+ """Decide an unfinished debit whose invoice we will not (or cannot) pay.++ ``invoice`` must have been read under the lock: a stale ``open`` copy of+ an invoice another delivery has since paid would refund a paid bill.+ """+ status = invoice.get("status")+ if status != "paid":+ # Voided, or no longer for a live plan: the wallet must not keep it.+ await _refund(payment, f"wallet_payment_not_used:{status}")+ return+ collected = await paid_by_stripe_collection(invoice)+ if collected is None:+ logger.error(+ f"Invoice {payment.invoice_id} is paid but it does not say whether"+ f" by card or by the wallet debit of user {payment.user_id}; keeping"+ " the debit for a manual check"+ )+ return+ if collected:+ await _refund(payment, "invoice_paid_by_card")+ return+ # Paid out of band by our own earlier attempt, before it was recorded.+ await _drop_cached_lookup(invoice)+ await mark_wallet_payment_settled(payment)+++async def _drop_cached_lookup(invoice: dict) -> None:+ """Paying the invoice makes its plan active again, with a new period.++ Stripe sends ``invoice.paid`` for an out-of-band payment, not+ ``invoice.payment_succeeded``, so the success handler that drops the+ billing page's shared lookups (the active subscription and any pending+ change) for a card payment may never run; without this the page waits+ for ``customer.subscription.updated`` to see it.+ """+ customer_id = invoice.get("customer")+ if isinstance(customer_id, str) and customer_id:+ await invalidate_billing_lookups(customer_id)+++async def _refund(payment: WalletPayment, reason: str) -> None:+ """Keyed per invoice, so repeated or concurrent calls refund once."""+ try:+ await UserCredit()._add_transaction(+ user_id=payment.user_id,+ amount=payment.debited,+ transaction_type=CreditTransactionType.SUBSCRIPTION,+ fail_insufficient_credits=False,+ transaction_key=wallet_refund_key(payment.invoice_id),+ metadata=SafeJson(+ {"stripe_invoice_id": payment.invoice_id, "reason": reason}+ ),+ )+ except UniqueViolationError:+ return+ logger.warning(+ f"Refunded wallet debit of {payment.debited} cents to user"+ f" {payment.user_id} for invoice {payment.invoice_id} ({reason})"+ )diff --git a/autogpt_platform/backend/backend/data/subscription_wallet_payment_test.py b/autogpt_platform/backend/backend/data/subscription_wallet_payment_test.pynew file mode 100644index 0000000000..00a561f4f8--- /dev/null+++ b/autogpt_platform/backend/backend/data/subscription_wallet_payment_test.py@@ -0,0 +1,395 @@+"""Wallet payment settlement across Stripe API versions, crashes and replays.++Uses the stateful fakes of ``subscription_payment_failure_test``. From+2025-03-31.basil the Invoice has no ``paid_out_of_band``/``payment_intent``,+so these tests prove the wallet's own record decides, not those fields.+"""++import asyncio+from unittest.mock import AsyncMock, patch++import pytest+import stripe++from backend.data.stripe_invoice_payments import paid_by_stripe_collection+from backend.data.subscription_payment_failure import (+ FailedInvoice,+ handle_subscription_payment_failure,+)+from backend.data.subscription_payment_failure_test import (+ ACACIA,+ CUSTOMER,+ ENDIVE,+ USER,+ World,+ _renewal_failed,+)+from backend.data.subscription_wallet_payment import (+ pay_invoice_from_wallet,+ reconcile_wallet_payment_on_paid_invoice,+)++# (SDK version our calls use, webhook endpoint version of the payloads)+VERSIONS = [(ACACIA, ACACIA), (ENDIVE, ACACIA), (ENDIVE, ENDIVE), (ACACIA, ENDIVE)]++[email protected]("sdk_version,endpoint_version", VERSIONS)+async def test_success_event_after_a_wallet_payment_never_refunds(+ sdk_version, endpoint_version+):+ """Sentry's finding: without ``paid_out_of_band`` the success event used+ to read a wallet-paid invoice as card-paid and refund it (a free period)."""+ with World(balance=5000, api_version=sdk_version) as world:+ event = _renewal_failed(world)+ await handle_subscription_payment_failure(event)+ paid = world.stripe.view("in_1", endpoint_version)+ await reconcile_wallet_payment_on_paid_invoice(paid)+ await reconcile_wallet_payment_on_paid_invoice(paid)++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.ledger.balance == 3000+ assert world.ledger.settled("in_1")++[email protected]("sdk_version,endpoint_version", VERSIONS)+async def test_card_paying_an_unsettled_wallet_debit_refunds_it_once(+ sdk_version, endpoint_version+):+ with World(balance=5000, api_version=sdk_version) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ world.stripe.card_pays("in_1")++ paid = world.stripe.view("in_1", endpoint_version)+ await reconcile_wallet_payment_on_paid_invoice(paid)+ await reconcile_wallet_payment_on_paid_invoice(paid)+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {"in_1": -2000, "in_1:wallet-refund": 2000}+ assert world.ledger.balance == 5000+ assert world.stripe.paid_out_of_band == []++[email protected]("sdk_version,endpoint_version", VERSIONS)+async def test_crash_after_stripe_paid_before_settlement_was_recorded(+ sdk_version, endpoint_version+):+ """Stripe accepted the out-of-band pay, then our settlement write died.+ The success event finds the debit unsettled and a paid invoice that no+ card paid: it records the settlement and keeps the money."""+ with World(balance=5000, api_version=sdk_version) as world:+ event = _renewal_failed(world)+ world.ledger.fail_next_update = 1+ with pytest.raises(ConnectionError):+ await handle_subscription_payment_failure(event)+ assert world.stripe.invoices["in_1"]["status"] == "paid"+ assert not world.ledger.settled("in_1")++ await reconcile_wallet_payment_on_paid_invoice(+ world.stripe.view("in_1", endpoint_version)+ )+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.ledger.settled("in_1")+ assert world.stripe.paid_out_of_band == ["in_1"]++[email protected]("sdk_version", [ACACIA, ENDIVE])+async def test_lost_pay_response_is_settled_not_refunded(sdk_version):+ """The pay reached Stripe but its response was lost: re-reading the+ invoice finds it paid with no card payment, so it settles, not refunds."""+ with World(balance=5000, api_version=sdk_version) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay_response"] = 1+ await handle_subscription_payment_failure(event)+ assert world.ledger.settled("in_1")++ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.ledger.settled("in_1")+++async def test_paid_invoice_that_does_not_say_who_paid_keeps_the_debit():+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ bare = {"id": "in_1", "customer": "cus_1", "status": "paid"}+ with patch.object(+ stripe.Invoice, "retrieve_async", AsyncMock(return_value=bare)+ ):+ assert await paid_by_stripe_collection(bare) is None+ await reconcile_wallet_payment_on_paid_invoice(bare)++ assert world.ledger.transactions == {"in_1": -2000}+ assert not world.ledger.settled("in_1")+++async def test_refunded_debit_never_pays_a_reopened_invoice():+ """CodeRabbit's finding: a refunded debit stayed visible as a wallet+ payment, so a later payable delivery marked the invoice paid out of band+ after the money was already back in the wallet."""+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ # A delivery while the subscription is not unpaid refunds the debit.+ world.stripe.subscriptions["sub_1"]["status"] = "active"+ await handle_subscription_payment_failure(event)+ assert world.ledger.balance == 5000+ # The invoice is payable again and the failure is retried.+ world.stripe.subscriptions["sub_1"]["status"] = "past_due"+ await handle_subscription_payment_failure(event)++ assert world.stripe.paid_out_of_band == []+ assert world.stripe.invoices["in_1"]["status"] == "open"+ assert world.ledger.transactions == {"in_1": -2000, "in_1:wallet-refund": 2000}+ assert world.ledger.balance == 5000++[email protected]("sdk_version", [ACACIA, ENDIVE])+async def test_processing_payment_is_left_alone_before_touching_the_wallet(+ sdk_version,+):+ """Capy's finding: with balance available and a bank debit processing,+ the wallet was debited and Stripe refused the pay on every retry."""+ with World(balance=5000, api_version=sdk_version) as world:+ event = _renewal_failed(world, payment_intent="pi_1")+ world.stripe.payment_intents["pi_1"] = {"id": "pi_1", "status": "processing"}+ await handle_subscription_payment_failure(event)+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {}+ assert world.stripe.paid_out_of_band == []+ assert world.stripe.cancelled == []+ assert world.stripe.voided == []+ assert [s["status"] for s in world.synced] == ["past_due", "past_due"]+++async def test_unfinished_wallet_payment_waits_for_a_processing_payment():+ with World(balance=5000) as world:+ event = _renewal_failed(world, payment_intent="pi_1")+ world.stripe.payment_intents["pi_1"] = {+ "id": "pi_1",+ "status": "requires_payment_method",+ }+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ world.stripe.payment_intents["pi_1"]["status"] = "processing"++ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.stripe.paid_out_of_band == []+ assert world.synced[-1]["status"] == "past_due"+++async def test_concurrent_delivery_without_balance_for_a_second_bill_finishes_it():+ """Two deliveries of one failure: the second finds too little balance+ (checked before the key) and must finish the first's payment, not report+ that the wallet cannot pay, which would cancel the plan being paid for."""+ with World(balance=3000) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await pay_invoice_from_wallet("user-1", "cus_1", "sub_1", event)+ assert world.ledger.balance == 1000++ assert await pay_invoice_from_wallet("user-1", "cus_1", "sub_1", event)++ assert world.stripe.paid_out_of_band == ["in_1"]+ assert world.ledger.transactions == {"in_1": -2000}+ assert world.ledger.settled("in_1")+ assert world.stripe.cancelled == []+++def _stale_delivery(world: World, stale_invoice: dict):+ """A delivery that read the invoice before another one paid it and the+ subscription after: ``open`` but ``active``, so it will not pay."""+ stale = FailedInvoice(+ user_id=USER,+ user_tier=world.tier,+ customer_id=CUSTOMER,+ invoice=stale_invoice,+ subscription=dict(world.stripe.subscriptions["sub_1"]),+ )+ return patch(+ "backend.data.subscription_payment_failure._load_failed_invoice",+ AsyncMock(return_value=stale),+ )++[email protected]("sdk_version", [ACACIA, ENDIVE])+async def test_racing_delivery_with_a_stale_open_invoice_never_refunds(sdk_version):+ """Sentry's race: delivery A pays the invoice; delivery B, holding a stale+ ``open`` copy, found A's debit unsettled and refunded it while Stripe kept+ the invoice paid. B now waits for A's lock and finds the debit settled."""+ with World(balance=5000, api_version=sdk_version) as world:+ event = _renewal_failed(world)+ stale_invoice = world.stripe.view("in_1")+ a_paid, release_a = asyncio.Event(), asyncio.Event()++ async def pause_between_pay_and_settlement():+ a_paid.set()+ await release_a.wait()++ world.stripe.after_pay = pause_between_pay_and_settlement+ a = asyncio.create_task(handle_subscription_payment_failure(event))+ await a_paid.wait()+ world.stripe.after_pay = None+ with _stale_delivery(world, stale_invoice):+ b = asyncio.create_task(handle_subscription_payment_failure(event))+ for _ in range(50):+ await asyncio.sleep(0)+ assert not b.done()+ release_a.set()+ await asyncio.gather(a, b)++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.ledger.settled("in_1")+ assert world.stripe.paid_out_of_band == ["in_1"]++[email protected]("sdk_version", [ACACIA, ENDIVE])+async def test_stale_open_copy_after_an_unrecorded_wallet_pay_never_refunds(+ sdk_version,+):+ """A paid at Stripe and died before recording it; B arrives with a stale+ ``open`` copy. The decision re-reads the invoice, finds it paid with no+ card payment, and settles instead of refunding."""+ with World(balance=5000, api_version=sdk_version) as world:+ event = _renewal_failed(world)+ stale_invoice = world.stripe.view("in_1")+ world.ledger.fail_next_update = 1+ with pytest.raises(ConnectionError):+ await handle_subscription_payment_failure(event)+ with _stale_delivery(world, stale_invoice):+ await handle_subscription_payment_failure(event)++ assert world.ledger.transactions == {"in_1": -2000}+ assert world.ledger.settled("in_1")+++def _redis_down(invoice_id: str):+ raise ConnectionError("redis is down")+++async def test_paid_invoice_without_a_wallet_payment_never_takes_the_lock():+ """Sentry's finding: every paid-invoice webhook took the Redis lock, so a+ Redis outage failed them all, though almost none has a wallet payment."""+ with World(balance=0) as world:+ world.stripe.add_subscription("sub_1", "active", latest_invoice="in_1")+ paid = world.stripe.add_invoice("in_1", "sub_1", status="paid")+ with patch(+ "backend.data.subscription_wallet_payment._wallet_payment_lock",+ _redis_down,+ ):+ await reconcile_wallet_payment_on_paid_invoice(paid)++ assert world.ledger.transactions == {}+++async def test_unfinished_wallet_payment_is_settled_under_the_lock():+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ world.stripe.card_pays("in_1")+ with patch(+ "backend.data.subscription_wallet_payment._wallet_payment_lock",+ _redis_down,+ ), pytest.raises(ConnectionError):+ await reconcile_wallet_payment_on_paid_invoice(world.stripe.view("in_1"))+ assert world.ledger.balance == 3000++ await reconcile_wallet_payment_on_paid_invoice(world.stripe.view("in_1"))++ assert world.ledger.transactions == {"in_1": -2000, "in_1:wallet-refund": 2000}+ assert world.ledger.balance == 5000++[email protected]("lost_response", [False, True])+async def test_wallet_payment_drops_the_shared_lookup_after_paying(lost_response):+ """An out-of-band payment sends no ``invoice.payment_succeeded``, so the+ billing page's shared lookups (the active subscription and any pending+ change) are dropped here, once the plan is active, not left to the+ subscription update that follows."""+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ if lost_response:+ world.stripe.fail_next["pay_response"] = 1+ seen: list[tuple[str, str, str, str]] = []++ def recorder(cache: str):+ async def invalidate(customer_id: str) -> None:+ seen.append(+ (+ cache,+ customer_id,+ world.stripe.invoices["in_1"]["status"],+ world.stripe.subscriptions["sub_1"]["status"],+ )+ )++ return invalidate++ with (+ patch(+ "backend.data.credit.invalidate_active_subscription_cache",+ recorder("active"),+ ),+ patch(+ "backend.data.credit.invalidate_pending_subscription_change",+ recorder("pending"),+ ),+ ):+ await handle_subscription_payment_failure(event)++ assert world.ledger.settled("in_1")+ assert sorted(seen) == [+ ("active", CUSTOMER, "paid", "active"),+ ("pending", CUSTOMER, "paid", "active"),+ ]+++async def test_refunded_wallet_debit_does_not_drop_the_shared_lookup():+ """A card that paid first changed Stripe, and its own success event+ drops the lookup; the wallet only refunds, which changes nothing there."""+ with World(balance=5000) as world:+ event = _renewal_failed(world)+ world.stripe.fail_next["pay"] = 1+ with pytest.raises(stripe.APIConnectionError):+ await handle_subscription_payment_failure(event)+ world.stripe.card_pays("in_1")+ invalidate = AsyncMock()+ with patch(+ "backend.data.subscription_wallet_payment.invalidate_billing_lookups",+ invalidate,+ ):+ await reconcile_wallet_payment_on_paid_invoice(world.stripe.view("in_1"))++ assert world.ledger.transactions == {"in_1": -2000, "in_1:wallet-refund": 2000}+ invalidate.assert_not_awaited()diff --git a/autogpt_platform/backend/backend/data/wallet_payment_state.py b/autogpt_platform/backend/backend/data/wallet_payment_state.pynew file mode 100644index 0000000000..fb330f7ae2--- /dev/null+++ b/autogpt_platform/backend/backend/data/wallet_payment_state.py@@ -0,0 +1,98 @@+"""Where a wallet payment for a subscription invoice stands.++The wallet debit is keyed by the invoice id. It is settled once Stripe holds+the invoice paid out of band with it, and refunded once the money went back+under the ``<invoice>:wallet-refund`` key. Both are our own records, so no+decision depends on which fields a Stripe API version puts on the invoice.+Refunded is final: that debit may never pay the invoice afterwards.+"""++from enum import Enum+from typing import cast++from prisma.enums import CreditTransactionType+from prisma.models import CreditTransaction+from pydantic import BaseModel++from backend.util.json import SafeJson++_SETTLED_METADATA_KEY = "wallet_payment"+++class WalletPaymentState(str, Enum):+ DEBITED = "debited"+ """The wallet was charged; Stripe does not hold the invoice paid with it yet."""+ SETTLED = "settled"+ """The invoice is paid out of band with the debit; the wallet keeps it."""+ REFUNDED = "refunded"+ """The debit was given back. Terminal."""+++class WalletPayment(BaseModel):+ user_id: str+ invoice_id: str+ debited: int+ """Cents taken from the wallet, as a positive number."""+ state: WalletPaymentState+ metadata: dict+++def wallet_refund_key(invoice_id: str) -> str:+ return f"{invoice_id}:wallet-refund"+++async def find_wallet_payment(user_id: str, invoice_id: str) -> WalletPayment | None:+ rows = await CreditTransaction.prisma().find_many(+ where={+ "userId": user_id,+ "transactionKey": {"in": [invoice_id, wallet_refund_key(invoice_id)]},+ }+ )+ by_key = {row.transactionKey: row for row in rows}+ debit = by_key.get(invoice_id)+ if (+ debit is None+ or debit.type != CreditTransactionType.SUBSCRIPTION+ or debit.amount >= 0+ ):+ return None+ metadata: dict = cast(dict, debit.metadata) or {}+ if wallet_refund_key(invoice_id) in by_key:+ state = WalletPaymentState.REFUNDED+ elif metadata.get(_SETTLED_METADATA_KEY) == WalletPaymentState.SETTLED.value:+ state = WalletPaymentState.SETTLED+ else:+ state = WalletPaymentState.DEBITED+ return WalletPayment(+ user_id=user_id,+ invoice_id=invoice_id,+ debited=-debit.amount,+ state=state,+ metadata=metadata,+ )+++async def mark_wallet_payment_settled(payment: WalletPayment) -> None:+ await CreditTransaction.prisma().update(+ where={+ "creditTransactionIdentifier": {+ "transactionKey": payment.invoice_id,+ "userId": payment.user_id,+ }+ },+ data={+ "metadata": SafeJson(+ {+ **payment.metadata,+ _SETTLED_METADATA_KEY: WalletPaymentState.SETTLED.value,+ }+ )+ },+ )+++async def invoice_paid_from_wallet(user_id: str, invoice_id: str) -> bool:+ """Whether the wallet paid (or is still paying) the invoice, so nothing+ else may treat it as a payment collected from the customer's card."""+ payment = await find_wallet_payment(user_id, invoice_id)+ return payment is not None and payment.state != WalletPaymentState.REFUNDEDdiff --git a/autogpt_platform/backend/backend/notifications/lifecycle.py b/autogpt_platform/backend/backend/notifications/lifecycle.pyindex 32b6912f15..3932a7c126 100644--- a/autogpt_platform/backend/backend/notifications/lifecycle.py+++ b/autogpt_platform/backend/backend/notifications/lifecycle.py@@ -25,7 +25,7 @@ from datetime import datetime, timezoneimport stripefrom prisma.enums import NotificationType-from backend.data.credit import PAYMENT_FAILURE_CANCELLATION_COMMENT+from backend.data.credit import REPLACED_PLAN_CANCELLATION_COMMENTfrom backend.data.notifications import (AudienceAction,NotificationEventModel,@@ -305,6 +305,10 @@ async def on_subscription_updated(subscription: dict, previous: dict) -> None:async def on_subscription_deleted(subscription: dict) -> None:"""Two roads lead here — a cancellation reaching period end, and dunningexhaustion — so the copy branches on which one the customer took."""+ details = subscription.get("cancellation_details") or {}+ if details.get("comment") == REPLACED_PLAN_CANCELLATION_COMMENT:+ # Ended because the customer has another plan; they did not leave.+ returnif await notify_trial(subscription, "ended"):returnuser = await _user_for(subscription.get("customer"))@@ -346,11 +350,9 @@ async def on_subscription_deleted(subscription: dict) -> None:def _churn_reason(subscription: dict) -> str | None:- """Stripe's ``cancellation_details.reason``, except that our own cancel- after a failed renewal is involuntary churn, not a requested one."""+ """Stripe's ``cancellation_details.reason``: ``payment_failed`` when+ Stripe ended the subscription after its retries failed."""details = subscription.get("cancellation_details") or {}- if details.get("comment") == PAYMENT_FAILURE_CANCELLATION_COMMENT:- return "payment_failed"return details.get("reason")diff --git a/autogpt_platform/backend/backend/notifications/lifecycle_test.py b/autogpt_platform/backend/backend/notifications/lifecycle_test.pyindex e6597b6a1b..a114bcc87a 100644--- a/autogpt_platform/backend/backend/notifications/lifecycle_test.py+++ b/autogpt_platform/backend/backend/notifications/lifecycle_test.py@@ -13,7 +13,7 @@ from unittest.mock import AsyncMock, patchimport pytestfrom prisma.enums import NotificationType, SubscriptionTier-from backend.data.credit import PAYMENT_FAILURE_CANCELLATION_COMMENT+from backend.data.credit import REPLACED_PLAN_CANCELLATION_COMMENTfrom backend.data.notifications import (AudienceAction,NotificationResult,@@ -323,6 +323,24 @@ async def test_the_ended_email_branches_on_which_road_they_took():assert calls["notify"].await_args.args[0].data.due_to_payment is False+async def test_an_old_plan_ended_because_another_replaced_it_is_not_churn():+ calls = await _run(+ lambda: lifecycle.on_subscription_deleted(+ _subscription(+ cancellation_details={+ "reason": "cancellation_requested",+ "comment": REPLACED_PLAN_CANCELLATION_COMMENT,+ }+ )+ ),+ _User(),+ )+ calls["notify"].assert_not_awaited()+ calls["ended"].assert_not_called()+ calls["audience"].assert_not_awaited()++@pytest.mark.asyncioasync def test_a_subscription_end_is_sent_to_analytics_once():calls = await _run(@@ -347,30 +365,6 @@ async def test_a_subscription_end_is_sent_to_analytics_once():replay["ended"].assert_not_called()-async def test_our_own_cancel_after_a_failed_renewal_is_involuntary_churn():- # Stripe stamps any API cancel "cancellation_requested"; the comment- # handle_subscription_payment_failure leaves is what tells them apart.- calls = await _run(- lambda: lifecycle.on_subscription_deleted(- _subscription(- cancellation_details={- "reason": "cancellation_requested",- "comment": PAYMENT_FAILURE_CANCELLATION_COMMENT,- }- )- ),- _User(),- )- calls["ended"].assert_called_once_with(- user_id="user-1",- subscription_tier="PRO",- billing_cycle="monthly",- reason="payment_failed",- )- assert calls["notify"].await_args.args[0].data.due_to_payment is True--@pytest.mark.asyncioasync def test_a_failed_ended_email_sends_no_analytics_event():patches = _context(_User())