Files
Backend-Api/CompanyManagment.Application/PaymentTransactionApplication.cs
2025-07-20 14:56:19 +03:30

113 lines
4.1 KiB
C#

using _0_Framework.Application;
using _0_Framework.Application.PaymentGateway;
using Company.Domain.PaymentTransactionAgg;
using CompanyManagment.App.Contracts.PaymentTransaction;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Net.Http;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using _0_Framework.Application;
using _0_Framework.Application.PaymentGateway;
using Company.Domain.ContarctingPartyAgg;
using Company.Domain.PaymentTransactionAgg;
using CompanyManagment.App.Contracts.PaymentTransaction;
namespace CompanyManagment.Application;
public class PaymentTransactionApplication : IPaymentTransactionApplication
{
private readonly IPaymentTransactionRepository _paymentTransactionRepository;
private readonly IPaymentGateway _paymentGateway;
private readonly IPersonalContractingPartyRepository _personalContractingPartyRepository;
public PaymentTransactionApplication(IPaymentTransactionRepository paymentTransactionRepository,IHttpClientFactory httpClientFactory, IOptions<AppSettingConfiguration> appSetting, IPersonalContractingPartyRepository personalContractingPartyRepository)
{
_paymentTransactionRepository = paymentTransactionRepository;
_personalContractingPartyRepository = personalContractingPartyRepository;
_paymentGateway = new AqayePardakhtPaymentGateway(httpClientFactory, appSetting);
}
public async Task<List<GetPaymentTransactionListViewModel>> GetPaymentTransactionList(
GetPaymentTransactionListSearchModel searchModel)
{
return await _paymentTransactionRepository.GetPaymentTransactionList(searchModel);
}
public async Task<OperationResult> Create(CreatePaymentTransaction command)
{
var operationResult = new OperationResult();
var contractingPartyName = _personalContractingPartyRepository.GetFullName(command.ContractingPartyId);
if (string.IsNullOrWhiteSpace(contractingPartyName))
{
return operationResult.Failed("مشتری مورد نظر یافت نشد");
}
var entity = new PaymentTransaction(
command.ContractingPartyId,
command.Amount,
contractingPartyName,
command.CallBackUrl);
await _paymentTransactionRepository.CreateAsync(entity);
await _paymentTransactionRepository.SaveChangesAsync();
return operationResult.Succcedded(entity.id);
}
public async Task<WalletAmountResponse> GetWalletAmount(CancellationToken cancellationToken)
{
var result = await _paymentGateway.GetWalletAmount(cancellationToken);
return result;
}
public async Task<PaymentTransactionDetailsViewModel> GetDetails(long id)
{
var result = await _paymentTransactionRepository.GetDetails(id);
return result;
}
public OperationResult SetFailed(long paymentTransactionId)
{
var op = new OperationResult();
var paymentTransaction = _paymentTransactionRepository.Get(paymentTransactionId);
if (paymentTransaction == null)
{
return op.Failed("تراکنش مورد نظر یافت نشد");
}
paymentTransaction.SetFailed();
_paymentTransactionRepository.SaveChanges();
return op.Succcedded();
}
public OperationResult SetSuccess(long paymentTransactionId,string cardNumber, string bankName)
{
var op = new OperationResult();
var paymentTransaction = _paymentTransactionRepository.Get(paymentTransactionId);
if (paymentTransaction == null)
{
return op.Failed("تراکنش مورد نظر یافت نشد");
}
paymentTransaction.SetPaid(cardNumber, bankName);
_paymentTransactionRepository.SaveChanges();
return op.Succcedded();
}
public async Task<OperationResult> SetTransactionId(long id, string transactionId)
{
var paymentTransaction = _paymentTransactionRepository.Get(id);
paymentTransaction.SetTransactionId(transactionId);
await _paymentTransactionRepository.SaveChangesAsync();
return new OperationResult().Succcedded();
}
}