2025-12-02 03:30:36 +03:30
|
|
|
using CMSMicroservice.Application.Common.Exceptions;
|
|
|
|
|
using CMSMicroservice.Application.Common.Interfaces;
|
|
|
|
|
using CMSMicroservice.Application.Common.Models;
|
|
|
|
|
using CMSMicroservice.Domain.Entities;
|
|
|
|
|
using CMSMicroservice.Domain.Enums;
|
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ValidationException = FluentValidation.ValidationException;
|
|
|
|
|
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
namespace CMSMicroservice.Application.PackageCQ.Commands.VerifyPackagePurchase;
|
2025-12-02 03:30:36 +03:30
|
|
|
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
public class VerifyPackagePurchaseCommandHandler
|
|
|
|
|
: IRequestHandler<VerifyPackagePurchaseCommand, bool>
|
2025-12-02 03:30:36 +03:30
|
|
|
{
|
|
|
|
|
private readonly IApplicationDbContext _context;
|
|
|
|
|
private readonly IPaymentGatewayService _paymentGateway;
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
private readonly ILogger<VerifyPackagePurchaseCommandHandler> _logger;
|
2025-12-02 03:30:36 +03:30
|
|
|
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
public VerifyPackagePurchaseCommandHandler(
|
2025-12-02 03:30:36 +03:30
|
|
|
IApplicationDbContext context,
|
|
|
|
|
IPaymentGatewayService paymentGateway,
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
ILogger<VerifyPackagePurchaseCommandHandler> logger)
|
2025-12-02 03:30:36 +03:30
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
_paymentGateway = paymentGateway;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Handle(
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
VerifyPackagePurchaseCommand request,
|
2025-12-02 03:30:36 +03:30
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation(
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
"Verifying package purchase. OrderId: {OrderId}, Authority: {Authority}",
|
2025-12-02 03:30:36 +03:30
|
|
|
request.OrderId,
|
|
|
|
|
request.Authority
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 1. پیدا کردن سفارش
|
|
|
|
|
var order = await _context.UserOrders
|
|
|
|
|
.Include(o => o.Package)
|
|
|
|
|
.Include(o => o.User)
|
|
|
|
|
.FirstOrDefaultAsync(o => o.Id == request.OrderId, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Order not found: {OrderId}", request.OrderId);
|
|
|
|
|
throw new NotFoundException(nameof(UserOrder), request.OrderId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. بررسی اینکه سفارش قبلاً پرداخت نشده باشد
|
|
|
|
|
if (order.PaymentStatus == PaymentStatus.Success)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Order {OrderId} is already paid", request.OrderId);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Verify با درگاه بانکی
|
|
|
|
|
var verifyResult = await _paymentGateway.VerifyPaymentAsync(
|
|
|
|
|
request.Authority,
|
|
|
|
|
request.Authority // verificationToken - در بعضی درگاهها همان Authority است
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!verifyResult.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning(
|
|
|
|
|
"Payment verification failed for OrderId {OrderId}: {Message}",
|
|
|
|
|
request.OrderId,
|
|
|
|
|
verifyResult.Message
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
order.PaymentStatus = PaymentStatus.Reject;
|
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
throw new ValidationException($"تراکنش ناموفق: {verifyResult.Message}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. شارژ کیف پول کاربر
|
|
|
|
|
var wallet = await _context.UserWallets
|
|
|
|
|
.FirstOrDefaultAsync(w => w.UserId == order.UserId, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (wallet == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("Wallet not found for UserId: {UserId}", order.UserId);
|
|
|
|
|
throw new NotFoundException($"کیف پول کاربر با شناسه {order.UserId} یافت نشد");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// شارژ Balance (موجودی عادی)
|
|
|
|
|
var oldBalance = wallet.Balance;
|
|
|
|
|
wallet.Balance += order.Amount;
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation(
|
|
|
|
|
"Charging Balance for UserId {UserId}: {OldBalance} -> {NewBalance}",
|
|
|
|
|
order.UserId,
|
|
|
|
|
oldBalance,
|
|
|
|
|
wallet.Balance
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// شارژ DiscountBalance (موجودی تخفیف)
|
|
|
|
|
var oldDiscountBalance = wallet.DiscountBalance;
|
|
|
|
|
wallet.DiscountBalance += order.Amount;
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation(
|
|
|
|
|
"Charging DiscountBalance for UserId {UserId}: {OldBalance} -> {NewBalance}",
|
|
|
|
|
order.UserId,
|
|
|
|
|
oldDiscountBalance,
|
|
|
|
|
wallet.DiscountBalance
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 5. ثبت Transaction
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
var transaction = new Transaction
|
2025-12-02 03:30:36 +03:30
|
|
|
{
|
|
|
|
|
Amount = order.Amount,
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
Description = $"خرید پکیج از درگاه - سفارش #{order.Id}",
|
2025-12-02 03:30:36 +03:30
|
|
|
PaymentStatus = PaymentStatus.Success,
|
2025-12-12 01:40:26 +03:30
|
|
|
PaymentDate = DateTime.Now,
|
2025-12-02 03:30:36 +03:30
|
|
|
RefId = verifyResult.RefId,
|
|
|
|
|
Type = TransactionType.DepositIpg
|
|
|
|
|
};
|
|
|
|
|
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
_context.Transactions.Add(transaction);
|
2025-12-02 03:30:36 +03:30
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
// 6. ثبت لاگ تغییر Balance
|
|
|
|
|
var balanceLog = new UserWalletChangeLog
|
|
|
|
|
{
|
|
|
|
|
WalletId = wallet.Id,
|
|
|
|
|
CurrentBalance = wallet.Balance,
|
|
|
|
|
ChangeValue = order.Amount,
|
|
|
|
|
CurrentNetworkBalance = wallet.NetworkBalance,
|
|
|
|
|
ChangeNerworkValue = 0,
|
|
|
|
|
CurrentDiscountBalance = wallet.DiscountBalance - order.Amount, // قبل از شارژ DiscountBalance
|
|
|
|
|
ChangeDiscountValue = 0,
|
|
|
|
|
IsIncrease = true,
|
|
|
|
|
RefrenceId = transaction.Id
|
|
|
|
|
};
|
|
|
|
|
await _context.UserWalletChangeLogs.AddAsync(balanceLog, cancellationToken);
|
|
|
|
|
|
|
|
|
|
// 7. ثبت لاگ تغییر DiscountBalance
|
|
|
|
|
var discountLog = new UserWalletChangeLog
|
|
|
|
|
{
|
|
|
|
|
WalletId = wallet.Id,
|
|
|
|
|
CurrentBalance = wallet.Balance,
|
|
|
|
|
ChangeValue = 0,
|
|
|
|
|
CurrentNetworkBalance = wallet.NetworkBalance,
|
|
|
|
|
ChangeNerworkValue = 0,
|
|
|
|
|
CurrentDiscountBalance = wallet.DiscountBalance,
|
|
|
|
|
ChangeDiscountValue = order.Amount,
|
|
|
|
|
IsIncrease = true,
|
|
|
|
|
RefrenceId = transaction.Id
|
|
|
|
|
};
|
|
|
|
|
await _context.UserWalletChangeLogs.AddAsync(discountLog, cancellationToken);
|
|
|
|
|
|
|
|
|
|
// 8. بهروزرسانی Order
|
|
|
|
|
order.TransactionId = transaction.Id;
|
|
|
|
|
order.PaymentStatus = PaymentStatus.Success;
|
2025-12-12 01:40:26 +03:30
|
|
|
order.PaymentDate = DateTime.Now;
|
2025-12-02 03:30:36 +03:30
|
|
|
order.PaymentMethod = PaymentMethod.IPG;
|
|
|
|
|
|
|
|
|
|
// 9. تغییر User.PackagePurchaseMethod
|
|
|
|
|
order.User.PackagePurchaseMethod = PackagePurchaseMethod.DirectPurchase;
|
|
|
|
|
|
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation(
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
"Package purchase verified successfully. " +
|
2025-12-02 03:30:36 +03:30
|
|
|
"OrderId: {OrderId}, UserId: {UserId}, TransactionId: {TransactionId}, RefId: {RefId}",
|
|
|
|
|
order.Id,
|
|
|
|
|
order.UserId,
|
|
|
|
|
transaction.Id,
|
|
|
|
|
verifyResult.RefId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(
|
|
|
|
|
ex,
|
Add validators and services for Product Galleries and Product Tags
- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
2025-12-04 02:40:49 +03:30
|
|
|
"Error in VerifyPackagePurchaseCommand. OrderId: {OrderId}",
|
2025-12-02 03:30:36 +03:30
|
|
|
request.OrderId
|
|
|
|
|
);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|