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 p.licence l " +
|
||||||
"LEFT JOIN FETCH l.adherent a " +
|
"LEFT JOIN FETCH l.adherent a " +
|
||||||
"LEFT JOIN FETCH l.categorie c " +
|
"LEFT JOIN FETCH l.categorie c " +
|
||||||
|
"LEFT JOIN FETCH l.saison s " +
|
||||||
"LEFT JOIN FETCH p.modePaiement m " +
|
"LEFT JOIN FETCH p.modePaiement m " +
|
||||||
"ORDER BY p.datePaiement DESC, p.id DESC")
|
"ORDER BY p.datePaiement DESC, p.id DESC")
|
||||||
java.util.List<Paiement> findAllWithAssociations();
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,10 +4,12 @@ import org.springframework.boot.SpringApplication;
|
|||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
|
||||||
@SpringBootApplication(scanBasePackages = "com.astalange")
|
@SpringBootApplication(scanBasePackages = "com.astalange")
|
||||||
@EntityScan(basePackages = "com.astalange.core.entity")
|
@EntityScan(basePackages = "com.astalange.core.entity")
|
||||||
@EnableJpaRepositories(basePackages = "com.astalange.core.repository")
|
@EnableJpaRepositories(basePackages = "com.astalange.core.repository")
|
||||||
|
@EnableAsync
|
||||||
public class AsTalangeApplication {
|
public class AsTalangeApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(AsTalangeApplication.class, args);
|
SpringApplication.run(AsTalangeApplication.class, args);
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ import com.astalange.core.repository.AuditLogPaiementRepository;
|
|||||||
import com.astalange.core.repository.LicenceRepository;
|
import com.astalange.core.repository.LicenceRepository;
|
||||||
import com.astalange.core.repository.ModePaiementRepository;
|
import com.astalange.core.repository.ModePaiementRepository;
|
||||||
import com.astalange.core.repository.PaiementRepository;
|
import com.astalange.core.repository.PaiementRepository;
|
||||||
|
import com.astalange.core.service.PaiementEmailService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
@@ -21,16 +24,24 @@ import java.time.LocalDate;
|
|||||||
@Controller
|
@Controller
|
||||||
public class PaiementController {
|
public class PaiementController {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(PaiementController.class);
|
||||||
|
|
||||||
private final LicenceRepository licenceRepository;
|
private final LicenceRepository licenceRepository;
|
||||||
private final ModePaiementRepository modePaiementRepository;
|
private final ModePaiementRepository modePaiementRepository;
|
||||||
private final PaiementRepository paiementRepository;
|
private final PaiementRepository paiementRepository;
|
||||||
private final AuditLogPaiementRepository auditLogPaiementRepository;
|
private final AuditLogPaiementRepository auditLogPaiementRepository;
|
||||||
|
private final PaiementEmailService paiementEmailService;
|
||||||
|
|
||||||
public PaiementController(LicenceRepository licenceRepository, ModePaiementRepository modePaiementRepository, PaiementRepository paiementRepository, AuditLogPaiementRepository auditLogPaiementRepository) {
|
public PaiementController(LicenceRepository licenceRepository,
|
||||||
|
ModePaiementRepository modePaiementRepository,
|
||||||
|
PaiementRepository paiementRepository,
|
||||||
|
AuditLogPaiementRepository auditLogPaiementRepository,
|
||||||
|
PaiementEmailService paiementEmailService) {
|
||||||
this.licenceRepository = licenceRepository;
|
this.licenceRepository = licenceRepository;
|
||||||
this.modePaiementRepository = modePaiementRepository;
|
this.modePaiementRepository = modePaiementRepository;
|
||||||
this.paiementRepository = paiementRepository;
|
this.paiementRepository = paiementRepository;
|
||||||
this.auditLogPaiementRepository = auditLogPaiementRepository;
|
this.auditLogPaiementRepository = auditLogPaiementRepository;
|
||||||
|
this.paiementEmailService = paiementEmailService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/licences/{id}/paiements")
|
@PostMapping("/licences/{id}/paiements")
|
||||||
@@ -49,6 +60,8 @@ public class PaiementController {
|
|||||||
ModePaiement mode = modePaiementRepository.findById(modePaiementId)
|
ModePaiement mode = modePaiementRepository.findById(modePaiementId)
|
||||||
.orElseThrow(() -> new IllegalArgumentException("Invalid ModePaiement ID"));
|
.orElseThrow(() -> new IllegalArgumentException("Invalid ModePaiement ID"));
|
||||||
|
|
||||||
|
log.info(">>> [PaiementController] Demande de création de paiement reçue pour Licence ID: {}, Adhérent ID: {}, Montant: {} €", id, adherentId, montant);
|
||||||
|
|
||||||
// Validation basique pour ne pas payer plus que le reste à payer
|
// Validation basique pour ne pas payer plus que le reste à payer
|
||||||
if (montant.compareTo(licence.getResteAPayer()) > 0) {
|
if (montant.compareTo(licence.getResteAPayer()) > 0) {
|
||||||
montant = licence.getResteAPayer();
|
montant = licence.getResteAPayer();
|
||||||
@@ -67,14 +80,23 @@ public class PaiementController {
|
|||||||
}
|
}
|
||||||
paiementRepository.save(paiement);
|
paiementRepository.save(paiement);
|
||||||
|
|
||||||
AuditLogPaiement log = new AuditLogPaiement();
|
licence.addPaiement(paiement);
|
||||||
log.setAction("CREATE");
|
|
||||||
log.setUtilisateur(principal != null ? principal.getName() : "Système");
|
AuditLogPaiement auditLog = new AuditLogPaiement();
|
||||||
log.setPaiementId(paiement.getId());
|
auditLog.setAction("CREATE");
|
||||||
log.setMontant(montant);
|
auditLog.setUtilisateur(principal != null ? principal.getName() : "Système");
|
||||||
log.setAdherentNomComplet(licence.getAdherent().getNom() + " " + licence.getAdherent().getPrenom());
|
auditLog.setPaiementId(paiement.getId());
|
||||||
log.setDetails("Création d'un paiement de " + montant + "€ via " + mode.getNom() + " pour la licence " + (licence.getNumeroLicence() != null ? licence.getNumeroLicence() : "sans numéro"));
|
auditLog.setMontant(montant);
|
||||||
auditLogPaiementRepository.save(log);
|
auditLog.setAdherentNomComplet(licence.getAdherent().getNom() + " " + licence.getAdherent().getPrenom());
|
||||||
|
auditLog.setDetails("Création d'un paiement de " + montant + "€ via " + mode.getNom() + " pour la licence " + (licence.getNumeroLicence() != null ? licence.getNumeroLicence() : "sans numéro"));
|
||||||
|
auditLogPaiementRepository.save(auditLog);
|
||||||
|
|
||||||
|
try {
|
||||||
|
log.info("Appel de sendPaiementConfirmation depuis addPaiement pour le paiement ID: {}", paiement.getId());
|
||||||
|
paiementEmailService.sendPaiementConfirmation(paiement);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Erreur lors de l'appel à sendPaiementConfirmation: ", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "redirect:/adherents/" + adherentId + "/edit";
|
return "redirect:/adherents/" + adherentId + "/edit";
|
||||||
@@ -119,14 +141,14 @@ public class PaiementController {
|
|||||||
}
|
}
|
||||||
paiementRepository.save(paiement);
|
paiementRepository.save(paiement);
|
||||||
|
|
||||||
AuditLogPaiement log = new AuditLogPaiement();
|
AuditLogPaiement auditLog = new AuditLogPaiement();
|
||||||
log.setAction("UPDATE");
|
auditLog.setAction("UPDATE");
|
||||||
log.setUtilisateur(principal != null ? principal.getName() : "Système");
|
auditLog.setUtilisateur(principal != null ? principal.getName() : "Système");
|
||||||
log.setPaiementId(paiement.getId());
|
auditLog.setPaiementId(paiement.getId());
|
||||||
log.setMontant(montant);
|
auditLog.setMontant(montant);
|
||||||
log.setAdherentNomComplet(licence.getAdherent().getNom() + " " + licence.getAdherent().getPrenom());
|
auditLog.setAdherentNomComplet(licence.getAdherent().getNom() + " " + licence.getAdherent().getPrenom());
|
||||||
log.setDetails("Modification du paiement: montant " + ancienMontant + "€ -> " + montant + "€, mode " + ancienMode + " -> " + mode.getNom());
|
auditLog.setDetails("Modification du paiement: montant " + ancienMontant + "€ -> " + montant + "€, mode " + ancienMode + " -> " + mode.getNom());
|
||||||
auditLogPaiementRepository.save(log);
|
auditLogPaiementRepository.save(auditLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (redirect != null && !redirect.isEmpty()) {
|
if (redirect != null && !redirect.isEmpty()) {
|
||||||
@@ -146,14 +168,14 @@ public class PaiementController {
|
|||||||
|
|
||||||
Long adherentId = paiement.getLicence().getAdherent().getId();
|
Long adherentId = paiement.getLicence().getAdherent().getId();
|
||||||
|
|
||||||
AuditLogPaiement log = new AuditLogPaiement();
|
AuditLogPaiement auditLog = new AuditLogPaiement();
|
||||||
log.setAction("DELETE");
|
auditLog.setAction("DELETE");
|
||||||
log.setUtilisateur(principal != null ? principal.getName() : "Système");
|
auditLog.setUtilisateur(principal != null ? principal.getName() : "Système");
|
||||||
log.setPaiementId(paiement.getId());
|
auditLog.setPaiementId(paiement.getId());
|
||||||
log.setMontant(paiement.getMontant());
|
auditLog.setMontant(paiement.getMontant());
|
||||||
log.setAdherentNomComplet(paiement.getLicence().getAdherent().getNom() + " " + paiement.getLicence().getAdherent().getPrenom());
|
auditLog.setAdherentNomComplet(paiement.getLicence().getAdherent().getNom() + " " + paiement.getLicence().getAdherent().getPrenom());
|
||||||
log.setDetails("Suppression du paiement de " + paiement.getMontant() + "€ via " + paiement.getModePaiement().getNom());
|
auditLog.setDetails("Suppression du paiement de " + paiement.getMontant() + "€ via " + paiement.getModePaiement().getNom());
|
||||||
auditLogPaiementRepository.save(log);
|
auditLogPaiementRepository.save(auditLog);
|
||||||
|
|
||||||
paiementRepository.delete(paiement);
|
paiementRepository.delete(paiement);
|
||||||
|
|
||||||
@@ -162,4 +184,27 @@ public class PaiementController {
|
|||||||
}
|
}
|
||||||
return "redirect:/adherents/" + adherentId + "/edit";
|
return "redirect:/adherents/" + adherentId + "/edit";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/paiements/{id}/resend-email")
|
||||||
|
public String resendPaiementEmail(
|
||||||
|
@PathVariable Long id,
|
||||||
|
@RequestParam(required = false) String redirect) {
|
||||||
|
|
||||||
|
log.info(">>> [PaiementController] Demande de renvoi d'e-mail reçue pour le paiement ID: {}", id);
|
||||||
|
|
||||||
|
Paiement paiement = paiementRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Invalid Paiement ID"));
|
||||||
|
|
||||||
|
try {
|
||||||
|
log.info("Appel de sendPaiementConfirmation depuis resendPaiementEmail pour le paiement ID: {}", id);
|
||||||
|
paiementEmailService.sendPaiementConfirmation(paiement);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Erreur lors du renvoi de l'e-mail pour le paiement ID {}: ", id, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (redirect != null && !redirect.isEmpty()) {
|
||||||
|
return "redirect:" + redirect;
|
||||||
|
}
|
||||||
|
return "redirect:/adherents/" + paiement.getLicence().getAdherent().getId() + "/edit";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -327,6 +327,14 @@
|
|||||||
<td class="py-2 px-3 text-right font-semibold text-green-600" th:text="${paiement.montant + ' €'}">50.00 €</td>
|
<td class="py-2 px-3 text-right font-semibold text-green-600" th:text="${paiement.montant + ' €'}">50.00 €</td>
|
||||||
<td class="py-2 px-3 text-right pr-4">
|
<td class="py-2 px-3 text-right pr-4">
|
||||||
<div class="flex justify-end items-center space-x-2">
|
<div class="flex justify-end items-center space-x-2">
|
||||||
|
<form th:action="@{/paiements/{id}/resend-email(id=${paiement.id})}" method="post" class="inline m-0" onsubmit="return confirm('Renvoyer l\'e-mail de reçu de paiement à l\'adhérent ?');">
|
||||||
|
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
|
||||||
|
<button type="submit" class="text-emerald-600 hover:text-emerald-900 bg-emerald-50 hover:bg-emerald-100 p-1.5 rounded transition-colors inline-flex items-center" title="Renvoyer le reçu par e-mail">
|
||||||
|
<svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
<button type="button"
|
<button type="button"
|
||||||
th:data-paiement-id="${paiement.id}"
|
th:data-paiement-id="${paiement.id}"
|
||||||
th:data-montant="${paiement.montant}"
|
th:data-montant="${paiement.montant}"
|
||||||
|
|||||||
@@ -93,6 +93,15 @@
|
|||||||
<td class="py-4 px-6 text-right font-semibold text-green-600" th:text="${p.montant + ' €'}">50.00 €</td>
|
<td class="py-4 px-6 text-right font-semibold text-green-600" th:text="${p.montant + ' €'}">50.00 €</td>
|
||||||
<td class="py-4 px-6 text-right pr-6">
|
<td class="py-4 px-6 text-right pr-6">
|
||||||
<div class="flex justify-end items-center space-x-2">
|
<div class="flex justify-end items-center space-x-2">
|
||||||
|
<form th:action="@{/paiements/{id}/resend-email(id=${p.id})}" method="post" class="inline m-0" onsubmit="return confirm('Renvoyer l\'e-mail de reçu de paiement à l\'adhérent ?');">
|
||||||
|
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
|
||||||
|
<input type="hidden" name="redirect" value="/paiements" />
|
||||||
|
<button type="submit" class="text-emerald-600 hover:text-emerald-900 bg-emerald-50 hover:bg-emerald-100 p-1.5 rounded transition-colors inline-flex items-center" title="Renvoyer le reçu par e-mail">
|
||||||
|
<svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
<button type="button"
|
<button type="button"
|
||||||
th:data-paiement-id="${p.id}"
|
th:data-paiement-id="${p.id}"
|
||||||
th:data-montant="${p.montant}"
|
th:data-montant="${p.montant}"
|
||||||
|
|||||||
Reference in New Issue
Block a user