144 lines
6.1 KiB
C#
144 lines
6.1 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Tiku.Api.Contracts;
|
|
using Tiku.Application.PlatformBilling;
|
|
using Tiku.Application.Security;
|
|
using Tiku.Domain.Platform;
|
|
|
|
namespace Tiku.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Tags("租户端-账务")]
|
|
[Route("api/tenant/billing")]
|
|
[Authorize(Policy = BackendPermissions.TenantBillingManage)]
|
|
public sealed class TenantBillingController(
|
|
ITenantBillingService billingService,
|
|
ICurrentAccessContext accessContext) : ControllerBase
|
|
{
|
|
[HttpGet("catalog")]
|
|
[EndpointSummary("查询租户可购买 SaaS 目录")]
|
|
public async Task<TenantBillingCatalog> Catalog(CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.GetCatalogAsync(await ActorAsync(cancellationToken), cancellationToken);
|
|
}
|
|
|
|
[HttpPost("quotes")]
|
|
[EndpointSummary("创建租户账务报价")]
|
|
public async Task<PlatformBillingQuoteView> Quote(CreatePlatformBillingQuoteDto request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.CreateQuoteAsync(await ActorAsync(cancellationToken), request.ToCommand(),
|
|
cancellationToken);
|
|
}
|
|
|
|
[HttpPost("orders")]
|
|
[EndpointSummary("创建租户账务订单")]
|
|
public async Task<PlatformBillingOrderView> CreateOrder(CreatePlatformBillingOrderDto request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.CreateOrderAsync(await ActorAsync(cancellationToken), request.ToCommand(),
|
|
cancellationToken);
|
|
}
|
|
|
|
[HttpPost("orders/{orderNo}/payments")]
|
|
[EndpointSummary("创建租户账务订单支付")]
|
|
public async Task<PlatformBillingPaymentView> CreatePayment(string orderNo, CreatePlatformBillingPaymentDto request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.CreatePaymentAsync(await ActorAsync(cancellationToken), request.ToCommand(orderNo),
|
|
cancellationToken);
|
|
}
|
|
|
|
[HttpGet("orders")]
|
|
[EndpointSummary("查询租户账务订单")]
|
|
public async Task<IReadOnlyCollection<PlatformBillingOrderView>> Orders(int limit = 100,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return await billingService.GetOrdersAsync(await ActorAsync(cancellationToken), limit, cancellationToken);
|
|
}
|
|
|
|
[HttpPost("orders/{orderNo}/cancel")]
|
|
[EndpointSummary("取消待支付租户账务订单")]
|
|
public async Task<PlatformBillingOrderView> CancelOrder(string orderNo, CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.CancelOrderAsync(await ActorAsync(cancellationToken), orderNo, cancellationToken);
|
|
}
|
|
|
|
[HttpGet("orders/{orderNo}")]
|
|
[EndpointSummary("查询租户账务订单详情")]
|
|
public async Task<PlatformBillingOrderView> Order(string orderNo, CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.GetOrderAsync(await ActorAsync(cancellationToken), orderNo, cancellationToken);
|
|
}
|
|
|
|
[HttpGet("subscription")]
|
|
[EndpointSummary("查询当前租户 SaaS 订阅")]
|
|
public async Task<TenantSubscriptionView?> Subscription(CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.GetSubscriptionAsync(await ActorAsync(cancellationToken), cancellationToken);
|
|
}
|
|
|
|
[HttpPost("subscription/change")]
|
|
[EndpointSummary("变更当前租户 SaaS 订阅")]
|
|
public async Task<PlatformBillingOrderView> Change(ChangeTenantSubscriptionDto request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.ChangeSubscriptionAsync(await ActorAsync(cancellationToken), request.ToCommand(),
|
|
request.IdempotencyKey, cancellationToken);
|
|
}
|
|
|
|
[HttpPost("subscription/renew")]
|
|
[EndpointSummary("续费当前租户 SaaS 订阅")]
|
|
public async Task<PlatformBillingOrderView> Renew(IdempotentTenantBillingDto request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.RenewSubscriptionAsync(await ActorAsync(cancellationToken), request.IdempotencyKey,
|
|
cancellationToken);
|
|
}
|
|
|
|
[HttpPost("subscription/cancel")]
|
|
[EndpointSummary("取消当前租户 SaaS 订阅")]
|
|
public async Task<TenantSubscriptionView> Cancel(CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.CancelSubscriptionAsync(await ActorAsync(cancellationToken), cancellationToken);
|
|
}
|
|
|
|
[HttpGet("usage")]
|
|
[EndpointSummary("查询当前租户功能用量")]
|
|
public async Task<IReadOnlyCollection<FeatureQuotaSnapshot>> Usage(CancellationToken cancellationToken)
|
|
{
|
|
return await billingService.GetUsageAsync(await ActorAsync(cancellationToken), cancellationToken);
|
|
}
|
|
|
|
[HttpGet("invoices")]
|
|
[EndpointSummary("查询当前租户发票")]
|
|
public async Task<IReadOnlyCollection<PlatformBillingInvoice>> Invoices(int limit = 100,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return await billingService.GetInvoicesAsync(await ActorAsync(cancellationToken), limit, cancellationToken);
|
|
}
|
|
|
|
[HttpGet("receivables")]
|
|
[EndpointSummary("查询当前租户应收账单")]
|
|
public async Task<IReadOnlyCollection<TenantReceivableView>> Receivables(int limit = 100,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return await billingService.GetReceivablesAsync(await ActorAsync(cancellationToken), limit, cancellationToken);
|
|
}
|
|
|
|
[HttpGet("refunds")]
|
|
[EndpointSummary("查询当前租户 SaaS 退款")]
|
|
public async Task<IReadOnlyCollection<PlatformBillingRefund>> Refunds(int limit = 100,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
return await billingService.GetRefundsAsync(await ActorAsync(cancellationToken), limit, cancellationToken);
|
|
}
|
|
|
|
private async Task<TenantBillingActor> ActorAsync(CancellationToken cancellationToken)
|
|
{
|
|
var access = await accessContext.GetAsync(cancellationToken);
|
|
return access.UserId is { } userId && access.TenantId is { } tenantId && access.IsCurrentTenantMember
|
|
? new TenantBillingActor(userId, tenantId)
|
|
: throw new PlatformBillingException("Tenant billing actor was not resolved.", "tenant_access_denied");
|
|
}
|
|
} |