This commit is contained in:
2024-10-22 16:41:11 -04:00
parent 114a10416a
commit 0c11d0aa5e
25 changed files with 1146 additions and 508 deletions

View File

@@ -1,27 +1,22 @@
using Hutopy.Web.Common;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Features.Memberships.Data;
using Hutopy.Web.Features.Memberships.Services;
using Hutopy.Web.Features.Memberships.Infrastructure;
namespace Hutopy.Web.Features.Memberships.Handlers;
[PublicAPI]
public record SendTipRequest
{
public Guid CreatorId { get; set; }
public required decimal Amount { get; init; }
public required string Currency { get; init; }
public required string Message { get; init; }
public required string CheckoutSuccessUrl { get; init; }
public required string CheckoutCancelledUrl { get; init; }
}
public record SendTipRequest(
Guid CreatorId,
decimal Amount,
string Currency,
string Message,
string CheckoutSuccessUrl,
string CheckoutCancelledUrl);
[PublicAPI]
public class SendTipResponse
{
public required string Status { get; init; }
public required string StripeCheckoutUrl { get; init; }
}
public record SendTipResponse(
string Status,
string StripeCheckoutUrl);
[PublicAPI]
public class SendTipRequestValidator : Validator<SendTipRequest>
@@ -35,11 +30,11 @@ public class SendTipRequestValidator : Validator<SendTipRequest>
RuleFor(x => x.CreatorId)
.NotEmpty()
.WithMessage("Creator ID is required");
RuleFor(x => x.CheckoutSuccessUrl)
.NotEmpty()
.WithMessage("CheckoutSuccessUrl is required");
RuleFor(x => x.CheckoutCancelledUrl)
.NotEmpty()
.WithMessage("CheckoutCancelledUrl is required");
@@ -48,13 +43,13 @@ public class SendTipRequestValidator : Validator<SendTipRequest>
[PublicAPI]
public class SendTipHandler(
MembershipDbContext dbDbContext,
MembershipDbContext dbContext,
StripeService stripeService)
: Endpoint<SendTipRequest, SendTipResponse>
{
public override void Configure()
{
Post("/api/tips/{CreatorId}");
Post("/api/tips");
Options(o => o.WithTags("Memberships"));
}
@@ -62,57 +57,30 @@ public class SendTipHandler(
SendTipRequest req,
CancellationToken ct)
{
var userId = User.GetUserId();
var userName = User.GetName();
var creator = await dbDbContext.Creators.FindAsync(
var creator = await dbContext.Creators.FindAsync(
[req.CreatorId],
cancellationToken: ct);
if (creator == null)
{
await SendNotFoundAsync(ct);
return;
}
var checkoutSession = await stripeService.CreateTipCheckoutSession(
userId,
req.Amount,
req.Currency,
var checkoutSession = await stripeService.CreateTipCheckoutSessionAsync(
User.GetUserId(),
User.GetAlias()!,
creator.Id,
creator.Name,
req.Amount,
req.Currency,
req.Message,
creator.StripeAccountId,
req.CheckoutSuccessUrl,
req.CheckoutCancelledUrl);
dbDbContext.Tips.Add(
new Tip
{
Id = Guid.NewGuid(),
CreatedAt = DateTimeOffset.UtcNow,
TipperId = userId,
TipperName = userName,
CreatorId = creator.Id,
CreatorName = creator.Name,
Amount = req.Amount,
Currency = req.Currency,
Message = req.Message,
StripeSessionId = checkoutSession.Id
});
dbDbContext.Transactions.Add(
new Transaction
{
Id = Guid.NewGuid(),
StripeCheckoutSessionId = checkoutSession.Id,
Amount = req.Amount,
Type = "Tip",
Timestamp = DateTime.UtcNow
});
await dbDbContext.SaveChangesAsync(ct);
await SendAsync(
new SendTipResponse { Status = "Pending", StripeCheckoutUrl = checkoutSession.Url },
new SendTipResponse("Pending", checkoutSession.Url),
cancellation: ct);
}
}