From 244e87ed1864d23c238f9379f31b1b3793d70419 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Thu, 13 Jul 2023 21:29:20 +0200 Subject: [PATCH] Implemented a basic stripe interation --- .../Api/Moonlight/BillingController.cs | 11 +++++++ .../Background/DiscordNotificationService.cs | 16 ++++++++++ Moonlight/App/Services/BillingService.cs | 31 +++++++++++++++---- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/Moonlight/App/Http/Controllers/Api/Moonlight/BillingController.cs b/Moonlight/App/Http/Controllers/Api/Moonlight/BillingController.cs index 0c9990c5..9878b736 100644 --- a/Moonlight/App/Http/Controllers/Api/Moonlight/BillingController.cs +++ b/Moonlight/App/Http/Controllers/Api/Moonlight/BillingController.cs @@ -21,6 +21,17 @@ public class BillingController : Controller BillingService = billingService; } + [HttpGet("cancel")] + public async Task Cancel() + { + var user = await IdentityService.Get(); + + if (user == null) + return Redirect("/login"); + + return Redirect("/profile/subscriptions/close"); + } + [HttpGet("success")] public async Task Success() { diff --git a/Moonlight/App/Services/Background/DiscordNotificationService.cs b/Moonlight/App/Services/Background/DiscordNotificationService.cs index f4257d52..014b9c66 100644 --- a/Moonlight/App/Services/Background/DiscordNotificationService.cs +++ b/Moonlight/App/Services/Background/DiscordNotificationService.cs @@ -35,6 +35,7 @@ public class DiscordNotificationService Event.On("supportChat.message", this, OnSupportChatMessage); Event.On("supportChat.close", this, OnSupportChatClose); Event.On("user.rating", this, OnUserRated); + Event.On("billing.completed", this, OnBillingCompleted); } else { @@ -42,6 +43,21 @@ public class DiscordNotificationService } } + private async Task OnBillingCompleted(User user) + { + await SendNotification("", builder => + { + builder.Color = Color.Red; + builder.Title = "New payment received"; + + builder.AddField("User", user.Email); + builder.AddField("Firstname", user.FirstName); + builder.AddField("Lastname", user.LastName); + builder.AddField("Amount", user.CurrentSubscription!.Price); + builder.AddField("Currency", user.CurrentSubscription!.Currency); + }); + } + private async Task OnUserRated(User user) { await SendNotification("", builder => diff --git a/Moonlight/App/Services/BillingService.cs b/Moonlight/App/Services/BillingService.cs index 6ef407f5..9b13af1a 100644 --- a/Moonlight/App/Services/BillingService.cs +++ b/Moonlight/App/Services/BillingService.cs @@ -1,7 +1,9 @@ -using Moonlight.App.Database.Entities; +using System.Globalization; +using Moonlight.App.Database.Entities; using Moonlight.App.Events; using Moonlight.App.Exceptions; using Moonlight.App.Repositories; +using Moonlight.App.Services.Mail; using Moonlight.App.Services.Sessions; using Stripe.Checkout; using Subscription = Moonlight.App.Database.Entities.Subscription; @@ -15,19 +17,22 @@ public class BillingService private readonly Repository SubscriptionRepository; private readonly SessionServerService SessionServerService; private readonly EventSystem Event; + private readonly MailService MailService; public BillingService( ConfigService configService, SubscriptionService subscriptionService, Repository subscriptionRepository, EventSystem eventSystem, - SessionServerService sessionServerService) + SessionServerService sessionServerService, + MailService mailService) { ConfigService = configService; SubscriptionService = subscriptionService; SubscriptionRepository = subscriptionRepository; Event = eventSystem; SessionServerService = sessionServerService; + MailService = mailService; } public async Task StartCheckout(User user, Subscription subscription) @@ -97,12 +102,26 @@ public class BillingService if (subscription == null) throw new DisplayException("No subscription for this product found"); - if (await SubscriptionService.GetActiveSubscription(user) != null) - { - return; - } + // if (await SubscriptionService.GetActiveSubscription(user) != null) + // { + // return; + // } await SubscriptionService.SetActiveSubscription(user, subscription); + + await MailService.SendMail(user, "checkoutComplete", values => + { + values.Add("SubscriptionName", subscription.Name); + values.Add("SubscriptionPrice", subscription.Price + .ToString(CultureInfo.InvariantCulture)); + values.Add("SubscriptionCurrency", subscription.Currency + .ToString()); + values.Add("SubscriptionDuration", subscription.Duration + .ToString(CultureInfo.InvariantCulture)); + }); + + await Event.Emit("billing.completed", user); + await SessionServerService.ReloadUserSessions(user); } } \ No newline at end of file