# 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, )