Files
social-media/backend/Modules/Tipping/Services/TipPaymentNotifier.cs

121 lines
4.7 KiB
C#

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<TipPaymentNotifier> 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 / 100m,
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 = $"""
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; color: #333;">
<h1 style="color: #2c3e50; margin-bottom: 20px;">{creatorUsername} vous remercie !</h1>
<p style="font-size: 16px; line-height: 1.5; margin-bottom: 15px;">
Votre paiement de <strong>{amount} {currency}</strong> a é traité avec succès.
</p>
<div style="background-color: #f8f9fa; border-radius: 4px; padding: 20px; margin: 30px 0; border-left: 4px solid #3498db;">
<p style="font-size: 16px; margin: 0; line-height: 1.5;">
Ce reçu confirme votre soutien à <strong>{creatorUsername}</strong>. Merci de contribuer à son travail !
</p>
</div>
<div style="text-align: center; margin: 30px 0;">
<a href='{receiptUrl}'
style="background-color: #3498db;
color: white;
text-decoration: none;
padding: 12px 24px;
border-radius: 4px;
font-weight: bold;
display: inline-block;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
Voir le reçu
</a>
</div>
<p style="font-size: 14px; color: #7f8c8d; margin-top: 30px;">
Cet email sert de reçu pour votre transaction. Nous vous conseillons de le conserver pour vos archives.
</p>
<p style="font-size: 14px; color: #7f8c8d; margin-top: 20px; text-align: center; border-top: 1px solid #eee; padding-top: 20px;">
Merci d'utiliser Hutopy pour soutenir vos créateurs préférés !
</p>
</div>
""";
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
}
}
}