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
|
||||
);
|
||||
}
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
package com.astalange.core.service;
|
||||
|
||||
import com.astalange.core.entity.Adherent;
|
||||
import com.astalange.core.entity.Categorie;
|
||||
import com.astalange.core.entity.Licence;
|
||||
import com.astalange.core.entity.ModePaiement;
|
||||
import com.astalange.core.entity.Paiement;
|
||||
import com.astalange.core.entity.Saison;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.astalange.core.repository.PaiementRepository;
|
||||
import java.util.Optional;
|
||||
|
||||
class PaiementEmailServiceTest {
|
||||
|
||||
private PaiementEmailService paiementEmailService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
paiementEmailService = new PaiementEmailService(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendPaiementConfirmation_Totalite_SimulatedMode() {
|
||||
Adherent adherent = new Adherent();
|
||||
adherent.setNom("DUPONT");
|
||||
adherent.setPrenom("Jean");
|
||||
adherent.setEmail("jean.dupont@example.com");
|
||||
|
||||
Categorie categorie = new Categorie();
|
||||
categorie.setNom("U13");
|
||||
categorie.setTarifBase(new BigDecimal("150.00"));
|
||||
categorie.setTarifExterieur(new BigDecimal("150.00"));
|
||||
|
||||
Saison saison = new Saison();
|
||||
saison.setNom("2026/2027");
|
||||
|
||||
Licence licence = new Licence();
|
||||
licence.setId(1L);
|
||||
licence.setAdherent(adherent);
|
||||
licence.setCategorie(categorie);
|
||||
licence.setSaison(saison);
|
||||
|
||||
ModePaiement modePaiement = new ModePaiement();
|
||||
modePaiement.setId(1L);
|
||||
modePaiement.setNom("Carte bancaire");
|
||||
|
||||
Paiement paiement = new Paiement();
|
||||
paiement.setId(10L);
|
||||
paiement.setLicence(licence);
|
||||
paiement.setMontant(new BigDecimal("150.00"));
|
||||
paiement.setDatePaiement(LocalDate.now());
|
||||
paiement.setModePaiement(modePaiement);
|
||||
|
||||
licence.addPaiement(paiement);
|
||||
|
||||
boolean result = paiementEmailService.sendPaiementConfirmation(paiement);
|
||||
assertTrue(result, "Dev simulation mode should return true when email processing succeeds");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendPaiementConfirmation_Partiel_SimulatedMode() {
|
||||
Adherent adherent = new Adherent();
|
||||
adherent.setNom("MARTIN");
|
||||
adherent.setPrenom("Sophie");
|
||||
adherent.setEmail("sophie.martin@example.com");
|
||||
|
||||
Categorie categorie = new Categorie();
|
||||
categorie.setNom("Senior");
|
||||
categorie.setTarifBase(new BigDecimal("200.00"));
|
||||
categorie.setTarifExterieur(new BigDecimal("200.00"));
|
||||
|
||||
Saison saison = new Saison();
|
||||
saison.setNom("2026/2027");
|
||||
|
||||
Licence licence = new Licence();
|
||||
licence.setId(2L);
|
||||
licence.setAdherent(adherent);
|
||||
licence.setCategorie(categorie);
|
||||
licence.setSaison(saison);
|
||||
|
||||
ModePaiement modePaiement = new ModePaiement();
|
||||
modePaiement.setId(2L);
|
||||
modePaiement.setNom("Espèces");
|
||||
|
||||
Paiement paiement = new Paiement();
|
||||
paiement.setId(11L);
|
||||
paiement.setLicence(licence);
|
||||
paiement.setMontant(new BigDecimal("80.00"));
|
||||
paiement.setDatePaiement(LocalDate.now());
|
||||
paiement.setModePaiement(modePaiement);
|
||||
|
||||
licence.addPaiement(paiement);
|
||||
|
||||
boolean result = paiementEmailService.sendPaiementConfirmation(paiement);
|
||||
assertTrue(result);
|
||||
assertEquals(new BigDecimal("120.00"), licence.getResteAPayer(), "Remaining amount should be 120.00 €");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendPaiementConfirmation_SansEmail() {
|
||||
Adherent adherent = new Adherent();
|
||||
adherent.setNom("SANS");
|
||||
adherent.setPrenom("Email");
|
||||
adherent.setEmail(null);
|
||||
|
||||
Licence licence = new Licence();
|
||||
licence.setAdherent(adherent);
|
||||
|
||||
Paiement paiement = new Paiement();
|
||||
paiement.setLicence(licence);
|
||||
|
||||
boolean result = paiementEmailService.sendPaiementConfirmation(paiement);
|
||||
assertFalse(result, "Should return false if adherent has no email");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendPaiementConfirmation_WithJavaMailSender() {
|
||||
org.springframework.mail.javamail.JavaMailSender mailSenderMock = mock(org.springframework.mail.javamail.JavaMailSender.class);
|
||||
MimeMessage mimeMessage = new MimeMessage((jakarta.mail.Session) null);
|
||||
|
||||
when(mailSenderMock.createMimeMessage()).thenReturn(mimeMessage);
|
||||
|
||||
PaiementEmailService serviceWithMailSender = new PaiementEmailService(mailSenderMock, null);
|
||||
|
||||
Adherent adherent = new Adherent();
|
||||
adherent.setNom("DURAND");
|
||||
adherent.setPrenom("Paul");
|
||||
adherent.setEmail("paul.durand@example.com");
|
||||
|
||||
Categorie categorie = new Categorie();
|
||||
categorie.setNom("U11");
|
||||
categorie.setTarifBase(new BigDecimal("100.00"));
|
||||
categorie.setTarifExterieur(new BigDecimal("100.00"));
|
||||
|
||||
Saison saison = new Saison();
|
||||
saison.setNom("2026/2027");
|
||||
|
||||
Licence licence = new Licence();
|
||||
licence.setId(3L);
|
||||
licence.setAdherent(adherent);
|
||||
licence.setCategorie(categorie);
|
||||
licence.setSaison(saison);
|
||||
|
||||
ModePaiement modePaiement = new ModePaiement();
|
||||
modePaiement.setId(1L);
|
||||
modePaiement.setNom("Chèque");
|
||||
|
||||
Paiement paiement = new Paiement();
|
||||
paiement.setId(12L);
|
||||
paiement.setLicence(licence);
|
||||
paiement.setMontant(new BigDecimal("100.00"));
|
||||
paiement.setDatePaiement(LocalDate.now());
|
||||
paiement.setModePaiement(modePaiement);
|
||||
paiement.setNumeroCheque("CHK-999");
|
||||
|
||||
licence.addPaiement(paiement);
|
||||
|
||||
boolean result = serviceWithMailSender.sendPaiementConfirmation(paiement);
|
||||
|
||||
assertTrue(result);
|
||||
verify(mailSenderMock, timeout(2000).times(1)).send(any(MimeMessage.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user