27303e34eb
Implement 5 subscription endpoints matching frontend subscription.ts: API Endpoints: - GET /api/v1/subscription/current — get current subscription info - GET /api/v1/subscription/billing-records — get billing history - POST /api/v1/subscription/change-plan — upgrade/downgrade plan - POST /api/v1/subscription/cancel — cancel subscription - POST /api/v1/subscription/toggle-auto-renew — toggle auto-renewal Implementation Details: - Created subscription schemas (SubscriptionInfo, BillingRecord, etc.) - Created subscription routes with proper authentication - Integrated with quota_registry for plan limits - Added plan validation (free/standard/pro/enterprise) - Added billing cycle validation (monthly/yearly) - Registered subscription router in api_router Note: Billing records currently returns empty list (TODO: implement billing system) Auto-renew toggle is simulated (TODO: add auto_renew field to User model) All endpoints follow existing patterns from titles/voices/duplication APIs.
93 lines
1.9 KiB
Python
93 lines
1.9 KiB
Python
"""Subscription schemas for API request/response models."""
|
|
from __future__ import annotations
|
|
|
|
from typing import List, Optional
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
# ============ Enums / Types ============
|
|
|
|
class PlanType(str):
|
|
"""套餐类型"""
|
|
FREE = "free"
|
|
STANDARD = "standard"
|
|
PRO = "pro"
|
|
ENTERPRISE = "enterprise"
|
|
|
|
|
|
class SubscriptionStatus(str):
|
|
"""订阅状态"""
|
|
ACTIVE = "active"
|
|
EXPIRED = "expired"
|
|
CANCELLED = "cancelled"
|
|
TRIAL = "trial"
|
|
|
|
|
|
class BillingStatus(str):
|
|
"""账单状态"""
|
|
PAID = "paid"
|
|
PENDING = "pending"
|
|
FAILED = "failed"
|
|
REFUNDED = "refunded"
|
|
|
|
|
|
class BillingCycle(str):
|
|
"""计费周期"""
|
|
MONTHLY = "monthly"
|
|
YEARLY = "yearly"
|
|
|
|
|
|
# ============ Response Schemas ============
|
|
|
|
class SubscriptionInfo(BaseModel):
|
|
"""当前订阅信息"""
|
|
id: str
|
|
plan_id: str
|
|
plan_name: str
|
|
status: str
|
|
billing_cycle: str
|
|
current_period_start: str
|
|
current_period_end: str
|
|
amount: float
|
|
auto_renew: bool
|
|
created_at: str
|
|
|
|
|
|
class BillingRecord(BaseModel):
|
|
"""账单记录"""
|
|
id: str
|
|
plan_name: str
|
|
amount: float
|
|
billing_cycle: str
|
|
status: str
|
|
payment_method: str
|
|
created_at: str
|
|
invoice_url: Optional[str] = None
|
|
|
|
|
|
class ChangePlanResponse(BaseModel):
|
|
"""升级/降级响应"""
|
|
success: bool
|
|
message: str
|
|
new_subscription: Optional[SubscriptionInfo] = None
|
|
|
|
|
|
class SimpleResponse(BaseModel):
|
|
"""简单响应(用于取消订阅、切换自动续费等)"""
|
|
success: bool
|
|
message: str
|
|
|
|
|
|
# ============ Request Schemas ============
|
|
|
|
class ChangePlanRequest(BaseModel):
|
|
"""升级/降级请求"""
|
|
target_plan_id: str = Field(..., description="目标套餐ID")
|
|
billing_cycle: str = Field(..., description="计费周期: monthly/yearly")
|
|
|
|
|
|
class ToggleAutoRenewRequest(BaseModel):
|
|
"""切换自动续费请求"""
|
|
enabled: bool = Field(..., description="是否开启自动续费")
|