Files
orion/app/modules/billing/routes/pages/merchant.py
Samir Boulahtit 4aa6f76e46
Some checks failed
CI / validate (push) Has been cancelled
CI / dependency-scanning (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / pytest (push) Has been cancelled
CI / ruff (push) Successful in 10s
refactor(arch): move auth schemas to tenancy module and add cross-module service methods
Move all auth schemas (UserContext, UserLogin, LoginResponse, etc.) from
legacy models/schema/auth.py to app/modules/tenancy/schemas/auth.py per
MOD-019. Update 84 import sites across 14 modules. Legacy file now
re-exports for backwards compatibility.

Add missing tenancy service methods for cross-module consumers:
- merchant_service.get_merchant_by_owner_id()
- merchant_service.get_merchant_count_for_owner()
- admin_service.get_user_by_id() (public, was private-only)
- platform_service.get_active_store_count()

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 23:57:04 +01:00

143 lines
4.3 KiB
Python

# app/modules/billing/routes/pages/merchant.py
"""
Merchant Billing Page Routes (HTML rendering).
Page routes for the merchant billing portal:
- Subscriptions list
- Subscription detail per platform
- Billing history / invoices
Authentication: merchant_token cookie or Authorization header.
Login and dashboard routes have moved to core module
(app/modules/core/routes/pages/merchant.py) to match the admin pattern.
Auto-discovered by the route system (merchant.py in routes/pages/ triggers
registration under /merchants/billing/*).
"""
from fastapi import APIRouter, Depends, Path, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from sqlalchemy.orm import Session
from app.api.deps import get_current_merchant_from_cookie_or_header
from app.core.database import get_db
from app.modules.core.utils.page_context import get_context_for_frontend
from app.modules.enums import FrontendType
from app.modules.tenancy.schemas.auth import UserContext
from app.templates_config import templates
ROUTE_CONFIG = {
"prefix": "/billing",
}
router = APIRouter()
# ============================================================================
# Helper
# ============================================================================
def _get_merchant_context(
request: Request,
db: Session,
current_user: UserContext,
**extra_context,
) -> dict:
"""
Build template context for merchant portal pages.
Uses the module-driven context builder with FrontendType.MERCHANT,
and adds the authenticated user to the context.
"""
return get_context_for_frontend(
FrontendType.MERCHANT,
request,
db,
user=current_user,
**extra_context,
)
# ============================================================================
# BILLING ROOT
# ============================================================================
@router.get("/", response_class=RedirectResponse, include_in_schema=False)
async def merchant_billing_root():
"""Redirect /merchants/billing/ to subscriptions page."""
return RedirectResponse(url="/merchants/billing/subscriptions", status_code=302)
# ============================================================================
# SUBSCRIPTIONS
# ============================================================================
@router.get("/subscriptions", response_class=HTMLResponse, include_in_schema=False)
async def merchant_subscriptions_page(
request: Request,
current_user: UserContext = Depends(get_current_merchant_from_cookie_or_header),
db: Session = Depends(get_db),
):
"""
Render merchant subscriptions list page.
Shows all subscriptions across platforms with status and tier info.
"""
context = _get_merchant_context(request, db, current_user)
return templates.TemplateResponse(
"billing/merchant/subscriptions.html",
context,
)
@router.get(
"/subscriptions/{platform_id}",
response_class=HTMLResponse,
include_in_schema=False,
)
async def merchant_subscription_detail_page(
request: Request,
platform_id: int = Path(..., description="Platform ID"),
current_user: UserContext = Depends(get_current_merchant_from_cookie_or_header),
db: Session = Depends(get_db),
):
"""
Render subscription detail page for a specific platform.
Shows subscription status, tier details, usage, and upgrade options.
"""
context = _get_merchant_context(
request, db, current_user, platform_id=platform_id
)
return templates.TemplateResponse(
"billing/merchant/subscription-detail.html",
context,
)
# ============================================================================
# BILLING HISTORY
# ============================================================================
@router.get("/invoices", response_class=HTMLResponse, include_in_schema=False)
async def merchant_billing_history_page(
request: Request,
current_user: UserContext = Depends(get_current_merchant_from_cookie_or_header),
db: Session = Depends(get_db),
):
"""
Render billing history page.
Shows invoice history and payment records for the merchant.
"""
context = _get_merchant_context(request, db, current_user)
return templates.TemplateResponse(
"billing/merchant/billing-history.html",
context,
)