add payment for api gateway

This commit is contained in:
MahanCh
2025-07-17 15:20:34 +03:30
parent ccbc180c96
commit 62bcd4d6b6
26 changed files with 10708 additions and 302 deletions

View File

@@ -1,15 +1,22 @@
using System.Globalization;
using _0_Framework.Application;
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;
namespace ServiceHost.Controllers;
public class GeneralController:GeneralBaseController
public class GeneralController : GeneralBaseController
{
private readonly IPaymentTransactionApplication _paymentTransactionApplication;
public GeneralController(IPaymentTransactionApplication paymentTransactionApplication)
{
_paymentTransactionApplication = paymentTransactionApplication;
}
/// <summary>
/// نمایش اطلاعات عمومی مانند تاریخ ها و سال ها
@@ -26,9 +33,65 @@ public class GeneralController:GeneralBaseController
var currentDate = new { Year = currentYear, Month = pc.GetMonth(now), Day = pc.GetDayOfMonth(now) };
return new JsonResult(new
{
years,months,currentDate
years,
months,
currentDate
});
}
[HttpPost("callback")]
public async Task<IActionResult> OnGetCallBack(string? transid, string? cardnumber, string? tracking_number,
string bank, string invoice_id, string? status)
{
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 (string.IsNullOrWhiteSpace(cardnumber) || string.IsNullOrWhiteSpace(tracking_number))
{
return await HandleFailedTransaction(transaction, paymentTransactionId);
}
// اگر استاتوس 1 باشد، تراکنش موفق است
if (status == "1")
{
var setSuccessResult = _paymentTransactionApplication.SetSuccess(paymentTransactionId, cardnumber, bank);
if (!setSuccessResult.IsSuccedded)
{
return new JsonResult(setSuccessResult);
}
return Redirect(BuildCallbackUrl(transaction.CallBackUrl, true, transaction.Id));
}
// در غیر این صورت تراکنش ناموفق است
return await HandleFailedTransaction(transaction, paymentTransactionId);
}
private async Task<IActionResult> 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}?Status={statusCode}&transactionId={transactionId}";
}
}