From 8b5343e8f46882986cecac4b11fb082a3fa7d83e Mon Sep 17 00:00:00 2001 From: Jonathan Bourdon Date: Mon, 4 Aug 2025 15:12:02 -0400 Subject: [PATCH] fix(stripe): correct webhook - adds logging --- ...ndleStripe.cs => StripeWebhookEndpoint.cs} | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) rename backend/Modules/Memberships/Handlers/{HandleStripe.cs => StripeWebhookEndpoint.cs} (78%) diff --git a/backend/Modules/Memberships/Handlers/HandleStripe.cs b/backend/Modules/Memberships/Handlers/StripeWebhookEndpoint.cs similarity index 78% rename from backend/Modules/Memberships/Handlers/HandleStripe.cs rename to backend/Modules/Memberships/Handlers/StripeWebhookEndpoint.cs index 1675553..8dc7b8a 100644 --- a/backend/Modules/Memberships/Handlers/HandleStripe.cs +++ b/backend/Modules/Memberships/Handlers/StripeWebhookEndpoint.cs @@ -3,7 +3,6 @@ using Hutopy.Infrastructure.Payments.Stripe.Configuration; using Hutopy.Modules.Memberships.Contracts; using Hutopy.Modules.Tipping.Contracts; using Microsoft.Extensions.Options; -using Microsoft.Extensions.Primitives; using Stripe; using Stripe.Checkout; @@ -12,7 +11,8 @@ namespace Hutopy.Modules.Memberships.Handlers; public class StripeWebhookEndpoint( ITipPaymentNotifier tipPaymentNotifier, IMembershipNotifier membershipNotifier, - IOptions options) + IOptions options, + ILogger logger) : EndpointWithoutRequest { public override void Configure() @@ -25,30 +25,31 @@ public class StripeWebhookEndpoint( public override async Task HandleAsync(CancellationToken ct) { using StreamReader streamReader = new(HttpContext.Request.Body); - string json = await streamReader.ReadToEndAsync(ct); + var json = await streamReader.ReadToEndAsync(ct); - StringValues signatureHeader = HttpContext.Request.Headers["Stripe-Signature"]; - Event? stripeEvent = EventUtility.ConstructEvent(json, signatureHeader, options.Value.WebhookSecret); + var signatureHeader = HttpContext.Request.Headers["Stripe-Signature"]; + var stripeEvent = EventUtility.ConstructEvent(json, signatureHeader, options.Value.WebhookSecret); - Session? stripeSession = stripeEvent.Data.Object as Session; - Subscription? stripeSubscription = stripeEvent.Data.Object as Subscription; + var stripeSession = stripeEvent.Data.Object as Session; + var stripeSubscription = stripeEvent.Data.Object as Subscription; switch (stripeEvent.Type) { case "checkout.session.completed": Debug.Assert(stripeSession != null); + logger.LogWarning(stripeSession.ToJson()); switch (stripeSession.Mode) { // Check if this is a one-time tip case "payment" when stripeSession.PaymentIntentId != null && stripeSession.PaymentIntent.Status == "paid": // Get the customer email from the appropriate place - string customerEmail = stripeSession.CustomerDetails?.Email ?? - stripeSession.Customer?.Email ?? - ""; + var customerEmail = stripeSession.CustomerDetails?.Email ?? + stripeSession.Customer?.Email ?? + ""; // Get the receipt URL, preferring the one directly on the charge if available - string receiptUrl = stripeSession.Invoice?.HostedInvoiceUrl ?? ""; + var receiptUrl = stripeSession.Invoice?.HostedInvoiceUrl ?? ""; await tipPaymentNotifier.NotifyPaymentSucceedAsync( stripeSession.Id, @@ -70,7 +71,7 @@ public class StripeWebhookEndpoint( break; case "invoice.payment_succeeded": - Invoice? invoice = stripeEvent.Data.Object as Invoice; + var invoice = stripeEvent.Data.Object as Invoice; Debug.Assert(invoice != null); Debug.Assert(invoice.Subscription != null); await membershipNotifier.NotifyPaymentSucceedAsync(