Creates a Stripe Checkout session for the currently authenticated user to purchase the Max plan. The user's Auth0 ID is passed to Stripe for identification in webhooks.
(current_user: AuthUser, body: CheckoutRequest = None)
| 378 | summary="Create Stripe Checkout Session for Max Subscription" |
| 379 | ) |
| 380 | async def create_checkout_session_max(current_user: AuthUser, body: CheckoutRequest = None): |
| 381 | """ |
| 382 | Creates a Stripe Checkout session for the currently authenticated user to |
| 383 | purchase the Max plan. The user's Auth0 ID is passed to Stripe for |
| 384 | identification in webhooks. |
| 385 | """ |
| 386 | base_url = get_base_url(body.return_base_url if body else None) |
| 387 | |
| 388 | has_sub, existing_id, provider, active_customer_id = _get_subscription_from_metadata(current_user) |
| 389 | if has_sub: |
| 390 | if provider == "apple": |
| 391 | raise HTTPException( |
| 392 | status_code=400, |
| 393 | detail="You have an active Apple subscription. Please cancel it in iOS Settings before purchasing via Stripe." |
| 394 | ) |
| 395 | elif provider == "stripe" and active_customer_id: |
| 396 | portal = stripe.billing_portal.Session.create( |
| 397 | customer=active_customer_id, |
| 398 | return_url=f"{base_url}/refresh", |
| 399 | ) |
| 400 | return {"url": portal.url, "redirect": "portal"} |
| 401 | |
| 402 | if not MAX_PRICE_ID: |
| 403 | logger.error("MAX_PRICE_ID not configured but Max checkout was requested.") |
| 404 | raise HTTPException(status_code=503, detail="Max tier is not currently available.") |
| 405 | |
| 406 | try: |
| 407 | # Get or create Stripe customer with Auth0 email to prevent Link email mismatch |
| 408 | customer_id = get_or_create_stripe_customer(current_user) |
| 409 | |
| 410 | checkout_params = { |
| 411 | "line_items": [{"price": MAX_PRICE_ID, "quantity": 1}], |
| 412 | "mode": "subscription", |
| 413 | "client_reference_id": current_user.id, |
| 414 | "success_url": f"{base_url}/upgrade-success", |
| 415 | "cancel_url": base_url, |
| 416 | "allow_promotion_codes": True, |
| 417 | } |
| 418 | |
| 419 | if customer_id: |
| 420 | checkout_params["customer"] = customer_id |
| 421 | |
| 422 | checkout_session = stripe.checkout.Session.create(**checkout_params) |
| 423 | return {"url": checkout_session.url} |
| 424 | except Exception as e: |
| 425 | logger.error(f"Stripe Checkout creation failed for Max tier for user {current_user.id}: {e}") |
| 426 | raise HTTPException(status_code=500, detail="Could not create payment session.") |
| 427 | |
| 428 | |
| 429 | @payments_router.post( |
nothing calls this directly
no test coverage detected