diff options
| author | Ben Sima <ben@bsima.me> | 2025-11-13 15:56:31 -0500 |
|---|---|---|
| committer | Ben Sima <ben@bsima.me> | 2025-11-13 15:56:31 -0500 |
| commit | 1ad2f8b234bcf227605f285450a41f40a0e0e3a7 (patch) | |
| tree | 56d43d101c257dfbf3867ccac3e4c5e7470179a7 /Biz/PodcastItLater/Billing.py | |
| parent | abc8230c04a787045a599327455445585edff46a (diff) | |
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 <amp@ampcode.com>
Diffstat (limited to 'Biz/PodcastItLater/Billing.py')
| -rw-r--r-- | Biz/PodcastItLater/Billing.py | 25 |
1 files changed, 19 insertions, 6 deletions
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 |
