Skip to content

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): acknowledge Stripe refunds and disputes of subscription invoices instead of failing the webhook (#15211)

Reinier van der Leerd458786851ab
diff --git a/autogpt_platform/backend/backend/api/features/v1_stripe_webhook_test.py b/autogpt_platform/backend/backend/api/features/v1_stripe_webhook_test.py
index 7746e96cfd..4d8592ffb4 100644
--- a/autogpt_platform/backend/backend/api/features/v1_stripe_webhook_test.py
+++ b/autogpt_platform/backend/backend/api/features/v1_stripe_webhook_test.py
@@ -1,5 +1,9 @@
"""Unit tests for Stripe webhook handler and subscription checkout helpers."""
+import hashlib
+import hmac
+import json
+import time
from unittest.mock import AsyncMock, MagicMock
import fastapi
@@ -7,14 +11,18 @@ import fastapi.testclient
import pytest
import pytest_mock
import stripe
+from httpx import ASGITransport, AsyncClient
from prisma.enums import SubscriptionTier
+from prisma.models import CreditTransaction
+from backend.api.rest_api import app as rest_app
from backend.data.credit import (
_expire_open_subscription_sessions,
reconcile_stripe_tier_for_user,
sync_tier_from_checkout_session,
)
from backend.data.notifications import NotificationResult, PassWorkEvent, PassWorkKind
+from backend.util.test import SpinTestServer
from .billing.subscriptions.routes import (
_claim_stripe_event,
@@ -25,6 +33,10 @@ from .billing.subscriptions.routes import (
app = fastapi.FastAPI()
app.include_router(router)
client = fastapi.testclient.TestClient(app)
+SIGNING_SECRET = "whsec_refund_test_only"
+# The real app's handlers, so a raised refund answers what Stripe would see.
+handled_app = fastapi.FastAPI(exception_handlers=rest_app.exception_handlers)
+handled_app.include_router(router)
@pytest.fixture(autouse=True)
@@ -346,6 +358,117 @@ def test_stripe_webhook_releases_dedup_on_invoice_retrieve_failure(
mock_release.assert_awaited_once_with("evt_retrieve_fails")
[email protected](loop_scope="session")
+ "event_type, data_object",
+ [
+ (
+ "refund.created",
+ {"object": "refund", "id": "re_non_topup", "status": "succeeded"},
+ ),
+ (
+ "charge.dispute.created",
+ {"object": "dispute", "id": "du_non_topup", "status": "needs_response"},
+ ),
+ (
+ "charge.dispute.closed",
+ {"object": "dispute", "id": "du_non_topup", "status": "lost"},
+ ),
+ ],
+)
+ "invoice, status_code, released",
+ [("in_subscription", 200, False), (None, 500, True)],
+ ids=["subscription-invoice", "maybe-unfulfilled-top-up"],
+)
+async def test_stripe_webhook_refund_or_dispute_matching_no_top_up(
+ mocker: pytest_mock.MockFixture,
+ server: SpinTestServer,
+ event_type: str,
+ data_object: dict,
+ invoice: str | None,
+ status_code: int,
+ released: bool,
+ caplog: pytest.LogCaptureFixture,
+) -> None:
+ """A refund or dispute matching no active top-up is acknowledged with its
+ dedup claim kept only when it paid a subscription invoice; otherwise it may
+ be a Checkout top-up not yet fulfilled, so it answers 500 and releases the
+ claim for Stripe's retry. An acknowledged dispute is logged at ERROR, the
+ lowest level Sentry raises an event for."""
+ mocker.patch(
+ "stripe.PaymentIntent.retrieve_async",
+ new_callable=AsyncMock,
+ return_value=stripe.PaymentIntent.construct_from(
+ {
+ "id": "pi_non_topup_payment",
+ "object": "payment_intent",
+ "invoice": invoice,
+ },
+ "sk_test",
+ ),
+ )
+ mocker.patch(
+ "backend.api.features.billing.subscriptions.routes.settings.secrets.stripe_webhook_secret",
+ new=SIGNING_SECRET,
+ )
+ mocker.patch(
+ "backend.api.features.billing.subscriptions.routes._claim_stripe_event",
+ new_callable=AsyncMock,
+ return_value=True,
+ )
+ mock_release = mocker.patch(
+ "backend.api.features.billing.subscriptions.routes._release_stripe_event",
+ new_callable=AsyncMock,
+ )
+ payload = json.dumps(
+ {
+ "id": "evt_non_topup",
+ "object": "event",
+ "type": event_type,
+ "data": {
+ "object": {
+ **data_object,
+ "payment_intent": "pi_non_topup_payment",
+ "amount": 6000,
+ }
+ },
+ }
+ )
+ timestamp = int(time.time())
+ signature = hmac.new(
+ SIGNING_SECRET.encode(), f"{timestamp}.{payload}".encode(), hashlib.sha256
+ ).hexdigest()
+
+ async with AsyncClient(
+ transport=ASGITransport(app=handled_app, raise_app_exceptions=False),
+ base_url="http://test",
+ ) as async_client:
+ response = await async_client.post(
+ "/credits/stripe_webhook",
+ content=payload,
+ headers={"stripe-signature": f"t={timestamp},v1={signature}"},
+ )
+
+ assert response.status_code == status_code
+ assert mock_release.await_args_list == (
+ [mocker.call("evt_non_topup")] if released else []
+ )
+ assert (
+ await CreditTransaction.prisma().count(
+ where={"transactionKey": data_object["id"]}
+ )
+ == 0
+ )
+ acknowledgments = [
+ record.levelname
+ for record in caplog.records
+ if record.getMessage().startswith(f"Ignoring {data_object['id']}")
+ ]
+ expected = "ERROR" if data_object["object"] == "dispute" else "WARNING"
+ assert acknowledgments == ([] if released else [expected])
+
+
# ---------------------------------------------------------------------------
# _claim_stripe_event / _release_stripe_event unit tests
# ---------------------------------------------------------------------------
diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.py
index 3ede573f97..0e80a0d252 100644
--- a/autogpt_platform/backend/backend/data/credit.py
+++ b/autogpt_platform/backend/backend/data/credit.py
@@ -972,13 +972,9 @@ class UserCredit(UserCreditBase):
)
return
- transaction = await CreditTransaction.prisma().find_first_or_raise(
- where={
- "transactionKey": str(request.payment_intent),
- "isActive": True,
- "type": CreditTransactionType.TOP_UP,
- }
- )
+ transaction = await self._find_top_up(request)
+ if not transaction:
+ return
if request.amount <= 0 or request.amount > transaction.amount:
raise AssertionError(
f"Invalid amount to deduct ${request.amount / 100} from ${transaction.amount / 100} top-up"
@@ -1018,13 +1014,9 @@ class UserCredit(UserCreditBase):
)
async def handle_dispute(self, dispute: stripe.Dispute):
- transaction = await CreditTransaction.prisma().find_first_or_raise(
- where={
- "transactionKey": str(dispute.payment_intent),
- "isActive": True,
- "type": CreditTransactionType.TOP_UP,
- }
- )
+ transaction = await self._find_top_up(dispute)
+ if not transaction:
+ return
user_id = transaction.userId
amount = dispute.amount
balance = await self.get_credits(user_id)
@@ -1080,6 +1072,47 @@ class UserCredit(UserCreditBase):
}
await stripe_call(stripe.Dispute.modify_async, dispute.id, evidence=evidence)
+ async def _find_top_up(
+ self, request: stripe.Refund | stripe.Dispute
+ ) -> CreditTransaction | None:
+ transaction = await CreditTransaction.prisma().find_first(
+ where={
+ "transactionKey": str(request.payment_intent),
+ "isActive": True,
+ "type": CreditTransactionType.TOP_UP,
+ }
+ )
+ if transaction:
+ return transaction
+ # A Checkout top-up stays keyed by its session until fulfilled, so a miss
+ # raises (Stripe retries) unless the payment cannot have bought credits.
+ if not request.payment_intent or not await self._paid_invoice_without_credits(
+ str(request.payment_intent)
+ ):
+ raise RuntimeError(
+ f"No active top-up matches {request.id} (payment intent "
+ f"{request.payment_intent}); retrying until one is fulfilled"
+ )
+ # A dispute is a chargeback someone must answer in Stripe; only ERROR reaches Sentry.
+ log = logger.error if isinstance(request, stripe.Dispute) else logger.warning
+ log(
+ f"Ignoring {request.id} (${request.amount / 100:.2f}): payment intent "
+ f"{request.payment_intent} paid a subscription invoice, which grants no credits"
+ )
+ return None
+
+ async def _paid_invoice_without_credits(self, payment_intent_id: str) -> bool:
+ # With the grant on, a paid subscription invoice buys credits too.
+ if settings.config.enable_subscription_credit_grant:
+ return False
+ # Pinned: 2025-03-31.basil dropped PaymentIntent.invoice for InvoicePayment.
+ payment_intent = await stripe_call(
+ stripe.PaymentIntent.retrieve_async,
+ payment_intent_id,
+ stripe_version="2025-02-24.acacia",
+ )
+ return bool(payment_intent.invoice)
+
async def _top_up_credits(
self,
user_id: str,
diff --git a/autogpt_platform/backend/backend/data/credit_refund_test.py b/autogpt_platform/backend/backend/data/credit_refund_test.py
index 792ec04b10..68d8184e47 100644
--- a/autogpt_platform/backend/backend/data/credit_refund_test.py
+++ b/autogpt_platform/backend/backend/data/credit_refund_test.py
@@ -6,13 +6,14 @@ are atomic and maintain data consistency.
"""
from datetime import datetime, timezone
-from unittest.mock import MagicMock, patch
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import stripe
-from prisma.enums import CreditTransactionType
+from prisma.enums import CreditRefundRequestStatus, CreditTransactionType
from prisma.models import CreditRefundRequest, CreditTransaction, User, UserBalance
+from backend.data import credit as credit_module
from backend.data.credit import UserCredit
from backend.util.json import SafeJson
from backend.util.test import SpinTestServer
@@ -144,21 +145,51 @@ async def test_deduct_credits_atomic(server: SpinTestServer):
@pytest.mark.asyncio(loop_scope="session")
-async def test_deduct_credits_user_not_found(server: SpinTestServer):
- """Test that deduct_credits raises error if transaction not found (which means user doesn't exist)."""
- # Create a mock refund object that references a non-existent payment intent
- refund = MagicMock(spec=stripe.Refund)
- refund.id = "re_test_refund_nonexistent"
- refund.payment_intent = "pi_test_nonexistent" # This payment intent doesn't exist
- refund.amount = 500
- refund.status = "succeeded"
- refund.reason = "requested_by_customer"
- refund.created = int(datetime.now(timezone.utc).timestamp())
+async def test_deduct_credits_ignores_refund_of_subscription_invoice(
+ subscription_payment_intent, server: SpinTestServer
+):
+ """A refund of a subscription invoice, which bought no credits, returns
+ without touching the ledger or the user's refund requests."""
+ topup_tx = await setup_test_user_with_topup()
+
+ try:
+ await CreditRefundRequest.prisma().create(
+ data={
+ "userId": REFUND_TEST_USER_ID,
+ "amount": 500,
+ "transactionKey": topup_tx.transactionKey,
+ "reason": "Test refund",
+ }
+ )
+ refund = MagicMock(spec=stripe.Refund)
+ refund.id = "re_test_non_topup"
+ refund.payment_intent = "pi_test_subscription_invoice"
+ refund.amount = 6000
+ refund.status = "succeeded"
+ refund.reason = "requested_by_customer"
- # Should raise error for missing transaction
- with pytest.raises(Exception): # Should raise NotFoundError for missing transaction
await credit_system.deduct_credits(refund)
+ assert subscription_payment_intent.await_args.args == (
+ "pi_test_subscription_invoice",
+ )
+ assert (
+ await CreditTransaction.prisma().count(where={"transactionKey": refund.id})
+ == 0
+ )
+ user_balance = await UserBalance.prisma().find_unique(
+ where={"userId": REFUND_TEST_USER_ID}
+ )
+ assert user_balance is not None
+ assert user_balance.balance == 1000
+ refund_request = await CreditRefundRequest.prisma().find_first(
+ where={"userId": REFUND_TEST_USER_ID}
+ )
+ assert refund_request is not None
+ assert refund_request.status == CreditRefundRequestStatus.PENDING
+ finally:
+ await cleanup_test_user()
+
@pytest.mark.asyncio(loop_scope="session")
@patch("backend.data.credit.settings")
@@ -268,6 +299,189 @@ async def test_handle_dispute_with_insufficient_balance(
await cleanup_test_user()
[email protected](loop_scope="session")
+@patch("stripe.Dispute.modify_async")
+async def test_handle_dispute_ignores_dispute_of_subscription_invoice(
+ mock_stripe_modify, subscription_payment_intent, server: SpinTestServer
+):
+ """A dispute of a subscription invoice is neither accepted nor contested,
+ and the user's unrelated top-up is left as it was."""
+ topup_tx = await setup_test_user_with_topup()
+
+ try:
+ dispute = MagicMock(spec=stripe.Dispute)
+ dispute.id = "du_test_non_topup"
+ dispute.payment_intent = "pi_test_subscription_invoice"
+ dispute.amount = 6000
+ dispute.status = "needs_response"
+ dispute.close = MagicMock()
+
+ await credit_system.handle_dispute(dispute)
+
+ dispute.close.assert_not_called()
+ mock_stripe_modify.assert_not_called()
+ assert await CreditTransaction.prisma().find_many(
+ where={"userId": REFUND_TEST_USER_ID}
+ ) == [topup_tx]
+ user_balance = await UserBalance.prisma().find_unique(
+ where={"userId": REFUND_TEST_USER_ID}
+ )
+ assert user_balance is not None
+ assert user_balance.balance == 1000
+ finally:
+ await cleanup_test_user()
+
+
[email protected](loop_scope="session")
+ "request_type, request_id, status",
+ [
+ (stripe.Refund, "re_test_early", "succeeded"),
+ (stripe.Dispute, "du_test_early", "lost"),
+ ],
+ ids=["refund", "lost-dispute"],
+)
+async def test_refund_or_lost_dispute_before_checkout_fulfillment_deducts_on_retry(
+ request_type, request_id, status, server: SpinTestServer
+):
+ """A refund or lost dispute that arrives while its Checkout top-up is still
+ unfulfilled raises, so Stripe retries it, and deducts once fulfillment ran."""
+ await setup_test_user_with_topup()
+ try:
+ await CreditTransaction.prisma().create(
+ data={
+ "userId": REFUND_TEST_USER_ID,
+ "amount": 2000,
+ "type": CreditTransactionType.TOP_UP,
+ "transactionKey": "cs_test_unfulfilled",
+ "isActive": False,
+ }
+ )
+ request = MagicMock(spec=request_type)
+ request.id = request_id
+ request.payment_intent = "pi_test_checkout"
+ request.amount = 2000
+ request.status = status
+ request.reason = "requested_by_customer"
+ paid_session = stripe.checkout.Session.construct_from(
+ {
+ "id": "cs_test_unfulfilled",
+ "object": "checkout.session",
+ "payment_status": "paid",
+ "amount_total": 2000,
+ "currency": "usd",
+ "payment_intent": {
+ "id": "pi_test_checkout",
+ "object": "payment_intent",
+ },
+ },
+ "sk_test",
+ )
+
+ with (
+ patch(
+ "stripe.PaymentIntent.retrieve_async",
+ new=_stripe_payment_intents(invoice=None),
+ ),
+ patch(
+ "stripe.checkout.Session.retrieve_async",
+ new_callable=AsyncMock,
+ return_value=paid_session,
+ ),
+ ):
+ with pytest.raises(RuntimeError):
+ await credit_system.deduct_credits(request)
+ assert (
+ await CreditTransaction.prisma().count(
+ where={"transactionKey": request_id}
+ )
+ == 0
+ )
+
+ await credit_system.fulfill_checkout(session_id="cs_test_unfulfilled")
+ await credit_system.deduct_credits(request)
+
+ deduction = await CreditTransaction.prisma().find_first(
+ where={"transactionKey": request_id}
+ )
+ assert deduction is not None
+ assert deduction.amount == -2000
+ user_balance = await UserBalance.prisma().find_unique(
+ where={"userId": REFUND_TEST_USER_ID}
+ )
+ assert user_balance is not None
+ assert user_balance.balance == 1000
+ finally:
+ await cleanup_test_user()
+
+
[email protected](loop_scope="session")
+ "grant_on, lookup_error, error",
+ [
+ (True, None, RuntimeError),
+ (False, stripe.APIConnectionError("down"), stripe.StripeError),
+ ],
+ ids=["invoices-grant-credits", "lookup-fails"],
+)
+async def test_refund_without_a_top_up_raises_unless_shown_creditless(
+ grant_on, lookup_error, error, monkeypatch, server: SpinTestServer
+):
+ """A refund matching no top-up is acknowledged only on proof it bought no
+ credits: an invoice bought some when invoices grant credits, and a failed
+ lookup proves nothing."""
+ monkeypatch.setattr(
+ credit_module.settings.config, "enable_subscription_credit_grant", grant_on
+ )
+ refund = MagicMock(spec=stripe.Refund)
+ refund.id = "re_test_unproven"
+ refund.payment_intent = "pi_test_subscription_invoice"
+ refund.amount = 6000
+ refund.status = "succeeded"
+
+ retrieve = _stripe_payment_intents(invoice="in_test_subscription")
+ if lookup_error:
+ retrieve.side_effect = lookup_error
+
+ with (
+ patch("stripe.PaymentIntent.retrieve_async", new=retrieve),
+ pytest.raises(error),
+ ):
+ await credit_system.deduct_credits(refund)
+
+ assert (
+ await CreditTransaction.prisma().count(where={"transactionKey": refund.id}) == 0
+ )
+
+
[email protected](loop_scope="session")
+ "request_type, status",
+ [(stripe.Refund, "succeeded"), (stripe.Dispute, "lost")],
+ ids=["refund", "lost-dispute"],
+)
+async def test_refund_or_lost_dispute_without_a_payment_intent_raises(
+ request_type, status, server: SpinTestServer
+):
+ """A refund or lost dispute with no PaymentIntent, as on a charge made
+ outside one, cannot be shown creditless: it raises, so Stripe retries,
+ without a lookup of a PaymentIntent named "None"."""
+ request = MagicMock(spec=request_type)
+ request.id = "re_test_no_intent"
+ request.payment_intent = None
+ request.amount = 6000
+ request.status = status
+ retrieve = _stripe_payment_intents(invoice="in_test_subscription")
+
+ with (
+ patch("stripe.PaymentIntent.retrieve_async", new=retrieve),
+ pytest.raises(RuntimeError),
+ ):
+ await credit_system.deduct_credits(request)
+
+ retrieve.assert_not_awaited()
+
+
@pytest.mark.asyncio(loop_scope="session")
async def test_concurrent_refunds(server: SpinTestServer):
"""Test that concurrent refunds are handled atomically."""
@@ -364,3 +578,34 @@ async def test_concurrent_refunds(server: SpinTestServer):
finally:
await cleanup_test_user()
+
+
+def subscription_payment_intent(monkeypatch):
+ """A PaymentIntent that paid a subscription invoice, with the SDK's default
+ API version moved past 2025-03-31.basil: only a request pinned before it
+ sees the invoice."""
+ monkeypatch.setattr(stripe, "api_version", "2026-09-30.endive")
+ retrieve = _stripe_payment_intents(invoice="in_test_subscription")
+ with patch("stripe.PaymentIntent.retrieve_async", new=retrieve):
+ yield retrieve
+
+
+def _stripe_payment_intents(invoice: str | None) -> AsyncMock:
+ """Answers as Stripe does: an unknown id is a 404, and only API versions
+ before 2025-03-31.basil name the invoice a PaymentIntent paid."""
+
+ async def retrieve(payment_intent_id: str, stripe_version: str | None = None):
+ if not payment_intent_id.startswith("pi_"):
+ raise stripe.InvalidRequestError(
+ f"No such payment_intent: '{payment_intent_id}'",
+ "intent",
+ code="resource_missing",
+ http_status=404,
+ )
+ fields: dict = {"id": payment_intent_id, "object": "payment_intent"}
+ if (stripe_version or stripe.api_version) < "2025-03-31":
+ fields["invoice"] = invoice
+ return stripe.PaymentIntent.construct_from(fields, "sk_test")
+
+ return AsyncMock(side_effect=retrieve)