2026-02-18 16:14:17 +07:00
|
|
|
package ru.soune.nocopy.service.payment;
|
|
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
2026-02-19 11:27:01 +07:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2026-02-18 16:14:17 +07:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2026-04-01 15:32:36 +07:00
|
|
|
import ru.soune.ReferralRepo;
|
2026-02-20 20:51:14 +07:00
|
|
|
import ru.soune.ReferralService;
|
2026-03-18 15:27:43 +07:00
|
|
|
import ru.soune.nocopy.client.YooKassaClient;
|
|
|
|
|
import ru.soune.nocopy.dto.payment.PaymentMethodDTO;
|
2026-04-01 15:32:36 +07:00
|
|
|
import ru.soune.nocopy.entity.notification.NotificationType;
|
2026-02-18 16:14:17 +07:00
|
|
|
import ru.soune.nocopy.entity.payment.Payment;
|
2026-03-18 15:27:43 +07:00
|
|
|
import ru.soune.nocopy.entity.payment.PaymentMethod;
|
2026-02-18 16:14:17 +07:00
|
|
|
import ru.soune.nocopy.entity.payment.PaymentOperationType;
|
|
|
|
|
import ru.soune.nocopy.entity.payment.PaymentStatus;
|
|
|
|
|
import ru.soune.nocopy.entity.tarif.Tariff;
|
|
|
|
|
import ru.soune.nocopy.entity.tarif.TariffInfo;
|
|
|
|
|
import ru.soune.nocopy.entity.tarif.TariffStatus;
|
|
|
|
|
import ru.soune.nocopy.entity.user.User;
|
2026-02-18 18:26:11 +07:00
|
|
|
import ru.soune.nocopy.exception.PaymentNotFoundException;
|
2026-02-18 18:42:55 +07:00
|
|
|
import ru.soune.nocopy.exception.TariffNotFoundException;
|
2026-02-18 18:26:11 +07:00
|
|
|
import ru.soune.nocopy.exception.UserNotFoundException;
|
2026-03-18 15:27:43 +07:00
|
|
|
import ru.soune.nocopy.repository.*;
|
2026-04-01 15:32:36 +07:00
|
|
|
import ru.soune.nocopy.service.notification.NotificationService;
|
2026-02-18 16:14:17 +07:00
|
|
|
import ru.soune.nocopy.service.tariff.TariffInfoService;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
2026-02-18 18:26:11 +07:00
|
|
|
import java.util.List;
|
2026-02-18 16:14:17 +07:00
|
|
|
import java.util.Map;
|
2026-03-18 15:27:43 +07:00
|
|
|
import java.util.stream.Collectors;
|
2026-02-18 16:14:17 +07:00
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
2026-02-19 11:27:01 +07:00
|
|
|
@Slf4j
|
2026-02-18 16:14:17 +07:00
|
|
|
public class PaymentService {
|
|
|
|
|
private final PaymentRepository paymentRepository;
|
|
|
|
|
|
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
|
|
|
|
|
private final TariffRepository tariffRepository;
|
|
|
|
|
|
2026-02-19 14:15:59 +07:00
|
|
|
private final TariffInfoRepository tariffInfoRepository;
|
|
|
|
|
|
2026-02-18 16:14:17 +07:00
|
|
|
private final TariffInfoService tariffInfoService;
|
|
|
|
|
|
2026-02-20 20:51:14 +07:00
|
|
|
private final ReferralService referralService;
|
|
|
|
|
|
2026-03-18 15:27:43 +07:00
|
|
|
private final PaymentMethodRepository paymentMethodRepository;
|
|
|
|
|
|
|
|
|
|
private final YooKassaClient yooKassaClient;
|
|
|
|
|
|
2026-04-01 15:32:36 +07:00
|
|
|
private final ReferralRepo referralRepo;
|
|
|
|
|
|
|
|
|
|
private final NotificationService notificationService;
|
|
|
|
|
|
2026-02-18 16:14:17 +07:00
|
|
|
@Transactional
|
|
|
|
|
public Payment createPayment(String email, Long tariffId, String operationType, String paymentUuid) {
|
|
|
|
|
User user = userRepository.findByEmail(email);
|
2026-02-18 18:42:55 +07:00
|
|
|
|
|
|
|
|
if (user == null) {
|
|
|
|
|
throw new UserNotFoundException(email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tariff tariff = tariffRepository.findById(tariffId).orElseThrow(() ->
|
|
|
|
|
new TariffNotFoundException("Tariff not found"));
|
2026-02-18 16:14:17 +07:00
|
|
|
|
|
|
|
|
Payment payment = new Payment();
|
|
|
|
|
payment.setStatus(PaymentStatus.PENDING);
|
|
|
|
|
payment.setAmount(tariff.getPrice());
|
|
|
|
|
payment.setOperationType(PaymentOperationType.valueOf(operationType));
|
|
|
|
|
payment.setUser(user);
|
|
|
|
|
payment.setTariff(tariff);
|
|
|
|
|
payment.setPaymentUuid(paymentUuid);
|
|
|
|
|
|
|
|
|
|
return paymentRepository.save(payment);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 15:27:43 +07:00
|
|
|
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)
|
2026-03-18 16:08:37 +07:00
|
|
|
public List<PaymentMethodDTO> getUserPaymentMethods(Long userId) {
|
|
|
|
|
User user = userRepository.findById(userId).orElseThrow();
|
2026-03-18 15:27:43 +07:00
|
|
|
|
|
|
|
|
return paymentMethodRepository.findByUserAndActiveTrue(user)
|
|
|
|
|
.stream()
|
|
|
|
|
.map(pm -> new PaymentMethodDTO(
|
|
|
|
|
pm.getPaymentMethodId(),
|
|
|
|
|
pm.getLastFour(),
|
|
|
|
|
pm.getCardType(),
|
|
|
|
|
pm.getExpiryMonth(),
|
|
|
|
|
pm.getExpiryYear()
|
|
|
|
|
))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
2026-03-18 16:08:37 +07:00
|
|
|
public void deletePaymentMethod(Long userId, String paymentMethodId) {
|
|
|
|
|
User user = userRepository.findById(userId).orElseThrow();
|
2026-03-18 15:27:43 +07:00
|
|
|
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
|
2026-03-18 16:26:47 +07:00
|
|
|
public void changeAutoRenewal(Long userId, boolean autoRenewal) {
|
2026-03-18 16:08:37 +07:00
|
|
|
User user = userRepository.findById(userId).orElseThrow();
|
2026-03-18 15:27:43 +07:00
|
|
|
|
|
|
|
|
TariffInfo tariffInfo = user.getActiveTariffInfo();
|
|
|
|
|
if (tariffInfo != null) {
|
2026-03-18 16:26:47 +07:00
|
|
|
tariffInfo.setAutoRenewal(autoRenewal);
|
2026-03-18 15:27:43 +07:00
|
|
|
tariffInfoRepository.save(tariffInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 16:14:17 +07:00
|
|
|
@Transactional
|
|
|
|
|
public void handlePaymentNotification(Map<String, Object> notification) {
|
|
|
|
|
String eventType = (String) notification.get("event");
|
|
|
|
|
Map<String, Object> object = (Map<String, Object>) notification.get("object");
|
|
|
|
|
String paymentUuid = (String) object.get("id");
|
2026-02-19 14:15:59 +07:00
|
|
|
|
2026-02-18 16:14:17 +07:00
|
|
|
Payment payment = paymentRepository.findByPaymentUuid(paymentUuid)
|
2026-02-19 15:06:48 +07:00
|
|
|
.orElseThrow(() -> new PaymentNotFoundException("Payment not found"));
|
2026-02-18 16:14:17 +07:00
|
|
|
|
|
|
|
|
if ("payment.succeeded".equals(eventType)) {
|
2026-02-20 20:51:14 +07:00
|
|
|
Tariff tariff = payment.getTariff();
|
|
|
|
|
double price = tariff.getPrice();
|
|
|
|
|
int intPrice = (int) Math.round(price);
|
|
|
|
|
|
2026-02-18 16:14:17 +07:00
|
|
|
payment.setStatus(PaymentStatus.SUCCEEDED);
|
|
|
|
|
payment.setCancellationReason(null);
|
2026-03-18 15:27:43 +07:00
|
|
|
|
|
|
|
|
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);
|
2026-02-20 20:51:14 +07:00
|
|
|
|
|
|
|
|
referralService.onUserAccountRefill(payment.getUser().getId(), intPrice);
|
2026-04-01 15:32:36 +07:00
|
|
|
|
|
|
|
|
long inviterIdForUser = referralRepo.getInviterIdForUser(payment.getUser().getId());
|
|
|
|
|
|
|
|
|
|
notificationService.addNotification(NotificationType.REFERRAL_ACTIVATED, inviterIdForUser);
|
2026-02-18 16:14:17 +07:00
|
|
|
} else if ("payment.canceled".equals(eventType)) {
|
|
|
|
|
payment.setStatus(PaymentStatus.CANCELED);
|
|
|
|
|
|
|
|
|
|
Map<String, Object> cancellationDetails = (Map<String, Object>) object.get("cancellation_details");
|
|
|
|
|
|
2026-04-01 15:32:36 +07:00
|
|
|
String reason = (String) cancellationDetails.get("reason");
|
|
|
|
|
payment.setCancellationReason(reason);
|
|
|
|
|
|
|
|
|
|
notificationService.addNotification(NotificationType.PAYOUT_RESULT, payment.getUser().getId(), reason);
|
2026-02-18 16:14:17 +07:00
|
|
|
} else if ("payment.waiting_for_capture".equals(eventType)) {
|
|
|
|
|
payment.setStatus(PaymentStatus.WAITING);
|
|
|
|
|
payment.setCancellationReason(null);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
payment.setStatus(PaymentStatus.FAILED);
|
|
|
|
|
|
|
|
|
|
Map<String, Object> cancellationDetails = (Map<String, Object>) object.get("cancellation_details");
|
2026-04-01 15:32:36 +07:00
|
|
|
|
|
|
|
|
String reason = (String) cancellationDetails.get("reason");
|
|
|
|
|
payment.setCancellationReason(reason);
|
|
|
|
|
|
|
|
|
|
notificationService.addNotification(NotificationType.PAYOUT_RESULT, payment.getUser().getId(), reason);
|
2026-02-18 16:14:17 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
paymentRepository.save(payment);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 18:26:11 +07:00
|
|
|
public List<Payment> userPayments(String email) {
|
|
|
|
|
User user = userRepository.findByEmail(email);
|
|
|
|
|
|
|
|
|
|
if (user == null) {
|
|
|
|
|
throw new UserNotFoundException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return paymentRepository.findByUserId(user.getId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Payment paymentInfo(String uuid) {
|
|
|
|
|
return paymentRepository.findByPaymentUuid(uuid).orElseThrow(() ->
|
|
|
|
|
new PaymentNotFoundException("Payment not found"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 15:27:43 +07:00
|
|
|
private void activateTariffForUser(User user, Tariff tariff, boolean autoRenewal, String paymentMethodId) {
|
2026-03-10 12:26:43 +07:00
|
|
|
String type = tariff.getTariffAccountType();
|
|
|
|
|
TariffInfo activeTariffInfo = user.getActiveTariffInfo();
|
2026-02-18 16:14:17 +07:00
|
|
|
|
2026-02-19 14:15:59 +07:00
|
|
|
if (type.equals("token")) {
|
2026-03-10 12:26:43 +07:00
|
|
|
Integer tokens = activeTariffInfo.getBoughtTokens();
|
|
|
|
|
activeTariffInfo.setBoughtTokens(tokens + tariff.getTokens());
|
2026-02-19 14:15:59 +07:00
|
|
|
|
|
|
|
|
tariffInfoRepository.save(activeTariffInfo);
|
|
|
|
|
} else {
|
|
|
|
|
TariffInfo tariffInfo = new TariffInfo();
|
|
|
|
|
tariffInfo.setStatus(TariffStatus.ACTIVE);
|
|
|
|
|
tariffInfo.setStartTariff(LocalDateTime.now());
|
|
|
|
|
tariffInfo.setEndTariff(LocalDateTime.now().plusMonths(1));
|
|
|
|
|
tariffInfo.setTokens(tariff.getTokens());
|
2026-03-10 12:26:43 +07:00
|
|
|
tariffInfo.setBoughtTokens(activeTariffInfo.getBoughtTokens());
|
2026-02-19 14:15:59 +07:00
|
|
|
tariffInfo.setTariff(tariff);
|
2026-03-18 15:27:43 +07:00
|
|
|
activeTariffInfo.setAutoRenewal(autoRenewal);
|
|
|
|
|
tariffInfo.setPaymentMethodId(paymentMethodId);
|
2026-02-19 14:15:59 +07:00
|
|
|
|
|
|
|
|
tariffInfoService.linkUserTariffInfo(user, tariffInfoService.addTariffInfo(tariffInfo));
|
|
|
|
|
}
|
2026-02-18 16:14:17 +07:00
|
|
|
}
|
|
|
|
|
}
|