From 1ad2f8b234bcf227605f285450a41f40a0e0e3a7 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Thu, 13 Nov 2025 15:56:31 -0500 Subject: Add error handling for unconfigured Stripe billing portal - Catch Stripe exceptions when portal not configured - Redirect to account page with user-friendly error message - Display error alert on account page when present - Change portal return URL to /account instead of / Fixes issue when Stripe billing portal settings haven't been configured in test/production dashboard. Amp-Thread-ID: https://ampcode.com/threads/T-8edacbeb-b343-49ca-b524-1c999272acb6 Co-authored-by: Amp --- Biz/PodcastItLater/Billing.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'Biz/PodcastItLater/Billing.py') diff --git a/Biz/PodcastItLater/Billing.py b/Biz/PodcastItLater/Billing.py index 0e2537a..bf907bf 100644 --- a/Biz/PodcastItLater/Billing.py +++ b/Biz/PodcastItLater/Billing.py @@ -201,7 +201,7 @@ def create_portal_session(user_id: int, base_url: str) -> str: Portal session URL to redirect user to Raises: - ValueError: If user has no Stripe customer ID + ValueError: If user has no Stripe customer ID or portal not configured """ user = Core.Database.get_user_by_id(user_id) if not user: @@ -212,12 +212,25 @@ def create_portal_session(user_id: int, base_url: str) -> str: msg = "User has no Stripe customer ID" raise ValueError(msg) - session = stripe.billing_portal.Session.create( - customer=user["stripe_customer_id"], - return_url=f"{base_url}/", - ) + try: + session = stripe.billing_portal.Session.create( + customer=user["stripe_customer_id"], + return_url=f"{base_url}/account", + ) + except Exception as e: + # Catch Stripe errors (portal not configured, etc.) + logger.exception("Stripe portal error") + msg = ( + "Billing portal not configured. " + "Please contact support or cancel via your account page." + ) + raise ValueError(msg) from e - logger.info("Created portal session for user %s: %s", user_id, session.id) + logger.info( + "Created portal session for user %s: %s", + user_id, + session.id, + ) return session.url -- cgit v1.2.3