feat: add payment confirmation email service and resend actions
This commit is contained in:
@@ -16,7 +16,17 @@ public interface PaiementRepository extends JpaRepository<Paiement, Long> {
|
||||
"LEFT JOIN FETCH p.licence l " +
|
||||
"LEFT JOIN FETCH l.adherent a " +
|
||||
"LEFT JOIN FETCH l.categorie c " +
|
||||
"LEFT JOIN FETCH l.saison s " +
|
||||
"LEFT JOIN FETCH p.modePaiement m " +
|
||||
"ORDER BY p.datePaiement DESC, p.id DESC")
|
||||
java.util.List<Paiement> findAllWithAssociations();
|
||||
|
||||
@Query("SELECT p FROM Paiement p " +
|
||||
"LEFT JOIN FETCH p.licence l " +
|
||||
"LEFT JOIN FETCH l.adherent a " +
|
||||
"LEFT JOIN FETCH l.categorie c " +
|
||||
"LEFT JOIN FETCH l.saison s " +
|
||||
"LEFT JOIN FETCH p.modePaiement m " +
|
||||
"WHERE p.id = :id")
|
||||
java.util.Optional<Paiement> findByIdWithDetails(@org.springframework.data.repository.query.Param("id") Long id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
package com.astalange.core.service;
|
||||
|
||||
import com.astalange.core.entity.Licence;
|
||||
import com.astalange.core.entity.Paiement;
|
||||
import com.astalange.core.repository.PaiementRepository;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Locale;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Service
|
||||
public class PaiementEmailService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(PaiementEmailService.class);
|
||||
|
||||
private final JavaMailSender mailSender;
|
||||
private final PaiementRepository paiementRepository;
|
||||
|
||||
@Value("${spring.mail.username:noreply@as-talange.fr}")
|
||||
private String fromEmail = "noreply@as-talange.fr";
|
||||
|
||||
public PaiementEmailService(@Autowired(required = false) JavaMailSender mailSender,
|
||||
PaiementRepository paiementRepository) {
|
||||
this.mailSender = mailSender;
|
||||
this.paiementRepository = paiementRepository;
|
||||
}
|
||||
|
||||
public void sendPaiementConfirmationAsync(Long paiementId) {
|
||||
if (paiementId == null) {
|
||||
log.warn("Impossible d'envoyer l'e-mail de confirmation : ID de paiement nul.");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Paiement paiement = paiementRepository.findByIdWithDetails(paiementId)
|
||||
.orElseGet(() -> paiementRepository.findById(paiementId).orElse(null));
|
||||
if (paiement == null) {
|
||||
log.warn("Impossible d'envoyer un e-mail de confirmation : paiement ID {} introuvable en base.", paiementId);
|
||||
return;
|
||||
}
|
||||
sendPaiementConfirmation(paiement);
|
||||
} catch (Exception e) {
|
||||
log.error("Erreur lors de l'envoi de l'e-mail de confirmation pour le paiement ID {}: ", paiementId, e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean sendPaiementConfirmation(Paiement paiement) {
|
||||
log.info(">>> [PaiementEmailService] Traitement de la confirmation de paiement pour le paiement ID: {}",
|
||||
paiement != null ? paiement.getId() : null);
|
||||
if (paiement == null || paiement.getLicence() == null) {
|
||||
log.warn("Impossible d'envoyer un e-mail de confirmation : paiement ou licence nulle.");
|
||||
return false;
|
||||
}
|
||||
|
||||
Licence licence = paiement.getLicence();
|
||||
String recipientEmail = licence.getAdherent() != null ? licence.getAdherent().getEmail() : null;
|
||||
log.info(">>> [PaiementEmailService] Adhérent: {} {}, Email: {}",
|
||||
licence.getAdherent() != null ? licence.getAdherent().getPrenom() : "?",
|
||||
licence.getAdherent() != null ? licence.getAdherent().getNom() : "?",
|
||||
recipientEmail);
|
||||
|
||||
if (recipientEmail == null || recipientEmail.trim().isEmpty()) {
|
||||
log.info("Aucune adresse e-mail renseignée pour l'adhérent de la licence ID {}. E-mail non envoyé.", licence.getId());
|
||||
return false;
|
||||
}
|
||||
|
||||
BigDecimal resteAPayer = licence.getResteAPayer();
|
||||
boolean estTotalite = resteAPayer.compareTo(BigDecimal.ZERO) <= 0;
|
||||
|
||||
String adherentNom = licence.getAdherent() != null
|
||||
? licence.getAdherent().getPrenom() + " " + licence.getAdherent().getNom()
|
||||
: "Adhérent";
|
||||
String categorieNom = licence.getCategorie() != null ? licence.getCategorie().getNom() : "AS Talange";
|
||||
String saisonNom = licence.getSaison() != null ? licence.getSaison().getNom() : "";
|
||||
|
||||
String subject = estTotalite
|
||||
? "AS Talange - Confirmation de paiement (Solde intégralement réglé)"
|
||||
: "AS Talange - Confirmation de paiement partiel";
|
||||
|
||||
String htmlContent = buildEmailHtml(paiement, licence, adherentNom, categorieNom, saisonNom, estTotalite, resteAPayer);
|
||||
BigDecimal montantPaiement = paiement.getMontant();
|
||||
|
||||
// Envoi asynchrone via CompletableFuture pour ne pas bloquer la réponse HTTP
|
||||
CompletableFuture.runAsync(() -> {
|
||||
if (mailSender != null) {
|
||||
try {
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||
helper.setFrom(fromEmail);
|
||||
helper.setTo(recipientEmail);
|
||||
helper.setSubject(subject);
|
||||
helper.setText(htmlContent, true);
|
||||
|
||||
mailSender.send(message);
|
||||
log.info("E-mail de confirmation de paiement envoyé avec succès via SMTP à {} (Montant: {} €)", recipientEmail, montantPaiement);
|
||||
} catch (Exception e) {
|
||||
log.warn("Impossible d'envoyer l'e-mail de paiement via SMTP pour {}: {}. Simulation en mode log.", recipientEmail, e.getMessage());
|
||||
logDevEmail(recipientEmail, subject, htmlContent);
|
||||
}
|
||||
} else {
|
||||
log.info("Aucun JavaMailSender configuré (Mode DEV). Simulation de l'envoi d'e-mail de paiement.");
|
||||
logDevEmail(recipientEmail, subject, htmlContent);
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void logDevEmail(String recipient, String subject, String body) {
|
||||
log.info("==================== [SIMULATION E-MAIL PAIEMENT] ====================");
|
||||
log.info("Destinataire: {}", recipient);
|
||||
log.info("Sujet: {}", subject);
|
||||
log.info("Contenu:\n{}", body);
|
||||
log.info("======================================================================");
|
||||
}
|
||||
|
||||
private String buildEmailHtml(Paiement paiement, Licence licence, String adherentNom,
|
||||
String categorieNom, String saisonNom,
|
||||
boolean estTotalite, BigDecimal resteAPayer) {
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
|
||||
String datePaiementStr = paiement.getDatePaiement() != null
|
||||
? paiement.getDatePaiement().format(dateFormatter)
|
||||
: "";
|
||||
|
||||
String montantStr = String.format(Locale.FRANCE, "%.2f €", paiement.getMontant());
|
||||
String prixTotalStr = String.format(Locale.FRANCE, "%.2f €", licence.getPrixTotal());
|
||||
String totalPayeStr = String.format(Locale.FRANCE, "%.2f €", licence.getSommePayee());
|
||||
String resteStr = String.format(Locale.FRANCE, "%.2f €", resteAPayer);
|
||||
|
||||
String modePaiementStr = paiement.getModePaiement() != null ? paiement.getModePaiement().getNom() : "-";
|
||||
if (paiement.getNumeroCheque() != null && !paiement.getNumeroCheque().isBlank()) {
|
||||
modePaiementStr += " (N° " + paiement.getNumeroCheque() + ")";
|
||||
}
|
||||
|
||||
String headerTitle = estTotalite ? "Confirmation de paiement intégral" : "Confirmation de paiement partiel";
|
||||
|
||||
String messageParagraph;
|
||||
if (estTotalite) {
|
||||
messageParagraph = """
|
||||
<p>Nous vous confirmons la bonne réception de votre versement de <strong>%s</strong> effectué le <strong>%s</strong> par <strong>%s</strong>.</p>
|
||||
<div class="alert alert-success">
|
||||
<strong>Paiement solde :</strong> Le paiement de votre cotisation pour la saison <strong>%s</strong> (catégorie <strong>%s</strong>) a été pris en compte et est désormais <strong>entièrement réglé</strong>.
|
||||
</div>
|
||||
""".formatted(montantStr, datePaiementStr, modePaiementStr, saisonNom, categorieNom);
|
||||
} else {
|
||||
messageParagraph = """
|
||||
<p>Nous vous confirmons la bonne réception de votre versement partiel de <strong>%s</strong> effectué le <strong>%s</strong> par <strong>%s</strong>.</p>
|
||||
<p>Ce paiement a bien été pris en compte pour la cotisation de la saison <strong>%s</strong> (catégorie <strong>%s</strong>).</p>
|
||||
<div class="alert alert-warning">
|
||||
<strong>Information Solde :</strong> Il reste actuellement la somme de <strong>%s</strong> à régler.
|
||||
</div>
|
||||
""".formatted(montantStr, datePaiementStr, modePaiementStr, saisonNom, categorieNom, resteStr);
|
||||
}
|
||||
|
||||
String soldeBadge = estTotalite
|
||||
? "<span class=\"badge badge-success\">0,00 € (Réglé)</span>"
|
||||
: "<span class=\"badge badge-warning\">" + resteStr + "</span>";
|
||||
|
||||
return """
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; background-color: #f4f6f8; margin: 0; padding: 20px; color: #333; }
|
||||
.container { max-width: 600px; margin: 0 auto; background: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
|
||||
.header { background-color: #1e3a8a; color: #ffffff; padding: 24px; text-align: center; }
|
||||
.header h1 { margin: 0; font-size: 22px; }
|
||||
.content { padding: 24px; line-height: 1.6; }
|
||||
.alert { padding: 14px 18px; border-radius: 6px; margin: 18px 0; font-size: 14px; }
|
||||
.alert-success { background-color: #d1fae5; border-left: 4px solid #10b981; color: #065f46; }
|
||||
.alert-warning { background-color: #fef3c7; border-left: 4px solid #f59e0b; color: #92400e; }
|
||||
.receipt-box { background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 6px; padding: 16px; margin-top: 20px; }
|
||||
.receipt-box h3 { margin-top: 0; margin-bottom: 12px; font-size: 16px; color: #1e3a8a; border-bottom: 1px solid #e5e7eb; padding-bottom: 8px; }
|
||||
.receipt-table { width: 100%%; border-collapse: collapse; font-size: 14px; }
|
||||
.receipt-table td { padding: 6px 0; }
|
||||
.receipt-table td.label { color: #6b7280; width: 50%%; }
|
||||
.receipt-table td.value { font-weight: bold; text-align: right; }
|
||||
.badge { display: inline-block; padding: 4px 10px; border-radius: 12px; font-weight: bold; font-size: 13px; }
|
||||
.badge-success { background-color: #d1fae5; color: #065f46; }
|
||||
.badge-warning { background-color: #fef3c7; color: #92400e; }
|
||||
.footer { background: #f9fafb; border-top: 1px solid #e5e7eb; padding: 16px; text-align: center; font-size: 12px; color: #6b7280; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<h1>AS Talange - %s</h1>
|
||||
</div>
|
||||
<div class="content">
|
||||
<p>Bonjour <strong>%s</strong>,</p>
|
||||
|
||||
%s
|
||||
|
||||
<div class="receipt-box">
|
||||
<h3>Reçu de Paiement</h3>
|
||||
<table class="receipt-table">
|
||||
<tr>
|
||||
<td class="label">Adhérent :</td>
|
||||
<td class="value">%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="label">Saison / Catégorie :</td>
|
||||
<td class="value">%s %s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="label">Date du paiement :</td>
|
||||
<td class="value">%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="label">Montant versé :</td>
|
||||
<td class="value">%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="label">Mode de règlement :</td>
|
||||
<td class="value">%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="label">Montant total cotisation :</td>
|
||||
<td class="value">%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="label">Total versé à ce jour :</td>
|
||||
<td class="value">%s</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="label">Reste à payer :</td>
|
||||
<td class="value">%s</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<p style="margin-top: 20px;">Ce courriel vous sert de justificatif de paiement.<br>Sportivement,<br><strong>L'équipe de l'AS Talange</strong></p>
|
||||
</div>
|
||||
<div class="footer">
|
||||
Cet e-mail a été envoyé automatiquement par le système de gestion de l'AS Talange.
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
""".formatted(
|
||||
headerTitle,
|
||||
adherentNom,
|
||||
messageParagraph,
|
||||
adherentNom,
|
||||
saisonNom,
|
||||
categorieNom,
|
||||
datePaiementStr,
|
||||
montantStr,
|
||||
modePaiementStr,
|
||||
prixTotalStr,
|
||||
totalPayeStr,
|
||||
soldeBadge
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user