Files
orion/app/modules/billing/routes/pages/merchant.py
Samir Boulahtit f20266167d
Some checks failed
CI / ruff (push) Failing after 7s
CI / pytest (push) Failing after 1s
CI / architecture (push) Failing after 9s
CI / dependency-scanning (push) Successful in 27s
CI / audit (push) Successful in 8s
CI / docs (push) Has been skipped
fix(lint): auto-fix ruff violations and tune lint rules
- Auto-fixed 4,496 lint issues (import sorting, modern syntax, etc.)
- Added ignore rules for patterns intentional in this codebase:
  E402 (late imports), E712 (SQLAlchemy filters), B904 (raise from),
  SIM108/SIM105/SIM117 (readability preferences)
- Added per-file ignores for tests and scripts
- Excluded broken scripts/rename_terminology.py (has curly quotes)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 23:10:42 +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.templates_config import templates
from models.schema.auth import UserContext
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,
)