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

71 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Affiliate commission engine — lifetime-recurring referral payouts.
A referrer earns a percentage of every payment their referee makes, for the
lifetime of the account (a strong PLG loop for creator tools). Pure integer-micro
math (matches the wallet money model): given a referral relationship + a payment,
compute the commission, honoring a per-referral lifetime cap and a tier rate.
Deterministic; the ledger + referral graph live in the domain service.
"""
from __future__ import annotations
from dataclasses import dataclass
# Tier rates in basis points (100 bps = 1%). Lifetime-recurring.
TIER_RATES_BPS = {
"standard": 2000, # 20%
"pro": 3000, # 30% (high-volume affiliates)
"partner": 4000, # 40% (negotiated)
}
_DEFAULT_TIER = "standard"
@dataclass(frozen=True)
class Referral:
"""A referrer→referee relationship with a tier + optional lifetime cap."""
referrer_id: str
referee_id: str
tier: str = _DEFAULT_TIER
lifetime_cap_micros: int = 0 # 0 == uncapped
earned_micros: int = 0 # already paid out on this referral
@dataclass(frozen=True)
class Commission:
"""A computed commission for one referee payment."""
payable_micros: int
rate_bps: int
capped: bool
reason: str = ""
class AffiliateError(ValueError):
"""An invalid referral (e.g. self-referral)."""
def rate_bps(tier: str) -> int:
return TIER_RATES_BPS.get(str(tier or "").strip().lower(), TIER_RATES_BPS[_DEFAULT_TIER])
def compute_commission(referral: Referral, payment_micros: int) -> Commission:
"""Commission = payment × tier_rate, clamped to the remaining lifetime cap.
Integer floor-division so no fractional micro is minted. A self-referral is
rejected (fraud guard). A payment ≤ 0 yields zero.
"""
if referral.referrer_id == referral.referee_id:
raise AffiliateError("self-referral is not allowed")
pay = max(0, int(payment_micros))
bps = rate_bps(referral.tier)
gross = (pay * bps) // 10000
if referral.lifetime_cap_micros > 0:
remaining = max(0, referral.lifetime_cap_micros - max(0, referral.earned_micros))
payable = min(gross, remaining)
return Commission(payable, bps, capped=payable < gross,
reason="lifetime-cap" if payable < gross else "ok")
return Commission(gross, bps, capped=False, reason="ok")