From ff3696cda36c1ecf2fd6d44139123cd8cdd78742 Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Sat, 28 Feb 2026 03:27:58 +0330 Subject: [PATCH] feat: implement payment verification methods for magic wallet and discount wallet --- src/FrontOffice.Main/FrontOffice.Main.csproj | 2 +- .../Pages/Profile/PaymentCallback.razor | 213 +++++++++++++++--- .../Utilities/DiscountOrderService.cs | 21 ++ .../Utilities/WalletService.cs | 38 ++++ 4 files changed, 239 insertions(+), 35 deletions(-) diff --git a/src/FrontOffice.Main/FrontOffice.Main.csproj b/src/FrontOffice.Main/FrontOffice.Main.csproj index c37f88b..61b82ff 100644 --- a/src/FrontOffice.Main/FrontOffice.Main.csproj +++ b/src/FrontOffice.Main/FrontOffice.Main.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/FrontOffice.Main/Pages/Profile/PaymentCallback.razor b/src/FrontOffice.Main/Pages/Profile/PaymentCallback.razor index 20edafd..d26bd56 100644 --- a/src/FrontOffice.Main/Pages/Profile/PaymentCallback.razor +++ b/src/FrontOffice.Main/Pages/Profile/PaymentCallback.razor @@ -35,8 +35,8 @@ - بازگشت به پروفایل + Href="@_successReturnUrl"> + @_successReturnText } else @@ -46,8 +46,8 @@ @_message - بازگشت به پروفایل + Href="@_failReturnUrl"> + @_failReturnText + /// نوع پرداخت: package (پیش‌فرض) | magic-wallet | discount-wallet | discount-order + /// + [SupplyParameterFromQuery(Name = "type")] + private string? PaymentType { get; set; } + [SupplyParameterFromQuery(Name = "orderId")] private long OrderId { get; set; } @@ -84,6 +91,12 @@ private string _message = string.Empty; private long _transactionId; private long _walletBalance; + + // دکمه‌های بازگشت بسته به نوع پرداخت + private string _successReturnUrl = "/profile"; + private string _successReturnText = "بازگشت به پروفایل"; + private string _failReturnUrl = "/profile"; + private string _failReturnText = "بازگشت به پروفایل"; protected override async Task OnAfterRenderAsync(bool firstRender) { @@ -97,37 +110,23 @@ { try { - if (OrderId <= 0 || string.IsNullOrEmpty(Authority)) - { - _isSuccess = false; - _message = "پارامترهای پرداخت نامعتبر است"; - _isLoading = false; - StateHasChanged(); - return; - } - - var response = await PackageContractClient.CustomerVerifyPackagePurchaseAsync(new CustomerVerifyPackagePurchaseRequest - { - OrderId = OrderId, - Authority = Authority ?? string.Empty, - Status = Status ?? string.Empty - }); - - _isSuccess = response.Success; - _message = response.Message; - _transactionId = response.TransactionId; + // تعیین نوع پرداخت — اگر type نباشد، پیش‌فرض خرید پکیج + var type = PaymentType?.ToLowerInvariant() ?? "package"; - // اگر پرداخت موفق بود، موجودی واقعی کیف پول رو بگیر و توکن رو refresh کن - if (_isSuccess) + switch (type) { - try - { - var balances = await WalletService.GetBalancesAsync(); - _walletBalance = balances.CreditBalance; - } - catch { /* اگر خطا شد، صفر نمایش بده */ } - - await RefreshTokenAsync(); + case "magic-wallet": + await VerifyMagicWalletCharge(); + break; + case "discount-wallet": + await VerifyDiscountWalletCharge(); + break; + case "discount-order": + await VerifyDiscountOrderPayment(); + break; + default: // "package" or missing + await VerifyPackagePurchase(); + break; } } catch (Exception ex) @@ -141,10 +140,156 @@ StateHasChanged(); } } + + /// + /// تأیید خرید پکیج — رفتار فعلی + /// + private async Task VerifyPackagePurchase() + { + _successReturnUrl = "/profile"; + _successReturnText = "بازگشت به پروفایل"; + _failReturnUrl = "/profile"; + _failReturnText = "بازگشت به پروفایل"; + + if (OrderId <= 0 || string.IsNullOrEmpty(Authority)) + { + _isSuccess = false; + _message = "پارامترهای پرداخت نامعتبر است"; + return; + } + + var response = await PackageContractClient.CustomerVerifyPackagePurchaseAsync(new CustomerVerifyPackagePurchaseRequest + { + OrderId = OrderId, + Authority = Authority ?? string.Empty, + Status = Status ?? string.Empty + }); + + _isSuccess = response.Success; + _message = response.Message; + _transactionId = response.TransactionId; + + if (_isSuccess) + { + await UpdateWalletBalanceAndRefreshToken(); + } + } + + /// + /// تأیید شارژ کیف‌پول جادویی + /// + private async Task VerifyMagicWalletCharge() + { + _successReturnUrl = "/profile/magic-wallet"; + _successReturnText = "بازگشت به کیف پول جادویی"; + _failReturnUrl = "/profile/magic-wallet"; + _failReturnText = "بازگشت به کیف پول جادویی"; + + if (string.IsNullOrEmpty(Authority)) + { + _isSuccess = false; + _message = "کد Authority نامعتبر است"; + return; + } + + var (success, message) = await WalletService.VerifyMagicChargeAsync( + Authority, Status ?? "NOK"); + + _isSuccess = success; + _message = message; + + if (_isSuccess) + { + await UpdateWalletBalance(); + } + } + + /// + /// تأیید شارژ کیف پول تخفیفی + /// + private async Task VerifyDiscountWalletCharge() + { + _successReturnUrl = "/profile/charge-discount-wallet"; + _successReturnText = "بازگشت به کیف پول تخفیفی"; + _failReturnUrl = "/profile/charge-discount-wallet"; + _failReturnText = "بازگشت به کیف پول تخفیفی"; + + if (string.IsNullOrEmpty(Authority)) + { + _isSuccess = false; + _message = "کد Authority نامعتبر است"; + return; + } + + var (success, message) = await WalletService.VerifyDiscountChargeAsync( + Authority, Status ?? "NOK"); + + _isSuccess = success; + _message = message; + + if (_isSuccess) + { + await UpdateWalletBalance(); + } + } + + /// + /// تأیید پرداخت سفارش فروشگاه تخفیفی + /// + private async Task VerifyDiscountOrderPayment() + { + _successReturnUrl = $"/discount-store/order/{OrderId}"; + _successReturnText = "مشاهده سفارش"; + _failReturnUrl = $"/discount-store/order/{OrderId}"; + _failReturnText = "مشاهده سفارش"; + + if (OrderId <= 0 || string.IsNullOrEmpty(Authority)) + { + _isSuccess = false; + _message = "پارامترهای پرداخت نامعتبر است"; + return; + } + + var (success, message, _) = await DiscountOrderService.VerifyDiscountOrderPaymentAsync( + OrderId, Authority, Status ?? "NOK"); + + _isSuccess = success; + _message = message; + } + + /// + /// بروزرسانی موجودی کیف پول + /// + private async Task UpdateWalletBalance() + { + try + { + var balances = await WalletService.GetBalancesAsync(); + _walletBalance = balances.CreditBalance; + } + catch { /* اگر خطا شد، صفر نمایش بده */ } + } + + /// + /// بروزرسانی موجودی + refresh توکن (فقط برای خرید پکیج) + /// + private async Task UpdateWalletBalanceAndRefreshToken() + { + await UpdateWalletBalance(); + await RefreshTokenAsync(); + } private void RetryPayment() { - NavManager.NavigateTo("/profile", forceLoad: true); + var type = PaymentType?.ToLowerInvariant() ?? "package"; + var url = type switch + { + "magic-wallet" => "/profile/magic-wallet", + "discount-wallet" => "/profile/charge-discount-wallet", + "discount-order" => "/discount-store", + _ => "/profile" + }; + NavManager.NavigateTo(url, forceLoad: true); } /// diff --git a/src/FrontOffice.Main/Utilities/DiscountOrderService.cs b/src/FrontOffice.Main/Utilities/DiscountOrderService.cs index 58aa5af..53c0d75 100644 --- a/src/FrontOffice.Main/Utilities/DiscountOrderService.cs +++ b/src/FrontOffice.Main/Utilities/DiscountOrderService.cs @@ -112,6 +112,27 @@ public class DiscountOrderService catch { return false; } } + public async Task<(bool Success, string Message, long OrderId)> VerifyDiscountOrderPaymentAsync( + long orderId, string authority, string status) + { + try + { + var response = await _client.CustomerVerifyDiscountOrderPaymentAsync( + new CustomerVerifyDiscountOrderPaymentRequest + { + OrderId = orderId, + Authority = authority, + Status = status + }); + + return (response.Success, response.Message, response.OrderId); + } + catch (Grpc.Core.RpcException ex) + { + return (false, ex.Status.Detail ?? "خطا در بررسی وضعیت پرداخت", orderId); + } + } + public async Task GetUserOrdersAsync(int page = 1, int pageSize = 10) { try diff --git a/src/FrontOffice.Main/Utilities/WalletService.cs b/src/FrontOffice.Main/Utilities/WalletService.cs index 5833b33..7d6648b 100644 --- a/src/FrontOffice.Main/Utilities/WalletService.cs +++ b/src/FrontOffice.Main/Utilities/WalletService.cs @@ -266,6 +266,44 @@ public class WalletService } } + // ============= Wallet Verify Methods ============= + + public async Task<(bool Success, string Message)> VerifyMagicChargeAsync(string authority, string status) + { + try + { + var response = await _client.VerifyMagicChargeAsync(new VerifyWalletChargeRequest + { + Authority = authority, + Status = status + }); + + return (response.Success, response.Message); + } + catch (Grpc.Core.RpcException ex) + { + return (false, ex.Status.Detail ?? "خطا در بررسی وضعیت پرداخت"); + } + } + + public async Task<(bool Success, string Message)> VerifyDiscountChargeAsync(string authority, string status) + { + try + { + var response = await _client.VerifyDiscountChargeAsync(new VerifyWalletChargeRequest + { + Authority = authority, + Status = status + }); + + return (response.Success, response.Message); + } + catch (Grpc.Core.RpcException ex) + { + return (false, ex.Status.Detail ?? "خطا در بررسی وضعیت پرداخت"); + } + } + public int GetWalletMode() { try