Files
social-media/src/Web/Features/Memberships/Handlers/HandleStripe.cs
2024-10-22 16:41:11 -04:00

52 lines
1.8 KiB
C#

using Hutopy.Web.Features.Memberships.Infrastructure;
using Microsoft.Extensions.Options;
using Stripe;
namespace Hutopy.Web.Features.Memberships.Handlers;
public class StripeWebhookEndpoint(
StripeService stripeService,
IOptions<StripeOptions> 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);
}
}