Add subcription feature 2
This commit is contained in:
@@ -861,6 +861,94 @@ class EnhancedAuthService:
|
||||
error=str(e))
|
||||
return None
|
||||
|
||||
async def create_registration_payment_setup_via_tenant_service(
|
||||
self,
|
||||
user_data: UserRegistration
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Create registration payment setup via tenant service orchestration
|
||||
|
||||
This method uses the tenant service's orchestration service to create
|
||||
payment customer and SetupIntent in a coordinated workflow for the
|
||||
secure architecture where users are only created after payment verification.
|
||||
|
||||
Args:
|
||||
user_data: User registration data (email, full_name, etc.)
|
||||
|
||||
Returns:
|
||||
Dictionary with payment setup results including SetupIntent if required
|
||||
"""
|
||||
try:
|
||||
from shared.clients.tenant_client import TenantServiceClient
|
||||
from shared.config.base import BaseServiceSettings
|
||||
|
||||
tenant_client = TenantServiceClient(BaseServiceSettings())
|
||||
|
||||
# Prepare user data for tenant service orchestration
|
||||
user_data_for_tenant = {
|
||||
"email": user_data.email,
|
||||
"full_name": user_data.full_name,
|
||||
"payment_method_id": user_data.payment_method_id,
|
||||
"plan_id": user_data.subscription_plan or "professional",
|
||||
"billing_cycle": user_data.billing_cycle or "monthly",
|
||||
"coupon_code": user_data.coupon_code
|
||||
}
|
||||
|
||||
# Call tenant service orchestration endpoint
|
||||
result = await tenant_client.create_registration_payment_setup(user_data_for_tenant)
|
||||
|
||||
logger.info("Registration payment setup completed via tenant service orchestration",
|
||||
email=user_data.email,
|
||||
requires_action=result.get('requires_action'),
|
||||
setup_intent_id=result.get('setup_intent_id'))
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Registration payment setup via tenant service failed",
|
||||
email=user_data.email,
|
||||
error=str(e),
|
||||
exc_info=True)
|
||||
raise
|
||||
|
||||
async def verify_setup_intent_via_tenant_service(
|
||||
self,
|
||||
setup_intent_id: str
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Verify SetupIntent via tenant service orchestration
|
||||
|
||||
This method uses the tenant service's orchestration service to verify
|
||||
SetupIntent status before proceeding with user creation.
|
||||
|
||||
Args:
|
||||
setup_intent_id: SetupIntent ID to verify
|
||||
|
||||
Returns:
|
||||
Dictionary with SetupIntent verification result
|
||||
"""
|
||||
try:
|
||||
from shared.clients.tenant_client import TenantServiceClient
|
||||
from shared.config.base import BaseServiceSettings
|
||||
|
||||
tenant_client = TenantServiceClient(BaseServiceSettings())
|
||||
|
||||
# Call tenant service orchestration endpoint
|
||||
result = await tenant_client.verify_setup_intent_for_registration(setup_intent_id)
|
||||
|
||||
logger.info("SetupIntent verified via tenant service orchestration",
|
||||
setup_intent_id=setup_intent_id,
|
||||
status=result.get('status'))
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error("SetupIntent verification via tenant service failed",
|
||||
setup_intent_id=setup_intent_id,
|
||||
error=str(e),
|
||||
exc_info=True)
|
||||
raise
|
||||
|
||||
async def get_user_data_for_tenant_service(self, user_id: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Get user data formatted for tenant service calls
|
||||
@@ -894,6 +982,101 @@ class EnhancedAuthService:
|
||||
error=str(e))
|
||||
raise
|
||||
|
||||
async def create_payment_customer_for_registration(
|
||||
self,
|
||||
user_data: UserRegistration
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Create payment customer for registration (BEFORE user creation)
|
||||
|
||||
This method creates a payment customer in the tenant service
|
||||
without requiring a user to exist first. This supports the
|
||||
secure architecture where users are only created after payment verification.
|
||||
|
||||
Args:
|
||||
user_data: User registration data
|
||||
|
||||
Returns:
|
||||
Dictionary with payment customer creation result
|
||||
|
||||
Raises:
|
||||
Exception: If payment customer creation fails
|
||||
"""
|
||||
try:
|
||||
from shared.clients.tenant_client import TenantServiceClient
|
||||
from app.core.config import settings
|
||||
|
||||
tenant_client = TenantServiceClient(settings)
|
||||
|
||||
# Prepare user data for tenant service (without user_id)
|
||||
user_data_for_tenant = {
|
||||
"email": user_data.email,
|
||||
"full_name": user_data.full_name,
|
||||
"name": user_data.full_name
|
||||
}
|
||||
|
||||
# Call tenant service to create payment customer
|
||||
payment_result = await tenant_client.create_payment_customer(
|
||||
user_data_for_tenant,
|
||||
user_data.payment_method_id
|
||||
)
|
||||
|
||||
logger.info("Payment customer created for registration (pre-user creation)",
|
||||
email=user_data.email,
|
||||
payment_customer_id=payment_result.get("payment_customer_id") if payment_result else "unknown")
|
||||
|
||||
return payment_result
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Payment customer creation failed for registration",
|
||||
email=user_data.email,
|
||||
error=str(e),
|
||||
exc_info=True)
|
||||
raise
|
||||
|
||||
async def verify_setup_intent(
|
||||
self,
|
||||
setup_intent_id: str
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Verify SetupIntent status with payment provider
|
||||
|
||||
This method checks if a SetupIntent has been successfully confirmed
|
||||
(either automatically or via 3DS authentication).
|
||||
|
||||
Args:
|
||||
setup_intent_id: SetupIntent ID to verify
|
||||
|
||||
Returns:
|
||||
Dictionary with SetupIntent verification result
|
||||
|
||||
Raises:
|
||||
Exception: If verification fails
|
||||
"""
|
||||
try:
|
||||
from shared.clients.tenant_client import TenantServiceClient
|
||||
from app.core.config import settings
|
||||
|
||||
tenant_client = TenantServiceClient(settings)
|
||||
|
||||
# Call tenant service to verify SetupIntent
|
||||
verification_result = await tenant_client.verify_setup_intent(
|
||||
setup_intent_id
|
||||
)
|
||||
|
||||
logger.info("SetupIntent verification result",
|
||||
setup_intent_id=setup_intent_id,
|
||||
status=verification_result.get("status") if verification_result else "unknown")
|
||||
|
||||
return verification_result
|
||||
|
||||
except Exception as e:
|
||||
logger.error("SetupIntent verification failed",
|
||||
setup_intent_id=setup_intent_id,
|
||||
error=str(e),
|
||||
exc_info=True)
|
||||
raise
|
||||
|
||||
async def save_subscription_to_onboarding_progress(
|
||||
self,
|
||||
user_id: str,
|
||||
|
||||
@@ -5,6 +5,7 @@ Updated to use repository pattern with dependency injection and improved error h
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Dict, Any, List, Optional
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from fastapi import HTTPException, status
|
||||
import structlog
|
||||
|
||||
@@ -27,22 +28,27 @@ class EnhancedUserService:
|
||||
"""Initialize service with database manager"""
|
||||
self.database_manager = database_manager
|
||||
|
||||
async def get_user_by_id(self, user_id: str) -> Optional[UserResponse]:
|
||||
async def get_user_by_id(self, user_id: str, session: Optional[AsyncSession] = None) -> Optional[UserResponse]:
|
||||
"""Get user by ID using repository pattern"""
|
||||
try:
|
||||
async with self.database_manager.get_session() as session:
|
||||
if session:
|
||||
# Use provided session (for direct session injection)
|
||||
user_repo = UserRepository(User, session)
|
||||
|
||||
user = await user_repo.get_by_id(user_id)
|
||||
if not user:
|
||||
return None
|
||||
|
||||
return UserResponse(
|
||||
id=str(user.id),
|
||||
email=user.email,
|
||||
full_name=user.full_name,
|
||||
is_active=user.is_active,
|
||||
is_verified=user.is_verified,
|
||||
else:
|
||||
# Use database manager to get session
|
||||
async with self.database_manager.get_session() as session:
|
||||
user_repo = UserRepository(User, session)
|
||||
|
||||
user = await user_repo.get_by_id(user_id)
|
||||
if not user:
|
||||
return None
|
||||
|
||||
return UserResponse(
|
||||
id=str(user.id),
|
||||
email=user.email,
|
||||
full_name=user.full_name,
|
||||
is_active=user.is_active,
|
||||
is_verified=user.is_verified,
|
||||
created_at=user.created_at,
|
||||
role=user.role,
|
||||
phone=getattr(user, 'phone', None),
|
||||
|
||||
Reference in New Issue
Block a user