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 Microsoft.Extensions.Options; using ServiceHost.BaseControllers; namespace ServiceHost.Areas.Client.Controllers; public record CreateFinancialPayRequest(long Id, string BaseUrl); 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,IOptions appSetting) { _financialStatementApplication = financialStatementApplication; _authHelper = authHelper; _paymentTransactionApplication = paymentTransactionApplication; _paymentGateway = new AqayePardakhtPaymentGateway(httpClientFactory, appSetting); } [HttpGet] public async Task> GetList(FinancialStatementSearchModel searchModel) { var accountId = _authHelper.CurrentAccountId(); var result =await _financialStatementApplication.GetClientFinancialStatement(searchModel,accountId); return result; } [AllowAnonymous] [HttpPost("preview/{id}")] public async Task>> GetStatementDetails(string id) { if(!Guid.TryParseExact(id,"N",out _)) return new OperationResult().Failed("شناسه ارسال شده نامعتبر است"); var result = await _financialStatementApplication.GetDetailsByPublicId(id); return result; } /// /// ساخت /// /// /// /// /// [HttpPost("CreatePay")] public async Task>> CreatePay([FromForm] CreateFinancialPayRequest request, CancellationToken cancellationToken) { var op = new OperationResult(); var balanceAmount = await _financialStatementApplication.GetBalanceAmount(request.Id); if (balanceAmount.Amount<=0) { return op.Failed("موجودی حساب شما صفر است"); } var callbackUrl = Url.Action( action: "OnGetCallBack", controller: "General", // نام کنترلر بدون کلمه‌ی "Controller" values: null, protocol: Request.Scheme); // http یا https var transactionCommand = new CreatePaymentTransaction() { Amount = balanceAmount.Amount, ContractingPartyId = balanceAmount.ContractingPartyId, CallBackUrl = request.BaseUrl }; var transaction = await _paymentTransactionApplication.Create(transactionCommand); if (!transaction.IsSuccedded) { return op.Failed(transaction.Message); } var command = new CreatePaymentGatewayRequest() { CallBackUrl = callbackUrl, Amount = balanceAmount.Amount/10, InvoiceId = transaction.SendId.ToString(), }; var gatewayResponse = await _paymentGateway.Create(command, cancellationToken); if (gatewayResponse.IsSuccess) { _ = await _paymentTransactionApplication.SetTransactionId(transaction.SendId, gatewayResponse.TransactionId); return Redirect(_paymentGateway.GetStartPayUrl(gatewayResponse.TransactionId)); } if (gatewayResponse.ErrorCode.HasValue) { return op.Failed($"خطا در ایجاد درگاه پرداخت: {gatewayResponse.ErrorCode.Value}"); } return op.Failed("خطا در ایجاد درگاه پرداخت"); } }