fix(stripe): correct webhook - adds logging

This commit is contained in:
2025-08-04 15:12:02 -04:00
parent 7227913615
commit 8b5343e8f4

View File

@@ -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<StripeOptions> options)
IOptions<StripeOptions> options,
ILogger<StripeWebhookEndpoint> 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 ??
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(