From 5bf58f0194eae9d1dd04e607f368628b478994ff Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 3 Oct 2025 16:38:01 +0800 Subject: [PATCH] :bug: Fix subscription gift --- .../Startup/BroadcastEventHandler.cs | 54 ++++++++++--------- .../Wallet/SubscriptionService.cs | 28 +--------- 2 files changed, 29 insertions(+), 53 deletions(-) diff --git a/DysonNetwork.Pass/Startup/BroadcastEventHandler.cs b/DysonNetwork.Pass/Startup/BroadcastEventHandler.cs index 83d17a1..631c412 100644 --- a/DysonNetwork.Pass/Startup/BroadcastEventHandler.cs +++ b/DysonNetwork.Pass/Startup/BroadcastEventHandler.cs @@ -53,32 +53,10 @@ public class BroadcastEventHandler( continue; // Handle subscription orders - if (evt.ProductIdentifier.StartsWith(SubscriptionType.StellarProgram)) - { - logger.LogInformation("Handling stellar program order: {OrderId}", evt.OrderId); - - await using var scope = serviceProvider.CreateAsyncScope(); - var db = scope.ServiceProvider.GetRequiredService(); - var subscriptions = scope.ServiceProvider.GetRequiredService(); - - var order = await db.PaymentOrders.FindAsync( - [evt.OrderId], - cancellationToken: stoppingToken - ); - if (order is null) - { - logger.LogWarning("Order with ID {OrderId} not found. Redelivering.", evt.OrderId); - await msg.NakAsync(cancellationToken: stoppingToken); - continue; - } - - await subscriptions.HandleSubscriptionOrder(order); - - logger.LogInformation("Subscription for order {OrderId} handled successfully.", evt.OrderId); - await msg.AckAsync(cancellationToken: stoppingToken); - } - // Handle gift orders - else if (evt.Meta?.TryGetValue("gift_id", out var giftIdValue) == true) + if ( + evt.ProductIdentifier.StartsWith(SubscriptionType.StellarProgram) && + evt.Meta?.TryGetValue("gift_id", out var giftIdValue) == true + ) { logger.LogInformation("Handling gift order: {OrderId}", evt.OrderId); @@ -102,6 +80,30 @@ public class BroadcastEventHandler( logger.LogInformation("Gift for order {OrderId} handled successfully.", evt.OrderId); await msg.AckAsync(cancellationToken: stoppingToken); } + else if (evt.ProductIdentifier.StartsWith(SubscriptionType.StellarProgram)) + { + logger.LogInformation("Handling stellar program order: {OrderId}", evt.OrderId); + + await using var scope = serviceProvider.CreateAsyncScope(); + var db = scope.ServiceProvider.GetRequiredService(); + var subscriptions = scope.ServiceProvider.GetRequiredService(); + + var order = await db.PaymentOrders.FindAsync( + [evt.OrderId], + cancellationToken: stoppingToken + ); + if (order is null) + { + logger.LogWarning("Order with ID {OrderId} not found. Redelivering.", evt.OrderId); + await msg.NakAsync(cancellationToken: stoppingToken); + continue; + } + + await subscriptions.HandleSubscriptionOrder(order); + + logger.LogInformation("Subscription for order {OrderId} handled successfully.", evt.OrderId); + await msg.AckAsync(cancellationToken: stoppingToken); + } else { // Not a subscription or gift order, skip diff --git a/DysonNetwork.Pass/Wallet/SubscriptionService.cs b/DysonNetwork.Pass/Wallet/SubscriptionService.cs index f8f9c84..7fff3e2 100644 --- a/DysonNetwork.Pass/Wallet/SubscriptionService.cs +++ b/DysonNetwork.Pass/Wallet/SubscriptionService.cs @@ -662,33 +662,7 @@ public class SubscriptionService( db.WalletGifts.Add(gift); await db.SaveChangesAsync(); - // Create order and process payment - var order = await payment.CreateOrderAsync( - null, // No specific payee wallet for gifts - subscriptionInfo.Currency, - finalPrice, - appIdentifier: "gift", - productIdentifier: subscriptionIdentifier, - meta: new Dictionary - { - ["gift_id"] = gift.Id.ToString() - } - ); - - // If payment method is in-app wallet, process payment immediately - if (paymentMethod == SubscriptionPaymentMethod.InAppWallet) - { - var gifterWallet = await db.Wallets.FirstOrDefaultAsync(w => w.AccountId == gifter.Id); - if (gifterWallet == null) - throw new InvalidOperationException("Gifter wallet not found."); - - await payment.PayOrderAsync(order.Id, gifterWallet); - - // Mark gift as sent after successful payment - gift.Status = DysonNetwork.Shared.Models.GiftStatus.Sent; - gift.UpdatedAt = SystemClock.Instance.GetCurrentInstant(); - await db.SaveChangesAsync(); - } + gift.Gifter = gifter; return gift; }