178 lines
5.8 KiB
C#
178 lines
5.8 KiB
C#
using _0_Framework.Application;
|
||
using CompanyManagment.App.Contracts.PaymentCallback;
|
||
using CompanyManagment.App.Contracts.PaymentTransaction;
|
||
using GozareshgirProgramManager.Application._Common.Constants;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using ServiceHost.BaseControllers;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.Globalization;
|
||
using System.Threading;
|
||
|
||
namespace ServiceHost.Controllers;
|
||
|
||
public class GeneralController : GeneralBaseController
|
||
{
|
||
private readonly IPaymentTransactionApplication _paymentTransactionApplication;
|
||
private readonly IPaymentCallbackHandler _paymentCallbackHandler;
|
||
|
||
public GeneralController(IPaymentTransactionApplication paymentTransactionApplication,
|
||
IPaymentCallbackHandler paymentCallbackHandler)
|
||
{
|
||
_paymentTransactionApplication = paymentTransactionApplication;
|
||
_paymentCallbackHandler = paymentCallbackHandler;
|
||
}
|
||
|
||
/// <summary>
|
||
/// نمایش اطلاعات عمومی مانند تاریخ ها و سال ها
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[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
|
||
});
|
||
}
|
||
|
||
// [HttpGet("pm-permissions")]
|
||
// public IActionResult GetPMPermissions()
|
||
// {
|
||
// var permissions = ProgramManagerPermissionCode.GetAllCodes();
|
||
// return new JsonResult(permissions);
|
||
// }
|
||
|
||
[HttpGet("/api/callback"), HttpPost("/api/callback")]
|
||
public async Task<IActionResult> Verify([FromForm] SepehrGatewayPayResponse payResponse)
|
||
{
|
||
if (!long.TryParse(payResponse.invoiceid, out var paymentTransactionId))
|
||
{
|
||
return BadRequest("Invalid invoice_id");
|
||
}
|
||
|
||
var transaction = await _paymentTransactionApplication.GetDetails(paymentTransactionId);
|
||
|
||
if (transaction == null)
|
||
{
|
||
return NotFound("Transaction not found");
|
||
}
|
||
|
||
// ایجاد command برای ارسال به PaymentCallbackHandler
|
||
var command = new VerifyPaymentCallbackCommand
|
||
{
|
||
ResponseCode = payResponse.respcode,
|
||
InvoiceId = paymentTransactionId,
|
||
Payload = payResponse.payload,
|
||
Amount = payResponse.amount,
|
||
TraceNumber = payResponse.tracenumber,
|
||
Rrn = payResponse.rrn,
|
||
DigitalReceipt = payResponse.digitalreceipt,
|
||
IssuerBank = payResponse.issuerbank,
|
||
CardNumber = payResponse.cardnumber
|
||
};
|
||
|
||
// پردازش callback در Application Layer
|
||
var result = await _paymentCallbackHandler.VerifySepehrPaymentCallback(command, CancellationToken.None);
|
||
|
||
if (result.IsSuccedded)
|
||
{
|
||
return Redirect(BuildCallbackUrl(transaction.CallBackUrl, true, transaction.Id));
|
||
}
|
||
|
||
// در صورت ناموفق بودن
|
||
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}";
|
||
}
|
||
}
|
||
|
||
public class TokenReq
|
||
{
|
||
public long Amount { get; set; }
|
||
|
||
public string CallbackUrl { get; set; }
|
||
|
||
[Display(Name = "شماره فاکتور")]
|
||
[MaxLength(100)]
|
||
[Required]
|
||
[Key]
|
||
// be ezaye har pazirande bayad yekta bashad
|
||
public string invoiceID { get; set; }
|
||
|
||
[Required] public long terminalID { get; set; }
|
||
|
||
/*
|
||
* JSON Bashad
|
||
* etelaate takmili site harchi
|
||
* nabayad char khas dashte bashe (*'"xp_%!+- ...)
|
||
*/
|
||
public string Payload { get; set; } = "";
|
||
public string email { get; set; }
|
||
}
|
||
|
||
public class TokenResp
|
||
{
|
||
// if 0 = success
|
||
public int Status { get; set; }
|
||
public string AccessToken { get; set; }
|
||
}
|
||
|
||
public class PayRequest
|
||
{
|
||
[Required] [MaxLength(3000)] public string token { get; set; }
|
||
[Required] public long terminalID { get; set; }
|
||
public string nationalCode { get; set; }
|
||
}
|
||
|
||
public class SepehrGatewayPayResponse
|
||
{
|
||
/* 0 yni movafaq
|
||
* -1 yni enseraf
|
||
* -2 yni etmam zaman
|
||
*/
|
||
public int respcode { get; set; }
|
||
[Display(Name = "متن نتیجه تراکنش")] public string respmsg { get; set; }
|
||
|
||
[Display(Name = "مبلغ کسر شده از مشتری")]
|
||
public long amount { get; set; }
|
||
|
||
[Display(Name = " شماره فاکتور ")] public string invoiceid { get; set; }
|
||
|
||
[Display(Name = " اطلاعاتی که پذیرنده ارسال کرد ")]
|
||
public string payload { get; set; }
|
||
|
||
[Display(Name = " شماره ترمینال ")] public long terminalid { get; set; }
|
||
|
||
[Display(Name = " شماره پیگیری ")] public long tracenumber { get; set; }
|
||
|
||
// bayad negah dari she hatman to db
|
||
[Display(Name = " شماره سند بانکی ")] public long rrn { get; set; }
|
||
|
||
[Display(Name = " زمان و تاریخ پرداخت ")]
|
||
public string datepaid { get; set; }
|
||
|
||
[Display(Name = " رسید دیجیتال ")] public string digitalreceipt { get; set; }
|
||
|
||
[Display(Name = " نام بانک صادر کننده کارت ")]
|
||
public string issuerbank { get; set; }
|
||
|
||
[Display(Name = " شماره کارت ")] public string cardnumber { get; set; }
|
||
}
|
||
|
||
public class AdviceReq
|
||
{
|
||
[Display(Name = " رسید دیجیتال ")] public string digitalreceipt { get; set; }
|
||
public long Tid { get; set; }
|
||
} |