Files
bakery-ia/services/tenant/app/api/subscription.py

1265 lines
52 KiB
Python
Raw Permalink Normal View History

2025-10-16 07:28:04 +02:00
"""
2026-01-16 15:19:34 +01:00
Subscription API - All subscription-related endpoints
2026-01-15 22:06:36 +01:00
2026-01-16 15:19:34 +01:00
This module contains all subscription-related endpoints following the new architecture:
2026-01-15 22:06:36 +01:00
2026-01-16 15:19:34 +01:00
PUBLIC ENDPOINTS (No Authentication):
- GET /api/v1/plans - Available from plans.py router
REGISTRATION FLOW (No Tenant Context):
- POST /api/v1/registration/payment-setup - Start registration with payment
- POST /api/v1/registration/complete - Complete registration after 3DS
- GET /api/v1/registration/state/{state_id} - Check registration state
TENANT-DEPENDENT SUBSCRIPTION ENDPOINTS:
- GET /api/v1/tenants/{tenant_id}/subscription/status - Get subscription status
- GET /api/v1/tenants/{tenant_id}/subscription/details - Get full subscription details
- GET /api/v1/tenants/{tenant_id}/subscription/tier - Get subscription tier (cached)
- GET /api/v1/tenants/{tenant_id}/subscription/limits - Get subscription limits
- GET /api/v1/tenants/{tenant_id}/subscription/usage - Get usage summary
- GET /api/v1/tenants/{tenant_id}/subscription/features/{feature} - Check feature access
SUBSCRIPTION MANAGEMENT:
- POST /api/v1/tenants/{tenant_id}/subscription/cancel - Cancel subscription
- POST /api/v1/tenants/{tenant_id}/subscription/reactivate - Reactivate subscription
- GET /api/v1/tenants/{tenant_id}/subscription/validate-upgrade/{new_plan} - Validate upgrade
- POST /api/v1/tenants/{tenant_id}/subscription/upgrade - Upgrade subscription
QUOTA & LIMIT CHECKS:
- GET /api/v1/tenants/{tenant_id}/subscription/limits/locations - Check location limits
- GET /api/v1/tenants/{tenant_id}/subscription/limits/products - Check product limits
- GET /api/v1/tenants/{tenant_id}/subscription/limits/users - Check user limits
- GET /api/v1/tenants/{tenant_id}/subscription/limits/recipes - Check recipe limits
- GET /api/v1/tenants/{tenant_id}/subscription/limits/suppliers - Check supplier limits
PAYMENT MANAGEMENT:
- GET /api/v1/tenants/{tenant_id}/subscription/payment-method - Get payment method
- POST /api/v1/tenants/{tenant_id}/subscription/payment-method - Update payment method
- GET /api/v1/tenants/{tenant_id}/subscription/invoices - Get invoices
2025-10-16 07:28:04 +02:00
"""
2026-01-15 20:45:49 +01:00
import logging
2026-01-16 15:19:34 +01:00
import json
from typing import Dict, Any, Optional
from datetime import datetime, timezone
from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException, status, Request, Query, Path
2025-10-16 07:28:04 +02:00
from sqlalchemy.ext.asyncio import AsyncSession
2026-01-16 15:19:34 +01:00
from sqlalchemy import select
2026-01-15 20:45:49 +01:00
from app.services.subscription_orchestration_service import SubscriptionOrchestrationService
2026-01-16 15:19:34 +01:00
from app.services.subscription_limit_service import SubscriptionLimitService
2026-01-15 22:06:36 +01:00
from app.services.coupon_service import CouponService
2026-01-15 20:45:49 +01:00
from app.core.database import get_db
2026-01-16 15:19:34 +01:00
from app.core.config import settings
from app.models.tenants import Subscription
2026-01-15 20:45:49 +01:00
from app.services.registration_state_service import (
registration_state_service,
RegistrationStateService,
RegistrationState
)
from shared.exceptions.payment_exceptions import (
PaymentServiceError,
SetupIntentError,
SubscriptionCreationFailed,
)
from shared.exceptions.registration_exceptions import (
RegistrationStateError,
)
2026-01-16 15:19:34 +01:00
from shared.auth.decorators import get_current_user_dep
from shared.database.base import create_database_manager
import shared.redis_utils
2026-01-15 20:45:49 +01:00
logger = logging.getLogger(__name__)
2026-01-16 15:19:34 +01:00
router = APIRouter(prefix="/api/v1", tags=["subscription"])
# Global Redis client
_redis_client = None
2026-01-15 20:45:49 +01:00
2026-01-16 15:19:34 +01:00
# ============================================================================
# DEPENDENCY INJECTION
# ============================================================================
2026-01-15 20:45:49 +01:00
async def get_subscription_orchestration_service(
db: AsyncSession = Depends(get_db)
) -> SubscriptionOrchestrationService:
"""Dependency injection for subscription orchestration service"""
return SubscriptionOrchestrationService(db)
2026-01-14 13:15:48 +01:00
2026-01-15 20:45:49 +01:00
async def get_registration_state_service() -> RegistrationStateService:
"""Dependency injection for registration state service"""
return registration_state_service
2026-01-14 13:15:48 +01:00
2026-01-16 15:19:34 +01:00
async def get_subscription_redis_client():
"""Get or create Redis client"""
global _redis_client
try:
if _redis_client is None:
_redis_client = await shared.redis_utils.initialize_redis(settings.REDIS_URL)
logger.info("Redis client initialized for subscription service")
return _redis_client
except Exception as e:
logger.warning("Failed to initialize Redis client", extra={"error": str(e)})
return None
def get_subscription_limit_service():
"""Create subscription limit service instance"""
try:
database_manager = create_database_manager(settings.DATABASE_URL, "tenant-service")
return SubscriptionLimitService(database_manager, None)
except Exception as e:
logger.error("Failed to create subscription limit service", extra={"error": str(e)})
raise HTTPException(status_code=500, detail="Service initialization failed")
# ============================================================================
# REGISTRATION FLOW ENDPOINTS (No Tenant Context)
# ============================================================================
@router.post("/registration/payment-setup",
response_model=Dict[str, Any],
summary="Start registration payment setup")
2026-01-15 20:45:49 +01:00
async def create_registration_payment_setup(
user_data: Dict[str, Any],
request: Request,
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service),
state_service: RegistrationStateService = Depends(get_registration_state_service)
) -> Dict[str, Any]:
2026-01-14 13:15:48 +01:00
"""
2026-01-15 22:06:36 +01:00
Start registration payment setup (SetupIntent-first architecture).
2026-01-16 15:19:34 +01:00
Creates customer + SetupIntent only (NO subscription).
Subscription is created in /registration/complete after 3DS verification.
2026-01-15 22:06:36 +01:00
2026-01-15 20:45:49 +01:00
Args:
user_data: User registration data with payment info
2026-01-16 15:19:34 +01:00
- email (required)
- payment_method_id (required)
- plan_id (required)
- billing_cycle (optional, defaults to 'monthly')
- coupon_code (optional)
2026-01-15 22:06:36 +01:00
2026-01-15 20:45:49 +01:00
Returns:
2026-01-15 22:06:36 +01:00
SetupIntent data for frontend confirmation
2026-01-14 13:15:48 +01:00
"""
2026-01-15 22:06:36 +01:00
state_id = None
2026-01-14 13:15:48 +01:00
try:
2026-01-15 22:06:36 +01:00
logger.info("Registration payment setup started",
2026-01-16 15:19:34 +01:00
extra={"email": user_data.get('email'), "plan_id": user_data.get('plan_id')})
2026-01-15 22:06:36 +01:00
2026-01-15 20:45:49 +01:00
if not user_data.get('email'):
2026-01-15 22:06:36 +01:00
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Email is required")
2026-01-15 20:45:49 +01:00
if not user_data.get('payment_method_id'):
2026-01-15 22:06:36 +01:00
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Payment method ID is required")
2026-01-14 13:15:48 +01:00
2026-01-15 20:45:49 +01:00
if not user_data.get('plan_id'):
2026-01-15 22:06:36 +01:00
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Plan ID is required")
2026-01-14 13:15:48 +01:00
2026-01-15 20:45:49 +01:00
state_id = await state_service.create_registration_state(
email=user_data['email'],
user_data=user_data
2026-01-14 13:15:48 +01:00
)
2026-01-15 20:45:49 +01:00
result = await orchestration_service.create_registration_payment_setup(
user_data=user_data,
plan_id=user_data.get('plan_id', 'professional'),
payment_method_id=user_data.get('payment_method_id'),
billing_interval=user_data.get('billing_cycle', 'monthly'),
coupon_code=user_data.get('coupon_code')
)
2026-01-15 22:06:36 +01:00
2026-01-15 20:45:49 +01:00
await state_service.update_state_context(state_id, {
'setup_intent_id': result.get('setup_intent_id'),
'customer_id': result.get('customer_id'),
2026-01-15 22:06:36 +01:00
'payment_method_id': result.get('payment_method_id'),
'plan_id': result.get('plan_id'),
'billing_interval': result.get('billing_interval'),
'trial_period_days': result.get('trial_period_days'),
'coupon_code': result.get('coupon_code')
2026-01-15 20:45:49 +01:00
})
2026-01-15 22:06:36 +01:00
await state_service.transition_state(state_id, RegistrationState.PAYMENT_VERIFICATION_PENDING)
logger.info("Registration payment setup completed",
2026-01-16 15:19:34 +01:00
extra={
"email": user_data.get('email'),
"setup_intent_id": result.get('setup_intent_id'),
"requires_action": result.get('requires_action')
})
2026-01-14 13:15:48 +01:00
2026-01-15 20:45:49 +01:00
return {
"success": True,
2026-01-15 22:06:36 +01:00
"requires_action": result.get('requires_action', True),
"action_type": result.get('action_type', 'use_stripe_sdk'),
2026-01-15 20:45:49 +01:00
"client_secret": result.get('client_secret'),
"setup_intent_id": result.get('setup_intent_id'),
"customer_id": result.get('customer_id'),
2026-01-15 22:06:36 +01:00
"payment_customer_id": result.get('customer_id'),
2026-01-15 20:45:49 +01:00
"plan_id": result.get('plan_id'),
"payment_method_id": result.get('payment_method_id'),
2026-01-15 22:06:36 +01:00
"trial_period_days": result.get('trial_period_days', 0),
"billing_cycle": result.get('billing_interval'),
2026-01-15 20:45:49 +01:00
"email": result.get('email'),
"state_id": state_id,
2026-01-15 22:06:36 +01:00
"message": result.get('message', 'Payment verification required')
2026-01-15 20:45:49 +01:00
}
2026-01-14 13:15:48 +01:00
2026-01-15 20:45:49 +01:00
except PaymentServiceError as e:
2026-01-15 22:06:36 +01:00
logger.error(f"Payment setup failed: {str(e)}", extra={"email": user_data.get('email')}, exc_info=True)
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Payment setup failed: {str(e)}") from e
2026-01-15 20:45:49 +01:00
except RegistrationStateError as e:
2026-01-15 22:06:36 +01:00
logger.error(f"Registration state error: {str(e)}", extra={"email": user_data.get('email')}, exc_info=True)
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Registration state error: {str(e)}") from e
except HTTPException:
raise
2026-01-11 21:40:04 +01:00
except Exception as e:
2026-01-15 22:06:36 +01:00
logger.error(f"Unexpected error: {str(e)}", extra={"email": user_data.get('email')}, exc_info=True)
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Registration failed: {str(e)}") from e
2026-01-13 22:22:38 +01:00
2026-01-16 15:19:34 +01:00
@router.post("/registration/complete",
response_model=Dict[str, Any],
summary="Complete registration after 3DS verification")
2026-01-15 20:45:49 +01:00
async def verify_and_complete_registration(
verification_data: Dict[str, Any],
request: Request,
2026-01-15 22:06:36 +01:00
db: AsyncSession = Depends(get_db),
2026-01-15 20:45:49 +01:00
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service),
state_service: RegistrationStateService = Depends(get_registration_state_service)
) -> Dict[str, Any]:
2026-01-13 22:22:38 +01:00
"""
2026-01-15 22:06:36 +01:00
Complete registration after frontend confirms SetupIntent.
2026-01-16 15:19:34 +01:00
Creates subscription AFTER payment verification is complete.
2026-01-15 22:06:36 +01:00
This is the ONLY place subscriptions are created during registration.
2026-01-15 20:45:49 +01:00
Args:
2026-01-15 22:06:36 +01:00
verification_data: SetupIntent verification data with user_data
2026-01-16 15:19:34 +01:00
- setup_intent_id (required)
- user_data (required)
- state_id (optional)
2026-01-15 22:06:36 +01:00
2026-01-15 20:45:49 +01:00
Returns:
2026-01-15 22:06:36 +01:00
Subscription creation result
2026-01-13 22:22:38 +01:00
"""
2026-01-15 22:06:36 +01:00
setup_intent_id = None
user_data = {}
state_id = None
2026-01-13 22:22:38 +01:00
try:
2026-01-15 20:45:49 +01:00
if not verification_data.get('setup_intent_id'):
2026-01-15 22:06:36 +01:00
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="SetupIntent ID is required")
2026-01-15 20:45:49 +01:00
if not verification_data.get('user_data'):
2026-01-15 22:06:36 +01:00
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="User data is required")
2026-01-15 20:45:49 +01:00
setup_intent_id = verification_data['setup_intent_id']
user_data = verification_data['user_data']
state_id = verification_data.get('state_id')
2026-01-13 22:22:38 +01:00
2026-01-15 22:06:36 +01:00
logger.info("Completing registration after verification",
2026-01-16 15:19:34 +01:00
extra={"email": user_data.get('email'), "setup_intent_id": setup_intent_id})
2026-01-15 20:45:49 +01:00
2026-01-15 22:06:36 +01:00
trial_period_days = 0
2026-01-15 20:45:49 +01:00
coupon_code = user_data.get('coupon_code')
2026-01-16 15:19:34 +01:00
2026-01-15 22:06:36 +01:00
if coupon_code:
logger.info("Validating coupon in completion call",
2026-01-16 15:19:34 +01:00
extra={"coupon_code": coupon_code, "email": user_data.get('email')})
2026-01-15 22:06:36 +01:00
coupon_service = CouponService(db)
success, discount_applied, error = await coupon_service.redeem_coupon(
coupon_code,
2026-01-16 15:19:34 +01:00
None,
2026-01-15 22:06:36 +01:00
base_trial_days=0
2026-01-15 20:45:49 +01:00
)
2026-01-16 15:19:34 +01:00
2026-01-15 22:06:36 +01:00
if success and discount_applied:
trial_period_days = discount_applied.get("total_trial_days", 0)
logger.info("Coupon validated in completion call",
2026-01-16 15:19:34 +01:00
extra={"coupon_code": coupon_code, "trial_period_days": trial_period_days})
2026-01-15 22:06:36 +01:00
else:
logger.warning("Failed to validate coupon in completion call",
2026-01-16 15:19:34 +01:00
extra={"coupon_code": coupon_code, "error": error})
2026-01-15 22:06:36 +01:00
elif 'trial_period_days' in user_data:
trial_period_days = int(user_data.get('trial_period_days', 0))
logger.info("Using explicitly provided trial period",
2026-01-16 15:19:34 +01:00
extra={"trial_period_days": trial_period_days})
2026-01-15 22:06:36 +01:00
result = await orchestration_service.complete_registration_subscription(
setup_intent_id=setup_intent_id,
customer_id=user_data.get('customer_id', ''),
plan_id=user_data.get('plan_id') or user_data.get('subscription_plan', 'professional'),
payment_method_id=user_data.get('payment_method_id', ''),
billing_interval=user_data.get('billing_cycle') or user_data.get('billing_interval', 'monthly'),
trial_period_days=trial_period_days,
user_id=user_data.get('user_id')
)
2026-01-15 20:45:49 +01:00
if state_id:
try:
await state_service.update_state_context(state_id, {
'subscription_id': result['subscription_id'],
'status': result['status']
})
2026-01-15 22:06:36 +01:00
await state_service.transition_state(state_id, RegistrationState.SUBSCRIPTION_CREATED)
2026-01-15 20:45:49 +01:00
except Exception as e:
2026-01-15 22:06:36 +01:00
logger.warning(f"Failed to update registration state: {e}", extra={"state_id": state_id})
logger.info("Registration subscription created successfully",
2026-01-16 15:19:34 +01:00
extra={
"email": user_data.get('email'),
"subscription_id": result['subscription_id'],
"status": result['status']
})
2026-01-15 22:06:36 +01:00
2026-01-15 20:45:49 +01:00
return {
"success": True,
"subscription_id": result['subscription_id'],
"customer_id": result['customer_id'],
"payment_customer_id": result.get('payment_customer_id', result['customer_id']),
"status": result['status'],
2026-01-15 22:06:36 +01:00
"plan_id": result.get('plan_id'),
2026-01-15 20:45:49 +01:00
"payment_method_id": result.get('payment_method_id'),
2026-01-15 22:06:36 +01:00
"trial_period_days": result.get('trial_period_days', 0),
2026-01-15 20:45:49 +01:00
"current_period_end": result.get('current_period_end'),
"state_id": state_id,
2026-01-15 22:06:36 +01:00
"message": "Subscription created successfully"
2026-01-15 20:45:49 +01:00
}
2026-01-15 22:06:36 +01:00
2026-01-15 20:45:49 +01:00
except SetupIntentError as e:
2026-01-15 22:06:36 +01:00
logger.error(f"SetupIntent verification failed: {e}",
2026-01-16 15:19:34 +01:00
extra={"setup_intent_id": setup_intent_id, "email": user_data.get('email')},
exc_info=True)
2026-01-15 20:45:49 +01:00
if state_id:
try:
2026-01-15 22:06:36 +01:00
await state_service.mark_registration_failed(state_id, f"Verification failed: {e}")
2026-01-15 20:45:49 +01:00
except Exception:
2026-01-15 22:06:36 +01:00
pass
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"Payment verification failed: {e}") from e
2026-01-15 20:45:49 +01:00
except SubscriptionCreationFailed as e:
2026-01-15 22:06:36 +01:00
logger.error(f"Subscription creation failed: {e}",
2026-01-16 15:19:34 +01:00
extra={"setup_intent_id": setup_intent_id, "email": user_data.get('email')},
exc_info=True)
2026-01-15 20:45:49 +01:00
if state_id:
try:
2026-01-15 22:06:36 +01:00
await state_service.mark_registration_failed(state_id, f"Subscription failed: {e}")
2026-01-15 20:45:49 +01:00
except Exception:
2026-01-15 22:06:36 +01:00
pass
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Subscription creation failed: {e}") from e
except HTTPException:
raise
2026-01-13 22:22:38 +01:00
except Exception as e:
2026-01-15 22:06:36 +01:00
logger.error(f"Unexpected error: {e}",
2026-01-16 15:19:34 +01:00
extra={"setup_intent_id": setup_intent_id, "email": user_data.get('email')},
exc_info=True)
2026-01-15 20:45:49 +01:00
if state_id:
try:
2026-01-15 22:06:36 +01:00
await state_service.mark_registration_failed(state_id, f"Registration failed: {e}")
2026-01-15 20:45:49 +01:00
except Exception:
2026-01-15 22:06:36 +01:00
pass
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Registration failed: {e}") from e
2026-01-13 22:22:38 +01:00
2026-01-16 15:19:34 +01:00
@router.get("/registration/state/{state_id}",
response_model=Dict[str, Any],
summary="Get registration state")
2026-01-15 20:45:49 +01:00
async def get_registration_state(
2026-01-16 15:19:34 +01:00
state_id: str = Path(..., description="Registration state ID"),
request: Request = None,
2026-01-15 20:45:49 +01:00
state_service: RegistrationStateService = Depends(get_registration_state_service)
) -> Dict[str, Any]:
2026-01-13 22:22:38 +01:00
"""
2026-01-15 20:45:49 +01:00
Get registration state by ID
2026-01-16 15:19:34 +01:00
2026-01-15 20:45:49 +01:00
Args:
state_id: Registration state ID
2026-01-16 15:19:34 +01:00
2026-01-15 20:45:49 +01:00
Returns:
Registration state data
2026-01-16 15:19:34 +01:00
2026-01-15 20:45:49 +01:00
Raises:
HTTPException: 404 if state not found, 500 for server errors
2026-01-13 22:22:38 +01:00
"""
try:
2026-01-16 15:19:34 +01:00
logger.info("Getting registration state", extra={"state_id": state_id})
2026-01-15 20:45:49 +01:00
state_data = await state_service.get_registration_state(state_id)
2026-01-16 15:19:34 +01:00
2026-01-15 20:45:49 +01:00
logger.info("Registration state retrieved",
2026-01-16 15:19:34 +01:00
extra={
"state_id": state_id,
"current_state": state_data['current_state']
})
2026-01-15 20:45:49 +01:00
return {
"state_id": state_data['state_id'],
"email": state_data['email'],
"current_state": state_data['current_state'],
"created_at": state_data['created_at'],
"updated_at": state_data['updated_at'],
"setup_intent_id": state_data.get('setup_intent_id'),
"customer_id": state_data.get('customer_id'),
"subscription_id": state_data.get('subscription_id'),
"error": state_data.get('error'),
"user_data": state_data.get('user_data')
}
2026-01-16 15:19:34 +01:00
2026-01-15 20:45:49 +01:00
except RegistrationStateError as e:
logger.error("Registration state not found",
2026-01-16 15:19:34 +01:00
extra={"error": str(e), "state_id": state_id})
2026-01-13 22:22:38 +01:00
raise HTTPException(
2026-01-15 20:45:49 +01:00
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Registration state not found: {str(e)}"
) from e
2026-01-13 22:22:38 +01:00
except Exception as e:
2026-01-15 20:45:49 +01:00
logger.error("Unexpected error getting registration state",
2026-01-16 15:19:34 +01:00
extra={"error": str(e), "state_id": state_id},
exc_info=True)
2026-01-13 22:22:38 +01:00
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
2026-01-15 20:45:49 +01:00
detail=f"Failed to get registration state: {str(e)}"
) from e
2026-01-13 22:22:38 +01:00
# ============================================================================
2026-01-16 15:19:34 +01:00
# TENANT SUBSCRIPTION STATUS ENDPOINTS
2026-01-13 22:22:38 +01:00
# ============================================================================
2026-01-16 15:19:34 +01:00
@router.get("/tenants/{tenant_id}/subscription/status",
response_model=Dict[str, Any],
summary="Get subscription status")
async def get_subscription_status(
tenant_id: str = Path(..., description="Tenant ID"),
db: AsyncSession = Depends(get_db)
) -> Dict[str, Any]:
"""
Get subscription status for read-only mode enforcement
"""
try:
result = await db.execute(
select(Subscription).where(
Subscription.tenant_id == tenant_id,
Subscription.status == "active"
)
)
subscription = result.scalars().first()
if subscription:
return {
"status": subscription.status,
"plan": subscription.plan,
"is_read_only": False,
"cancellation_effective_date": subscription.cancellation_effective_date.isoformat() if subscription.cancellation_effective_date else None
}
else:
return {
"status": "inactive",
"plan": None,
"is_read_only": True,
"cancellation_effective_date": None
}
except Exception as e:
logger.error(f"Failed to get subscription status: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to get subscription status: {str(e)}"
) from e
@router.get("/tenants/{tenant_id}/subscription/details",
response_model=Dict[str, Any],
summary="Get full subscription details")
async def get_subscription_details(
tenant_id: str = Path(..., description="Tenant ID"),
redis_client=Depends(get_subscription_redis_client)
) -> Dict[str, Any]:
"""
Get full active subscription with caching (10-minute cache)
"""
try:
from app.services.subscription_cache import get_subscription_cache_service
cache_service = get_subscription_cache_service(redis_client)
subscription = await cache_service.get_tenant_subscription_cached(tenant_id)
if not subscription:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="No active subscription found"
)
return subscription
except HTTPException:
raise
except Exception as e:
logger.error("Failed to get subscription details",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to get subscription details"
)
@router.get("/tenants/{tenant_id}/subscription/tier",
response_model=Dict[str, Any],
summary="Get subscription tier (cached)")
async def get_subscription_tier(
tenant_id: str = Path(..., description="Tenant ID"),
redis_client=Depends(get_subscription_redis_client)
) -> Dict[str, Any]:
"""
Fast cached lookup for tenant subscription tier
Optimized for high-frequency access (e.g., from gateway middleware)
with Redis caching (10-minute TTL).
"""
try:
from app.services.subscription_cache import get_subscription_cache_service
cache_service = get_subscription_cache_service(redis_client)
tier = await cache_service.get_tenant_tier_cached(tenant_id)
return {
"tenant_id": tenant_id,
"tier": tier,
"cached": True
}
except Exception as e:
logger.error("Failed to get subscription tier",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to get subscription tier"
)
@router.get("/tenants/{tenant_id}/subscription/limits",
2026-01-15 20:45:49 +01:00
response_model=Dict[str, Any],
2026-01-16 15:19:34 +01:00
summary="Get subscription limits")
async def get_subscription_limits(
tenant_id: str = Path(..., description="Tenant ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
limit_service: SubscriptionLimitService = Depends(get_subscription_limit_service)
) -> Dict[str, Any]:
"""Get current subscription limits for a tenant"""
try:
limits = await limit_service.get_tenant_subscription_limits(tenant_id)
return limits
except Exception as e:
logger.error("Failed to get subscription limits",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to get subscription limits"
)
@router.get("/tenants/{tenant_id}/subscription/usage",
response_model=Dict[str, Any],
summary="Get usage summary")
async def get_usage_summary(
tenant_id: str = Path(..., description="Tenant ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
limit_service: SubscriptionLimitService = Depends(get_subscription_limit_service)
) -> Dict[str, Any]:
"""Get usage summary vs limits for a tenant (cached for 30s for performance)"""
try:
from shared.redis_utils import get_redis_client
cache_key = f"usage_summary:{tenant_id}"
redis_client = await get_redis_client()
if redis_client:
cached = await redis_client.get(cache_key)
if cached:
logger.debug("Usage summary cache hit", extra={"tenant_id": tenant_id})
return json.loads(cached)
usage = await limit_service.get_usage_summary(tenant_id)
if redis_client:
await redis_client.setex(cache_key, 30, json.dumps(usage))
logger.debug("Usage summary cached", extra={"tenant_id": tenant_id})
return usage
except Exception as e:
logger.error("Failed to get usage summary",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to get usage summary"
)
@router.get("/tenants/{tenant_id}/subscription/features/{feature}",
response_model=Dict[str, Any],
summary="Check feature access")
async def check_feature_access(
tenant_id: str = Path(..., description="Tenant ID"),
feature: str = Path(..., description="Feature name"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
limit_service: SubscriptionLimitService = Depends(get_subscription_limit_service)
) -> Dict[str, Any]:
"""Check if tenant has access to a specific feature"""
try:
result = await limit_service.has_feature(tenant_id, feature)
return result
except Exception as e:
logger.error("Failed to check feature access",
extra={"tenant_id": tenant_id, "feature": feature, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to check feature access"
)
# ============================================================================
# SUBSCRIPTION MANAGEMENT ENDPOINTS
# ============================================================================
@router.post("/tenants/{tenant_id}/subscription/cancel",
response_model=Dict[str, Any],
summary="Cancel subscription")
2026-01-15 20:45:49 +01:00
async def cancel_subscription(
2026-01-16 15:19:34 +01:00
tenant_id: str = Path(..., description="Tenant ID"),
request: Request = None,
2026-01-15 20:45:49 +01:00
reason: str = Query("", description="Cancellation reason"),
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service)
) -> Dict[str, Any]:
2026-01-13 22:22:38 +01:00
"""
2026-01-15 20:45:49 +01:00
Cancel a subscription and set to read-only mode
2026-01-16 15:19:34 +01:00
2026-01-15 20:45:49 +01:00
This endpoint allows users to cancel their subscription, which will:
- Mark subscription as pending cancellation
- Set read-only mode effective date to end of current billing period
- Allow read-only access until the end of paid period
2026-01-13 22:22:38 +01:00
"""
try:
2026-01-15 20:45:49 +01:00
result = await orchestration_service.orchestrate_subscription_cancellation(
2026-01-13 22:22:38 +01:00
tenant_id,
2026-01-15 20:45:49 +01:00
reason
2026-01-13 22:22:38 +01:00
)
2026-01-15 20:45:49 +01:00
return {
"success": True,
"message": result.get("message", "Subscription cancellation initiated"),
"status": result.get("status", "pending_cancellation"),
"cancellation_effective_date": result.get("cancellation_effective_date"),
"days_remaining": result.get("days_remaining"),
"read_only_mode_starts": result.get("read_only_mode_starts")
}
2026-01-13 22:22:38 +01:00
except Exception as e:
2026-01-15 20:45:49 +01:00
logger.error("Failed to cancel subscription",
2026-01-16 15:19:34 +01:00
extra={"error": str(e), "tenant_id": tenant_id},
exc_info=True)
2026-01-13 22:22:38 +01:00
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
2026-01-15 20:45:49 +01:00
detail=f"Failed to cancel subscription: {str(e)}"
) from e
2026-01-14 13:15:48 +01:00
2026-01-16 15:19:34 +01:00
@router.post("/tenants/{tenant_id}/subscription/reactivate",
response_model=Dict[str, Any],
summary="Reactivate subscription")
async def reactivate_subscription(
tenant_id: str = Path(..., description="Tenant ID"),
plan: str = Query("starter", description="Plan to reactivate to"),
2026-01-15 20:45:49 +01:00
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service)
) -> Dict[str, Any]:
2026-01-14 13:15:48 +01:00
"""
2026-01-16 15:19:34 +01:00
Reactivate a cancelled or inactive subscription
2026-01-14 13:15:48 +01:00
"""
try:
2026-01-16 15:19:34 +01:00
result = await orchestration_service.orchestrate_subscription_reactivation(
tenant_id,
plan
)
2026-01-14 13:15:48 +01:00
return {
"success": True,
2026-01-16 15:19:34 +01:00
"message": result.get("message", "Subscription reactivated successfully"),
"status": result.get("status", "active"),
"plan": result.get("plan", plan),
"next_billing_date": result.get("next_billing_date")
2026-01-14 13:15:48 +01:00
}
except Exception as e:
2026-01-16 15:19:34 +01:00
logger.error("Failed to reactivate subscription",
extra={"error": str(e), "tenant_id": tenant_id},
exc_info=True)
2026-01-14 13:15:48 +01:00
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
2026-01-16 15:19:34 +01:00
detail=f"Failed to reactivate subscription: {str(e)}"
2026-01-15 20:45:49 +01:00
) from e
2026-01-14 13:15:48 +01:00
2026-01-16 15:19:34 +01:00
@router.get("/tenants/{tenant_id}/subscription/validate-upgrade/{new_plan}",
response_model=Dict[str, Any],
summary="Validate plan upgrade eligibility")
2026-01-15 20:45:49 +01:00
async def validate_plan_upgrade(
2026-01-16 15:19:34 +01:00
tenant_id: str = Path(..., description="Tenant ID"),
new_plan: str = Path(..., description="New plan name"),
2026-01-15 20:45:49 +01:00
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service)
) -> Dict[str, Any]:
2026-01-14 13:15:48 +01:00
"""
2026-01-15 20:45:49 +01:00
Validate if a tenant can upgrade to a new plan
2026-01-16 15:19:34 +01:00
2026-01-15 20:45:49 +01:00
Checks plan hierarchy and subscription status before allowing upgrade.
2026-01-14 13:15:48 +01:00
"""
try:
2026-01-15 20:45:49 +01:00
validation = await orchestration_service.validate_plan_upgrade(tenant_id, new_plan)
return validation
except Exception as e:
logger.error("Failed to validate plan upgrade",
2026-01-16 15:19:34 +01:00
extra={"error": str(e), "tenant_id": tenant_id, "new_plan": new_plan},
exc_info=True)
2026-01-15 20:45:49 +01:00
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to validate plan upgrade: {str(e)}"
) from e
2026-01-16 15:19:34 +01:00
@router.post("/tenants/{tenant_id}/subscription/upgrade",
response_model=Dict[str, Any],
summary="Upgrade subscription plan")
async def upgrade_subscription_plan(
tenant_id: str = Path(..., description="Tenant ID"),
new_plan: str = Query(..., description="New plan name"),
2026-01-16 20:25:45 +01:00
billing_cycle: str = Query("monthly", description="Billing cycle (monthly/yearly)"),
2026-01-16 15:19:34 +01:00
current_user: Dict[str, Any] = Depends(get_current_user_dep),
2026-01-16 20:25:45 +01:00
limit_service: SubscriptionLimitService = Depends(get_subscription_limit_service),
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service)
2026-01-15 20:45:49 +01:00
) -> Dict[str, Any]:
"""
2026-01-16 15:19:34 +01:00
Upgrade subscription plan for a tenant
2026-01-16 20:25:45 +01:00
This endpoint handles:
- Plan upgrade validation
- Stripe subscription update (preserves trial status if in trial)
- Local database update
- Cache invalidation
- Token refresh for immediate UI update
Trial handling:
- If user is in trial, they remain in trial after upgrade
- The upgraded tier price will be charged when trial ends
2026-01-15 20:45:49 +01:00
"""
try:
2026-01-16 20:25:45 +01:00
# Step 1: Validate upgrade eligibility
2026-01-16 15:19:34 +01:00
validation = await limit_service.validate_plan_upgrade(tenant_id, new_plan)
if not validation.get("can_upgrade", False):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=validation.get("reason", "Cannot upgrade to this plan")
)
from app.repositories.subscription_repository import SubscriptionRepository
database_manager = create_database_manager(settings.DATABASE_URL, "tenant-service")
async with database_manager.get_session() as session:
subscription_repo = SubscriptionRepository(Subscription, session)
active_subscription = await subscription_repo.get_active_subscription(tenant_id)
if not active_subscription:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="No active subscription found for this tenant"
)
2026-01-16 20:25:45 +01:00
old_plan = active_subscription.plan
is_trialing = active_subscription.status == 'trialing'
trial_ends_at = active_subscription.trial_ends_at
logger.info("Starting subscription upgrade",
extra={
"tenant_id": tenant_id,
"subscription_id": str(active_subscription.id),
"stripe_subscription_id": active_subscription.subscription_id,
"old_plan": old_plan,
"new_plan": new_plan,
"is_trialing": is_trialing,
"trial_ends_at": str(trial_ends_at) if trial_ends_at else None,
"user_id": current_user["user_id"]
})
# Step 2: Update Stripe subscription if Stripe subscription ID exists
stripe_updated = False
if active_subscription.subscription_id:
try:
# Use orchestration service to handle Stripe update with trial preservation
upgrade_result = await orchestration_service.orchestrate_plan_upgrade(
tenant_id=tenant_id,
new_plan=new_plan,
proration_behavior="none" if is_trialing else "create_prorations",
immediate_change=not is_trialing, # Don't change billing anchor if trialing
billing_cycle=billing_cycle
)
stripe_updated = True
logger.info("Stripe subscription updated successfully",
extra={
"tenant_id": tenant_id,
"stripe_subscription_id": active_subscription.subscription_id,
"upgrade_result": upgrade_result
})
except Exception as stripe_error:
logger.error("Failed to update Stripe subscription, falling back to local update only",
extra={"tenant_id": tenant_id, "error": str(stripe_error)})
# Continue with local update even if Stripe fails
# This ensures the user gets access to features immediately
# Step 3: Update local database
2026-01-16 15:19:34 +01:00
updated_subscription = await subscription_repo.update_subscription_plan(
str(active_subscription.id),
new_plan
)
2026-01-16 20:25:45 +01:00
# Preserve trial status if was trialing
if is_trialing and trial_ends_at:
# Ensure trial_ends_at is preserved after plan update
await subscription_repo.update_subscription_status(
str(active_subscription.id),
'trialing',
{'trial_ends_at': trial_ends_at}
)
2026-01-16 15:19:34 +01:00
await session.commit()
2026-01-16 20:25:45 +01:00
logger.info("Subscription plan upgraded successfully in database",
2026-01-16 15:19:34 +01:00
extra={
"tenant_id": tenant_id,
"subscription_id": str(active_subscription.id),
2026-01-16 20:25:45 +01:00
"old_plan": old_plan,
2026-01-16 15:19:34 +01:00
"new_plan": new_plan,
2026-01-16 20:25:45 +01:00
"stripe_updated": stripe_updated,
"preserved_trial": is_trialing,
2026-01-16 15:19:34 +01:00
"user_id": current_user["user_id"]
})
2026-01-16 20:25:45 +01:00
# Step 4: Invalidate subscription cache
redis_client = None
2026-01-16 15:19:34 +01:00
try:
from app.services.subscription_cache import get_subscription_cache_service
redis_client = await shared.redis_utils.initialize_redis(settings.REDIS_URL)
cache_service = get_subscription_cache_service(redis_client)
await cache_service.invalidate_subscription_cache(tenant_id)
logger.info("Subscription cache invalidated after upgrade",
extra={"tenant_id": tenant_id, "new_plan": new_plan})
except Exception as cache_error:
logger.error("Failed to invalidate subscription cache after upgrade",
extra={"tenant_id": tenant_id, "error": str(cache_error)})
2026-01-16 20:25:45 +01:00
# Step 5: Invalidate tokens for immediate UI refresh
2026-01-16 15:19:34 +01:00
try:
2026-01-16 20:25:45 +01:00
if redis_client:
await _invalidate_tenant_tokens(tenant_id, redis_client)
logger.info("Invalidated all tokens for tenant after subscription upgrade",
extra={"tenant_id": tenant_id})
2026-01-16 15:19:34 +01:00
except Exception as token_error:
logger.error("Failed to invalidate tenant tokens after upgrade",
extra={"tenant_id": tenant_id, "error": str(token_error)})
2026-01-16 20:25:45 +01:00
# Step 6: Publish subscription change event for other services
2026-01-16 15:19:34 +01:00
try:
from shared.messaging import UnifiedEventPublisher
event_publisher = UnifiedEventPublisher()
await event_publisher.publish_business_event(
event_type="subscription.changed",
tenant_id=tenant_id,
data={
"tenant_id": tenant_id,
2026-01-16 20:25:45 +01:00
"old_tier": old_plan,
2026-01-16 15:19:34 +01:00
"new_tier": new_plan,
2026-01-16 20:25:45 +01:00
"action": "upgrade",
"is_trialing": is_trialing,
"trial_ends_at": trial_ends_at.isoformat() if trial_ends_at else None,
"stripe_updated": stripe_updated
2026-01-16 15:19:34 +01:00
}
2026-01-15 20:45:49 +01:00
)
2026-01-16 15:19:34 +01:00
logger.info("Published subscription change event",
extra={"tenant_id": tenant_id, "event_type": "subscription.changed"})
except Exception as event_error:
logger.error("Failed to publish subscription change event",
extra={"tenant_id": tenant_id, "error": str(event_error)})
return {
"success": True,
2026-01-16 20:25:45 +01:00
"message": f"Plan successfully upgraded to {new_plan}" + (" (trial preserved)" if is_trialing else ""),
"old_plan": old_plan,
2026-01-16 15:19:34 +01:00
"new_plan": new_plan,
"new_monthly_price": updated_subscription.monthly_price,
2026-01-16 20:25:45 +01:00
"is_trialing": is_trialing,
"trial_ends_at": trial_ends_at.isoformat() if trial_ends_at else None,
"stripe_updated": stripe_updated,
2026-01-16 15:19:34 +01:00
"validation": validation,
"requires_token_refresh": True
}
except HTTPException:
raise
except Exception as e:
logger.error("Failed to upgrade subscription plan",
extra={"tenant_id": tenant_id, "new_plan": new_plan, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to upgrade subscription plan"
2026-01-14 13:15:48 +01:00
)
2026-01-16 15:19:34 +01:00
# ============================================================================
# QUOTA & LIMIT CHECK ENDPOINTS
# ============================================================================
@router.get("/tenants/{tenant_id}/subscription/limits/locations",
response_model=Dict[str, Any],
summary="Check location limits")
async def check_location_limits(
tenant_id: str = Path(..., description="Tenant ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
limit_service: SubscriptionLimitService = Depends(get_subscription_limit_service)
) -> Dict[str, Any]:
"""Check if tenant can add another location"""
try:
result = await limit_service.can_add_location(tenant_id)
return result
except Exception as e:
logger.error("Failed to check location limits",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to check location limits"
)
@router.get("/tenants/{tenant_id}/subscription/limits/products",
response_model=Dict[str, Any],
summary="Check product limits")
async def check_product_limits(
tenant_id: str = Path(..., description="Tenant ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
limit_service: SubscriptionLimitService = Depends(get_subscription_limit_service)
) -> Dict[str, Any]:
"""Check if tenant can add another product"""
try:
result = await limit_service.can_add_product(tenant_id)
return result
except Exception as e:
logger.error("Failed to check product limits",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to check product limits"
)
@router.get("/tenants/{tenant_id}/subscription/limits/users",
response_model=Dict[str, Any],
summary="Check user limits")
async def check_user_limits(
tenant_id: str = Path(..., description="Tenant ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
limit_service: SubscriptionLimitService = Depends(get_subscription_limit_service)
) -> Dict[str, Any]:
"""Check if tenant can add another user/member"""
try:
result = await limit_service.can_add_user(tenant_id)
return result
except Exception as e:
logger.error("Failed to check user limits",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to check user limits"
)
@router.get("/tenants/{tenant_id}/subscription/limits/recipes",
response_model=Dict[str, Any],
summary="Check recipe limits")
async def check_recipe_limits(
tenant_id: str = Path(..., description="Tenant ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
limit_service: SubscriptionLimitService = Depends(get_subscription_limit_service)
) -> Dict[str, Any]:
"""Check if tenant can add another recipe"""
try:
result = await limit_service.can_add_recipe(tenant_id)
return result
except Exception as e:
logger.error("Failed to check recipe limits",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to check recipe limits"
)
@router.get("/tenants/{tenant_id}/subscription/limits/suppliers",
response_model=Dict[str, Any],
summary="Check supplier limits")
async def check_supplier_limits(
tenant_id: str = Path(..., description="Tenant ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
limit_service: SubscriptionLimitService = Depends(get_subscription_limit_service)
) -> Dict[str, Any]:
"""Check if tenant can add another supplier"""
try:
result = await limit_service.can_add_supplier(tenant_id)
return result
except Exception as e:
logger.error("Failed to check supplier limits",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to check supplier limits"
)
# ============================================================================
# PAYMENT MANAGEMENT ENDPOINTS
# ============================================================================
@router.get("/tenants/{tenant_id}/subscription/payment-method",
response_model=Dict[str, Any],
summary="Get payment method")
async def get_payment_method(
tenant_id: str = Path(..., description="Tenant ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service)
) -> Dict[str, Any]:
"""Get current payment method for a subscription"""
try:
result = await orchestration_service.get_payment_method(tenant_id)
2026-01-14 13:15:48 +01:00
2026-01-16 15:19:34 +01:00
# Ensure we always return a proper response structure
if result is None:
2026-01-15 20:45:49 +01:00
return {
2026-01-16 15:19:34 +01:00
"brand": None,
"last4": None,
"exp_month": None,
"exp_year": None
2026-01-15 20:45:49 +01:00
}
2026-01-16 15:19:34 +01:00
return result
2026-01-15 20:45:49 +01:00
except Exception as e:
2026-01-16 15:19:34 +01:00
logger.error("Failed to get payment method",
extra={"tenant_id": tenant_id, "error": str(e)})
2026-01-15 20:45:49 +01:00
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
2026-01-16 15:19:34 +01:00
detail="Failed to get payment method"
)
2026-01-15 20:45:49 +01:00
2026-01-16 15:19:34 +01:00
@router.post("/tenants/{tenant_id}/subscription/payment-method",
response_model=Dict[str, Any],
summary="Update payment method")
async def update_payment_method(
tenant_id: str = Path(..., description="Tenant ID"),
payment_method_id: str = Query(..., description="New payment method ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
2026-01-15 20:45:49 +01:00
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service)
) -> Dict[str, Any]:
2026-01-16 15:19:34 +01:00
"""Update the default payment method for a subscription"""
try:
result = await orchestration_service.update_payment_method(tenant_id, payment_method_id)
return result
2026-01-15 20:45:49 +01:00
2026-01-16 15:19:34 +01:00
except Exception as e:
logger.error("Failed to update payment method",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to update payment method"
)
2026-01-15 20:45:49 +01:00
2026-01-16 15:19:34 +01:00
@router.get("/tenants/{tenant_id}/subscription/invoices",
response_model=Dict[str, Any],
summary="Get invoices")
async def get_invoices(
tenant_id: str = Path(..., description="Tenant ID"),
current_user: Dict[str, Any] = Depends(get_current_user_dep),
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service)
) -> Dict[str, Any]:
"""Get invoice history for a tenant"""
try:
result = await orchestration_service.get_invoices(tenant_id)
return result
2026-01-15 20:45:49 +01:00
2026-01-16 15:19:34 +01:00
except Exception as e:
logger.error("Failed to get invoices",
extra={"tenant_id": tenant_id, "error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to get invoices"
)
# ============================================================================
# SETUP INTENT VERIFICATION
# ============================================================================
@router.get("/setup-intents/{setup_intent_id}/verify",
response_model=Dict[str, Any],
summary="Verify SetupIntent status")
async def verify_setup_intent(
setup_intent_id: str = Path(..., description="SetupIntent ID"),
request: Request = None,
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service)
) -> Dict[str, Any]:
"""
Verify SetupIntent status for registration flow
2026-01-15 20:45:49 +01:00
"""
try:
logger.info("Verifying SetupIntent for registration",
2026-01-16 15:19:34 +01:00
extra={"setup_intent_id": setup_intent_id})
2026-01-15 20:45:49 +01:00
result = await orchestration_service.verify_setup_intent_for_registration(setup_intent_id)
logger.info("SetupIntent verification completed",
2026-01-16 15:19:34 +01:00
extra={
"setup_intent_id": setup_intent_id,
"status": result.get('status')
})
2026-01-15 20:45:49 +01:00
return {
"success": True,
"setup_intent_id": setup_intent_id,
"status": result.get('status'),
"payment_method_id": result.get('payment_method_id'),
"customer_id": result.get('customer_id'),
"requires_action": result.get('requires_action', False),
"action_type": result.get('action_type'),
"client_secret": result.get('client_secret'),
"message": result.get('message', 'SetupIntent verification completed successfully')
}
2026-01-14 13:15:48 +01:00
except Exception as e:
2026-01-15 20:45:49 +01:00
logger.error("SetupIntent verification failed",
2026-01-16 15:19:34 +01:00
extra={"error": str(e), "setup_intent_id": setup_intent_id},
exc_info=True)
2026-01-14 13:15:48 +01:00
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
2026-01-15 20:45:49 +01:00
detail=f"SetupIntent verification failed: {str(e)}"
) from e
2026-01-16 15:19:34 +01:00
# ============================================================================
# PAYMENT CUSTOMER CREATION
# ============================================================================
@router.post("/payment-customers/create",
response_model=Dict[str, Any],
summary="Create payment customer")
async def create_payment_customer(
user_data: Dict[str, Any],
payment_method_id: Optional[str] = Query(None, description="Optional payment method ID"),
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service)
) -> Dict[str, Any]:
"""
Create a payment customer in the payment provider
This endpoint is designed for service-to-service communication from auth service
during user registration.
"""
try:
from app.services.payment_service import PaymentService
payment_service = PaymentService()
logger.info("Creating payment customer via service-to-service call",
extra={"email": user_data.get('email'), "user_id": user_data.get('user_id')})
customer = await payment_service.create_customer(user_data)
logger.info("Payment customer created successfully",
extra={"customer_id": customer.id, "email": customer.email})
payment_method_details = None
if payment_method_id:
try:
payment_method = await payment_service.update_payment_method(
customer.id,
payment_method_id
)
payment_method_details = {
"id": payment_method.id,
"type": payment_method.type,
"brand": payment_method.brand,
"last4": payment_method.last4,
"exp_month": payment_method.exp_month,
"exp_year": payment_method.exp_year
}
logger.info("Payment method attached to customer",
extra={"customer_id": customer.id, "payment_method_id": payment_method.id})
except Exception as e:
logger.warning("Failed to attach payment method to customer",
extra={"customer_id": customer.id, "error": str(e), "payment_method_id": payment_method_id})
return {
"success": True,
"payment_customer_id": customer.id,
"payment_method": payment_method_details,
"customer": {
"id": customer.id,
"email": customer.email,
"name": customer.name,
"created_at": customer.created_at.isoformat()
}
}
except Exception as e:
logger.error("Failed to create payment customer via service-to-service call",
extra={"error": str(e), "email": user_data.get('email'), "user_id": user_data.get('user_id')})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to create payment customer: {str(e)}"
)
# ============================================================================
# HELPER FUNCTIONS
# ============================================================================
async def _invalidate_tenant_tokens(tenant_id: str, redis_client):
"""
Invalidate all tokens for users in this tenant.
Forces re-authentication to get fresh subscription data.
"""
try:
changed_timestamp = datetime.now(timezone.utc).timestamp()
await redis_client.set(
f"tenant:{tenant_id}:subscription_changed_at",
str(changed_timestamp),
ex=86400 # 24 hour TTL
)
logger.info("Set subscription change timestamp for token invalidation",
extra={"tenant_id": tenant_id, "timestamp": changed_timestamp})
except Exception as e:
logger.error("Failed to invalidate tenant tokens",
extra={"tenant_id": tenant_id, "error": str(e)})
raise