Files
Backend-Api/ServiceHost/Areas/Client/Controllers/FinancialController.cs
2025-07-19 13:11:46 +03:30

97 lines
3.6 KiB
C#

using _0_Framework.Application;
using _0_Framework.Application.PaymentGateway;
using CompanyManagment.App.Contracts.FinancialStatment;
using CompanyManagment.App.Contracts.FinancilTransaction;
using CompanyManagment.App.Contracts.PaymentTransaction;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ServiceHost.BaseControllers;
namespace ServiceHost.Areas.Client.Controllers;
public class FinancialController : ClientBaseController
{
private readonly IFinancialStatmentApplication _financialStatementApplication;
private readonly IAuthHelper _authHelper;
private readonly IPaymentGateway _paymentGateway;
private readonly IPaymentTransactionApplication _paymentTransactionApplication;
public FinancialController(IFinancialStatmentApplication financialStatementApplication, IAuthHelper authHelper,IHttpClientFactory httpClientFactory, IPaymentTransactionApplication paymentTransactionApplication)
{
_financialStatementApplication = financialStatementApplication;
_authHelper = authHelper;
_paymentTransactionApplication = paymentTransactionApplication;
_paymentGateway = new AqayePardakhtPaymentGateway(httpClientFactory);
}
[HttpGet]
public async Task<ActionResult<ClientFinancialStatementViewModel>> GetList(ClientFinancialStatementSearchModel searchModel)
{
var accountId = _authHelper.CurrentAccountId();
var result =await _financialStatementApplication.GetClientFinancialStatement(searchModel,accountId);
return result;
}
[AllowAnonymous]
[HttpGet("{hashCode}")]
public async Task<ActionResult<OperationResult<ClientFinancialStatementViewModel>>> GetStatementDetails(string hashCode)
{
var result = await _financialStatementApplication.GetDetailsByHashCode(hashCode);
return result;
}
/// <summary>
/// ساخت
/// </summary>
/// <param name="id"></param>
/// <param name="baseUrl"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost("CreatePay")]
public async Task<ActionResult<OperationResult<string>>> CreatePay(long id,string baseUrl,CancellationToken cancellationToken)
{
var op = new OperationResult<string>();
var balanceAmount = await _financialStatementApplication.GetBalanceAmount(id);
if (balanceAmount.Amount<=0)
{
return op.Failed("موجودی حساب شما صفر است");
}
var callBack = baseUrl+ "/api/CallBack";
var transactionCommand = new CreatePaymentTransaction()
{
Amount = balanceAmount.Amount,
ContractingPartyId = balanceAmount.ContractingPartyId,
CallBackUrl = baseUrl
};
var transaction = await _paymentTransactionApplication.Create(transactionCommand);
if (!transaction.IsSuccedded)
{
return op.Failed(transaction.Message);
}
var command = new CreatePaymentGatewayRequest()
{
CallBackUrl = callBack,
Amount = balanceAmount.Amount/10,
InvoiceId = transaction.SendId.ToString(),
};
var gatewayResponse = await _paymentGateway.Create(command, cancellationToken);
if (gatewayResponse.IsSuccess)
{
return op.Succcedded(_paymentGateway.GetStartPayUrl(gatewayResponse.TransactionId));
}
if (gatewayResponse.ErrorCode.HasValue)
{
return op.Failed($"خطا در ایجاد درگاه پرداخت: {gatewayResponse.ErrorCode.Value}");
}
return op.Failed("خطا در ایجاد درگاه پرداخت");
}
}