videogen/apps/api/app/controllers/v1/premium_credits.py
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

113 lines
4.3 KiB
Python

"""Premium voice and AI-video credits: balances, packs and Dodo checkout.
* ``GET /billing/premium`` - the caller's premium balances, the packs
on sale and how AI-video credits are priced (3x provider cost, 1 credit = 1
US cent).
* ``POST /billing/premium/checkout`` - open a Dodo checkout for one pack.
Credits land once, from the ``payment.succeeded`` webhook or the verified
browser return (``POST /billing/dodo/confirm``), never here.
A standard reel credit never pays for ElevenLabs or AI-video / presenter
provider calls; these balances do (see ``app.services.billing.premium_credits``).
"""
from __future__ import annotations
from fastapi import Request
from loguru import logger
from pydantic import BaseModel, Field
from app.controllers import base
from app.controllers.v1.base import new_router
from app.core.auth import resolve_principal
from app.core.launch import require_checkout_enabled
from app.models.db.base import get_session
from app.models.exception import HttpException
from app.services.billing import premium_credits
from app.utils import utils
router = new_router()
def _record_checkout_started(user_id: str) -> None:
"""Funnel step "checkout started" (best-effort; never affects the request)."""
if not user_id:
return
try:
from app.services.funnel.store import record_step
record_step(user_id, "checkout_started", session_factory=get_session)
except Exception as exc: # noqa: BLE001 - instrumentation only
logger.warning(f"premium_credits: funnel record skipped: {type(exc).__name__}")
class PremiumCheckoutRequest(BaseModel):
pack_key: str = Field(min_length=1, max_length=40)
def _caller(request: Request, request_id: str) -> dict[str, str]:
with get_session() as session:
user = resolve_principal(request, session, request_id)
return {"id": str(user.id), "email": str(getattr(user, "email", "") or "")}
@router.get("/billing/premium", summary="Premium voice and AI-video credits")
def premium_status(request: Request):
request_id = base.get_task_id(request)
user = _caller(request, request_id)
return utils.get_response(
200,
{
"balances": premium_credits.balances(user["id"]),
"packs": [pack.as_dict() for pack in premium_credits.list_packs()],
"ai_video_markup": premium_credits.AI_VIDEO_MARKUP,
"ai_video_credit_usd": 0.01,
},
)
@router.post("/billing/premium/checkout", summary="Buy a premium voice or AI-video pack")
def premium_checkout(request: Request, body: PremiumCheckoutRequest):
request_id = base.get_task_id(request)
require_checkout_enabled(request_id)
pack = premium_credits.get_pack(body.pack_key)
if pack is None:
raise HttpException(task_id=request_id, status_code=400, message=f"{request_id}: unknown pack")
from app.services.billing import dodo_billing, payments_mode
if payments_mode.active_provider() != payments_mode.DODO:
raise HttpException(
task_id=request_id,
status_code=409,
message="Premium packs are not on sale right now.",
data={"code": "payments_paused"},
)
user = _caller(request, request_id)
try:
result = dodo_billing.open_premium_checkout(user, pack)
except ValueError as exc:
logger.error(f"premium checkout not configured, request_id: {request_id}: {exc}")
raise HttpException(
task_id=request_id,
status_code=409,
message="This pack is not on sale yet.",
data={"code": "pack_not_configured"},
) from exc
except Exception as exc: # noqa: BLE001 - provider failure -> 502
logger.error(f"premium checkout failed, request_id: {request_id}: {exc}")
raise HttpException(
task_id=request_id,
status_code=502,
message=f"{request_id}: payment provider is unavailable",
) from exc
url = str((result or {}).get("checkout_url") or "")
if not url:
raise HttpException(
task_id=request_id,
status_code=502,
message=f"{request_id}: payment provider returned no checkout url",
)
_record_checkout_started(user["id"])
return utils.get_response(200, {"checkout_url": url, "pack": pack.as_dict()})