forked from gongxuegit/tiku-backend.net
feat: add tenant commerce operations
This commit is contained in:
136
Tiku.Api/Contracts/TenantCommerceDtos.cs
Normal file
136
Tiku.Api/Contracts/TenantCommerceDtos.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json;
|
||||
using Tiku.Application.Commerce;
|
||||
using Tiku.Domain.Commerce;
|
||||
using Tiku.Domain.Common;
|
||||
using Tiku.Domain.Tenancy;
|
||||
|
||||
namespace Tiku.Api.Contracts;
|
||||
|
||||
public sealed class TenantCommerceQueryDto
|
||||
{
|
||||
[StringLength(50)]
|
||||
public string? Provider { get; set; }
|
||||
|
||||
[StringLength(32)]
|
||||
public string? Status { get; set; }
|
||||
|
||||
[Range(1, 200)]
|
||||
public int? Limit { get; set; }
|
||||
}
|
||||
|
||||
public sealed class UpsertPaymentAccountDto
|
||||
{
|
||||
[Required]
|
||||
[StringLength(50)]
|
||||
public string Provider { get; set; } = string.Empty;
|
||||
|
||||
public TenantPaymentMode Mode { get; set; } = TenantPaymentMode.TenantCollect;
|
||||
|
||||
[StringLength(200)]
|
||||
public string? DisplayName { get; set; }
|
||||
|
||||
public TenantPaymentAccountStatus Status { get; set; } = TenantPaymentAccountStatus.Disabled;
|
||||
|
||||
public JsonElement ConfigPublic { get; set; } = JsonDefaults.Object();
|
||||
|
||||
public UpsertPaymentAccountCommand ToCommand()
|
||||
{
|
||||
return new UpsertPaymentAccountCommand(Provider, Mode, DisplayName, Status, ConfigPublic);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class UpsertTenantSecretDto
|
||||
{
|
||||
[Required]
|
||||
[StringLength(80)]
|
||||
public string Purpose { get; set; } = "payment";
|
||||
|
||||
[Required]
|
||||
[StringLength(50)]
|
||||
public string Provider { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[StringLength(120)]
|
||||
public string SecretKey { get; set; } = string.Empty;
|
||||
|
||||
[StringLength(300)]
|
||||
public string SecretRef { get; set; } = string.Empty;
|
||||
|
||||
public TenantSecretStatus Status { get; set; } = TenantSecretStatus.Active;
|
||||
public JsonElement SecretPayload { get; set; } = JsonDefaults.Object();
|
||||
public DateTimeOffset? ExpiresAt { get; set; }
|
||||
|
||||
public UpsertTenantSecretCommand ToCommand()
|
||||
{
|
||||
return new UpsertTenantSecretCommand(
|
||||
Purpose,
|
||||
Provider,
|
||||
SecretKey,
|
||||
SecretRef,
|
||||
Status,
|
||||
SecretPayload,
|
||||
ExpiresAt);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class CreateCodeBatchDto
|
||||
{
|
||||
[Required]
|
||||
[StringLength(200)]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[Range(1, 1000)]
|
||||
public int TotalCount { get; set; }
|
||||
|
||||
[Range(1, 3650)]
|
||||
public int Days { get; set; }
|
||||
|
||||
public Guid? RegionId { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string? SaleType { get; set; }
|
||||
|
||||
[StringLength(100)]
|
||||
public string? Channel { get; set; }
|
||||
|
||||
[Range(0, int.MaxValue)]
|
||||
public int? DefaultUnitPriceCents { get; set; }
|
||||
|
||||
[Range(0, int.MaxValue)]
|
||||
public int? CostPriceCents { get; set; }
|
||||
|
||||
[StringLength(1000)]
|
||||
public string? Remark { get; set; }
|
||||
|
||||
public CreateCodeBatchCommand ToCommand()
|
||||
{
|
||||
return new CreateCodeBatchCommand(
|
||||
Name,
|
||||
TotalCount,
|
||||
Days,
|
||||
RegionId,
|
||||
SaleType,
|
||||
Channel,
|
||||
DefaultUnitPriceCents,
|
||||
CostPriceCents,
|
||||
Remark);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class RedeemActivationCodeDto
|
||||
{
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public string Code { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public Guid UserId { get; set; }
|
||||
|
||||
public Guid? RegionId { get; set; }
|
||||
|
||||
public RedeemActivationCodeCommand ToCommand()
|
||||
{
|
||||
return new RedeemActivationCodeCommand(Code, UserId, RegionId);
|
||||
}
|
||||
}
|
||||
136
Tiku.Api/Controllers/TenantCommerceController.cs
Normal file
136
Tiku.Api/Controllers/TenantCommerceController.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Tiku.Api.Contracts;
|
||||
using Tiku.Application.Commerce;
|
||||
using Tiku.Application.Security;
|
||||
|
||||
namespace Tiku.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authorize(Policy = TikuPolicies.TenantAdmin)]
|
||||
[Produces("application/json")]
|
||||
[Route("api/tenant-commerce")]
|
||||
public sealed class TenantCommerceController(
|
||||
ICommerceAdminService commerceAdminService,
|
||||
ICurrentUser currentUser,
|
||||
ICurrentTenant currentTenant) : ControllerBase
|
||||
{
|
||||
[HttpGet("payment-accounts")]
|
||||
[EndpointSummary("查询租户支付账号")]
|
||||
[ProducesResponseType<IReadOnlyCollection<TenantPaymentAccountItem>>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<IReadOnlyCollection<TenantPaymentAccountItem>>> PaymentAccounts(
|
||||
[FromQuery] TenantCommerceQueryDto query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await commerceAdminService.GetPaymentAccountsAsync(
|
||||
ResolveActor(),
|
||||
ToQuery(query),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpPut("payment-accounts")]
|
||||
[EndpointSummary("新增或更新租户支付账号")]
|
||||
[ProducesResponseType<TenantPaymentAccountItem>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<TenantPaymentAccountItem>> UpsertPaymentAccount(
|
||||
UpsertPaymentAccountDto request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await commerceAdminService.UpsertPaymentAccountAsync(
|
||||
ResolveActor(),
|
||||
request.ToCommand(),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpPut("secrets")]
|
||||
[EndpointSummary("写入或轮换租户密钥")]
|
||||
[ProducesResponseType<TenantSecretItem>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<TenantSecretItem>> UpsertSecret(
|
||||
UpsertTenantSecretDto request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await commerceAdminService.UpsertTenantSecretAsync(
|
||||
ResolveActor(),
|
||||
request.ToCommand(),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpGet("orders")]
|
||||
[EndpointSummary("查询租户订单")]
|
||||
[ProducesResponseType<AdminOrderList>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<AdminOrderList>> Orders(
|
||||
[FromQuery] TenantCommerceQueryDto query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await commerceAdminService.GetOrdersAsync(
|
||||
ResolveActor(),
|
||||
ToQuery(query),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpGet("payments")]
|
||||
[EndpointSummary("查询租户支付记录")]
|
||||
[ProducesResponseType<AdminPaymentList>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<AdminPaymentList>> Payments(
|
||||
[FromQuery] TenantCommerceQueryDto query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await commerceAdminService.GetPaymentsAsync(
|
||||
ResolveActor(),
|
||||
ToQuery(query),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpPost("code-batches")]
|
||||
[EndpointSummary("创建兑换码批次")]
|
||||
[ProducesResponseType<CodeBatchItem>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<CodeBatchItem>> CreateCodeBatch(
|
||||
CreateCodeBatchDto request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await commerceAdminService.CreateCodeBatchAsync(
|
||||
ResolveActor(),
|
||||
request.ToCommand(),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpGet("activation-codes")]
|
||||
[EndpointSummary("查询兑换码")]
|
||||
[ProducesResponseType<ActivationCodeList>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<ActivationCodeList>> ActivationCodes(
|
||||
[FromQuery] TenantCommerceQueryDto query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await commerceAdminService.GetActivationCodesAsync(
|
||||
ResolveActor(),
|
||||
ToQuery(query),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
[HttpPost("activation-codes/redeem")]
|
||||
[EndpointSummary("后台核销兑换码")]
|
||||
[ProducesResponseType<ActivationCodeItem>(StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<ActivationCodeItem>> RedeemActivationCode(
|
||||
RedeemActivationCodeDto request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
return Ok(await commerceAdminService.RedeemActivationCodeAsync(
|
||||
ResolveActor(),
|
||||
request.ToCommand(),
|
||||
cancellationToken));
|
||||
}
|
||||
|
||||
private CommerceAdminActor ResolveActor()
|
||||
{
|
||||
if (currentTenant.TenantId is null || currentUser.UserId is null)
|
||||
{
|
||||
throw new CommerceException("Tenant commerce actor was not resolved.", "tenant_admin_access_denied");
|
||||
}
|
||||
|
||||
return new CommerceAdminActor(currentTenant.TenantId.Value, currentUser.UserId.Value);
|
||||
}
|
||||
|
||||
private static CommerceAdminQuery ToQuery(TenantCommerceQueryDto query)
|
||||
{
|
||||
return new CommerceAdminQuery(query.Provider, query.Status, query.Limit);
|
||||
}
|
||||
}
|
||||
@@ -335,9 +335,10 @@ public sealed class ExceptionHandlingMiddleware(
|
||||
return code switch
|
||||
{
|
||||
"commerce_access_denied" or "tenant_access_denied" => StatusCodes.Status403Forbidden,
|
||||
"order_not_found" or "svip_plan_not_found" or "region_not_found" => StatusCodes.Status404NotFound,
|
||||
"tenant_admin_access_denied" => StatusCodes.Status403Forbidden,
|
||||
"order_not_found" or "svip_plan_not_found" or "region_not_found" or "activation_code_not_found" => StatusCodes.Status404NotFound,
|
||||
"payment_provider_not_configured" or "payment_secret_not_configured" => StatusCodes.Status503ServiceUnavailable,
|
||||
"order_status_invalid" => StatusCodes.Status409Conflict,
|
||||
"order_status_invalid" or "activation_code_used" or "payment_amount_mismatch" => StatusCodes.Status409Conflict,
|
||||
_ => StatusCodes.Status400BadRequest
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user