Support subcription payments
This commit is contained in:
@@ -8,6 +8,7 @@ from typing import List, Dict, Any, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from app.services.subscription_limit_service import SubscriptionLimitService
|
||||
from app.services.payment_service import PaymentService
|
||||
from app.repositories import SubscriptionRepository
|
||||
from app.models.tenants import Subscription
|
||||
from shared.auth.decorators import get_current_user_dep, require_admin_role_dep
|
||||
@@ -27,6 +28,13 @@ def get_subscription_limit_service():
|
||||
logger.error("Failed to create subscription limit service", error=str(e))
|
||||
raise HTTPException(status_code=500, detail="Service initialization failed")
|
||||
|
||||
def get_payment_service():
|
||||
try:
|
||||
return PaymentService()
|
||||
except Exception as e:
|
||||
logger.error("Failed to create payment service", error=str(e))
|
||||
raise HTTPException(status_code=500, detail="Payment service initialization failed")
|
||||
|
||||
def get_subscription_repository():
|
||||
try:
|
||||
from app.core.config import settings
|
||||
@@ -182,7 +190,7 @@ async def validate_plan_upgrade(
|
||||
"""Validate if tenant can upgrade to a new plan"""
|
||||
|
||||
try:
|
||||
# TODO: Add access control - verify user has admin access to tenant
|
||||
# TODO: Add access control - verify user is owner/admin of tenant
|
||||
result = await limit_service.validate_plan_upgrade(str(tenant_id), new_plan)
|
||||
return result
|
||||
|
||||
@@ -241,9 +249,9 @@ async def upgrade_subscription_plan(
|
||||
detail="Failed to upgrade subscription plan"
|
||||
)
|
||||
|
||||
@router.get("/plans/available")
|
||||
@router.get("/plans")
|
||||
async def get_available_plans():
|
||||
"""Get all available subscription plans with features and pricing"""
|
||||
"""Get all available subscription plans with features and pricing - Public endpoint"""
|
||||
|
||||
try:
|
||||
# This could be moved to a config service or database
|
||||
@@ -294,7 +302,7 @@ async def get_available_plans():
|
||||
"description": "Ideal para cadenas con obradores centrales",
|
||||
"monthly_price": 399.0,
|
||||
"max_users": -1, # Unlimited
|
||||
"max_locations": -1, # Unlimited
|
||||
"max_locations": -1, # Unlimited
|
||||
"max_products": -1, # Unlimited
|
||||
"features": {
|
||||
"inventory_management": "multi_location",
|
||||
@@ -321,4 +329,93 @@ async def get_available_plans():
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to get available plans"
|
||||
)
|
||||
)
|
||||
|
||||
# New endpoints for payment processing during registration
|
||||
@router.post("/subscriptions/register-with-subscription")
|
||||
async def register_with_subscription(
|
||||
user_data: Dict[str, Any],
|
||||
plan_id: str = Query(..., description="Plan ID to subscribe to"),
|
||||
payment_method_id: str = Query(..., description="Payment method ID from frontend"),
|
||||
use_trial: bool = Query(False, description="Whether to use trial period for pilot users"),
|
||||
payment_service: PaymentService = Depends(get_payment_service)
|
||||
):
|
||||
"""Process user registration with subscription creation"""
|
||||
|
||||
try:
|
||||
result = await payment_service.process_registration_with_subscription(
|
||||
user_data,
|
||||
plan_id,
|
||||
payment_method_id,
|
||||
use_trial
|
||||
)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": "Registration and subscription created successfully",
|
||||
"data": result
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error("Failed to register with subscription", error=str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to register with subscription"
|
||||
)
|
||||
|
||||
@router.post("/subscriptions/{tenant_id}/cancel")
|
||||
async def cancel_subscription(
|
||||
tenant_id: UUID = Path(..., description="Tenant ID"),
|
||||
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
||||
payment_service: PaymentService = Depends(get_payment_service)
|
||||
):
|
||||
"""Cancel subscription for a tenant"""
|
||||
|
||||
try:
|
||||
# TODO: Add access control - verify user is owner/admin of tenant
|
||||
# In a real implementation, you would need to retrieve the subscription ID from the database
|
||||
# For now, this is a placeholder
|
||||
subscription_id = "sub_test" # This would come from the database
|
||||
|
||||
result = await payment_service.cancel_subscription(subscription_id)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": "Subscription cancelled successfully",
|
||||
"data": {
|
||||
"subscription_id": result.id,
|
||||
"status": result.status
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error("Failed to cancel subscription", error=str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to cancel subscription"
|
||||
)
|
||||
|
||||
@router.get("/subscriptions/{tenant_id}/invoices")
|
||||
async def get_invoices(
|
||||
tenant_id: UUID = Path(..., description="Tenant ID"),
|
||||
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
||||
payment_service: PaymentService = Depends(get_payment_service)
|
||||
):
|
||||
"""Get invoices for a tenant"""
|
||||
|
||||
try:
|
||||
# TODO: Add access control - verify user has access to tenant
|
||||
# In a real implementation, you would need to retrieve the customer ID from the database
|
||||
# For now, this is a placeholder
|
||||
customer_id = "cus_test" # This would come from the database
|
||||
|
||||
invoices = await payment_service.get_invoices(customer_id)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": invoices
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error("Failed to get invoices", error=str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="Failed to get invoices"
|
||||
)
|
||||
|
||||
133
services/tenant/app/api/webhooks.py
Normal file
133
services/tenant/app/api/webhooks.py
Normal file
@@ -0,0 +1,133 @@
|
||||
"""
|
||||
Webhook endpoints for handling payment provider events
|
||||
These endpoints receive events from payment providers like Stripe
|
||||
"""
|
||||
|
||||
import structlog
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Request
|
||||
from typing import Dict, Any
|
||||
|
||||
from app.services.payment_service import PaymentService
|
||||
from shared.auth.decorators import get_current_user_dep
|
||||
from shared.monitoring.metrics import track_endpoint_metrics
|
||||
|
||||
logger = structlog.get_logger()
|
||||
router = APIRouter()
|
||||
|
||||
def get_payment_service():
|
||||
try:
|
||||
return PaymentService()
|
||||
except Exception as e:
|
||||
logger.error("Failed to create payment service", error=str(e))
|
||||
raise HTTPException(status_code=500, detail="Payment service initialization failed")
|
||||
|
||||
@router.post("/webhooks/stripe")
|
||||
async def stripe_webhook(
|
||||
request: Request,
|
||||
payment_service: PaymentService = Depends(get_payment_service)
|
||||
):
|
||||
"""
|
||||
Stripe webhook endpoint to handle payment events
|
||||
"""
|
||||
try:
|
||||
# Get the payload
|
||||
payload = await request.body()
|
||||
sig_header = request.headers.get('stripe-signature')
|
||||
|
||||
# In a real implementation, you would verify the signature
|
||||
# using the webhook signing secret
|
||||
# event = stripe.Webhook.construct_event(
|
||||
# payload, sig_header, settings.STRIPE_WEBHOOK_SECRET
|
||||
# )
|
||||
|
||||
# For now, we'll just log the event
|
||||
logger.info("Received Stripe webhook", payload=payload.decode('utf-8'))
|
||||
|
||||
# Process different types of events
|
||||
# event_type = event['type']
|
||||
# event_data = event['data']['object']
|
||||
|
||||
# Example processing for different event types:
|
||||
# if event_type == 'checkout.session.completed':
|
||||
# # Handle successful checkout
|
||||
# pass
|
||||
# elif event_type == 'customer.subscription.created':
|
||||
# # Handle new subscription
|
||||
# pass
|
||||
# elif event_type == 'customer.subscription.updated':
|
||||
# # Handle subscription update
|
||||
# pass
|
||||
# elif event_type == 'customer.subscription.deleted':
|
||||
# # Handle subscription cancellation
|
||||
# pass
|
||||
# elif event_type == 'invoice.payment_succeeded':
|
||||
# # Handle successful payment
|
||||
# pass
|
||||
# elif event_type == 'invoice.payment_failed':
|
||||
# # Handle failed payment
|
||||
# pass
|
||||
|
||||
return {"success": True}
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error processing Stripe webhook", error=str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Webhook error"
|
||||
)
|
||||
|
||||
@router.post("/webhooks/generic")
|
||||
async def generic_webhook(
|
||||
request: Request,
|
||||
payment_service: PaymentService = Depends(get_payment_service)
|
||||
):
|
||||
"""
|
||||
Generic webhook endpoint that can handle events from any payment provider
|
||||
"""
|
||||
try:
|
||||
# Get the payload
|
||||
payload = await request.json()
|
||||
|
||||
# Log the event for debugging
|
||||
logger.info("Received generic webhook", payload=payload)
|
||||
|
||||
# Process the event based on its type
|
||||
event_type = payload.get('type', 'unknown')
|
||||
event_data = payload.get('data', {})
|
||||
|
||||
# Process different types of events
|
||||
if event_type == 'subscription.created':
|
||||
# Handle new subscription
|
||||
logger.info("Processing new subscription event", subscription_id=event_data.get('id'))
|
||||
# Update database with new subscription
|
||||
elif event_type == 'subscription.updated':
|
||||
# Handle subscription update
|
||||
logger.info("Processing subscription update event", subscription_id=event_data.get('id'))
|
||||
# Update database with subscription changes
|
||||
elif event_type == 'subscription.deleted':
|
||||
# Handle subscription cancellation
|
||||
logger.info("Processing subscription cancellation event", subscription_id=event_data.get('id'))
|
||||
# Update database with cancellation
|
||||
elif event_type == 'payment.succeeded':
|
||||
# Handle successful payment
|
||||
logger.info("Processing successful payment event", payment_id=event_data.get('id'))
|
||||
# Update payment status in database
|
||||
elif event_type == 'payment.failed':
|
||||
# Handle failed payment
|
||||
logger.info("Processing failed payment event", payment_id=event_data.get('id'))
|
||||
# Update payment status and notify user
|
||||
elif event_type == 'invoice.created':
|
||||
# Handle new invoice
|
||||
logger.info("Processing new invoice event", invoice_id=event_data.get('id'))
|
||||
# Store invoice information
|
||||
else:
|
||||
logger.warning("Unknown event type received", event_type=event_type)
|
||||
|
||||
return {"success": True}
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error processing generic webhook", error=str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Webhook error"
|
||||
)
|
||||
Reference in New Issue
Block a user