101 lines
4.2 KiB
C#
101 lines
4.2 KiB
C#
using CmsPackage = CMSMicroservice.Protobuf.Protos.Package;
|
|
using PYMSMicroservice.Protobuf.Protos;
|
|
using PYMSMicroservice.Protobuf.Protos.Transaction;
|
|
|
|
namespace FrontOffice.BFF.Application.PackageCQ.Commands.InitiateBasePackagePayment;
|
|
|
|
/// <summary>
|
|
/// Handler برای درخواست پرداخت پکیج پایه 56 میلیون تومان
|
|
/// 1. ابتدا CMS فراخوانی میشود برای ثبت Transaction و Order
|
|
/// 2. سپس PYMS فراخوانی میشود برای دریافت URL درگاه پرداخت
|
|
/// </summary>
|
|
public class InitiateBasePackagePaymentCommandHandler : IRequestHandler<InitiateBasePackagePaymentCommand, InitiateBasePackagePaymentResponseDto>
|
|
{
|
|
private readonly IApplicationContractContext _context;
|
|
private readonly ICurrentUserService _currentUserService;
|
|
private const string ZarinpalMerchantId = "6b098fc8-f490-47a1-aac3-1de1a1b84404";
|
|
|
|
public InitiateBasePackagePaymentCommandHandler(IApplicationContractContext context, ICurrentUserService currentUserService)
|
|
{
|
|
_context = context;
|
|
_currentUserService = currentUserService;
|
|
}
|
|
|
|
public async Task<InitiateBasePackagePaymentResponseDto> Handle(InitiateBasePackagePaymentCommand request, CancellationToken cancellationToken)
|
|
{
|
|
// 1. ابتدا CMS را فراخوانی کن برای ثبت تراکنش و سفارش
|
|
var cmsRequest = new CmsPackage.InitiateBasePackagePaymentRequest
|
|
{
|
|
UserId = _currentUserService.UserId.Value
|
|
};
|
|
|
|
var cmsResponse = await _context.Package.InitiateBasePackagePaymentAsync(
|
|
cmsRequest,
|
|
cancellationToken: cancellationToken);
|
|
|
|
// اگر CMS خطا داد
|
|
if (!cmsResponse.Success)
|
|
{
|
|
return new InitiateBasePackagePaymentResponseDto
|
|
{
|
|
Success = false,
|
|
Message = cmsResponse.Message
|
|
};
|
|
}
|
|
|
|
// 2. حالا PYMS را برای دریافت URL درگاه فراخوانی کن
|
|
// مبلغ به ریال (56 میلیون تومان = 560,000,000 ریال)
|
|
var paymentRequest = new PaymentRequestRequest
|
|
{
|
|
MerchantId = ZarinpalMerchantId,
|
|
Amount = cmsResponse.Amount * 10, // تبدیل تومان به ریال
|
|
CallbackUrl = $"{request.CallbackUrl}?orderId={cmsResponse.OrderId}&transactionId={cmsResponse.TransactionId}",
|
|
Description = "پرداخت پکیج پایه",
|
|
Currency = CurrencyEnum.Irr,
|
|
Type = TransactionTypeEnum.Sandbox,
|
|
OrderId = cmsResponse.OrderId.ToString()
|
|
};
|
|
|
|
var paymentResponse = await _context.ZarinTransactions.PaymentRequestAsync(
|
|
paymentRequest,
|
|
cancellationToken: cancellationToken);
|
|
|
|
// اگر درگاه URL برنگردانده
|
|
if (string.IsNullOrEmpty(paymentResponse.PaymentGWUrl))
|
|
{
|
|
return new InitiateBasePackagePaymentResponseDto
|
|
{
|
|
Success = false,
|
|
Message = "خطا در دریافت آدرس درگاه پرداخت",
|
|
OrderId = cmsResponse.OrderId,
|
|
TransactionId = cmsResponse.TransactionId
|
|
};
|
|
}
|
|
|
|
// استخراج Authority از URL
|
|
// URL معمولا به شکل: https://www.zarinpal.com/pg/StartPay/{Authority}
|
|
var authority = ExtractAuthorityFromUrl(paymentResponse.PaymentGWUrl);
|
|
|
|
return new InitiateBasePackagePaymentResponseDto
|
|
{
|
|
Success = true,
|
|
Message = "لطفا برای تکمیل پرداخت به درگاه بانکی مراجعه کنید",
|
|
OrderId = cmsResponse.OrderId,
|
|
TransactionId = cmsResponse.TransactionId,
|
|
Amount = cmsResponse.Amount,
|
|
PaymentGatewayUrl = paymentResponse.PaymentGWUrl,
|
|
Authority = authority
|
|
};
|
|
}
|
|
|
|
private static string ExtractAuthorityFromUrl(string url)
|
|
{
|
|
// URL: https://www.zarinpal.com/pg/StartPay/{Authority}
|
|
if (string.IsNullOrEmpty(url))
|
|
return string.Empty;
|
|
|
|
var parts = url.TrimEnd('/').Split('/');
|
|
return parts.Length > 0 ? parts[^1] : string.Empty;
|
|
}
|
|
}
|