Fix the duplicate issue

This commit is contained in:
Cy Okeke 2025-12-09 13:09:02 +01:00
parent 2e0a4161be
commit c837c22caa
No known key found for this signature in database
GPG Key ID: 88B341B55C84B45C

View File

@ -289,20 +289,13 @@ public class StripeEventUtilityService : IStripeEventUtilityService
} }
var btInvoiceAmount = Math.Round(invoice.AmountDue / 100M, 2); var btInvoiceAmount = Math.Round(invoice.AmountDue / 100M, 2);
var existingTransactions = organizationId.HasValue // Check if this invoice already has a Braintree transaction ID to prevent duplicate charges
? await _transactionRepository.GetManyByOrganizationIdAsync(organizationId.Value) if (invoice.Metadata?.ContainsKey("btTransactionId") ?? false)
: userId.HasValue
? await _transactionRepository.GetManyByUserIdAsync(userId.Value)
: await _transactionRepository.GetManyByProviderIdAsync(providerId.Value);
var duplicateTimeSpan = TimeSpan.FromHours(24);
var now = DateTime.UtcNow;
var duplicateTransaction = existingTransactions?
.FirstOrDefault(t => (now - t.CreationDate) < duplicateTimeSpan);
if (duplicateTransaction != null)
{ {
_logger.LogWarning("There is already a recent PayPal transaction ({0}). " + _logger.LogWarning("Invoice {InvoiceId} already has a Braintree transaction ({TransactionId}). " +
"Do not charge again to prevent possible duplicate.", duplicateTransaction.GatewayId); "Do not charge again to prevent duplicate.",
invoice.Id,
invoice.Metadata["btTransactionId"]);
return false; return false;
} }