✨ Complete subscriptions
This commit is contained in:
parent
698442ad13
commit
de7a2cea09
@ -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")]
|
||||
|
@ -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<Subscription> 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)
|
||||
|
@ -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<ActionResult<Transaction>> 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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user