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): offer saved cards in subscription checkout (#13743)

Nicholas Tindle43e7631f3d84
diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.py
index 2813d0413d..f8c4a972c2 100644
--- a/autogpt_platform/backend/backend/data/credit.py
+++ b/autogpt_platform/backend/backend/data/credit.py
@@ -2590,6 +2590,13 @@ async def create_subscription_checkout(
automatic_tax={"enabled": True},
billing_address_collection="auto",
customer_update={"address": "auto"},
+ # Cards attached by a previous subscription-mode Checkout carry
+ # allow_redisplay=limited; without "limited" in the filters Stripe
+ # hides them and returning subscribers must re-enter card details.
+ saved_payment_method_options={
+ "payment_method_save": "enabled",
+ "allow_redisplay_filters": ["always", "limited"],
+ },
metadata=datafast,
)
if not session.url:
diff --git a/autogpt_platform/backend/backend/data/credit_subscription_test.py b/autogpt_platform/backend/backend/data/credit_subscription_test.py
index 6fafde5b12..26200fed49 100644
--- a/autogpt_platform/backend/backend/data/credit_subscription_test.py
+++ b/autogpt_platform/backend/backend/data/credit_subscription_test.py
@@ -903,6 +903,49 @@ async def test_create_subscription_checkout_returns_url():
assert url == "https://checkout.stripe.com/pay/cs_test_abc123"
+async def test_create_subscription_checkout_offers_saved_payment_methods():
+ """Returning subscribers must see their saved cards in Checkout.
+
+ Cards attached by a previous subscription-mode Checkout get
+ ``allow_redisplay=limited``, so the filters must include ``limited`` —
+ the default (``always`` only) would hide every card saved this way and
+ force the user to re-enter card details.
+ """
+ mock_session = MagicMock()
+ mock_session.url = "https://checkout.stripe.com/pay/cs_test_abc123"
+ with (
+ patch(
+ "backend.data.credit.get_subscription_price_id",
+ new_callable=AsyncMock,
+ return_value="price_pro_monthly",
+ ),
+ patch(
+ "backend.data.credit.get_stripe_customer_id",
+ new_callable=AsyncMock,
+ return_value="cus_123",
+ ),
+ patch(
+ "backend.data.credit._expire_open_subscription_sessions",
+ new_callable=AsyncMock,
+ ),
+ patch(
+ "backend.data.credit.stripe.checkout.Session.create",
+ return_value=mock_session,
+ ) as mock_create,
+ ):
+ await create_subscription_checkout(
+ user_id="user-1",
+ tier=SubscriptionTier.PRO,
+ success_url="https://app.example.com/success",
+ cancel_url="https://app.example.com/cancel",
+ )
+ assert mock_create.call_args.kwargs["saved_payment_method_options"] == {
+ "payment_method_save": "enabled",
+ "allow_redisplay_filters": ["always", "limited"],
+ }
+
+
@pytest.mark.asyncio
async def test_create_subscription_checkout_no_price_raises():
with patch(