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; public class TipPaymentNotifier( TippingDbContext dbContext, IEmailSender emailSender, ICreatorLookup creatorLookup, ILogger logger) : ITipPaymentNotifier { public async Task NotifyPaymentSucceedAsync( string sessionId, string receiptUrl, string customerEmail, CancellationToken ct) { Tip? tip = await dbContext.Tips.SingleOrDefaultAsync( t => t.StripeSessionId == sessionId, ct); if (tip is not null) { tip.Status = TipStatus.Paid; tip.StripeInvoiceUrl = receiptUrl; // Store the receipt URL await dbContext.SaveChangesAsync(ct); // Look up creator information CreatorReference? creator = await creatorLookup.GetCreatorAsync(tip.CreatorId, ct); if (!string.IsNullOrEmpty(customerEmail)) { await SendTipConfirmationEmailAsync( customerEmail, creator?.Name ?? "le créateur", tip.Amount, tip.Currency, receiptUrl); // 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, string receiptUrl) // Add receipt URL parameter { string subject = $"Merci pour votre soutien à {creatorUsername}"; string 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 !

{(string.IsNullOrEmpty(receiptUrl) ? "" : $"""
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); 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 } } }