refactor(auth): cleanup auth module and streamline the registration flow
This commit is contained in:
@@ -3,6 +3,7 @@ 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;
|
||||
|
||||
@@ -18,19 +19,19 @@ public class StripeWebhookEndpoint(
|
||||
{
|
||||
Post("/api/stripe");
|
||||
AllowAnonymous();
|
||||
Options(o => o.WithTags( "Webhooks"));
|
||||
Options(o => o.WithTags("Webhooks"));
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
using var streamReader = new StreamReader(HttpContext.Request.Body);
|
||||
var json = await streamReader.ReadToEndAsync(ct);
|
||||
using StreamReader streamReader = new(HttpContext.Request.Body);
|
||||
string json = await streamReader.ReadToEndAsync(ct);
|
||||
|
||||
var signatureHeader = HttpContext.Request.Headers["Stripe-Signature"];
|
||||
var stripeEvent = EventUtility.ConstructEvent(json, signatureHeader, options.Value.WebhookSecret);
|
||||
StringValues signatureHeader = HttpContext.Request.Headers["Stripe-Signature"];
|
||||
Event? stripeEvent = EventUtility.ConstructEvent(json, signatureHeader, options.Value.WebhookSecret);
|
||||
|
||||
var stripeSession = stripeEvent.Data.Object as Session;
|
||||
var stripeSubscription = stripeEvent.Data.Object as Subscription;
|
||||
Session? stripeSession = stripeEvent.Data.Object as Session;
|
||||
Subscription? stripeSubscription = stripeEvent.Data.Object as Subscription;
|
||||
|
||||
switch (stripeEvent.Type)
|
||||
{
|
||||
@@ -41,11 +42,23 @@ public class StripeWebhookEndpoint(
|
||||
// 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 ??
|
||||
"";
|
||||
|
||||
// Get the receipt URL, preferring the one directly on the charge if available
|
||||
string receiptUrl = stripeSession.PaymentIntent?.Charges?.Data.FirstOrDefault()?.ReceiptUrl ??
|
||||
stripeSession.Invoice?.HostedInvoiceUrl ??
|
||||
"";
|
||||
|
||||
await tipPaymentNotifier.NotifyPaymentSucceedAsync(
|
||||
stripeSession.Id,
|
||||
stripeSession.Invoice.HostedInvoiceUrl,
|
||||
receiptUrl,
|
||||
customerEmail,
|
||||
ct);
|
||||
break;
|
||||
|
||||
// Check if this is a subscription
|
||||
case "subscription" when stripeSession.SubscriptionId != null:
|
||||
await membershipNotifier.NotifyPaymentSucceedAsync(
|
||||
@@ -53,13 +66,13 @@ public class StripeWebhookEndpoint(
|
||||
stripeSession.Invoice.HostedInvoiceUrl,
|
||||
stripeSession.Invoice.Total,
|
||||
stripeSession.Invoice.Currency,
|
||||
cancellationToken: ct);
|
||||
ct);
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
case "invoice.payment_succeeded":
|
||||
var invoice = (stripeEvent.Data.Object as Invoice);
|
||||
Invoice? invoice = stripeEvent.Data.Object as Invoice;
|
||||
Debug.Assert(invoice != null);
|
||||
Debug.Assert(invoice.Subscription != null);
|
||||
await membershipNotifier.NotifyPaymentSucceedAsync(
|
||||
@@ -67,7 +80,7 @@ public class StripeWebhookEndpoint(
|
||||
invoice.HostedInvoiceUrl,
|
||||
invoice.Total,
|
||||
invoice.Currency,
|
||||
cancellationToken: ct);
|
||||
ct);
|
||||
break;
|
||||
|
||||
case "customer.subscription.updated":
|
||||
|
||||
Reference in New Issue
Block a user