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.Memberships.Contracts;
using Hutopy.Modules.Tipping.Contracts; using Hutopy.Modules.Tipping.Contracts;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Stripe; using Stripe;
using Stripe.Checkout; using Stripe.Checkout;
@@ -12,7 +11,8 @@ namespace Hutopy.Modules.Memberships.Handlers;
public class StripeWebhookEndpoint( public class StripeWebhookEndpoint(
ITipPaymentNotifier tipPaymentNotifier, ITipPaymentNotifier tipPaymentNotifier,
IMembershipNotifier membershipNotifier, IMembershipNotifier membershipNotifier,
IOptions<StripeOptions> options) IOptions<StripeOptions> options,
ILogger<StripeWebhookEndpoint> logger)
: EndpointWithoutRequest : EndpointWithoutRequest
{ {
public override void Configure() public override void Configure()
@@ -25,30 +25,31 @@ public class StripeWebhookEndpoint(
public override async Task HandleAsync(CancellationToken ct) public override async Task HandleAsync(CancellationToken ct)
{ {
using StreamReader streamReader = new(HttpContext.Request.Body); 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"]; var signatureHeader = HttpContext.Request.Headers["Stripe-Signature"];
Event? stripeEvent = EventUtility.ConstructEvent(json, signatureHeader, options.Value.WebhookSecret); var stripeEvent = EventUtility.ConstructEvent(json, signatureHeader, options.Value.WebhookSecret);
Session? stripeSession = stripeEvent.Data.Object as Session; var stripeSession = stripeEvent.Data.Object as Session;
Subscription? stripeSubscription = stripeEvent.Data.Object as Subscription; var stripeSubscription = stripeEvent.Data.Object as Subscription;
switch (stripeEvent.Type) switch (stripeEvent.Type)
{ {
case "checkout.session.completed": case "checkout.session.completed":
Debug.Assert(stripeSession != null); Debug.Assert(stripeSession != null);
logger.LogWarning(stripeSession.ToJson());
switch (stripeSession.Mode) switch (stripeSession.Mode)
{ {
// Check if this is a one-time tip // Check if this is a one-time tip
case "payment" when stripeSession.PaymentIntentId != null case "payment" when stripeSession.PaymentIntentId != null
&& stripeSession.PaymentIntent.Status == "paid": && stripeSession.PaymentIntent.Status == "paid":
// Get the customer email from the appropriate place // Get the customer email from the appropriate place
string customerEmail = stripeSession.CustomerDetails?.Email ?? var customerEmail = stripeSession.CustomerDetails?.Email ??
stripeSession.Customer?.Email ?? stripeSession.Customer?.Email ??
""; "";
// Get the receipt URL, preferring the one directly on the charge if available // 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( await tipPaymentNotifier.NotifyPaymentSucceedAsync(
stripeSession.Id, stripeSession.Id,
@@ -70,7 +71,7 @@ public class StripeWebhookEndpoint(
break; break;
case "invoice.payment_succeeded": 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 != null);
Debug.Assert(invoice.Subscription != null); Debug.Assert(invoice.Subscription != null);
await membershipNotifier.NotifyPaymentSucceedAsync( await membershipNotifier.NotifyPaymentSucceedAsync(