fix: return 200 status if payment has already been added to db

This commit is contained in:
Stevan Freeborn
2026-03-18 21:29:23 -05:00
parent 313336aa99
commit e222794384
@@ -3,6 +3,7 @@ using GroundsForSupport.Server.Payments.Stripe;
using GroundsForSupport.Server.TextToSpeech; using GroundsForSupport.Server.TextToSpeech;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Stripe; using Stripe;
@@ -25,10 +26,11 @@ internal static class EventsEndpoint
[FromServices] Context dbContext, [FromServices] Context dbContext,
[FromServices] TimeProvider timeProvider, [FromServices] TimeProvider timeProvider,
[FromServices] IStreamerBotService streamerBotService, [FromServices] IStreamerBotService streamerBotService,
[FromServices] ILogger<Program> logger [FromServices] ILogger<Program> logger,
CancellationToken ct
) )
{ {
var json = await new StreamReader(httpContext.Request.Body).ReadToEndAsync(); var json = await new StreamReader(httpContext.Request.Body).ReadToEndAsync(ct);
try try
{ {
@@ -50,6 +52,15 @@ internal static class EventsEndpoint
? metaMessage ? metaMessage
: string.Empty; : string.Empty;
var existingPayment = await dbContext.Payments
.FirstOrDefaultAsync(p => p.Id == paymentIntent.Id, ct);
if (existingPayment is not null)
{
logger.LogWarning("Payment with ID {PaymentId} already exists. Skipping.", paymentIntent.Id);
return Results.Ok();
}
var payment = new Payment() var payment = new Payment()
{ {
Id = paymentIntent.Id, Id = paymentIntent.Id,
@@ -59,15 +70,16 @@ internal static class EventsEndpoint
CreatedAtUnix = timeProvider.GetUtcNow().ToUnixTimeMilliseconds(), CreatedAtUnix = timeProvider.GetUtcNow().ToUnixTimeMilliseconds(),
}; };
dbContext.Payments.Add(payment); await dbContext.Payments.AddAsync(payment, ct);
await dbContext.SaveChangesAsync(); await dbContext.SaveChangesAsync(ct);
try try
{ {
await streamerBotService.TriggerTextToSpeechAsync( await streamerBotService.TriggerTextToSpeechAsync(
name, name,
paymentIntent.AmountReceived, paymentIntent.AmountReceived,
message message,
ct
); );
} }
catch (Exception ex) catch (Exception ex)