Add subcription feature 4
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
"""
|
||||
Tenant Service API Endpoints for Subscription and Registration
|
||||
Updated with new atomic registration flow support
|
||||
|
||||
NEW ARCHITECTURE (SetupIntent-first):
|
||||
1. /registration-payment-setup - Creates customer + SetupIntent only (NO subscription)
|
||||
2. Frontend confirms SetupIntent (handles 3DS if needed)
|
||||
3. /verify-and-complete-registration - Creates subscription AFTER verification
|
||||
|
||||
This eliminates duplicate subscriptions by only creating the subscription
|
||||
after payment verification is complete.
|
||||
"""
|
||||
|
||||
import logging
|
||||
@@ -8,6 +15,7 @@ from typing import Dict, Any
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Request, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.services.subscription_orchestration_service import SubscriptionOrchestrationService
|
||||
from app.services.coupon_service import CouponService
|
||||
from app.core.database import get_db
|
||||
from app.services.registration_state_service import (
|
||||
registration_state_service,
|
||||
@@ -18,17 +26,13 @@ from shared.exceptions.payment_exceptions import (
|
||||
PaymentServiceError,
|
||||
SetupIntentError,
|
||||
SubscriptionCreationFailed,
|
||||
ThreeDSAuthenticationRequired
|
||||
)
|
||||
from shared.exceptions.registration_exceptions import (
|
||||
RegistrationStateError,
|
||||
InvalidStateTransitionError
|
||||
)
|
||||
|
||||
# Configure logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Create router
|
||||
router = APIRouter(prefix="/api/v1/tenants", tags=["tenant"])
|
||||
|
||||
|
||||
@@ -46,7 +50,7 @@ async def get_registration_state_service() -> RegistrationStateService:
|
||||
|
||||
@router.post("/registration-payment-setup",
|
||||
response_model=Dict[str, Any],
|
||||
summary="Initiate registration payment setup")
|
||||
summary="Start registration payment setup")
|
||||
async def create_registration_payment_setup(
|
||||
user_data: Dict[str, Any],
|
||||
request: Request,
|
||||
@@ -54,69 +58,46 @@ async def create_registration_payment_setup(
|
||||
state_service: RegistrationStateService = Depends(get_registration_state_service)
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Initiate registration payment setup with SetupIntent-first approach
|
||||
|
||||
This is the FIRST step in secure registration flow:
|
||||
1. Creates payment customer
|
||||
2. Attaches payment method
|
||||
3. Creates SetupIntent for verification
|
||||
4. Returns SetupIntent to frontend for 3DS handling
|
||||
|
||||
Start registration payment setup (SetupIntent-first architecture).
|
||||
|
||||
NEW ARCHITECTURE: Only creates customer + SetupIntent here.
|
||||
NO subscription is created - subscription is created in verify-and-complete-registration.
|
||||
|
||||
Flow:
|
||||
1. Create Stripe customer
|
||||
2. Create SetupIntent for payment verification
|
||||
3. Return SetupIntent to frontend for 3DS handling
|
||||
4. Frontend confirms SetupIntent
|
||||
5. (Next endpoint) Creates subscription after verification
|
||||
|
||||
Args:
|
||||
user_data: User registration data with payment info
|
||||
|
||||
|
||||
Returns:
|
||||
Payment setup result (may require 3DS)
|
||||
|
||||
Raises:
|
||||
HTTPException: 400 for validation errors, 500 for server errors
|
||||
SetupIntent data for frontend confirmation
|
||||
"""
|
||||
state_id = None
|
||||
try:
|
||||
print(f"DEBUG_PRINT: Registration payment setup request received for {user_data.get('email')}")
|
||||
logger.critical(
|
||||
"Registration payment setup request received (CRITICAL)",
|
||||
extra={
|
||||
"email": user_data.get('email'),
|
||||
"plan_id": user_data.get('plan_id')
|
||||
}
|
||||
)
|
||||
|
||||
logger.info("Registration payment setup started",
|
||||
extra={"email": user_data.get('email'), "plan_id": user_data.get('plan_id')})
|
||||
|
||||
# Validate required fields
|
||||
if not user_data.get('email'):
|
||||
logger.error("Registration payment setup failed: Email missing")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Email is required"
|
||||
)
|
||||
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Email is required")
|
||||
|
||||
if not user_data.get('payment_method_id'):
|
||||
logger.error("Registration payment setup failed: Payment method ID missing", extra={"email": user_data.get('email')})
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Payment method ID is required"
|
||||
)
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Payment method ID is required")
|
||||
|
||||
if not user_data.get('plan_id'):
|
||||
logger.error("Registration payment setup failed: Plan ID missing", extra={"email": user_data.get('email')})
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Plan ID is required"
|
||||
)
|
||||
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Plan ID is required")
|
||||
|
||||
# Create registration state
|
||||
print(f"DEBUG_PRINT: Creating registration state for {user_data['email']}")
|
||||
logger.critical("Creating registration state", extra={"email": user_data['email']})
|
||||
|
||||
|
||||
state_id = await state_service.create_registration_state(
|
||||
email=user_data['email'],
|
||||
user_data=user_data
|
||||
)
|
||||
logger.critical("Registration state created", extra={"state_id": state_id, "email": user_data['email']})
|
||||
|
||||
# Initiate payment setup
|
||||
print(f"DEBUG_PRINT: Calling orchestration service for {user_data['email']}")
|
||||
logger.critical("Calling orchestration service for payment setup", extra={"state_id": state_id, "email": user_data['email']})
|
||||
# Create customer + SetupIntent (NO subscription yet!)
|
||||
result = await orchestration_service.create_registration_payment_setup(
|
||||
user_data=user_data,
|
||||
plan_id=user_data.get('plan_id', 'professional'),
|
||||
@@ -124,108 +105,55 @@ async def create_registration_payment_setup(
|
||||
billing_interval=user_data.get('billing_cycle', 'monthly'),
|
||||
coupon_code=user_data.get('coupon_code')
|
||||
)
|
||||
logger.critical("Payment orchestration completed", extra={"state_id": state_id, "email": user_data['email'], "requires_action": result.get('requires_action')})
|
||||
|
||||
# Update state with payment setup results
|
||||
# Note: setup_intent_id may not be present if no 3DS was required
|
||||
|
||||
# Update state with setup results
|
||||
await state_service.update_state_context(state_id, {
|
||||
'setup_intent_id': result.get('setup_intent_id'),
|
||||
'subscription_id': result.get('subscription_id'),
|
||||
'customer_id': result.get('customer_id'),
|
||||
'payment_customer_id': result.get('payment_customer_id'),
|
||||
'payment_method_id': result.get('payment_method_id')
|
||||
'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')
|
||||
})
|
||||
|
||||
# Transition to payment verification pending state
|
||||
await state_service.transition_state(
|
||||
state_id,
|
||||
RegistrationState.PAYMENT_VERIFICATION_PENDING
|
||||
)
|
||||
|
||||
logger.critical(
|
||||
"Registration payment setup flow successful",
|
||||
extra={
|
||||
"email": user_data.get('email'),
|
||||
"state_id": state_id
|
||||
}
|
||||
)
|
||||
|
||||
await state_service.transition_state(state_id, RegistrationState.PAYMENT_VERIFICATION_PENDING)
|
||||
|
||||
logger.info("Registration payment setup completed",
|
||||
extra={
|
||||
"email": user_data.get('email'),
|
||||
"setup_intent_id": result.get('setup_intent_id'),
|
||||
"requires_action": result.get('requires_action')
|
||||
})
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"requires_action": result.get('requires_action', False),
|
||||
"action_type": result.get('action_type'),
|
||||
"requires_action": result.get('requires_action', True),
|
||||
"action_type": result.get('action_type', 'use_stripe_sdk'),
|
||||
"client_secret": result.get('client_secret'),
|
||||
"setup_intent_id": result.get('setup_intent_id'),
|
||||
"customer_id": result.get('customer_id'),
|
||||
"payment_customer_id": result.get('payment_customer_id'),
|
||||
"payment_customer_id": result.get('customer_id'),
|
||||
"plan_id": result.get('plan_id'),
|
||||
"payment_method_id": result.get('payment_method_id'),
|
||||
"subscription_id": result.get('subscription_id'),
|
||||
"billing_cycle": result.get('billing_cycle'),
|
||||
"trial_period_days": result.get('trial_period_days', 0),
|
||||
"billing_cycle": result.get('billing_interval'),
|
||||
"email": result.get('email'),
|
||||
"state_id": state_id,
|
||||
"message": result.get('message') or "Payment setup completed successfully."
|
||||
}
|
||||
|
||||
except ThreeDSAuthenticationRequired as e:
|
||||
# 3DS authentication required - return SetupIntent data for frontend
|
||||
logger.info(f"3DS authentication required for registration: email={user_data.get('email')}, setup_intent_id={e.setup_intent_id}", extra={"email": user_data.get('email'), "setup_intent_id": e.setup_intent_id})
|
||||
|
||||
# Update state with payment setup results
|
||||
await state_service.update_state_context(state_id, {
|
||||
'setup_intent_id': e.setup_intent_id,
|
||||
'customer_id': e.extra_data.get('customer_id'),
|
||||
'payment_customer_id': e.extra_data.get('customer_id'),
|
||||
'payment_method_id': e.extra_data.get('payment_method_id')
|
||||
})
|
||||
|
||||
# Transition to payment verification pending state
|
||||
await state_service.transition_state(
|
||||
state_id,
|
||||
RegistrationState.PAYMENT_VERIFICATION_PENDING
|
||||
)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"requires_action": True,
|
||||
"action_type": e.action_type,
|
||||
"client_secret": e.client_secret,
|
||||
"setup_intent_id": e.setup_intent_id,
|
||||
"subscription_id": e.extra_data.get('subscription_id'),
|
||||
"customer_id": e.extra_data.get('customer_id'),
|
||||
"payment_customer_id": e.extra_data.get('customer_id'),
|
||||
"plan_id": e.extra_data.get('plan_id'),
|
||||
"payment_method_id": e.extra_data.get('payment_method_id'),
|
||||
"billing_cycle": e.extra_data.get('billing_interval'),
|
||||
"email": e.extra_data.get('email'),
|
||||
"state_id": state_id,
|
||||
"message": e.extra_data.get('message') or "Payment verification required. Frontend must confirm SetupIntent to handle 3DS."
|
||||
"message": result.get('message', 'Payment verification required')
|
||||
}
|
||||
|
||||
except PaymentServiceError as e:
|
||||
logger.error(f"Payment service error in registration setup: {str(e)}, email: {user_data.get('email')}",
|
||||
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
|
||||
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
|
||||
except RegistrationStateError as e:
|
||||
logger.error(f"Registration state error in payment setup: {str(e)}, email: {user_data.get('email')}",
|
||||
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
|
||||
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
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error in registration payment setup: {str(e)}, email: {user_data.get('email')}",
|
||||
extra={"email": user_data.get('email')},
|
||||
exc_info=True)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Registration payment setup failed: {str(e)}"
|
||||
) from e
|
||||
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
|
||||
|
||||
|
||||
@router.post("/verify-and-complete-registration",
|
||||
@@ -234,258 +162,153 @@ async def create_registration_payment_setup(
|
||||
async def verify_and_complete_registration(
|
||||
verification_data: Dict[str, Any],
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
orchestration_service: SubscriptionOrchestrationService = Depends(get_subscription_orchestration_service),
|
||||
state_service: RegistrationStateService = Depends(get_registration_state_service)
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Complete registration after frontend confirms SetupIntent (3DS handled)
|
||||
|
||||
This is the SECOND step in registration architecture:
|
||||
1. Verifies SetupIntent status
|
||||
2. Creates subscription with verified payment method
|
||||
3. Updates registration state
|
||||
|
||||
Complete registration after frontend confirms SetupIntent.
|
||||
|
||||
NEW ARCHITECTURE: Creates subscription HERE (not in payment-setup).
|
||||
This is the ONLY place subscriptions are created during registration.
|
||||
|
||||
Flow:
|
||||
1. Verify SetupIntent status is 'succeeded'
|
||||
2. Create subscription with verified payment method
|
||||
3. Update registration state
|
||||
|
||||
Args:
|
||||
verification_data: SetupIntent verification data
|
||||
|
||||
verification_data: SetupIntent verification data with user_data
|
||||
|
||||
Returns:
|
||||
Complete registration result with subscription
|
||||
|
||||
Raises:
|
||||
HTTPException: 400 for validation errors, 500 for server errors
|
||||
Subscription creation result
|
||||
"""
|
||||
setup_intent_id = None
|
||||
user_data = {}
|
||||
state_id = None
|
||||
|
||||
try:
|
||||
# Validate required fields
|
||||
if not verification_data.get('setup_intent_id'):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="SetupIntent ID is required"
|
||||
)
|
||||
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="SetupIntent ID is required")
|
||||
|
||||
if not verification_data.get('user_data'):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="User data is required"
|
||||
)
|
||||
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="User data is required")
|
||||
|
||||
setup_intent_id = verification_data['setup_intent_id']
|
||||
user_data = verification_data['user_data']
|
||||
state_id = verification_data.get('state_id')
|
||||
|
||||
logger.info(
|
||||
"Completing registration after SetupIntent verification",
|
||||
extra={
|
||||
"email": user_data.get('email'),
|
||||
"setup_intent_id": setup_intent_id
|
||||
}
|
||||
)
|
||||
|
||||
# Get registration state if provided
|
||||
if state_id:
|
||||
try:
|
||||
registration_state = await state_service.get_registration_state(state_id)
|
||||
logger.info(
|
||||
"Retrieved registration state",
|
||||
extra={
|
||||
"state_id": state_id,
|
||||
"current_state": registration_state['current_state']
|
||||
}
|
||||
)
|
||||
except RegistrationStateError:
|
||||
logger.warning("Registration state not found, proceeding without state tracking",
|
||||
extra={"state_id": state_id})
|
||||
state_id = None
|
||||
|
||||
# First verify the setup intent to get the actual customer_id and payment_method_id
|
||||
verification_result = await orchestration_service.verify_setup_intent_for_registration(
|
||||
setup_intent_id
|
||||
)
|
||||
|
||||
if not verification_result.get('verified', False):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="SetupIntent verification failed"
|
||||
)
|
||||
logger.info("Completing registration after verification",
|
||||
extra={"email": user_data.get('email'), "setup_intent_id": setup_intent_id})
|
||||
|
||||
# Extract actual values from verification result
|
||||
actual_customer_id = verification_result.get('customer_id', user_data.get('customer_id', ''))
|
||||
actual_payment_method_id = verification_result.get('payment_method_id', user_data.get('payment_method_id', ''))
|
||||
|
||||
# Get trial period from coupon if available
|
||||
trial_period_days = user_data.get('trial_period_days', 0)
|
||||
# Calculate trial period from coupon if provided in the completion call
|
||||
trial_period_days = 0
|
||||
coupon_code = user_data.get('coupon_code')
|
||||
|
||||
# If we have a coupon code but no trial period, redeem the coupon to get trial days
|
||||
if coupon_code and trial_period_days == 0:
|
||||
try:
|
||||
from app.services.coupon_service import CouponService
|
||||
coupon_service = CouponService(db_session)
|
||||
success, discount_applied, error = await coupon_service.redeem_coupon(
|
||||
coupon_code,
|
||||
None, # No tenant_id yet
|
||||
base_trial_days=0
|
||||
)
|
||||
if success and discount_applied:
|
||||
trial_period_days = discount_applied.get("total_trial_days", 0)
|
||||
logger.info("Retrieved trial period from coupon for verification",
|
||||
extra={"coupon_code": coupon_code, "trial_period_days": trial_period_days})
|
||||
except Exception as e:
|
||||
logger.error("Failed to redeem coupon during verification, using default trial period",
|
||||
extra={"coupon_code": coupon_code, "error": str(e)},
|
||||
exc_info=True)
|
||||
# Fall back to 0 if coupon redemption fails
|
||||
trial_period_days = 0
|
||||
if coupon_code:
|
||||
logger.info("Validating coupon in completion call",
|
||||
extra={"coupon_code": coupon_code, "email": user_data.get('email')})
|
||||
|
||||
# Create coupon service to validate coupon
|
||||
coupon_service = CouponService(db)
|
||||
success, discount_applied, error = await coupon_service.redeem_coupon(
|
||||
coupon_code,
|
||||
None, # No tenant_id yet
|
||||
base_trial_days=0
|
||||
)
|
||||
|
||||
if success and discount_applied:
|
||||
trial_period_days = discount_applied.get("total_trial_days", 0)
|
||||
logger.info("Coupon validated in completion call",
|
||||
extra={"coupon_code": coupon_code, "trial_period_days": trial_period_days})
|
||||
else:
|
||||
logger.warning("Failed to validate coupon in completion call",
|
||||
extra={"coupon_code": coupon_code, "error": error})
|
||||
elif 'trial_period_days' in user_data:
|
||||
# Fallback: use trial_period_days if explicitly provided
|
||||
trial_period_days = int(user_data.get('trial_period_days', 0))
|
||||
logger.info("Using explicitly provided trial period",
|
||||
extra={"trial_period_days": trial_period_days})
|
||||
|
||||
# Create subscription AFTER verification (the core fix!)
|
||||
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')
|
||||
)
|
||||
|
||||
# Check if a subscription already exists for this customer
|
||||
existing_subscriptions = await orchestration_service.get_subscriptions_by_customer_id(actual_customer_id)
|
||||
|
||||
if existing_subscriptions:
|
||||
# If we already have a trial subscription, update it instead of creating a new one
|
||||
existing_subscription = existing_subscriptions[0] # Get the first subscription
|
||||
|
||||
logger.info("Found existing subscription, updating with verified payment method",
|
||||
extra={
|
||||
"customer_id": actual_customer_id,
|
||||
"subscription_id": existing_subscription.provider_subscription_id,
|
||||
"existing_status": existing_subscription.status
|
||||
})
|
||||
|
||||
# Update the existing subscription with the verified payment method
|
||||
result = await orchestration_service.update_subscription_with_verified_payment(
|
||||
existing_subscription.provider_subscription_id,
|
||||
actual_customer_id,
|
||||
actual_payment_method_id,
|
||||
trial_period_days
|
||||
)
|
||||
else:
|
||||
# No existing subscription, create a new one
|
||||
result = await orchestration_service.complete_subscription_after_setup_intent(
|
||||
setup_intent_id,
|
||||
actual_customer_id,
|
||||
user_data.get('plan_id', 'starter'),
|
||||
actual_payment_method_id, # Use the verified payment method ID
|
||||
trial_period_days, # Use the trial period we obtained (90 for PILOT2025)
|
||||
user_data.get('user_id') if user_data.get('user_id') else None, # Convert empty string to None
|
||||
user_data.get('billing_interval', 'monthly')
|
||||
)
|
||||
|
||||
# Update registration state if tracking
|
||||
# Update registration state
|
||||
if state_id:
|
||||
try:
|
||||
await state_service.update_state_context(state_id, {
|
||||
'subscription_id': result['subscription_id'],
|
||||
'status': result['status']
|
||||
})
|
||||
|
||||
await state_service.transition_state(
|
||||
state_id,
|
||||
RegistrationState.SUBSCRIPTION_CREATED
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"Registration state updated after subscription creation",
|
||||
extra={
|
||||
"state_id": state_id,
|
||||
"subscription_id": result['subscription_id']
|
||||
}
|
||||
)
|
||||
await state_service.transition_state(state_id, RegistrationState.SUBSCRIPTION_CREATED)
|
||||
except Exception as e:
|
||||
logger.error("Failed to update registration state after subscription creation",
|
||||
extra={
|
||||
"error": str(e),
|
||||
"state_id": state_id
|
||||
},
|
||||
exc_info=True)
|
||||
|
||||
logger.info("Registration completed successfully after 3DS verification",
|
||||
logger.warning(f"Failed to update registration state: {e}", extra={"state_id": state_id})
|
||||
|
||||
logger.info("Registration subscription created successfully",
|
||||
extra={
|
||||
"email": user_data.get('email'),
|
||||
"subscription_id": result['subscription_id']
|
||||
"subscription_id": result['subscription_id'],
|
||||
"status": result['status']
|
||||
})
|
||||
|
||||
|
||||
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'],
|
||||
"plan_id": result.get('plan_id', result.get('plan')),
|
||||
"plan_id": result.get('plan_id'),
|
||||
"payment_method_id": result.get('payment_method_id'),
|
||||
"trial_period_days": result.get('trial_period_days'),
|
||||
"trial_period_days": result.get('trial_period_days', 0),
|
||||
"current_period_end": result.get('current_period_end'),
|
||||
"state_id": state_id,
|
||||
"message": "Registration completed successfully after 3DS verification"
|
||||
"message": "Subscription created successfully"
|
||||
}
|
||||
|
||||
|
||||
except SetupIntentError as e:
|
||||
logger.error("SetupIntent verification failed",
|
||||
extra={
|
||||
"error": str(e),
|
||||
"setup_intent_id": setup_intent_id,
|
||||
"email": user_data.get('email')
|
||||
},
|
||||
logger.error(f"SetupIntent verification failed: {e}",
|
||||
extra={"setup_intent_id": setup_intent_id, "email": user_data.get('email')},
|
||||
exc_info=True)
|
||||
|
||||
# Mark registration as failed if state tracking
|
||||
if state_id:
|
||||
try:
|
||||
await state_service.mark_registration_failed(
|
||||
state_id,
|
||||
f"SetupIntent verification failed: {str(e)}"
|
||||
)
|
||||
await state_service.mark_registration_failed(state_id, f"Verification failed: {e}")
|
||||
except Exception:
|
||||
pass # Don't fail main operation for state tracking failure
|
||||
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"SetupIntent verification failed: {str(e)}"
|
||||
) from e
|
||||
pass
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"Payment verification failed: {e}") from e
|
||||
|
||||
except SubscriptionCreationFailed as e:
|
||||
logger.error("Subscription creation failed after verification",
|
||||
extra={
|
||||
"error": str(e),
|
||||
"setup_intent_id": setup_intent_id,
|
||||
"email": user_data.get('email')
|
||||
},
|
||||
logger.error(f"Subscription creation failed: {e}",
|
||||
extra={"setup_intent_id": setup_intent_id, "email": user_data.get('email')},
|
||||
exc_info=True)
|
||||
|
||||
# Mark registration as failed if state tracking
|
||||
if state_id:
|
||||
try:
|
||||
await state_service.mark_registration_failed(
|
||||
state_id,
|
||||
f"Subscription creation failed: {str(e)}"
|
||||
)
|
||||
await state_service.mark_registration_failed(state_id, f"Subscription failed: {e}")
|
||||
except Exception:
|
||||
pass # Don't fail main operation for state tracking failure
|
||||
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Subscription creation failed: {str(e)}"
|
||||
) from e
|
||||
pass
|
||||
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Subscription creation failed: {e}") from e
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Unexpected error in registration completion",
|
||||
extra={
|
||||
"error": str(e),
|
||||
"setup_intent_id": setup_intent_id,
|
||||
"email": user_data.get('email')
|
||||
},
|
||||
logger.error(f"Unexpected error: {e}",
|
||||
extra={"setup_intent_id": setup_intent_id, "email": user_data.get('email')},
|
||||
exc_info=True)
|
||||
|
||||
# Mark registration as failed if state tracking
|
||||
if state_id:
|
||||
try:
|
||||
await state_service.mark_registration_failed(
|
||||
state_id,
|
||||
f"Registration completion failed: {str(e)}"
|
||||
)
|
||||
await state_service.mark_registration_failed(state_id, f"Registration failed: {e}")
|
||||
except Exception:
|
||||
pass # Don't fail main operation for state tracking failure
|
||||
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Registration completion failed: {str(e)}"
|
||||
) from e
|
||||
pass
|
||||
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Registration failed: {e}") from e
|
||||
|
||||
|
||||
@router.get("/registration-state/{state_id}",
|
||||
|
||||
Reference in New Issue
Block a user