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 appSetting, IPersonalContractingPartyRepository personalContractingPartyRepository) { _paymentTransactionRepository = paymentTransactionRepository; _personalContractingPartyRepository = personalContractingPartyRepository; _paymentGateway = new AqayePardakhtPaymentGateway(httpClientFactory, appSetting); } public async Task> GetPaymentTransactionList( GetPaymentTransactionListSearchModel searchModel) { return await _paymentTransactionRepository.GetPaymentTransactionList(searchModel); } public async Task 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,command.Gateway); await _paymentTransactionRepository.CreateAsync(entity); await _paymentTransactionRepository.SaveChangesAsync(); return operationResult.Succcedded(entity.id); } public async Task GetWalletAmount(CancellationToken cancellationToken) { var result = await _paymentGateway.GetWalletAmount(cancellationToken); return result; } public async Task 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, string rrn, string digitalReceipt) { var op = new OperationResult(); var paymentTransaction = _paymentTransactionRepository.Get(paymentTransactionId); if (paymentTransaction == null) { return op.Failed("تراکنش مورد نظر یافت نشد"); } paymentTransaction.SetPaid(cardNumber, bankName,rrn, digitalReceipt); _paymentTransactionRepository.SaveChanges(); return op.Succcedded(); } public async Task SetTransactionId(long id, string transactionId) { var paymentTransaction = _paymentTransactionRepository.Get(id); paymentTransaction.SetTransactionId(transactionId); await _paymentTransactionRepository.SaveChangesAsync(); return new OperationResult().Succcedded(); } }