using Hutopy.Infrastructure.Payments.Stripe.Configuration; using Hutopy.Infrastructure.Security; using Hutopy.Modules.Creators.Data; using Microsoft.Extensions.Options; using Stripe; namespace Hutopy.Modules.Creators.Features; [PublicAPI] public record CheckStatusStripeResponse( bool IsStripeAccountPresent, bool IsStripeOnboardingComplete, bool IsStripeChargesEnabled, bool IsStripePayoutReady ); public class CheckStatusStripeIdHandler( IOptionsSnapshot stripeOptions, CreatorsDbContext dbContext) : EndpointWithoutRequest { public override void Configure() { Post("/api/stripe/check-status"); Options(o => o.WithTags("Creators")); } public override async Task HandleAsync( CancellationToken ct) { // 1. Get the creator's information Guid creatorId = HttpContext.User.GetUserId(); // 2. Get or create the creator Creator? creator = await dbContext.Creators.SingleOrDefaultAsync(c => c.Id == creatorId, ct); if (creator is null) { await SendNotFoundAsync(ct); return; } // 3. The Creator is not being onboarded if (string.IsNullOrWhiteSpace(creator.StripeAccountId)) { await SendErrorsAsync(cancellation: ct); return; } // 4. Update Creator's stripe account information StripeConfiguration.ApiKey = stripeOptions.Value.SecretKey; AccountService accountService = new(); Account? account = await accountService.GetAsync(creator.StripeAccountId, cancellationToken: ct); creator.IsStripePayoutReady = account.PayoutsEnabled; creator.IsStripeChargesEnabled = account.ChargesEnabled; creator.IsStripeDetailsSubmitted = account.DetailsSubmitted; await dbContext.SaveChangesAsync(ct); // 6. Return the account link URL to the client await SendOkAsync( new CheckStatusStripeResponse( creator.StripeAccountId != null, creator.IsStripeDetailsSubmitted, creator.IsStripeChargesEnabled, creator.IsStripePayoutReady ), ct); } }