Files
meezi/src/Meezi.API/Controllers/BillingController.cs
T
soroush.asadi 7a5ea75b50
CI/CD / CI · API (dotnet build + test) (push) Successful in 40s
CI/CD / CI · Admin API (dotnet build) (push) Successful in 30s
CI/CD / CI · Dashboard (tsc) (push) Successful in 1m9s
CI/CD / CI · Admin Web (tsc) (push) Successful in 37s
CI/CD / CI · Website (tsc) (push) Successful in 45s
CI/CD / CI · Koja (tsc) (push) Has been cancelled
CI/CD / Deploy · all services (push) Has been cancelled
feat(rbac): enforce permissions on every café write endpoint
Closes the gap where the custom-role matrix was defined but unenforced — most
write endpoints only checked café membership, so the API would accept writes a
role's UI hid. Adds EnsurePermission(...) to all mutating/sensitive endpoints
across 32 controllers, mapped to the granular catalog:

- menu/inventory/coupons/customers/expenses/reservations/taxes/branches → CRUD perms
- tables/queue/kitchen-stations/print-settings → manage perms
- orders → ProcessOrders / EditOrder / VoidOrder / UpdateOrderStatus / HandlePayments,
  payment corrections → ManageFinancials
- HR → CreateStaff / ManageSchedules / ReviewLeave / View+ManageSalaries /
  ManageStaffCredentials (self-service clock-in/leave preserved)
- reports → ViewReports, export → ExportReports, audit → ViewAuditLog
- billing → ManageBilling, sms → SendSms/ManageSmsSettings, reviews → ManageReviews,
  discover/public profile → ManageDiscoverProfile, café settings → ManageCafeSettings,
  custom roles → ManageRoles

Removes legacy [Authorize(Roles=...)] attributes that would have overridden the
permission model (orders, branch-menu, pos-device, print). Manual discount/comp
have no backend endpoint yet (discounts come from coupons) — gated on the POS UI.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-21 05:43:07 +03:30

123 lines
4.4 KiB
C#

using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Meezi.API.Models.Billing;
using Meezi.API.Services;
using Meezi.Core.Authorization;
using Meezi.Core.Interfaces;
using Meezi.Shared;
namespace Meezi.API.Controllers;
[ApiController]
public class BillingController : CafeApiControllerBase
{
private readonly IBillingService _billing;
private readonly IValidator<SubscribeRequest> _subscribeValidator;
public BillingController(IBillingService billing, IValidator<SubscribeRequest> subscribeValidator)
{
_billing = billing;
_subscribeValidator = subscribeValidator;
}
[Authorize]
[HttpPost("api/billing/subscribe")]
public async Task<IActionResult> Subscribe(
[FromBody] SubscribeRequest request,
ITenantContext tenant,
CancellationToken ct)
{
if (EnsurePermission(tenant, Permission.ManageBilling) is { } permDenied) return permDenied;
if (string.IsNullOrEmpty(tenant.CafeId))
return Unauthorized();
var validation = await _subscribeValidator.ValidateAsync(request, ct);
if (!validation.IsValid)
{
var first = validation.Errors.First();
return BadRequest(new ApiResponse<object>(false, null, new ApiError("VALIDATION_ERROR", first.ErrorMessage, first.PropertyName)));
}
var (data, code, message) = await _billing.InitiateSubscriptionAsync(tenant.CafeId, request, ct);
if (data is null)
return BadRequest(new ApiResponse<object>(false, null, new ApiError(code ?? "ERROR", message ?? "Failed.")));
return Ok(new ApiResponse<SubscribeResponse>(true, data));
}
[AllowAnonymous]
[HttpGet("api/billing/verify")]
public async Task<IActionResult> Verify(
[FromQuery] string Authority,
[FromQuery] string? Status,
CancellationToken ct)
{
var result = await _billing.VerifyZarinPalAsync(Authority, Status, ct);
return Redirect(result.RedirectUrl);
}
[AllowAnonymous]
[HttpGet("api/billing/verify/snapppay")]
public async Task<IActionResult> VerifySnappPay(
[FromQuery] string? paymentToken,
[FromQuery] string? state,
CancellationToken ct)
{
var result = await _billing.VerifySnappPayAsync(paymentToken, state, ct);
return Redirect(result.RedirectUrl);
}
[AllowAnonymous]
[HttpGet("api/billing/verify/tara")]
public async Task<IActionResult> VerifyTara(
[FromQuery] string? traceNumber,
[FromQuery] string? status,
CancellationToken ct)
{
var result = await _billing.VerifyTaraAsync(traceNumber, status, ct);
return Redirect(result.RedirectUrl);
}
[Authorize]
[HttpGet("api/billing/payment-methods")]
public async Task<IActionResult> PaymentMethods(CancellationToken ct)
{
var methods = await _billing.GetPaymentMethodsAsync(ct);
return Ok(new ApiResponse<IReadOnlyList<PaymentMethodDto>>(true, methods));
}
[Authorize]
[HttpGet("api/billing/status")]
public async Task<IActionResult> Status(ITenantContext tenant, CancellationToken ct)
{
if (string.IsNullOrEmpty(tenant.CafeId) || tenant.PlanTier is null)
return Unauthorized();
var data = await _billing.GetStatusAsync(tenant.CafeId, tenant.PlanTier.Value, ct);
if (data is null)
return NotFound(new ApiResponse<object>(false, null, new ApiError("NOT_FOUND", "Cafe not found.")));
return Ok(new ApiResponse<BillingStatusDto>(true, data));
}
[Authorize]
[HttpDelete("api/billing/queued/{paymentId}")]
public async Task<IActionResult> CancelQueued(string paymentId, ITenantContext tenant, CancellationToken ct)
{
if (EnsurePermission(tenant, Permission.ManageBilling) is { } permDenied) return permDenied;
if (string.IsNullOrEmpty(tenant.CafeId))
return Unauthorized();
var (ok, code, message) = await _billing.CancelQueuedAsync(tenant.CafeId, paymentId, ct);
if (!ok)
{
return code == "NOT_FOUND"
? NotFound(new ApiResponse<object>(false, null, new ApiError(code, message ?? "Not found.")))
: BadRequest(new ApiResponse<object>(false, null, new ApiError(code ?? "ERROR", message ?? "Failed.")));
}
return Ok(new ApiResponse<object>(true, new { id = paymentId }));
}
}