using Hutopy.Web.Features.Memberships.Infrastructure; using Microsoft.Extensions.Options; using Stripe; namespace Hutopy.Web.Features.Memberships.Handlers; public class StripeWebhookEndpoint( StripeService stripeService, IOptions options) : EndpointWithoutRequest { public override void Configure() { Post("/api/stripe"); AllowAnonymous(); Options(o => o.WithTags("Memberships")); } public override async Task HandleAsync(CancellationToken ct) { using var streamReader = new StreamReader(HttpContext.Request.Body); var json = await streamReader.ReadToEndAsync(ct); var signatureHeader = HttpContext.Request.Headers["Stripe-Signature"]; var stripeEvent = EventUtility.ConstructEvent(json, signatureHeader, options.Value.WebhookSecret); switch (stripeEvent.Type) { case "checkout.session.completed": await stripeService.HandleCheckoutSessionCompleted(stripeEvent, ct); break; case "invoice.payment_succeeded": await stripeService.HandleInvoicePaymentSucceeded(stripeEvent, ct); break; case "invoice.payment_failed": await stripeService.HandleInvoicePaymentFailed(stripeEvent, ct); break; case "customer.subscription.created": await stripeService.HandleCustomerSubscriptionCreated(stripeEvent, ct); break; case "customer.subscription.updated": await stripeService.HandleCustomerSubscriptionUpdated(stripeEvent, ct); break; case "customer.subscription.deleted": await stripeService.HandleCustomerSubscriptionDeleted(stripeEvent, ct); break; } await SendOkAsync(ct); } }