Add subcription feature 2

This commit is contained in:
Urtzi Alfaro
2026-01-14 13:15:48 +01:00
parent 6ddf608d37
commit a4c3b7da3f
32 changed files with 4240 additions and 965 deletions

View File

@@ -622,6 +622,184 @@ class TenantServiceClient(BaseServiceClient):
return None
async def create_payment_customer(
self,
user_data: Dict[str, Any],
payment_method_id: Optional[str] = None
) -> Dict[str, Any]:
"""
Create payment customer (supports pre-user-creation flow)
This method creates a payment customer without requiring a user_id,
supporting the secure architecture where users are only created after
payment verification.
Args:
user_data: User data (email, full_name, etc.)
payment_method_id: Optional payment method ID
Returns:
Dictionary with payment customer creation result
"""
try:
logger.info("Creating payment customer via tenant service",
email=user_data.get('email'),
payment_method_id=payment_method_id)
# Call tenant service endpoint
result = await self.post(
"/payment-customers/create",
{
"user_data": user_data,
"payment_method_id": payment_method_id
}
)
if result and result.get("success"):
logger.info("Payment customer created successfully via tenant service",
email=user_data.get('email'),
payment_customer_id=result.get('payment_customer_id'))
return result
else:
logger.error("Payment customer creation failed via tenant service",
email=user_data.get('email'),
error=result.get('detail') if result else 'No detail provided')
raise Exception("Payment customer creation failed: " +
(result.get('detail') if result else 'Unknown error'))
except Exception as e:
logger.error("Failed to create payment customer via tenant service",
email=user_data.get('email'),
error=str(e))
raise
async def create_registration_payment_setup(
self,
user_data: Dict[str, Any]
) -> Dict[str, Any]:
"""
Create registration payment setup via tenant service orchestration
This method calls the tenant service's orchestration endpoint to create
payment customer and SetupIntent in a coordinated workflow.
Args:
user_data: User data including email, full_name, payment_method_id, etc.
Returns:
Dictionary with payment setup results including SetupIntent if required
"""
try:
logger.info("Creating registration payment setup via tenant service orchestration",
email=user_data.get('email'),
payment_method_id=user_data.get('payment_method_id'))
# Call tenant service orchestration endpoint
result = await self.post(
"/payment-customers/create",
user_data
)
if result and result.get("success"):
logger.info("Registration payment setup completed via tenant service orchestration",
email=user_data.get('email'),
requires_action=result.get('requires_action'))
return result
else:
logger.error("Registration payment setup failed via tenant service orchestration",
email=user_data.get('email'),
error=result.get('detail') if result else 'No detail provided')
raise Exception("Registration payment setup failed: " +
(result.get('detail') if result else 'Unknown error'))
except Exception as e:
logger.error("Failed to create registration payment setup via tenant service orchestration",
email=user_data.get('email'),
error=str(e))
raise
async def verify_setup_intent_for_registration(
self,
setup_intent_id: str
) -> Dict[str, Any]:
"""
Verify SetupIntent status via tenant service orchestration
This method calls the tenant service's orchestration endpoint to verify
SetupIntent status before proceeding with user creation.
Args:
setup_intent_id: SetupIntent ID to verify
Returns:
Dictionary with SetupIntent verification result
"""
try:
logger.info("Verifying SetupIntent via tenant service orchestration",
setup_intent_id=setup_intent_id)
# Call tenant service orchestration endpoint
result = await self.get(
f"/setup-intents/{setup_intent_id}/verify"
)
if result:
logger.info("SetupIntent verification result from tenant service orchestration",
setup_intent_id=setup_intent_id,
status=result.get('status'))
return result
else:
logger.error("SetupIntent verification failed via tenant service orchestration",
setup_intent_id=setup_intent_id,
error='No result returned')
raise Exception("SetupIntent verification failed: No result returned")
except Exception as e:
logger.error("Failed to verify SetupIntent via tenant service orchestration",
setup_intent_id=setup_intent_id,
error=str(e))
raise
async def verify_setup_intent(
self,
setup_intent_id: str
) -> Dict[str, Any]:
"""
Verify SetupIntent status with payment provider
Args:
setup_intent_id: SetupIntent ID to verify
Returns:
Dictionary with SetupIntent verification result
"""
try:
logger.info("Verifying SetupIntent via tenant service",
setup_intent_id=setup_intent_id)
# Call tenant service endpoint
result = await self.get(
f"/setup-intents/{setup_intent_id}/verify"
)
if result:
logger.info("SetupIntent verification result from tenant service",
setup_intent_id=setup_intent_id,
status=result.get('status'))
return result
else:
logger.error("SetupIntent verification failed via tenant service",
setup_intent_id=setup_intent_id,
error='No result returned')
raise Exception("SetupIntent verification failed: No result returned")
except Exception as e:
logger.error("Failed to verify SetupIntent via tenant service",
setup_intent_id=setup_intent_id,
error=str(e))
raise
# Factory function for dependency injection
def create_tenant_client(config: BaseServiceSettings) -> TenantServiceClient:
"""Create tenant service client instance"""