A declined card left the account in a state with no way out. `past_due` grants no minutes, so the metering layer resolved the user to the free plan — which meant /api/me reported status=null, the account page swapped "Manage billing" for "Upgrade" (hiding the only button that fixes a card), and the checkout guard then rejected the upgrade with a 409 that every call site swallowed into a generic "Could not start checkout." Silently free, unwarned, unable to pay. Found on 25-jul-2026 in prod: 14 subscriptions in this state, all first charges that failed when the (now retired) 3-day trial ended. The trial is gone, but the trap is not — a renewal failure or an abandoned 3DS payment reproduces it. - metering: split `_subscription_row` (raw) from `_active_subscription` (entitling), and expose both on the balance. Entitlement logic is unchanged. - /api/me: report status/interval from a row needing attention when no subscription entitles, plus `has_billing_account` to route the portal button. - checkout: drop `incomplete` from the blocking states. Stripe collects nothing on it and expires it within 24h, so blocking only stopped the user's retry. Give past_due/unpaid a message that names the problem and the fix. - account page: explain the failed payment inline with a link to the portal instead of showing a bare "past_due" chip, and keep the portal reachable for any Stripe relationship, live or broken. - api client: throw a typed ApiError carrying the server's detail so callers stop replacing real, actionable messages with generic copy. Verified against a real DB: past_due stays blocked but visible and fixable, incomplete can retry, canceled is silent and can subscribe again.
267 lines
9.5 KiB
Python
267 lines
9.5 KiB
Python
"""Central configuration for the optional cloud (paid / managed-keys) mode.
|
|
|
|
Everything here is read lazily from environment variables so that importing the
|
|
`cloud` package never has side effects. Nothing in this module is loaded unless
|
|
``BILLING_ENABLED`` is truthy (see ``cloud.is_enabled``).
|
|
"""
|
|
import os
|
|
from functools import lru_cache
|
|
|
|
|
|
def _flag(name: str, default: str = "") -> bool:
|
|
return os.environ.get(name, default).lower() in ("1", "true", "yes")
|
|
|
|
|
|
def is_enabled() -> bool:
|
|
"""Master switch. When False the whole cloud package stays dormant."""
|
|
return _flag("BILLING_ENABLED")
|
|
|
|
|
|
# --- Plan catalog -----------------------------------------------------------
|
|
# minutes granted per billing period, keyed by internal plan name. This is the
|
|
# AUTHORITATIVE quota source (not Stripe price metadata, which a dashboard edit
|
|
# could change). Plan name is resolved from the Stripe price; minutes from here.
|
|
PLAN_MINUTES = {
|
|
"starter": 100,
|
|
"creator": 300,
|
|
"pro": 750,
|
|
}
|
|
|
|
# Free plan: monthly allowance for Google-authenticated users with no
|
|
# subscription. No Stripe object exists for it — it is resolved entirely in
|
|
# cloud/metering.py against a synthetic calendar-month period. Setting
|
|
# FREE_PLAN_MINUTES = 0 disables the free plan.
|
|
FREE_PLAN_MINUTES = 20
|
|
# Free is open to Google accounts AND permanent email accounts; disposable /
|
|
# temp-mail domains are blocked at sign-up (cloud/email_policy) and aliases are
|
|
# normalized, so the plan isn't a multi-account faucet.
|
|
# The 20-minute monthly quota is the only bound on free usage — no daily job
|
|
# cap (it confused users and the minute ledger already caps real cost).
|
|
# Free users' clips expire from R2 after this many days (paid libraries are
|
|
# durable). Also an upgrade lever, mirroring OpusClip's 3-day free exports.
|
|
FREE_CLIP_RETENTION_DAYS = 7
|
|
|
|
# Free trial length (days) for new subscriptions. Trials are retired in favor
|
|
# of the free plan; the checkout only injects trial_period_days when > 0, and
|
|
# existing 'trialing' subscriptions are still honored until they convert or
|
|
# cancel (grandfathering).
|
|
TRIAL_DAYS = 0
|
|
|
|
# Subscription states that grant no minutes but still need the customer to act
|
|
# (fix a card, finish a payment). They must stay VISIBLE in /api/me and keep the
|
|
# billing-portal route reachable — otherwise the account silently reads as free
|
|
# and the user has no way out. Terminal states (canceled, incomplete_expired)
|
|
# are deliberately absent: nothing to fix, so nothing to nag about.
|
|
BILLING_ATTENTION_STATES = ("past_due", "unpaid", "incomplete", "paused")
|
|
|
|
# Subscription states that must block a NEW subscription checkout. Narrower than
|
|
# BILLING_ATTENTION_STATES on purpose: Stripe is still trying to collect on
|
|
# past_due/unpaid/paused, so a second subscription would double-bill. It is NOT
|
|
# collecting on 'incomplete' (payment abandoned at 3DS/confirm, expired by Stripe
|
|
# within 24h), so blocking that would only stop the retry.
|
|
CHECKOUT_BLOCKING_STATES = ("active", "trialing", "past_due", "unpaid", "paused")
|
|
|
|
# Minute cap DURING the trial (across all plans). Kept for grandfathered
|
|
# 'trialing' subscriptions; removable once no subscription has status
|
|
# 'trialing'.
|
|
TRIAL_MINUTE_CAP = 20
|
|
|
|
# Gemini IMAGE generation (thumbnails) is the one expensive managed Gemini call
|
|
# (~$0.04/image, batch of ~3). It isn't naturally minute-metered, so each
|
|
# thumbnail generation batch consumes this many minutes from the plan quota —
|
|
# roughly matching its cost to the per-minute economics. Text (titles/desc) is free.
|
|
THUMBNAIL_MINUTES = 3
|
|
|
|
# Other managed Gemini calls that upload a video for context (AI effect/filter
|
|
# generation, thumbnail analysis, SaaS-shorts analysis). Cheaper than image gen
|
|
# but not free — meter a small fixed cost so an entitled user can't loop them to
|
|
# burn the operator's managed Gemini budget. Pure-text calls (titles/desc) stay free.
|
|
MANAGED_ANALYSIS_MINUTES = 1
|
|
|
|
# Post-processing FFmpeg re-encodes (subtitle burn, hook overlay) and the
|
|
# Remotion render proxy do real server compute per call. Meter a small fixed
|
|
# cost so an entitled user can't loop them for free off-quota.
|
|
SUBTITLE_MINUTES = 2
|
|
HOOK_MINUTES = 1
|
|
RENDER_MINUTES = 3
|
|
|
|
# The thumbnail-studio upload kicks off a (possibly YouTube) download + a full
|
|
# Whisper transcription in the background — expensive and proxy-bandwidth-heavy.
|
|
# Charge a fixed guard cost up front, settled when the background job finishes.
|
|
TRANSCRIBE_MINUTES = 2
|
|
|
|
# Stripe prices are resolved at runtime by these stable lookup_keys, so no price
|
|
# IDs need to be copied into env vars (they differ between test and live anyway).
|
|
SUBSCRIPTION_LOOKUP_KEYS = [
|
|
"starter_monthly", "starter_yearly",
|
|
"creator_monthly", "creator_yearly",
|
|
"pro_monthly", "pro_yearly",
|
|
]
|
|
TOPUP_LOOKUP_KEYS = ["topup_60", "topup_200"]
|
|
|
|
# Queue priority per plan (lower dispatches first). BYOK / anonymous = 2.
|
|
PLAN_PRIORITY = {
|
|
"pro": 0,
|
|
"creator": 1,
|
|
"starter": 1,
|
|
"free": 2,
|
|
}
|
|
|
|
# Max simultaneous managed jobs per user, by plan.
|
|
PLAN_JOB_LIMIT = {
|
|
"pro": 3,
|
|
"creator": 2,
|
|
"starter": 2,
|
|
"free": 1,
|
|
}
|
|
|
|
|
|
class Settings:
|
|
"""Lazily-evaluated env-backed settings. Access attributes, not the class."""
|
|
|
|
# Core
|
|
@property
|
|
def database_url(self) -> str:
|
|
return os.environ.get("DATABASE_URL", "")
|
|
|
|
@property
|
|
def jwt_secret(self) -> str:
|
|
return os.environ.get("JWT_SECRET", "")
|
|
|
|
@property
|
|
def frontend_url(self) -> str:
|
|
return os.environ.get("FRONTEND_URL", "https://openshorts.app").rstrip("/")
|
|
|
|
@property
|
|
def allowed_origins(self) -> list:
|
|
raw = os.environ.get("ALLOWED_ORIGINS", "")
|
|
origins = [o.strip() for o in raw.split(",") if o.strip()]
|
|
return origins or [self.frontend_url]
|
|
|
|
# Email (SMTP — e.g. Namecheap Private Email)
|
|
@property
|
|
def smtp_host(self) -> str:
|
|
return os.environ.get("SMTP_HOST", "mail.privateemail.com")
|
|
|
|
@property
|
|
def smtp_port(self) -> int:
|
|
return int(os.environ.get("SMTP_PORT", "465"))
|
|
|
|
@property
|
|
def smtp_user(self) -> str:
|
|
return os.environ.get("SMTP_USER", "")
|
|
|
|
@property
|
|
def smtp_password(self) -> str:
|
|
return os.environ.get("SMTP_PASSWORD", "")
|
|
|
|
@property
|
|
def smtp_configured(self) -> bool:
|
|
return bool(self.smtp_user and self.smtp_password)
|
|
|
|
@property
|
|
def email_from(self) -> str:
|
|
# Namecheap requires the From to be the authenticated mailbox.
|
|
return os.environ.get("EMAIL_FROM") or (f"OpenShorts <{self.smtp_user}>" if self.smtp_user else "OpenShorts")
|
|
|
|
@property
|
|
def admin_email(self) -> str:
|
|
# Where operational alerts (proxy out of credits, high failure rate) go.
|
|
return os.environ.get("ADMIN_EMAIL", "")
|
|
|
|
# Telegram (optional) — real-time admin alerts (purchases, churn, outages)
|
|
@property
|
|
def telegram_bot_token(self) -> str:
|
|
return os.environ.get("TELEGRAM_BOT_TOKEN", "")
|
|
|
|
@property
|
|
def telegram_chat_id(self) -> str:
|
|
return os.environ.get("TELEGRAM_CHAT_ID", "")
|
|
|
|
@property
|
|
def telegram_configured(self) -> bool:
|
|
return bool(self.telegram_bot_token and self.telegram_chat_id)
|
|
|
|
# Google OAuth
|
|
@property
|
|
def google_client_id(self) -> str:
|
|
return os.environ.get("GOOGLE_CLIENT_ID", "")
|
|
|
|
@property
|
|
def google_client_secret(self) -> str:
|
|
return os.environ.get("GOOGLE_CLIENT_SECRET", "")
|
|
|
|
@property
|
|
def google_auth_enabled(self) -> bool:
|
|
return bool(self.google_client_id and self.google_client_secret)
|
|
|
|
# Stripe
|
|
@property
|
|
def stripe_secret_key(self) -> str:
|
|
return os.environ.get("STRIPE_SECRET_KEY", "")
|
|
|
|
@property
|
|
def stripe_webhook_secret(self) -> str:
|
|
return os.environ.get("STRIPE_WEBHOOK_SECRET", "")
|
|
|
|
# Managed provider keys (server-owned, only handed to entitled users)
|
|
@property
|
|
def managed_gemini_key(self) -> str:
|
|
return os.environ.get("MANAGED_GEMINI_API_KEY", "")
|
|
|
|
@property
|
|
def managed_upload_post_key(self) -> str:
|
|
return os.environ.get("MANAGED_UPLOAD_POST_API_KEY", "")
|
|
|
|
@property
|
|
def openshorts_logo_url(self) -> str:
|
|
return os.environ.get("OPENSHORTS_LOGO_URL", "https://openshorts.app/logo.png")
|
|
|
|
# Cloudflare R2 (S3-compatible) — durable video library storage
|
|
@property
|
|
def r2_endpoint(self) -> str:
|
|
return os.environ.get("R2_ENDPOINT", "")
|
|
|
|
@property
|
|
def r2_bucket(self) -> str:
|
|
return os.environ.get("R2_BUCKET", "")
|
|
|
|
@property
|
|
def r2_access_key_id(self) -> str:
|
|
return os.environ.get("R2_ACCESS_KEY_ID", "")
|
|
|
|
@property
|
|
def r2_secret_access_key(self) -> str:
|
|
return os.environ.get("R2_SECRET_ACCESS_KEY", "")
|
|
|
|
@property
|
|
def r2_configured(self) -> bool:
|
|
return bool(self.r2_endpoint and self.r2_bucket and self.r2_access_key_id
|
|
and self.r2_secret_access_key)
|
|
|
|
|
|
# Days a user's videos survive after their subscription ends (grace period).
|
|
VIDEO_RETENTION_GRACE_DAYS = 7
|
|
|
|
|
|
settings = Settings()
|
|
|
|
|
|
def validate_required():
|
|
"""Raise a clear error if mandatory cloud settings are missing.
|
|
|
|
Called from ``cloud.setup`` at startup so a misconfigured deploy fails fast
|
|
instead of erroring on the first paid request.
|
|
"""
|
|
missing = []
|
|
if not settings.database_url:
|
|
missing.append("DATABASE_URL")
|
|
if not settings.jwt_secret:
|
|
missing.append("JWT_SECRET")
|
|
if missing:
|
|
raise RuntimeError(
|
|
"BILLING_ENABLED is set but required settings are missing: "
|
|
+ ", ".join(missing)
|
|
+ ". Set them (see docker-compose.cloud.yml) or unset BILLING_ENABLED "
|
|
"to run in self-hosted BYOK mode."
|
|
)
|