@@ -19,6 +19,8 @@ import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@@ -45,9 +47,6 @@ public class EmailService {
|
||||
@Value("${server.baseurl}")
|
||||
private String baseUrl;
|
||||
|
||||
@Value("${server.front.port}")
|
||||
private String frontPort;
|
||||
|
||||
public void sendEmail(String from, String sendTo, String subject, String body) throws MessagingException {
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||
@@ -76,6 +75,35 @@ public class EmailService {
|
||||
sendEmail("noreply@no-copy.ru", user.getEmail(), standartAdressFrom, htmlContent);
|
||||
}
|
||||
|
||||
public void sendAutoRenewalFailedEmail(User user, String tariffName, Double price)
|
||||
throws MessagingException, IOException {
|
||||
|
||||
Map<String, String> templateData = new HashMap<>();
|
||||
templateData.put("username", user.getFullName());
|
||||
templateData.put("tariff_name", tariffName);
|
||||
templateData.put("price", String.format("%.2f", price));
|
||||
templateData.put("support_email", "support@no-copy.ru");
|
||||
|
||||
String htmlContent = loadAndProcessTemplate(templateData, "templates/failed-template.html");
|
||||
|
||||
sendEmail("noreply@no-copy.ru", user.getEmail(), standartAdressFrom, htmlContent);
|
||||
}
|
||||
|
||||
public void sendAutoRenewalSuccessEmail(User user, String tariffName, Double price, LocalDateTime nextPaymentDate)
|
||||
throws MessagingException, IOException {
|
||||
|
||||
Map<String, String> templateData = new HashMap<>();
|
||||
templateData.put("username", user.getFullName());
|
||||
templateData.put("tariff_name", tariffName);
|
||||
templateData.put("price", String.format("%.2f", price));
|
||||
templateData.put("renewal_date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm")));
|
||||
templateData.put("next_payment_date", nextPaymentDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy")));
|
||||
|
||||
String htmlContent = loadAndProcessTemplate(templateData, "templates/auto-renewal-success.html");
|
||||
|
||||
sendEmail("noreply@no-copy.ru", user.getEmail(), standartAdressFrom, htmlContent);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public EmailVerificationToken createEmailVerificationToken(AuthToken authToken) {
|
||||
EmailVerificationToken existingToken = emailVerificationTokenRepository
|
||||
|
||||
@@ -5,7 +5,10 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.ReferralService;
|
||||
import ru.soune.nocopy.client.YooKassaClient;
|
||||
import ru.soune.nocopy.dto.payment.PaymentMethodDTO;
|
||||
import ru.soune.nocopy.entity.payment.Payment;
|
||||
import ru.soune.nocopy.entity.payment.PaymentMethod;
|
||||
import ru.soune.nocopy.entity.payment.PaymentOperationType;
|
||||
import ru.soune.nocopy.entity.payment.PaymentStatus;
|
||||
import ru.soune.nocopy.entity.tarif.Tariff;
|
||||
@@ -15,15 +18,13 @@ import ru.soune.nocopy.entity.user.User;
|
||||
import ru.soune.nocopy.exception.PaymentNotFoundException;
|
||||
import ru.soune.nocopy.exception.TariffNotFoundException;
|
||||
import ru.soune.nocopy.exception.UserNotFoundException;
|
||||
import ru.soune.nocopy.repository.PaymentRepository;
|
||||
import ru.soune.nocopy.repository.TariffInfoRepository;
|
||||
import ru.soune.nocopy.repository.TariffRepository;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
import ru.soune.nocopy.repository.*;
|
||||
import ru.soune.nocopy.service.tariff.TariffInfoService;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -41,6 +42,10 @@ public class PaymentService {
|
||||
|
||||
private final ReferralService referralService;
|
||||
|
||||
private final PaymentMethodRepository paymentMethodRepository;
|
||||
|
||||
private final YooKassaClient yooKassaClient;
|
||||
|
||||
@Transactional
|
||||
public Payment createPayment(String email, Long tariffId, String operationType, String paymentUuid) {
|
||||
User user = userRepository.findByEmail(email);
|
||||
@@ -63,6 +68,72 @@ public class PaymentService {
|
||||
return paymentRepository.save(payment);
|
||||
}
|
||||
|
||||
private void savePaymentMethod(User user, Map<String, Object> paymentMethodData) {
|
||||
if (paymentMethodData == null) return;
|
||||
|
||||
String paymentMethodId = (String) paymentMethodData.get("id");
|
||||
if (paymentMethodRepository.findByPaymentMethodId(paymentMethodId).isPresent()) return;
|
||||
|
||||
Map<String, Object> card = (Map<String, Object>) paymentMethodData.get("card");
|
||||
if (card == null) return;
|
||||
|
||||
PaymentMethod pm = new PaymentMethod();
|
||||
pm.setUser(user);
|
||||
pm.setPaymentMethodId(paymentMethodId);
|
||||
pm.setLastFour((String) card.get("last4"));
|
||||
pm.setCardType((String) card.get("card_type"));
|
||||
pm.setExpiryMonth((String) card.get("expiry_month"));
|
||||
pm.setExpiryYear((String) card.get("expiry_year"));
|
||||
pm.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
paymentMethodRepository.save(pm);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<PaymentMethodDTO> getUserPaymentMethods(String email) {
|
||||
User user = userRepository.findByEmail(email);
|
||||
if (user == null) throw new UserNotFoundException(email);
|
||||
|
||||
return paymentMethodRepository.findByUserAndActiveTrue(user)
|
||||
.stream()
|
||||
.map(pm -> new PaymentMethodDTO(
|
||||
pm.getPaymentMethodId(),
|
||||
pm.getLastFour(),
|
||||
pm.getCardType(),
|
||||
pm.getExpiryMonth(),
|
||||
pm.getExpiryYear()
|
||||
))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deletePaymentMethod(String email, String paymentMethodId) {
|
||||
User user = userRepository.findByEmail(email);
|
||||
if (user == null) throw new UserNotFoundException(email);
|
||||
|
||||
PaymentMethod pm = paymentMethodRepository.findByPaymentMethodId(paymentMethodId)
|
||||
.orElseThrow(() -> new RuntimeException("Payment method not found"));
|
||||
|
||||
if (!pm.getUser().getId().equals(user.getId())) {
|
||||
throw new RuntimeException("Access denied");
|
||||
}
|
||||
|
||||
pm.setActive(false);
|
||||
paymentMethodRepository.save(pm);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void disableAutoRenewal(String email) {
|
||||
User user = userRepository.findByEmail(email);
|
||||
if (user == null) throw new UserNotFoundException(email);
|
||||
|
||||
TariffInfo tariffInfo = user.getActiveTariffInfo();
|
||||
if (tariffInfo != null) {
|
||||
tariffInfo.setAutoRenewal(false);
|
||||
tariffInfoRepository.save(tariffInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void handlePaymentNotification(Map<String, Object> notification) {
|
||||
String eventType = (String) notification.get("event");
|
||||
@@ -79,7 +150,16 @@ public class PaymentService {
|
||||
|
||||
payment.setStatus(PaymentStatus.SUCCEEDED);
|
||||
payment.setCancellationReason(null);
|
||||
activateTariffForUser(payment.getUser(), tariff);
|
||||
|
||||
Map<String, String> metadata = (Map<String, String>) object.get("metadata");
|
||||
Map<String, Object> paymentMethod = (Map<String, Object>) object.get("payment_method");
|
||||
String paymentMethodId = paymentMethod != null ? (String) paymentMethod.get("id") : null;
|
||||
|
||||
boolean autoRenewal = metadata != null && Boolean.parseBoolean(metadata.get("auto_renewal"));
|
||||
|
||||
activateTariffForUser(payment.getUser(), tariff, autoRenewal, paymentMethodId);
|
||||
|
||||
savePaymentMethod(payment.getUser(), paymentMethod);
|
||||
|
||||
referralService.onUserAccountRefill(payment.getUser().getId(), intPrice);
|
||||
} else if ("payment.canceled".equals(eventType)) {
|
||||
@@ -123,7 +203,7 @@ public class PaymentService {
|
||||
new PaymentNotFoundException("Payment not found"));
|
||||
}
|
||||
|
||||
private void activateTariffForUser(User user, Tariff tariff) {
|
||||
private void activateTariffForUser(User user, Tariff tariff, boolean autoRenewal, String paymentMethodId) {
|
||||
String type = tariff.getTariffAccountType();
|
||||
TariffInfo activeTariffInfo = user.getActiveTariffInfo();
|
||||
|
||||
@@ -140,6 +220,8 @@ public class PaymentService {
|
||||
tariffInfo.setTokens(tariff.getTokens());
|
||||
tariffInfo.setBoughtTokens(activeTariffInfo.getBoughtTokens());
|
||||
tariffInfo.setTariff(tariff);
|
||||
activeTariffInfo.setAutoRenewal(autoRenewal);
|
||||
tariffInfo.setPaymentMethodId(paymentMethodId);
|
||||
|
||||
tariffInfoService.linkUserTariffInfo(user, tariffInfoService.addTariffInfo(tariffInfo));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user