diff --git a/DysonNetwork.Sphere/Wallet/SubscriptionController.cs b/DysonNetwork.Sphere/Wallet/SubscriptionController.cs index da55eff..3dbc0d2 100644 --- a/DysonNetwork.Sphere/Wallet/SubscriptionController.cs +++ b/DysonNetwork.Sphere/Wallet/SubscriptionController.cs @@ -136,7 +136,7 @@ public class SubscriptionController(SubscriptionService subscriptions, AppDataba public class SubscriptionOrderRequest { - [Required] public string OrderId { get; set; } = null!; + [Required] public Guid OrderId { get; set; } } [HttpPost("order/handle")] diff --git a/DysonNetwork.Sphere/Wallet/SubscriptionService.cs b/DysonNetwork.Sphere/Wallet/SubscriptionService.cs index 2e0b19c..8f73fa2 100644 --- a/DysonNetwork.Sphere/Wallet/SubscriptionService.cs +++ b/DysonNetwork.Sphere/Wallet/SubscriptionService.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using DysonNetwork.Sphere.Storage; using Microsoft.EntityFrameworkCore; using NodaTime; @@ -139,16 +140,16 @@ public class SubscriptionService(AppDatabase db, PaymentService payment, ICacheS public async Task HandleSubscriptionOrder(Order order) { if (order.AppIdentifier != SubscriptionOrderIdentifier || order.Status != OrderStatus.Paid || - order.Meta?["subscription_id"] is not string subscriptionId) + order.Meta?["subscription_id"] is not JsonElement subscriptionIdJson) throw new InvalidOperationException("Invalid order."); - var subscriptionIdParsed = Guid.TryParse(subscriptionId, out var parsedSubscriptionId) + var subscriptionId = Guid.TryParse(subscriptionIdJson.ToString(), out var parsedSubscriptionId) ? parsedSubscriptionId : Guid.Empty; - if (subscriptionIdParsed == Guid.Empty) + if (subscriptionId == Guid.Empty) throw new InvalidOperationException("Invalid order."); var subscription = await db.WalletSubscriptions - .Where(s => s.Id == subscriptionIdParsed) + .Where(s => s.Id == subscriptionId) .Include(s => s.Coupon) .FirstOrDefaultAsync(); if (subscription is null) diff --git a/DysonNetwork.Sphere/Wallet/WalletController.cs b/DysonNetwork.Sphere/Wallet/WalletController.cs index 1e59d28..92d9208 100644 --- a/DysonNetwork.Sphere/Wallet/WalletController.cs +++ b/DysonNetwork.Sphere/Wallet/WalletController.cs @@ -1,3 +1,5 @@ +using System.ComponentModel.DataAnnotations; +using DysonNetwork.Sphere.Permission; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -6,7 +8,7 @@ namespace DysonNetwork.Sphere.Wallet; [ApiController] [Route("/wallets")] -public class WalletController(AppDatabase db, WalletService ws) : ControllerBase +public class WalletController(AppDatabase db, WalletService ws, PaymentService payment) : ControllerBase { [HttpPost] [Authorize] @@ -61,4 +63,39 @@ public class WalletController(AppDatabase db, WalletService ws) : ControllerBase return Ok(transactions); } + + public class WalletBalanceRequest + { + public string? Remark { get; set; } + [Required] public decimal Amount { get; set; } + [Required] public string Currency { get; set; } = null!; + [Required] public Guid AccountId { get; set; } + } + + [HttpPost("balance")] + [Authorize] + [RequiredPermission("maintenance", "wallets.balance.modify")] + public async Task> ModifyWalletBalance([FromBody] WalletBalanceRequest request) + { + var wallet = await ws.GetWalletAsync(request.AccountId); + if (wallet is null) return NotFound("Wallet was not found."); + + var transaction = request.Amount >= 0 + ? await payment.CreateTransactionAsync( + payerWalletId: null, + payeeWalletId: wallet.Id, + currency: request.Currency, + amount: request.Amount, + remarks: request.Remark + ) + : await payment.CreateTransactionAsync( + payerWalletId: wallet.Id, + payeeWalletId: null, + currency: request.Currency, + amount: request.Amount, + remarks: request.Remark + ); + + return Ok(transaction); + } } \ No newline at end of file