videogen/apps/api/app/controllers/v1/monthly_plans.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

182 lines
7.1 KiB
Python

"""Personal monthly plans (Creator / Pro / Agency) — web billing v1 API.
* ``GET /api/v1/billing/monthly-plans`` -> public catalog (no auth).
* ``GET /api/v1/billing/monthly-plan`` -> my plan, renewal date,
included reels left, balance and the recommended upgrade.
* ``POST /api/v1/billing/monthly-plan/checkout`` -> Creem recurring checkout.
* ``POST /api/v1/billing/monthly-plan/change`` -> switch plan (upgrade now).
* ``POST /api/v1/billing/monthly-plan/cancel`` -> one-click renewal off.
* ``POST /api/v1/billing/monthly-plan/portal`` -> Creem customer portal URL.
Webhooks arrive on the existing ``/billing/webhook/creem`` endpoint and the
signed return on ``/billing/creem/confirm`` (routed in the billing service).
Errors carry copy the web shows as-is. Mobile (RevenueCat) is separate.
"""
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.launch import require_checkout_enabled
from app.models.exception import HttpException
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")
except Exception as exc: # noqa: BLE001 - instrumentation only
logger.warning(f"monthly_plans: funnel record skipped: {type(exc).__name__}")
_UNAVAILABLE = "Monthly plans are not on sale yet. Credit packs work in the meantime."
_PROVIDER_DOWN = "The payment provider did not answer. Click again in a moment."
class MonthlyCheckoutRequest(BaseModel):
plan_key: str = Field(..., min_length=1, max_length=40)
interval: str = Field(default="month", max_length=10)
class MonthlyChangeRequest(BaseModel):
plan_key: str = Field(..., min_length=1, max_length=40)
def _fail(request_id: str, status: int, message: str) -> HttpException:
return HttpException(task_id=request_id, status_code=status, message=message)
def _user(request: Request, request_id: str) -> tuple[str, str]:
"""(user id, email) of the signed-in caller; 401 otherwise, 503 without a DB."""
try:
from app.models.db.base import get_session
except Exception as exc: # noqa: BLE001 - persistence missing -> 503
logger.error(f"monthly plans: persistence unavailable, request_id: {request_id}: {exc}")
raise _fail(request_id, 503, "Billing is temporarily unavailable.")
from app.core.auth.principal import resolve_principal
with get_session() as session:
user = resolve_principal(request, session, request_id)
return str(user.id), str(getattr(user, "email", "") or "")
def _actions():
from app.services.billing import creator_plan_actions
return creator_plan_actions
def _subs():
from app.services.billing import creator_subscriptions
return creator_subscriptions
@router.get("/billing/monthly-plans", summary="Monthly plans catalog")
def monthly_plans(request: Request):
"""Public catalog: price per month/year, reels, per-reel price, availability."""
from app.services.billing import creator_plans
return utils.get_response(
200,
{
"plans": creator_plans.catalog(),
"yearly_months_charged": creator_plans.YEARLY_MONTHS_CHARGED,
"best_pack_per_reel_cents": int(round(creator_plans.best_pack_per_reel_usd() * 100)),
},
)
@router.get("/billing/monthly-plan", summary="My monthly plan")
def my_monthly_plan(request: Request):
request_id = base.get_task_id(request)
user_id, _ = _user(request, request_id)
try:
data = _actions().status(user_id)
except Exception as exc: # noqa: BLE001 - status read failure -> 502
logger.error(f"monthly plan status failed, request_id: {request_id}: {exc}")
raise _fail(request_id, 502, "Could not load your plan. Refresh to try again.")
return utils.get_response(200, data)
@router.post("/billing/monthly-plan/checkout", summary="Subscribe to a monthly plan")
def monthly_checkout(request: Request, body: MonthlyCheckoutRequest):
request_id = base.get_task_id(request)
require_checkout_enabled(request_id)
user_id, email = _user(request, request_id)
try:
data = _actions().start_checkout(
user_id=user_id, user_email=email, plan_key=body.plan_key, interval=body.interval
)
except _subs().AlreadySubscribed as exc:
raise _fail(request_id, 409, str(exc))
except _actions().PaymentsPaused as exc:
raise _fail(request_id, 409, str(exc))
except ValueError as exc:
raise _fail(request_id, 400, str(exc))
except RuntimeError as exc:
logger.error(f"monthly checkout failed, request_id: {request_id}: {exc}")
configured = "not configured" not in str(exc)
raise _fail(request_id, 502 if configured else 503, _PROVIDER_DOWN if configured else _UNAVAILABLE)
_record_checkout_started(str(user_id))
return utils.get_response(200, data)
@router.post("/billing/monthly-plan/change", summary="Switch monthly plan")
def monthly_change(request: Request, body: MonthlyChangeRequest):
request_id = base.get_task_id(request)
user_id, _ = _user(request, request_id)
try:
data = _actions().change_plan(user_id, body.plan_key)
except _subs().NoSubscription as exc:
raise _fail(request_id, 404, str(exc))
except ValueError as exc:
raise _fail(request_id, 400, str(exc))
except RuntimeError as exc:
logger.error(f"monthly plan change failed, request_id: {request_id}: {exc}")
raise _fail(request_id, 502, "Your plan was not changed. Try again in a moment.")
return utils.get_response(200, data)
@router.post("/billing/monthly-plan/cancel", summary="Turn off renewal")
def monthly_cancel(request: Request):
request_id = base.get_task_id(request)
user_id, _ = _user(request, request_id)
try:
data = _actions().cancel(user_id)
except _subs().NoSubscription as exc:
raise _fail(request_id, 404, str(exc))
except (RuntimeError, ValueError) as exc:
logger.error(f"monthly plan cancel failed, request_id: {request_id}: {exc}")
raise _fail(
request_id,
502,
"Renewal is still on: the payment provider did not confirm. Try again, or email support@menivor.com.",
)
return utils.get_response(200, data)
@router.post("/billing/monthly-plan/portal", summary="Open the billing portal")
def monthly_portal(request: Request):
request_id = base.get_task_id(request)
user_id, _ = _user(request, request_id)
try:
url = _actions().portal(user_id)
except _subs().NoSubscription as exc:
raise _fail(request_id, 404, str(exc))
except (RuntimeError, ValueError) as exc:
logger.error(f"monthly plan portal failed, request_id: {request_id}: {exc}")
raise _fail(request_id, 502, _PROVIDER_DOWN)
return utils.get_response(200, {"url": url})