Skip to content
bitzorcas
EN

Reference

计费模块

平台计费 — 订阅管理、计划目录、用量记录、配额执行、发票生成和权益解析。

Last updated

PlatformBilling 模块提供 SaaS 原生计费,包含订阅生命周期管理、计划目录、用量跟踪、配额执行和发票生成。

端点

端点方法描述
/api/billing/plansGET列出可用计划
/api/billing/subscriptionsPOST开始订阅
/api/billing/subscriptionsGET列出订阅
/api/billing/subscriptions/{id}GET获取订阅详情
/api/billing/usagePOST记录用量
/api/billing/invoicesPOST生成发票
/api/billing/invoicesGET列出发票
/api/billing/quotaGET检查配额状态

架构

PlatformBillingService
├── IPlatformBillingRepository → SqlSugarPlatformBillingRepository
├── QuotaService → EntitlementResolver
└── IPlatformBillingEventPublisher → NullPlatformBillingEventPublisher
CatalogService
├── ICatalogRepository → SqlSugarCatalogRepository
├── ICatalogEntitlementMappingStore
└── ICatalogEventPublisher

关键实体

实体用途
PlatformBillingPlanEntity计划定义(名称、价格、功能)
PlatformBillingSubscriptionEntity租户的活跃订阅
PlatformBillingInvoiceEntity生成的发票
PlatformBillingUsageRecordEntity用量跟踪记录
CatalogItemEntity目录项(计划、附加项、权益)

订阅生命周期

开始
Active (试用)
Active (付费)
│ │
▼ ▼
Paused Cancelled
│ │
▼ ▼
Resumed Expired

配额执行

public class QuotaCheckResult
{
public bool IsAllowed { get; }
public long Remaining { get; }
public long Used { get; }
public long Limit { get; }
public OverQuotaBehavior Behavior { get; }
}

权益解析

租户 → 活跃订阅 → 计划 → 目录产品 → 权益

每个权益映射到一个配额键(例如 storage.filestickets.openchat.channels)。

用量记录

模块通过计费服务记录用量:

await billingService.RecordUsageAsync(
tenantId: "100",
entitlementKey: "storage.files",
delta: 1);

超额行为

策略描述
Deny以 429/403 阻止操作
Warn允许但发送警告通知
Allow无执行(特权租户)

另见