using Hutopy.Infrastructure.Emailer.Contracts; using Hutopy.Modules.Creators.Contracts; using Hutopy.Modules.Tipping.Contracts; using Hutopy.Modules.Tipping.Data; namespace Hutopy.Modules.Tipping.Services; internal class TipPaymentNotifier( TippingDbContext dbContext, IEmailSender emailSender, ICreatorLookup creatorLookup, ILogger logger) : ITipPaymentNotifier { public async Task NotifyPaymentSucceedAsync( string sessionId, Uri receiptUrl, string customerEmail, CancellationToken ct) { var tip = await dbContext .Tips .SingleOrDefaultAsync( t => t.StripeSessionId == sessionId, ct) .ConfigureAwait(false); if (tip is not null) { tip.Status = TipStatus.Paid; tip.StripeInvoiceUrl = receiptUrl.ToString(); // Store the receipt URL await dbContext .SaveChangesAsync(ct) .ConfigureAwait(false); // Look up creator information var creator = await creatorLookup .GetCreatorAsync(tip.CreatorId, ct) .ConfigureAwait(false); if (!string.IsNullOrEmpty(customerEmail)) { await SendTipConfirmationEmailAsync( customerEmail, creator?.Name ?? "le créateur", tip.Amount, tip.Currency, receiptUrl) .ConfigureAwait(false); // Pass the receipt URL } } else { logger.LogError("Tip with session ID {SessionId} not found", sessionId); } } private async Task SendTipConfirmationEmailAsync( string email, string creatorUsername, decimal amount, string currency, Uri receiptUrl) // Add receipt URL parameter { var subject = $"Merci pour votre soutien à {creatorUsername}"; var message = $"""

{creatorUsername} vous remercie !

Votre paiement de {amount} {currency} a été traité avec succès.

Ce reçu confirme votre soutien à {creatorUsername}. Merci de contribuer à son travail !

Voir le reçu

Cet email sert de reçu pour votre transaction. Nous vous conseillons de le conserver pour vos archives.

Merci d'utiliser Hutopy pour soutenir vos créateurs préférés !

"""; try { await emailSender .SendEmailAsync(email, subject, message) .ConfigureAwait(false); logger.LogInformation("Tip confirmation email sent to {Email} for tip to {Creator}", email, creatorUsername); } catch (Exception ex) { logger.LogError(ex, "Failed to send tip confirmation email to {Email}", email); // Don't throw the exception as this should not fail the payment processing } } }