feat(billing): 计费商业化重构(积分制)+ 团队差异化调价
统一计价引擎 apps/billing/pricing.py:1积分=¥0.1,平台成本(¥)与用户价(积分)双记账, 视频按火山真实 usage.total_tokens 结算(true-up,终结¥1/段倒贴)+ 首发×1.5毛利系数。 Team.price_multiplier 差异化调价(jimeng同款):挂牌价×系数两步HALF_UP取整,视频类 按下单时的价格/汇率快照结算(中途改配不影响在途任务)。 BillingConfig 单例(汇率/毛利系数/预留buffer,admin可调即刻生效)。存量数据 ×10 rescale 迁移(RunPython+atomic,MySQL 迁移不可中断重跑)。开户赠送归零(DEFAULT_TRIAL_CREDITS=0, 商业决策)。audit_billing 加 I9(卖亏审计)。271 条测试 + tsc/build 全绿。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
# Generated by Django 5.1.15 on 2026-07-03 03:08
|
||||
|
||||
import uuid
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("billing", "0002_remove_creditledger_billing_cre_team_id_e0f18f_idx_and_more"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="BillingConfig",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
(
|
||||
"points_per_yuan",
|
||||
models.DecimalField(decimal_places=2, default=10, max_digits=8),
|
||||
),
|
||||
(
|
||||
"video_margin_multiplier",
|
||||
models.DecimalField(decimal_places=2, default=1.5, max_digits=6),
|
||||
),
|
||||
(
|
||||
"video_reserve_buffer",
|
||||
models.DecimalField(decimal_places=2, default=1.1, max_digits=4),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"abstract": False,
|
||||
},
|
||||
),
|
||||
# seed 默认配置行(幂等):1¥=10积分 · 视频毛利×1.5(首发,商业决策) · 预留 buffer×1.10
|
||||
migrations.RunPython(
|
||||
lambda apps, schema_editor: apps.get_model("billing", "BillingConfig").objects.exists()
|
||||
or apps.get_model("billing", "BillingConfig").objects.create(),
|
||||
migrations.RunPython.noop,
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,44 @@
|
||||
"""积分制切换 · 账本全量 ×10 线性缩放(1 积分 = ¥0.1,points_per_yuan=10)。
|
||||
|
||||
全部金额列同乘 10:线性变换保持 audit_billing I1~I8 全部不变量。
|
||||
**不缩放**:CreditLedger.metadata 内的 paid_amount/bonus(真实支付¥,JSON 列不受影响)。
|
||||
|
||||
写法纪律(review 修正):
|
||||
· 用 RunPython + transaction.atomic 而非裸 RunSQL —— MySQL 的迁移不包事务
|
||||
(can_rollback_ddl=False),裸 RunSQL 逐条 autocommit,中途被杀后 django_migrations
|
||||
未记录 → 重跑会把已乘过的表再乘 10(×100 错账)。atomic 包住纯 DML(InnoDB 可回滚)
|
||||
后要么全做并记录、要么全回滚,重跑安全。
|
||||
· 反向除法用 10.0 —— sqlite NUMERIC 亲和下整数值 `/ 10` 是整数除法,回滚会静默截断。
|
||||
|
||||
⚠️ 部署纪律:必须与积分制新代码同一次停机窗口上线(短停机一段式)。
|
||||
"""
|
||||
from django.db import migrations, transaction
|
||||
|
||||
RESCALE_STATEMENTS = [
|
||||
"UPDATE billing_creditaccount SET balance = balance * 10, reserved_balance = reserved_balance * 10",
|
||||
"UPDATE billing_creditledger SET amount = amount * 10, balance_after = balance_after * 10",
|
||||
"UPDATE billing_creditreservation SET amount = amount * 10",
|
||||
"UPDATE billing_quotapolicy SET monthly_limit = monthly_limit * 10, project_limit = project_limit * 10, per_task_limit = per_task_limit * 10",
|
||||
]
|
||||
|
||||
REVERSE_STATEMENTS = [
|
||||
"UPDATE billing_creditaccount SET balance = balance / 10.0, reserved_balance = reserved_balance / 10.0",
|
||||
"UPDATE billing_creditledger SET amount = amount / 10.0, balance_after = balance_after / 10.0",
|
||||
"UPDATE billing_creditreservation SET amount = amount / 10.0",
|
||||
"UPDATE billing_quotapolicy SET monthly_limit = monthly_limit / 10.0, project_limit = project_limit / 10.0, per_task_limit = per_task_limit / 10.0",
|
||||
]
|
||||
|
||||
|
||||
def _run(statements):
|
||||
def apply(apps, schema_editor):
|
||||
with transaction.atomic(using=schema_editor.connection.alias):
|
||||
with schema_editor.connection.cursor() as cursor:
|
||||
for sql in statements:
|
||||
cursor.execute(sql)
|
||||
|
||||
return apply
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [("billing", "0003_billingconfig")]
|
||||
operations = [migrations.RunPython(_run(RESCALE_STATEMENTS), _run(REVERSE_STATEMENTS))]
|
||||
@@ -69,6 +69,23 @@ class CreditReservation(TimeStampedModel):
|
||||
expires_at = models.DateTimeField(null=True, blank=True)
|
||||
|
||||
|
||||
class BillingConfig(TimeStampedModel):
|
||||
"""平台计费全局配置 · 单行单例(pk 恒定,经 pricing.get_billing_config() 缓存读取)。
|
||||
|
||||
积分制核心参数:1 积分 = 1/points_per_yuan 元(默认 ¥0.1);用户侧余额/价格/流水全用积分,
|
||||
平台成本(base_cost)仍以 ¥ 记账。视频类用户价 = 火山成本价 × video_margin_multiplier 换积分
|
||||
(首发 ×1.5,商业决策;adminpanel 可调,改动即刻生效于下一次估价/结算)。
|
||||
"""
|
||||
|
||||
points_per_yuan = models.DecimalField(max_digits=8, decimal_places=2, default=10)
|
||||
video_margin_multiplier = models.DecimalField(max_digits=6, decimal_places=2, default=1.50)
|
||||
# 视频预留 buffer(收编 free_video.RESERVE_BUFFER):预留=预估积分×buffer,应对真实 tokens 略超预估
|
||||
video_reserve_buffer = models.DecimalField(max_digits=4, decimal_places=2, default=1.10)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"1¥={self.points_per_yuan}积分 · 视频毛利×{self.video_margin_multiplier}"
|
||||
|
||||
|
||||
class QuotaPolicy(TimeStampedModel):
|
||||
team = models.ForeignKey("accounts.Team", on_delete=models.CASCADE, related_name="quota_policies")
|
||||
user = models.ForeignKey("accounts.User", on_delete=models.CASCADE, null=True, blank=True, related_name="quota_policies")
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
"""平台统一计价引擎(积分制 · 参考 jimeng-clone 成本加成模型)。
|
||||
|
||||
单位约定:
|
||||
· 用户侧(余额/价格/流水/限额)一律**积分**,1 积分 = 1/points_per_yuan 元(默认 ¥0.1);
|
||||
· 平台成本(Quote.base_cost_yuan / AITask.base_cost)一律**人民币**——供应商结算是 ¥,
|
||||
两本账各自诚实,毛利 = actual_cost/points_per_yuan − base_cost。
|
||||
|
||||
规则来源:
|
||||
· flat 类型(文本/图像):ModelConfig.unit_price,语义已重定义为「积分/次(张)」(rescale 迁移 ×10);
|
||||
平台成本配在 metadata.pricing.base_cost_yuan(未配=0,毛利报表按未知处理)。
|
||||
· 配音:metadata.pricing = {"mode":"per_chars","chars_per_unit":500,"points_per_unit":10,
|
||||
"min_units":1,"base_cost_yuan_per_unit":...},按字符数阶梯。
|
||||
· 视频:metadata.pricing 是火山**成本价表**(元/百万tokens,见 apps/ai/video_pricing.py),
|
||||
用户价 = ¥成本 × video_margin_multiplier → 积分。按真实 usage.total_tokens 结算。
|
||||
|
||||
取整(前后端必须逐字一致,前端镜像在 frontend/src/components/free-create/constants.ts):
|
||||
¥成本先 quantize 到 0.01(video_pricing.calculate_cost 现状)→ ×毛利 ×汇率 → ROUND_HALF_UP
|
||||
到整数积分,最低 1 积分。
|
||||
"""
|
||||
import math
|
||||
from dataclasses import dataclass, field
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
|
||||
from django.core.cache import cache
|
||||
|
||||
from .models import BillingConfig
|
||||
|
||||
_CONFIG_CACHE_KEY = "billing_config_v1"
|
||||
_CONFIG_TTL = 60
|
||||
|
||||
# flat 类型 unit_price 未配置(≤0)时的兜底价(积分/次):按能力对齐公示价目
|
||||
# (图像 20 积分/张、其余 10 积分/次)——否则 unit_price=0 的图像模型(火山 Seedream)
|
||||
# 实扣会比消费页公示价低一半(review 确认的三方不一致)。
|
||||
FLAT_FALLBACK_POINTS = Decimal("10")
|
||||
FLAT_FALLBACK_POINTS_IMAGE = Decimal("20")
|
||||
|
||||
|
||||
def get_billing_config() -> BillingConfig:
|
||||
"""单例读取(60s 缓存;PATCH 端点写后主动失效)。无行则建默认行(幂等,迁移已 seed)。"""
|
||||
config = cache.get(_CONFIG_CACHE_KEY)
|
||||
if config is None:
|
||||
config = BillingConfig.objects.order_by("created_at").first()
|
||||
if config is None:
|
||||
config = BillingConfig.objects.create()
|
||||
# create() 返回的实例属性是模型默认值的 Python float,Decimal×float 直接 TypeError
|
||||
# 且会被缓存 60s(结算侧炸 = 成片被误判失败退费)。回读一次拿 DB 转换后的 Decimal。
|
||||
config.refresh_from_db()
|
||||
cache.set(_CONFIG_CACHE_KEY, config, _CONFIG_TTL)
|
||||
return config
|
||||
|
||||
|
||||
def invalidate_billing_config_cache() -> None:
|
||||
cache.delete(_CONFIG_CACHE_KEY)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Quote:
|
||||
points: Decimal # 用户应扣积分(整数,最低 1)
|
||||
base_cost_yuan: Decimal # 平台成本 ¥(未知配 0)
|
||||
meta: dict = field(default_factory=dict) # 计价快照(rule/units/tokens/margin/rate...)
|
||||
|
||||
|
||||
def team_price_multiplier(team) -> Decimal:
|
||||
"""团队价格系数(差异化调价):默认 1。非法/缺失一律回落 1,绝不因配置脏数据把计价打挂。"""
|
||||
raw = getattr(team, "price_multiplier", None) if team is not None else None
|
||||
try:
|
||||
value = Decimal(str(raw)) if raw is not None else Decimal("1")
|
||||
except Exception: # noqa: BLE001
|
||||
return Decimal("1")
|
||||
if not value.is_finite() or value <= 0:
|
||||
return Decimal("1")
|
||||
return value
|
||||
|
||||
|
||||
def apply_team_price(points: Decimal, multiplier: Decimal) -> Decimal:
|
||||
"""标准挂牌积分价 × 团队系数 → HALF_UP 取整,最低 1(0 价不强收)。
|
||||
这是差异化调价的唯一落点:与前端预估两步取整逐字对齐(先算挂牌价取整,再乘系数取整)。"""
|
||||
if points <= 0:
|
||||
return points
|
||||
if multiplier == Decimal("1"):
|
||||
return points
|
||||
scaled = (points * multiplier).quantize(Decimal("1"), rounding=ROUND_HALF_UP)
|
||||
return max(scaled, Decimal("1"))
|
||||
|
||||
|
||||
def yuan_to_points(yuan: Decimal, *, rate: Decimal | None = None) -> Decimal:
|
||||
"""¥ × points_per_yuan → ROUND_HALF_UP 整数积分,最低 1(0 元除外)。"""
|
||||
if rate is None:
|
||||
rate = get_billing_config().points_per_yuan
|
||||
if yuan <= 0:
|
||||
return Decimal("0")
|
||||
points = (Decimal(str(yuan)) * rate).quantize(Decimal("1"), rounding=ROUND_HALF_UP)
|
||||
return max(points, Decimal("1"))
|
||||
|
||||
|
||||
def _pricing_meta(model_config) -> dict:
|
||||
return ((getattr(model_config, "metadata", None) or {}).get("pricing")) or {}
|
||||
|
||||
|
||||
def quote_flat(model_config, *, units: int = 1, team=None) -> Quote:
|
||||
"""文本(次)/ 图像(张)。unit_price = 积分/单位;≤0 回落 10 积分。team 传入则乘团队价格系数。"""
|
||||
unit_points = Decimal(str(getattr(model_config, "unit_price", 0) or 0))
|
||||
if unit_points <= 0:
|
||||
is_image = str(getattr(model_config, "capability", "")) == "image"
|
||||
unit_points = FLAT_FALLBACK_POINTS_IMAGE if is_image else FLAT_FALLBACK_POINTS
|
||||
unit_points = unit_points.quantize(Decimal("1"), rounding=ROUND_HALF_UP)
|
||||
base_per_unit = Decimal(str(_pricing_meta(model_config).get("base_cost_yuan") or 0))
|
||||
multiplier = team_price_multiplier(team)
|
||||
points = apply_team_price(max(unit_points * units, Decimal("1")), multiplier)
|
||||
return Quote(
|
||||
points=points,
|
||||
base_cost_yuan=base_per_unit * units,
|
||||
meta={"rule": "flat", "units": units, "unit_points": str(unit_points), "price_multiplier": str(multiplier), "rate": str(get_billing_config().points_per_yuan)},
|
||||
)
|
||||
|
||||
|
||||
def quote_voiceover(model_config, *, char_count: int, team=None) -> Quote:
|
||||
"""配音字符阶梯:ceil(chars / chars_per_unit) × points_per_unit,最低 min_units 档。team 传入则乘系数。"""
|
||||
pricing = _pricing_meta(model_config)
|
||||
chars_per_unit = int(pricing.get("chars_per_unit") or 500)
|
||||
points_per_unit = Decimal(str(pricing.get("points_per_unit") or 10))
|
||||
min_units = int(pricing.get("min_units") or 1)
|
||||
base_per_unit = Decimal(str(pricing.get("base_cost_yuan_per_unit") or 0))
|
||||
units = max(math.ceil(max(char_count, 0) / chars_per_unit), min_units)
|
||||
multiplier = team_price_multiplier(team)
|
||||
points = apply_team_price(max((points_per_unit * units).quantize(Decimal("1"), rounding=ROUND_HALF_UP), Decimal("1")), multiplier)
|
||||
return Quote(
|
||||
points=points,
|
||||
base_cost_yuan=base_per_unit * units,
|
||||
meta={"rule": "per_chars", "char_count": char_count, "units": units, "chars_per_unit": chars_per_unit, "price_multiplier": str(multiplier), "rate": str(get_billing_config().points_per_yuan)},
|
||||
)
|
||||
|
||||
|
||||
def quote_video_from_cost(cost_yuan: Decimal, *, tokens: int = 0, multiplier: Decimal | None = None) -> Quote:
|
||||
"""视频通用:¥成本(video_pricing 已 quantize 0.01)× 毛利 → 挂牌积分 → ×团队系数。
|
||||
multiplier 由调用方决定来源:估价=团队当前系数;**按实结算=下单时的快照系数**
|
||||
(学 jimeng:中途改价不影响在途任务,否则预留 buffer 可能被中途涨价击穿)。"""
|
||||
config = get_billing_config()
|
||||
user_yuan = Decimal(str(cost_yuan)) * config.video_margin_multiplier
|
||||
list_points = yuan_to_points(user_yuan, rate=config.points_per_yuan)
|
||||
multiplier = multiplier if multiplier is not None else Decimal("1")
|
||||
points = apply_team_price(list_points, multiplier)
|
||||
return Quote(
|
||||
points=points,
|
||||
base_cost_yuan=Decimal(str(cost_yuan)),
|
||||
meta={
|
||||
"rule": "video_tokens",
|
||||
"tokens": tokens,
|
||||
"margin": str(config.video_margin_multiplier),
|
||||
"rate": str(config.points_per_yuan),
|
||||
"price_multiplier": str(multiplier),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def quote_video_estimate(model_config, *, aspect_ratio: str, resolution: str, duration: int, references: list, team=None) -> tuple[int, Quote]:
|
||||
"""预估:apps/ai/video_pricing 的 ¥成本 × 毛利 → 积分 → ×团队当前系数。返回 (tokens, Quote)。"""
|
||||
from apps.ai.video_pricing import estimate_video_cost
|
||||
|
||||
tokens, cost_yuan = estimate_video_cost(
|
||||
model_config, aspect_ratio=aspect_ratio, resolution=resolution, duration=duration, references=references
|
||||
)
|
||||
return tokens, quote_video_from_cost(cost_yuan, tokens=tokens, multiplier=team_price_multiplier(team))
|
||||
|
||||
|
||||
def quote_video_actual(model_config, *, tokens: int, with_video_ref: bool, resolution: str, multiplier: Decimal | None = None) -> Quote:
|
||||
"""按真实 usage.total_tokens 结算(与预估同一张成本表+同一毛利)。
|
||||
multiplier 必须传**下单时的快照系数**(request_payload.price_multiplier),不读团队当前值。"""
|
||||
from apps.ai.video_pricing import tokens_to_cost
|
||||
|
||||
cost_yuan = tokens_to_cost(model_config, tokens, with_video_ref=with_video_ref, resolution=resolution)
|
||||
return quote_video_from_cost(cost_yuan, tokens=tokens, multiplier=multiplier)
|
||||
|
||||
|
||||
def video_reserve_amount(points: Decimal) -> Decimal:
|
||||
"""视频预留额 = 预估积分 × buffer(真实 tokens 可能略超预估;ledger 禁超预留扣费)。"""
|
||||
buffer = get_billing_config().video_reserve_buffer
|
||||
return (points * buffer).quantize(Decimal("1"), rounding=ROUND_HALF_UP)
|
||||
@@ -34,20 +34,44 @@ def _enforce_member_monthly_limit(*, team, user, amount: Decimal) -> None:
|
||||
day_start = now.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
charged = _charged_since(day_start)
|
||||
if charged + reserved + amount > day_limit:
|
||||
raise ValueError(f"成员今日额度不足:每日限额 ¥{day_limit},今日已用 ¥{charged}(另有在途 ¥{reserved})")
|
||||
raise ValueError(f"成员今日额度不足:每日限额 {day_limit} 积分,今日已用 {charged}(另有在途 {reserved})")
|
||||
|
||||
month_limit = member.monthly_credit_limit or Decimal("0")
|
||||
if month_limit > 0:
|
||||
month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
||||
charged = _charged_since(month_start)
|
||||
if charged + reserved + amount > month_limit:
|
||||
raise ValueError(f"成员本月额度不足:限额 ¥{month_limit},本月已用 ¥{charged}(另有在途 ¥{reserved})")
|
||||
raise ValueError(f"成员本月额度不足:限额 {month_limit} 积分,本月已用 {charged}(另有在途 {reserved})")
|
||||
|
||||
total_limit = member.total_credit_limit or Decimal("0")
|
||||
if total_limit > 0:
|
||||
charged = _charged_since(None)
|
||||
if charged + reserved + amount > total_limit:
|
||||
raise ValueError(f"成员累计额度不足:总额度 ¥{total_limit},累计已用 ¥{charged}(另有在途 ¥{reserved})")
|
||||
raise ValueError(f"成员累计额度不足:总额度 {total_limit} 积分,累计已用 {charged}(另有在途 {reserved})")
|
||||
|
||||
|
||||
|
||||
def _enforce_team_monthly_limit(*, team, amount: Decimal) -> None:
|
||||
"""Team.monthly_credit_limit 真管控(此前是装饰性字段:团队页可配,reserve 从不读 → 形同虚设)。
|
||||
语义:None/-1/0 均不管控,仅正数为硬上限 —— 0 必须视为「未设置/不限」而非「冻结」:
|
||||
前端把 null 映射成 0 展示、成员限额也是 0=不限,存量数据里存在 0 行,若 0=冻结会在
|
||||
部署当天把这些团队全体静默冻结(review 确认的语义冲突)。冻结消费请用 QuotaPolicy(monthly=0)。
|
||||
口径与成员/QuotaPolicy 月度一致:自然月团队 CHARGE 合计 + 团队全部 ACTIVE 预留 + 本次。
|
||||
调用方(reserve_credit)已持 account 行锁,团队内 reserve 串行化,无竞态。"""
|
||||
limit = team.monthly_credit_limit
|
||||
if limit is None or limit <= 0:
|
||||
return
|
||||
month_start = timezone.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
||||
charged = (
|
||||
CreditLedger.objects.filter(team=team, ledger_type=CreditLedger.Type.CHARGE, created_at__gte=month_start)
|
||||
.aggregate(s=Sum("amount"))["s"] or Decimal("0")
|
||||
)
|
||||
reserved = (
|
||||
CreditReservation.objects.filter(team=team, status=CreditReservation.Status.ACTIVE)
|
||||
.aggregate(s=Sum("amount"))["s"] or Decimal("0")
|
||||
)
|
||||
if charged + reserved + amount > limit:
|
||||
raise ValueError(f"团队本月额度不足:限额 {limit} 积分,本月已用 {charged}(另有在途 {reserved})")
|
||||
|
||||
|
||||
def _enforce_quota_policy(*, team, project, amount: Decimal) -> None:
|
||||
@@ -61,7 +85,7 @@ def _enforce_quota_policy(*, team, project, amount: Decimal) -> None:
|
||||
if policy is None:
|
||||
return
|
||||
if policy.per_task_limit is not None and amount > policy.per_task_limit:
|
||||
raise ValueError(f"单任务额度超限:上限 ¥{policy.per_task_limit}")
|
||||
raise ValueError(f"单任务额度超限:上限 {policy.per_task_limit} 积分")
|
||||
now = timezone.now()
|
||||
month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
||||
if policy.monthly_limit is not None:
|
||||
@@ -74,7 +98,7 @@ def _enforce_quota_policy(*, team, project, amount: Decimal) -> None:
|
||||
.aggregate(s=Sum("amount"))["s"] or Decimal("0")
|
||||
)
|
||||
if charged + reserved + amount > policy.monthly_limit:
|
||||
raise ValueError(f"团队本月额度超限:上限 ¥{policy.monthly_limit}")
|
||||
raise ValueError(f"团队本月额度超限:上限 {policy.monthly_limit} 积分")
|
||||
if policy.project_limit is not None and project is not None:
|
||||
p_charged = (
|
||||
CreditLedger.objects.filter(team=team, project=project, ledger_type=CreditLedger.Type.CHARGE)
|
||||
@@ -85,7 +109,7 @@ def _enforce_quota_policy(*, team, project, amount: Decimal) -> None:
|
||||
.aggregate(s=Sum("amount"))["s"] or Decimal("0")
|
||||
)
|
||||
if p_charged + p_reserved + amount > policy.project_limit:
|
||||
raise ValueError(f"项目额度超限:上限 ¥{policy.project_limit}")
|
||||
raise ValueError(f"项目额度超限:上限 {policy.project_limit} 积分")
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
@@ -116,6 +140,7 @@ def reserve_credit(*, team, user, task, amount: Decimal) -> CreditReservation:
|
||||
if available < amount:
|
||||
raise ValueError("insufficient credit")
|
||||
_enforce_member_monthly_limit(team=team, user=user, amount=amount)
|
||||
_enforce_team_monthly_limit(team=team, amount=amount)
|
||||
_enforce_quota_policy(team=team, project=task.project, amount=amount)
|
||||
|
||||
account.reserved_balance += amount
|
||||
|
||||
@@ -190,8 +190,10 @@ class RechargePermissionTests(TestCase):
|
||||
client.force_authenticate(self.owner)
|
||||
response = client.post("/api/billing/recharge/", {"amount": "100"}, format="json")
|
||||
self.assertEqual(response.status_code, 201)
|
||||
# 积分制:支付 ¥100 × 10 = 到账 1000 积分;响应带换算快照
|
||||
self.assertEqual(response.json()["credited_points"], "1000")
|
||||
account = CreditAccount.objects.get(team=self.team)
|
||||
self.assertEqual(account.balance, Decimal("100.0000"))
|
||||
self.assertEqual(account.balance, Decimal("1000.0000"))
|
||||
|
||||
def test_member_cannot_recharge(self):
|
||||
client = APIClient()
|
||||
@@ -201,3 +203,287 @@ class RechargePermissionTests(TestCase):
|
||||
account = CreditAccount.objects.get(team=self.team)
|
||||
self.assertEqual(account.balance, Decimal("0.0000")) # 余额未变,越权被拦
|
||||
|
||||
|
||||
|
||||
class PricingEngineTests(TestCase):
|
||||
"""计价引擎(apps/billing/pricing):flat 回落 / 配音阶梯 / 视频毛利与取整 / 最低 1 积分。"""
|
||||
|
||||
def setUp(self):
|
||||
from apps.billing.pricing import invalidate_billing_config_cache
|
||||
|
||||
invalidate_billing_config_cache()
|
||||
provider, _ = ModelProvider.objects.get_or_create(name="volcengine", defaults={"display_name": "V"})
|
||||
self.text_model = ModelConfig.objects.create(
|
||||
provider=provider, name="pe-text", display_name="T", capability=ModelConfig.Capability.TEXT,
|
||||
unit_price=Decimal("10"), metadata={"pricing": {"mode": "flat", "base_cost_yuan": 0.1}},
|
||||
)
|
||||
self.audio_model = ModelConfig.objects.create(
|
||||
provider=provider, name="pe-audio", display_name="A", capability=ModelConfig.Capability.AUDIO,
|
||||
metadata={"pricing": {"mode": "per_chars", "chars_per_unit": 500, "points_per_unit": 10, "min_units": 1, "base_cost_yuan_per_unit": 0.25}},
|
||||
)
|
||||
|
||||
def test_flat_uses_unit_price_as_points(self):
|
||||
from apps.billing.pricing import quote_flat
|
||||
|
||||
quote = quote_flat(self.text_model)
|
||||
self.assertEqual(quote.points, Decimal("10"))
|
||||
self.assertEqual(quote.base_cost_yuan, Decimal("0.1"))
|
||||
# 多单位(单图批量按张)
|
||||
self.assertEqual(quote_flat(self.text_model, units=3).points, Decimal("30"))
|
||||
|
||||
def test_flat_fallback_when_unit_price_unset(self):
|
||||
from apps.billing.pricing import FLAT_FALLBACK_POINTS, quote_flat
|
||||
|
||||
self.text_model.unit_price = Decimal("0")
|
||||
quote = quote_flat(self.text_model)
|
||||
self.assertEqual(quote.points, FLAT_FALLBACK_POINTS)
|
||||
|
||||
def test_voiceover_char_tiers(self):
|
||||
from apps.billing.pricing import quote_voiceover
|
||||
|
||||
cases = {0: 1, 1: 1, 499: 1, 500: 1, 501: 2, 1500: 3, 1501: 4}
|
||||
for chars, units in cases.items():
|
||||
quote = quote_voiceover(self.audio_model, char_count=chars)
|
||||
self.assertEqual(quote.points, Decimal(units * 10), f"chars={chars}")
|
||||
self.assertEqual(quote.base_cost_yuan, Decimal("0.25") * units, f"chars={chars}")
|
||||
|
||||
def test_video_margin_and_rounding(self):
|
||||
from apps.billing.pricing import get_billing_config, quote_video_from_cost
|
||||
|
||||
cfg = get_billing_config()
|
||||
self.assertEqual(cfg.video_margin_multiplier, Decimal("1.50")) # 首发 ×1.5(商业决策)
|
||||
# ¥1.85 成本 × 1.5 × 10 = 27.75 → HALF_UP → 28 积分;base_cost 保留 ¥ 原值
|
||||
quote = quote_video_from_cost(Decimal("1.85"))
|
||||
self.assertEqual(quote.points, Decimal("28"))
|
||||
self.assertEqual(quote.base_cost_yuan, Decimal("1.85"))
|
||||
|
||||
def test_minimum_one_point(self):
|
||||
from apps.billing.pricing import quote_video_from_cost, yuan_to_points
|
||||
|
||||
self.assertEqual(quote_video_from_cost(Decimal("0.001")).points, Decimal("1"))
|
||||
self.assertEqual(yuan_to_points(Decimal("0")), Decimal("0")) # 0 成本不强收
|
||||
|
||||
def test_video_reserve_buffer(self):
|
||||
from apps.billing.pricing import video_reserve_amount
|
||||
|
||||
# 28 × 1.10 = 30.8 → 31 积分
|
||||
self.assertEqual(video_reserve_amount(Decimal("28")), Decimal("31"))
|
||||
|
||||
|
||||
class TeamMonthlyLimitTests(TestCase):
|
||||
"""Team.monthly_credit_limit 真管控:None/-1/0 均不管控,仅正数=硬上限(含在途预留)。"""
|
||||
|
||||
def setUp(self):
|
||||
self.user = User.objects.create_user(username="tml-owner", password="p")
|
||||
self.team = Team.objects.create(name="TML", owner=self.user)
|
||||
TeamMember.objects.create(team=self.team, user=self.user, role=TeamMember.Role.OWNER)
|
||||
CreditAccount.objects.create(team=self.team, balance=Decimal("1000.0000"))
|
||||
provider, _ = ModelProvider.objects.get_or_create(name="volcengine", defaults={"display_name": "V"})
|
||||
self.model = ModelConfig.objects.create(
|
||||
provider=provider, name="tml-m", display_name="M", capability=ModelConfig.Capability.TEXT,
|
||||
)
|
||||
|
||||
def _task(self, key):
|
||||
return AITask.objects.create(
|
||||
team=self.team, created_by=self.user, task_type=AITask.Type.SCRIPT_GENERATION,
|
||||
model_config=self.model, idempotency_key=f"tml-{key}",
|
||||
)
|
||||
|
||||
def _reserve(self, key, amount):
|
||||
return reserve_credit(team=self.team, user=self.user, task=self._task(key), amount=Decimal(str(amount)))
|
||||
|
||||
def test_none_means_no_enforcement(self):
|
||||
self.team.monthly_credit_limit = None
|
||||
self.team.save(update_fields=["monthly_credit_limit"])
|
||||
self._reserve("a", 500) # 不抛
|
||||
|
||||
def test_minus_one_means_unlimited(self):
|
||||
self.team.monthly_credit_limit = Decimal("-1")
|
||||
self.team.save(update_fields=["monthly_credit_limit"])
|
||||
self._reserve("b", 500) # 不抛
|
||||
|
||||
def test_zero_means_no_enforcement(self):
|
||||
# 0 必须不管控:前端把 null 映射成 0、成员限额 0=不限,存量 0 行若被解释成「冻结」
|
||||
# 会在部署当天静默冻结整个团队(review 确认)。冻结消费用 QuotaPolicy(monthly=0)。
|
||||
self.team.monthly_credit_limit = Decimal("0")
|
||||
self.team.save(update_fields=["monthly_credit_limit"])
|
||||
self._reserve("c", 500) # 不抛
|
||||
|
||||
def test_positive_limit_counts_active_reservations(self):
|
||||
self.team.monthly_credit_limit = Decimal("100")
|
||||
self.team.save(update_fields=["monthly_credit_limit"])
|
||||
reservation = self._reserve("d", 60) # 在途 60
|
||||
with self.assertRaisesMessage(ValueError, "团队本月额度不足"):
|
||||
self._reserve("e", 50) # 60+50 > 100
|
||||
# 结算 40(在途转已扣 40,当月已用 40):40+50 <= 100 → 放行
|
||||
charge_reserved_credit(reservation=reservation, actual_amount=Decimal("40"))
|
||||
self._reserve("f", 50)
|
||||
|
||||
|
||||
class RechargePointsTests(TestCase):
|
||||
"""充值积分语义:¥ × points_per_yuan + bonus_points;metadata 快照支付金额与汇率。"""
|
||||
|
||||
def setUp(self):
|
||||
self.owner = User.objects.create_user(username="rp-owner", password="p")
|
||||
self.team = Team.objects.create(name="RP", owner=self.owner)
|
||||
TeamMember.objects.create(team=self.team, user=self.owner, role=TeamMember.Role.OWNER)
|
||||
CreditAccount.objects.create(team=self.team, balance=Decimal("0"))
|
||||
self.client = APIClient()
|
||||
self.client.force_authenticate(self.owner)
|
||||
|
||||
def test_recharge_converts_yuan_to_points_with_bonus(self):
|
||||
resp = self.client.post("/api/billing/recharge/", {"amount": "500", "bonus_points": "300"}, format="json")
|
||||
self.assertEqual(resp.status_code, 201)
|
||||
body = resp.json()
|
||||
self.assertEqual(body["credited_points"], "5300")
|
||||
self.assertEqual(body["points_per_yuan"], "10.00")
|
||||
account = CreditAccount.objects.get(team=self.team)
|
||||
self.assertEqual(account.balance, Decimal("5300"))
|
||||
ledger = CreditLedger.objects.get(team=self.team, ledger_type=CreditLedger.Type.RECHARGE)
|
||||
# 真实支付金额只能从 metadata 读(老流水不可用 amount÷rate 反推)
|
||||
self.assertEqual(ledger.metadata["paid_amount"], "500")
|
||||
self.assertEqual(ledger.metadata["bonus_points"], "300")
|
||||
self.assertEqual(ledger.metadata["points_per_yuan"], "10.00")
|
||||
|
||||
def test_legacy_bonus_yuan_converted(self):
|
||||
resp = self.client.post("/api/billing/recharge/", {"amount": "100", "bonus": "20"}, format="json")
|
||||
self.assertEqual(resp.status_code, 201)
|
||||
self.assertEqual(resp.json()["credited_points"], "1200") # 100×10 + 20×10
|
||||
|
||||
|
||||
class BillingConfigEndpointTests(TestCase):
|
||||
"""公共 GET /api/billing/config/(鉴权)+ admin PATCH(权限/生效/审计)。"""
|
||||
|
||||
def setUp(self):
|
||||
from apps.billing.pricing import invalidate_billing_config_cache
|
||||
|
||||
invalidate_billing_config_cache()
|
||||
self.user = User.objects.create_user(username="bc-user", password="p")
|
||||
self.team = Team.objects.create(name="BC", owner=self.user)
|
||||
TeamMember.objects.create(team=self.team, user=self.user, role=TeamMember.Role.OWNER)
|
||||
self.admin = User.objects.create_user(username="bc-admin", password="p", is_platform_admin=True)
|
||||
|
||||
def test_public_config_requires_auth(self):
|
||||
self.assertEqual(APIClient().get("/api/billing/config/").status_code, 401)
|
||||
client = APIClient()
|
||||
client.force_authenticate(self.user)
|
||||
body = client.get("/api/billing/config/").json()
|
||||
self.assertEqual(body["currency_unit"], "points")
|
||||
self.assertEqual(body["points_per_yuan"], "10.00")
|
||||
self.assertEqual(body["video_margin_multiplier"], "1.50")
|
||||
|
||||
def test_admin_patch_updates_and_takes_effect(self):
|
||||
from apps.billing.pricing import get_billing_config, invalidate_billing_config_cache
|
||||
|
||||
client = APIClient()
|
||||
client.force_authenticate(self.admin)
|
||||
resp = client.patch("/api/admin/billing-config/", {"video_margin_multiplier": "2.5"}, format="json")
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
invalidate_billing_config_cache()
|
||||
self.assertEqual(get_billing_config().video_margin_multiplier, Decimal("2.5"))
|
||||
# 非超管 PATCH 拒绝
|
||||
stranger = APIClient()
|
||||
stranger.force_authenticate(self.user)
|
||||
self.assertEqual(stranger.patch("/api/admin/billing-config/", {"points_per_yuan": "1"}, format="json").status_code, 403)
|
||||
|
||||
|
||||
class TeamPriceMultiplierTests(TestCase):
|
||||
"""团队差异化调价:全类型统一系数、两步取整、视频结算用下单快照(中途改价不影响在途)。"""
|
||||
|
||||
def setUp(self):
|
||||
from apps.billing.pricing import invalidate_billing_config_cache
|
||||
|
||||
invalidate_billing_config_cache()
|
||||
self.user = User.objects.create_user(username="tpm-owner", password="p")
|
||||
self.team = Team.objects.create(name="TPM", owner=self.user, price_multiplier=Decimal("0.80"))
|
||||
TeamMember.objects.create(team=self.team, user=self.user, role=TeamMember.Role.OWNER)
|
||||
CreditAccount.objects.create(team=self.team, balance=Decimal("10000"))
|
||||
provider, _ = ModelProvider.objects.get_or_create(name="volcengine", defaults={"display_name": "V"})
|
||||
self.image_model = ModelConfig.objects.create(
|
||||
provider=provider, name="tpm-img", display_name="I", capability=ModelConfig.Capability.IMAGE,
|
||||
unit_price=Decimal("20"),
|
||||
)
|
||||
|
||||
def test_flat_applies_multiplier(self):
|
||||
from apps.billing.pricing import quote_flat
|
||||
|
||||
# 20 × 0.8 = 16;不传 team = 标准价(零回归)
|
||||
quote = quote_flat(self.image_model, team=self.team)
|
||||
self.assertEqual(quote.points, Decimal("16"))
|
||||
self.assertEqual(quote_flat(self.image_model).points, Decimal("20"))
|
||||
# meta.rate 是汇率快照契约:create_ai_task 落 payload.points_per_yuan_snapshot,毛利报表用它防汇率漂移
|
||||
self.assertEqual(Decimal(quote.meta["rate"]), Decimal("10"))
|
||||
|
||||
def test_two_step_rounding(self):
|
||||
from apps.billing.pricing import quote_video_from_cost
|
||||
|
||||
# 挂牌:¥1.85×1.5×10=27.75→28;再 ×0.8=22.4→22(两步取整,与前端镜像逐字一致)
|
||||
quote = quote_video_from_cost(Decimal("1.85"), multiplier=Decimal("0.80"))
|
||||
self.assertEqual(quote.points, Decimal("22"))
|
||||
self.assertEqual(quote.meta["price_multiplier"], "0.80")
|
||||
|
||||
def test_minimum_one_point_after_discount(self):
|
||||
from apps.billing.pricing import apply_team_price
|
||||
|
||||
self.assertEqual(apply_team_price(Decimal("1"), Decimal("0.10")), Decimal("1"))
|
||||
|
||||
def test_invalid_multiplier_falls_back_to_one(self):
|
||||
from apps.billing.pricing import team_price_multiplier
|
||||
|
||||
self.team.price_multiplier = Decimal("0")
|
||||
self.assertEqual(team_price_multiplier(self.team), Decimal("1"))
|
||||
self.assertEqual(team_price_multiplier(None), Decimal("1"))
|
||||
|
||||
def test_video_settle_uses_snapshot_not_current(self):
|
||||
"""下单时 0.8 → 中途管理员改成 2.0 → 结算仍按 0.8 快照(jimeng 同款纪律)。"""
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from apps.ai.free_video import finalize_free_video, submit_free_video
|
||||
from apps.ai.models import AITask
|
||||
|
||||
provider = MagicMock()
|
||||
provider.create_video_task.return_value = {"id": "ark-tpm", "status": "queued"}
|
||||
provider.extract_first_media_url.return_value = "http://x/v.mp4"
|
||||
patch("apps.ai.services.build_provider", return_value=provider).start()
|
||||
patch("apps.ai.tasks.poll_free_video_task.apply_async").start()
|
||||
patch("apps.ai.free_video._store_free_video_media").start()
|
||||
self.addCleanup(patch.stopall)
|
||||
|
||||
task = submit_free_video(team=self.team, user=self.user, params={
|
||||
"prompt": "测试", "mode": "universal", "model": "doubao-seedance-2-0-260128",
|
||||
"aspect_ratio": "16:9", "resolution": "480p", "duration": 4, "references": [],
|
||||
})
|
||||
self.assertEqual(task.request_payload["price_multiplier"], "0.80")
|
||||
# 挂牌 28 × 0.8 = 22.4 → 22 积分
|
||||
self.assertEqual(task.estimated_cost, Decimal("22"))
|
||||
# 中途涨价到 2.0(在途任务不受影响)
|
||||
self.team.price_multiplier = Decimal("2.00")
|
||||
self.team.save(update_fields=["price_multiplier"])
|
||||
provider.poll_video_task.return_value = {"status": "succeeded", "usage": {"total_tokens": 30000}}
|
||||
task = finalize_free_video(task=task)
|
||||
# 真实 30000 tokens:挂牌 1.38×1.5×10=20.7→21;×快照0.8=16.8→17(而非 ×2.0=42)
|
||||
self.assertEqual(task.status, AITask.Status.SUCCEEDED)
|
||||
self.assertEqual(task.actual_cost, Decimal("17"))
|
||||
|
||||
def test_admin_pricing_endpoint(self):
|
||||
admin = User.objects.create_user(username="tpm-admin", password="p", is_platform_admin=True)
|
||||
client = APIClient()
|
||||
client.force_authenticate(admin)
|
||||
resp = client.post(f"/api/admin/teams/{self.team.id}/pricing/", {"price_multiplier": "0.9"}, format="json")
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
self.assertEqual(resp.json()["price_multiplier"], "0.90")
|
||||
# 三位小数走 HALF_UP 而非银行家舍入:0.125 → 0.13(quantize 默认会给 0.12)
|
||||
resp = client.post(f"/api/admin/teams/{self.team.id}/pricing/", {"price_multiplier": "0.125"}, format="json")
|
||||
self.assertEqual(resp.json()["price_multiplier"], "0.13")
|
||||
# 边界与非法值
|
||||
self.assertEqual(client.post(f"/api/admin/teams/{self.team.id}/pricing/", {"price_multiplier": "0.05"}, format="json").status_code, 400)
|
||||
self.assertEqual(client.post(f"/api/admin/teams/{self.team.id}/pricing/", {"price_multiplier": "NaN"}, format="json").status_code, 400)
|
||||
# 非超管 403
|
||||
stranger = APIClient()
|
||||
stranger.force_authenticate(self.user)
|
||||
self.assertEqual(stranger.post(f"/api/admin/teams/{self.team.id}/pricing/", {"price_multiplier": "0.5"}, format="json").status_code, 403)
|
||||
|
||||
def test_public_config_returns_team_multiplier(self):
|
||||
client = APIClient()
|
||||
client.force_authenticate(self.user)
|
||||
self.assertEqual(client.get("/api/billing/config/").json()["team_price_multiplier"], "0.80")
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import ledgers, recharge, summary, trend
|
||||
from .views import config, ledgers, recharge, summary, trend
|
||||
|
||||
urlpatterns = [
|
||||
path("summary/", summary, name="billing-summary"),
|
||||
path("ledgers/", ledgers, name="billing-ledgers"),
|
||||
path("recharge/", recharge, name="billing-recharge"),
|
||||
path("trend/", trend, name="billing-trend"),
|
||||
path("config/", config, name="billing-config"),
|
||||
]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from datetime import timedelta
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
|
||||
|
||||
from django.db import transaction
|
||||
from django.db.models import Sum
|
||||
@@ -14,17 +14,21 @@ from apps.ai.models import AITask
|
||||
from apps.common.api import can_manage_team, get_current_team
|
||||
|
||||
from .models import CreditAccount, CreditLedger
|
||||
from .pricing import get_billing_config, team_price_multiplier
|
||||
from .serializers import CreditAccountSerializer, CreditLedgerSerializer
|
||||
|
||||
# AITask.task_type → 账户页「按阶段分布」的 4 个聚合桶
|
||||
_STAGE_BUCKET = {
|
||||
AITask.Type.SCRIPT_GENERATION: "script",
|
||||
AITask.Type.SCRIPT_OPTIMIZATION: "script",
|
||||
AITask.Type.ENTITY_EXTRACTION: "script",
|
||||
AITask.Type.PRODUCT_IMAGE: "base",
|
||||
AITask.Type.PERSON_IMAGE: "base",
|
||||
AITask.Type.SCENE_IMAGE: "base",
|
||||
AITask.Type.STORYBOARD: "storyboard",
|
||||
AITask.Type.VIDEO_SEGMENT: "video",
|
||||
AITask.Type.VOICEOVER: "video",
|
||||
AITask.Type.FREE_VIDEO: "video",
|
||||
AITask.Type.EXPORT: "video",
|
||||
}
|
||||
|
||||
@@ -90,24 +94,59 @@ def ledgers(request):
|
||||
)
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def config(request):
|
||||
"""计费公共配置:前端预估(自由创作 token 预估 / 图片单价 / 充值到账换算)统一读这里,
|
||||
与后端计价引擎同一来源,杜绝两端口径漂移。"""
|
||||
cfg = get_billing_config()
|
||||
# 团队价格系数(差异化调价):预估所见即所扣。平台超管无团队 → 回落 1。
|
||||
try:
|
||||
multiplier = team_price_multiplier(get_current_team(request.user))
|
||||
except Exception: # noqa: BLE001 — 无团队(平台超管)等情况一律按标准价
|
||||
multiplier = Decimal("1")
|
||||
return Response(
|
||||
{
|
||||
"currency_unit": "points",
|
||||
"points_per_yuan": str(cfg.points_per_yuan),
|
||||
"video_margin_multiplier": str(cfg.video_margin_multiplier),
|
||||
"video_reserve_buffer": str(cfg.video_reserve_buffer),
|
||||
"team_price_multiplier": str(multiplier),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@api_view(["POST"])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def recharge(request):
|
||||
"""充值:amount 是真实支付人民币(¥),到账积分 = ¥ × points_per_yuan + bonus_points(赠送积分)。
|
||||
metadata 快照 paid_amount(¥)与当时汇率——老流水的支付金额只能从这里读,不可拿 amount÷rate 反推。"""
|
||||
team = get_current_team(request.user)
|
||||
# 充值是团队资金操作:仅 owner/admin 可发起,普通成员/访客一律拒绝
|
||||
if not can_manage_team(request.user, team):
|
||||
return Response({"detail": "permission denied"}, status=status.HTTP_403_FORBIDDEN)
|
||||
try:
|
||||
amount = Decimal(str(request.data.get("amount", "0")))
|
||||
bonus = Decimal(str(request.data.get("bonus", "0")))
|
||||
# 兼容旧键 bonus(历史前端按 ¥ 传):按汇率折成积分;新契约 bonus_points 直接是积分
|
||||
if request.data.get("bonus_points") is not None:
|
||||
bonus_points = Decimal(str(request.data.get("bonus_points")))
|
||||
else:
|
||||
bonus_yuan = Decimal(str(request.data.get("bonus", "0")))
|
||||
bonus_points = bonus_yuan * get_billing_config().points_per_yuan
|
||||
except (InvalidOperation, TypeError):
|
||||
return Response({"detail": "invalid amount"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if amount <= 0:
|
||||
return Response({"detail": "amount must be positive"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if bonus < 0:
|
||||
# Decimal("NaN")/("Infinity") 构造不抛,后续比较/入库才炸 500 → 显式拦(review 确认)
|
||||
if not amount.is_finite() or not bonus_points.is_finite():
|
||||
return Response({"detail": "invalid amount"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
# 最低 ¥1:分币级支付按 HALF_UP 会到账 0 积分(付了钱拿 0,投诉必至);上限防长尾脏数据
|
||||
if amount < Decimal("1") or amount > Decimal("1000000"):
|
||||
return Response({"detail": "充值金额需在 ¥1 ~ ¥1,000,000 之间"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if bonus_points < 0:
|
||||
return Response({"detail": "bonus cannot be negative"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
channel = str(request.data.get("channel") or "manual")[:32]
|
||||
credited = amount + bonus
|
||||
rate = get_billing_config().points_per_yuan
|
||||
# 取整与计价引擎同口径 HALF_UP(quantize 默认是银行家舍入,¥0.25×10 会得 2 而非 3)
|
||||
credited = (amount * rate + bonus_points).quantize(Decimal("1"), rounding=ROUND_HALF_UP)
|
||||
with transaction.atomic():
|
||||
account, _ = CreditAccount.objects.select_for_update().get_or_create(team=team)
|
||||
account.balance += credited
|
||||
@@ -119,12 +158,19 @@ def recharge(request):
|
||||
amount=credited,
|
||||
balance_after=account.balance,
|
||||
reason="团队充值",
|
||||
metadata={"channel": channel, "paid_amount": str(amount), "bonus": str(bonus)},
|
||||
metadata={
|
||||
"channel": channel,
|
||||
"paid_amount": str(amount),
|
||||
"points_per_yuan": str(rate),
|
||||
"bonus_points": str(bonus_points),
|
||||
},
|
||||
)
|
||||
return Response(
|
||||
{
|
||||
"account": CreditAccountSerializer(account).data,
|
||||
"ledger": CreditLedgerSerializer(ledger).data,
|
||||
"credited_points": str(credited),
|
||||
"points_per_yuan": str(rate),
|
||||
},
|
||||
status=status.HTTP_201_CREATED,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user