fix(billing): remove invalid request_options from Stripe SDK calls

Stripe Python SDK doesn't accept request_options as a kwarg to create/retrieve/modify.
Timeouts are handled by the global max_network_retries setting.
Also gracefully handle webhook endpoint creation failure for localhost URLs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Deeman
2026-03-03 16:36:47 +01:00
parent fab16cb48f
commit 2682e810fa
2 changed files with 22 additions and 13 deletions

View File

@@ -21,9 +21,6 @@ from ..core import config
logger = logging.getLogger(__name__)
# Timeout for all Stripe API calls (seconds)
_STRIPE_TIMEOUT_SECONDS = 10
def _stripe_client():
"""Configure and return the stripe module with our API key."""
@@ -49,7 +46,8 @@ def build_checkout_payload(
cancel_url=success_url.rsplit("/success", 1)[0] + "/pricing",
automatic_tax={"enabled": True},
tax_id_collection={"enabled": True},
request_options={"timeout": _STRIPE_TIMEOUT_SECONDS},
)
return {"checkout_url": session.url}
@@ -78,7 +76,8 @@ def build_multi_item_checkout_payload(
cancel_url=success_url.rsplit("/success", 1)[0],
automatic_tax={"enabled": True},
tax_id_collection={"enabled": True},
request_options={"timeout": _STRIPE_TIMEOUT_SECONDS},
)
return {"checkout_url": session.url}
@@ -108,7 +107,8 @@ def cancel_subscription(provider_subscription_id: str) -> None:
s.Subscription.modify(
provider_subscription_id,
cancel_at_period_end=True,
request_options={"timeout": _STRIPE_TIMEOUT_SECONDS},
)
@@ -119,12 +119,14 @@ def get_management_url(provider_subscription_id: str) -> str:
# Get customer_id from the subscription
sub = s.Subscription.retrieve(
provider_subscription_id,
request_options={"timeout": _STRIPE_TIMEOUT_SECONDS},
)
portal = s.billing_portal.Session.create(
customer=sub.customer,
return_url=f"{config.BASE_URL}/billing/success",
request_options={"timeout": _STRIPE_TIMEOUT_SECONDS},
)
return portal.url
@@ -180,7 +182,8 @@ def parse_webhook(payload: bytes) -> dict:
s = _stripe_client()
sub = s.Subscription.retrieve(
subscription_id,
request_options={"timeout": _STRIPE_TIMEOUT_SECONDS},
)
period_end = _unix_to_iso(sub.current_period_end)
except Exception:

View File

@@ -197,10 +197,16 @@ def create(conn):
logger.info("Creating webhook endpoint...")
logger.info(" URL: %s", webhook_url)
try:
endpoint = stripe.WebhookEndpoint.create(
url=webhook_url,
enabled_events=enabled_events,
)
except stripe.InvalidRequestError as exc:
logger.warning(" Webhook endpoint creation failed: %s", exc.user_message)
logger.info(" For local dev, use Stripe CLI: stripe listen --forward-to %s", webhook_url)
logger.info("Done (products created, webhook skipped).")
return
webhook_secret = endpoint.secret
logger.info(" ID: %s", endpoint.id)