using CMSMicroservice.Application.Common; using CMSMicroservice.Application.Common.Exceptions; using CMSMicroservice.Application.Common.Interfaces; using CMSMicroservice.Application.Common.Models; using CMSMicroservice.Domain.Entities; using CMSMicroservice.Domain.Entities.Payment; using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; namespace CMSMicroservice.Application.WalletCQ.Commands.ChargeDiscountWallet; public class ChargeDiscountWalletCommandHandler : IRequestHandler { private readonly IApplicationDbContext _context; private readonly IPaymentGatewayService _paymentGateway; private readonly IConfiguration _configuration; private readonly ILogger _logger; private readonly IUserPaymentLock _paymentLock; public ChargeDiscountWalletCommandHandler( IApplicationDbContext context, IPaymentGatewayService paymentGateway, IConfiguration configuration, ILogger logger, IUserPaymentLock paymentLock) { _context = context; _paymentGateway = paymentGateway; _configuration = configuration; _logger = logger; _paymentLock = paymentLock; } public Task Handle( ChargeDiscountWalletCommand request, CancellationToken cancellationToken) => _paymentLock.ExecuteAsync( PaymentLockScopes.Initiate(request.UserId), PaymentLockStrategy.FailFast, ct => HandleCore(request, ct), cancellationToken); private async Task HandleCore( ChargeDiscountWalletCommand request, CancellationToken cancellationToken) { try { _logger.LogInformation( "Charging discount wallet for UserId: {UserId}, Amount: {Amount}", request.UserId, request.Amount ); // 1. بررسی وجود کاربر var user = await _context.Users .FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken); if (user == null) { _logger.LogWarning("User not found: {UserId}", request.UserId); throw new NotFoundException(nameof(User), request.UserId); } // 2. بررسی وجود کیف پول var wallet = await _context.UserWallets .FirstOrDefaultAsync(w => w.UserId == request.UserId, cancellationToken); if (wallet == null) { _logger.LogError("Wallet not found for UserId: {UserId}", request.UserId); throw new NotFoundException("کیف پول کاربر یافت نشد"); } // 3. ایجاد درخواست پرداخت — callback مستقیم به فرانت‌آفیس var frontOfficeBaseUrl = _configuration["FrontOfficeBaseUrl"] ?? "https://localhost:5268"; var callbackUrl = $"{frontOfficeBaseUrl}/profile/payment-callback?type=discount-wallet"; var paymentRequest = new PaymentRequest { Amount = request.Amount, UserId = user.Id, Mobile = user.Mobile ?? "", CallbackUrl = callbackUrl, Description = $"شارژ کیف پول تخفیفی - کاربر {user.Id}" }; var paymentResult = await _paymentGateway.InitiatePaymentAsync(paymentRequest); if (!paymentResult.IsSuccess) { _logger.LogError( "Payment gateway failed for UserId {UserId}: {ErrorMessage}", user.Id, paymentResult.ErrorMessage ); throw new Exception($"خطا در ارتباط با درگاه پرداخت: {paymentResult.ErrorMessage}"); } // 4. ثبت PaymentTransaction var paymentTx = new PaymentTransaction { GatewayProvider = _configuration["PaymentProvider"] ?? "zarinpal", MerchantId = _configuration["ZarinPal:MerchantId"] ?? "", Amount = request.Amount, CallbackUrl = callbackUrl, Description = $"شارژ کیف پول تخفیفی - کاربر {user.Id}", Mobile = user.Mobile, UserId = user.Id, RequestStatusCode = 100, RequestStatusMessage = "Success", Authority = paymentResult.RefId, PaymentStatus = false }; _context.PaymentTransactions.Add(paymentTx); await _context.SaveChangesAsync(cancellationToken); _logger.LogInformation( "Discount wallet charge initiated. UserId: {UserId}, RefId: {RefId}, PaymentTxId: {PaymentTxId}", user.Id, paymentResult.RefId, paymentTx.Id ); return paymentResult; } catch (Exception ex) { _logger.LogError( ex, "Error in ChargeDiscountWalletCommand for UserId: {UserId}", request.UserId ); throw; } } }