From e2227943849e60f89cd987649106f3b12b53b517 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Wed, 18 Mar 2026 21:26:49 -0500 Subject: [PATCH] fix: return 200 status if payment has already been added to db --- .../Payments/Endpoints/EventsEndpoint.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/GroundsForSupport.Server/Payments/Endpoints/EventsEndpoint.cs b/src/GroundsForSupport.Server/Payments/Endpoints/EventsEndpoint.cs index 01beca9..2ca6f1f 100644 --- a/src/GroundsForSupport.Server/Payments/Endpoints/EventsEndpoint.cs +++ b/src/GroundsForSupport.Server/Payments/Endpoints/EventsEndpoint.cs @@ -3,6 +3,7 @@ using GroundsForSupport.Server.Payments.Stripe; using GroundsForSupport.Server.TextToSpeech; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Stripe; @@ -25,10 +26,11 @@ internal static class EventsEndpoint [FromServices] Context dbContext, [FromServices] TimeProvider timeProvider, [FromServices] IStreamerBotService streamerBotService, - [FromServices] ILogger logger + [FromServices] ILogger logger, + CancellationToken ct ) { - var json = await new StreamReader(httpContext.Request.Body).ReadToEndAsync(); + var json = await new StreamReader(httpContext.Request.Body).ReadToEndAsync(ct); try { @@ -50,6 +52,15 @@ internal static class EventsEndpoint ? metaMessage : 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() { Id = paymentIntent.Id, @@ -59,15 +70,16 @@ internal static class EventsEndpoint CreatedAtUnix = timeProvider.GetUtcNow().ToUnixTimeMilliseconds(), }; - dbContext.Payments.Add(payment); - await dbContext.SaveChangesAsync(); + await dbContext.Payments.AddAsync(payment, ct); + await dbContext.SaveChangesAsync(ct); try { await streamerBotService.TriggerTextToSpeechAsync( name, paymentIntent.AmountReceived, - message + message, + ct ); } catch (Exception ex)