feat(withdrawal): normalize IBAN format in withdrawal requests
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 6m38s
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 6m38s
- Added IbanNormalizer to validate and normalize IBAN numbers in RequestWithdrawalCommandHandler and UserWalletService. - Implemented error handling for invalid IBAN formats, ensuring compliance with expected standards. - Updated relevant methods to handle normalized IBANs for cash withdrawal requests.
This commit is contained in:
+6
-1
@@ -1,3 +1,5 @@
|
||||
using CMSMicroservice.Application.Common;
|
||||
|
||||
namespace CMSMicroservice.Application.CommissionCQ.Commands.RequestWithdrawal;
|
||||
|
||||
public class RequestWithdrawalCommandHandler : IRequestHandler<RequestWithdrawalCommand, Unit>
|
||||
@@ -37,7 +39,10 @@ public class RequestWithdrawalCommandHandler : IRequestHandler<RequestWithdrawal
|
||||
|
||||
if (request.Method == WithdrawalMethod.Cash)
|
||||
{
|
||||
payout.IbanNumber = request.IbanNumber;
|
||||
var normalizedIban = IbanNormalizer.TryNormalize(request.IbanNumber);
|
||||
if (normalizedIban is null)
|
||||
throw new InvalidOperationException("فرمت شماره شبا معتبر نیست. باید IR و ۲۴ رقم باشد.");
|
||||
payout.IbanNumber = normalizedIban;
|
||||
}
|
||||
|
||||
_context.UserCommissionPayouts.Update(payout);
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace CMSMicroservice.Application.Common;
|
||||
|
||||
/// <summary>
|
||||
/// نرمالسازی و اعتبارسنجی شماره شبا ایران (IR + 24 رقم).
|
||||
/// </summary>
|
||||
public static class IbanNormalizer
|
||||
{
|
||||
private static readonly Regex IranianIbanRegex = new(@"^IR\d{24}$", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// فاصله و خط تیره را حذف میکند، به حروف بزرگ تبدیل میکند و یک پیشوند IR میگذارد.
|
||||
/// در صورت نامعتبر بودن، null برمیگرداند.
|
||||
/// </summary>
|
||||
public static string? TryNormalize(string? iban)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(iban))
|
||||
return null;
|
||||
|
||||
var normalized = iban.Trim().ToUpperInvariant()
|
||||
.Replace(" ", "", StringComparison.Ordinal)
|
||||
.Replace("-", "", StringComparison.Ordinal);
|
||||
|
||||
if (normalized.StartsWith("IR", StringComparison.Ordinal))
|
||||
normalized = normalized[2..];
|
||||
|
||||
normalized = "IR" + normalized;
|
||||
|
||||
return IranianIbanRegex.IsMatch(normalized) ? normalized : null;
|
||||
}
|
||||
|
||||
public static bool IsValid(string? iban) => TryNormalize(iban) is not null;
|
||||
}
|
||||
@@ -18,6 +18,7 @@ using CMSMicroservice.Application.UserWalletCQ.Queries.GetCustomerWalletHistory;
|
||||
using CMSMicroservice.Application.UserWalletCQ.Queries.GetCustomerWithdrawals;
|
||||
using CMSMicroservice.Application.UserWalletCQ.Queries.GetCustomerWithdrawalSettings;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Application.Common;
|
||||
using CMSMicroservice.Application.Common.Exceptions;
|
||||
using CMSMicroservice.Domain.Common;
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
@@ -175,7 +176,18 @@ public class UserWalletService : UserWalletContract.UserWalletContractBase
|
||||
|
||||
// Update payout with withdrawal info
|
||||
payout.WithdrawalMethod = (Domain.Enums.WithdrawalMethod)request.WithdrawalMethod;
|
||||
payout.IbanNumber = request.IbanNumber;
|
||||
if (payout.WithdrawalMethod == Domain.Enums.WithdrawalMethod.Cash)
|
||||
{
|
||||
var normalizedIban = IbanNormalizer.TryNormalize(request.IbanNumber);
|
||||
if (normalizedIban is null)
|
||||
throw new RpcException(new Status(StatusCode.InvalidArgument,
|
||||
"فرمت شماره شبا معتبر نیست. باید IR و ۲۴ رقم باشد."));
|
||||
payout.IbanNumber = normalizedIban;
|
||||
}
|
||||
else
|
||||
{
|
||||
payout.IbanNumber = null;
|
||||
}
|
||||
payout.Status = Domain.Enums.CommissionPayoutStatus.WithdrawRequested;
|
||||
|
||||
await _context.SaveChangesAsync(context.CancellationToken);
|
||||
|
||||
Reference in New Issue
Block a user