using _0_Framework.Application;
using CompanyManagment.App.Contracts.PaymentTransaction;
using CompanyManagment.EFCore.Migrations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ServiceHost.BaseControllers;
using System.Globalization;
using _0_Framework.Application.PaymentGateway;
using CompanyManagment.App.Contracts.FinancialStatment;
using CompanyManagment.App.Contracts.FinancilTransaction;
namespace ServiceHost.Controllers;
public class GeneralController : GeneralBaseController
{
private readonly IPaymentTransactionApplication _paymentTransactionApplication;
private readonly IPaymentGateway _paymentGateway;
private readonly IFinancialStatmentApplication _financialStatmentApplication;
public GeneralController(IPaymentTransactionApplication paymentTransactionApplication,IHttpClientFactory clientFactory, IFinancialStatmentApplication financialStatmentApplication)
{
_paymentTransactionApplication = paymentTransactionApplication;
_financialStatmentApplication = financialStatmentApplication;
_paymentGateway = new AqayePardakhtPaymentGateway(clientFactory);
}
///
/// نمایش اطلاعات عمومی مانند تاریخ ها و سال ها
///
///
[HttpGet("Dates")]
public IActionResult GetDates()
{
var pc = new PersianCalendar();
var now = DateTime.Now;
var currentYear = pc.GetYear(now);
var years = Enumerable.Range(1370, currentYear - 1370 + 1).ToList();
var months = Enumerable.Range(1, 12).ToList();
var currentDate = new { Year = currentYear, Month = pc.GetMonth(now), Day = pc.GetDayOfMonth(now) };
return new JsonResult(new
{
years,
months,
currentDate
});
}
[HttpPost("/api/callback")]
public async Task OnGetCallBack(string? transid, string? cardnumber, string? tracking_number,
string bank, string invoice_id, string? status,CancellationToken cancellationToken)
{
if (!long.TryParse(invoice_id, out var paymentTransactionId))
{
return BadRequest("Invalid invoice_id");
}
var transaction = await _paymentTransactionApplication.GetDetails(paymentTransactionId);
if (transaction == null)
{
return NotFound("Transaction not found");
}
if (transaction.Status != PaymentTransactionStatus.Pending)
{
return BadRequest("این تراکنش قبلا پرداخت شده است");
}
// اگر شماره کارت یا شماره پیگیری خالی باشد، تراکنش ناموفق است
if (string.IsNullOrWhiteSpace(cardnumber) || string.IsNullOrWhiteSpace(tracking_number))
{
return await HandleFailedTransaction(transaction, paymentTransactionId);
}
var verifyCommand = new VerifyPaymentGateWayRequest()
{
Amount = transaction.Amount,
TransactionId = transid
};
var verifyRes =await _paymentGateway.Verify(verifyCommand, cancellationToken);
// اگر استاتوس 1 باشد، تراکنش موفق است
if (verifyRes.IsSuccess)
{
var setSuccessResult = _paymentTransactionApplication.SetSuccess(paymentTransactionId, cardnumber, bank);
if (!setSuccessResult.IsSuccedded)
{
return new JsonResult(setSuccessResult);
}
//TODO : افزودن دریافت درآمد به وضعیت مالی
var command = new CreateFinancialStatment()
{
ContractingPartyId = 0,// نیاز هست آی دی طرف حساب ارسال بشه
TdateFa = DateTime.Now.ToFarsi(),
Deptor = 0,
Creditor = transaction.Amount,
DeptorString = "درگاه بانکی",
TypeOfTransaction = "credit",
DescriptionOption = "بابت قرارداد مابین (روابط کار)",
};
var result = _financialStatmentApplication.Create(command);
return Redirect(BuildCallbackUrl(transaction.CallBackUrl, true, transaction.Id));
}
// در غیر این صورت تراکنش ناموفق است
return await HandleFailedTransaction(transaction, paymentTransactionId);
}
private async Task HandleFailedTransaction(PaymentTransactionDetailsViewModel transaction, long transactionId)
{
var result = _paymentTransactionApplication.SetFailed(transactionId);
if (!result.IsSuccedded)
{
return new JsonResult(result);
}
return Redirect(BuildCallbackUrl(transaction.CallBackUrl, false, transaction.Id));
}
private string BuildCallbackUrl(string baseUrl, bool isSuccess, long transactionId)
{
var statusCode = isSuccess ? "1" : "0";
return $"{baseUrl}/callback?Status={statusCode}&transactionId={transactionId}";
}
}