diff options
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 |
