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;
|
|
|
|
|
import ru.soune.nocopy.entity.payment.Payment;
|
|
|
|
|
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-02-18 16:14:17 +07:00
|
|
|
import ru.soune.nocopy.repository.PaymentRepository;
|
|
|
|
|
import ru.soune.nocopy.repository.TariffRepository;
|
|
|
|
|
import ru.soune.nocopy.repository.UserRepository;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
@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;
|
|
|
|
|
|
|
|
|
|
private final TariffInfoService tariffInfoService;
|
|
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public void handlePaymentNotification(Map<String, Object> notification) {
|
2026-02-19 11:27:01 +07:00
|
|
|
log.info("INFO FROM YOKASSA:" + notification.toString());
|
|
|
|
|
|
|
|
|
|
|
2026-02-18 16:14:17 +07:00
|
|
|
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 11:27:01 +07:00
|
|
|
log.info("METADATA:" + (String) object.get("metadata"));
|
2026-02-18 16:14:17 +07:00
|
|
|
Payment payment = paymentRepository.findByPaymentUuid(paymentUuid)
|
|
|
|
|
.orElseThrow(() -> new RuntimeException("Payment not found"));
|
|
|
|
|
|
|
|
|
|
if ("payment.succeeded".equals(eventType)) {
|
|
|
|
|
payment.setStatus(PaymentStatus.SUCCEEDED);
|
|
|
|
|
payment.setCancellationReason(null);
|
|
|
|
|
activateTariffForUser(payment.getUser(), payment.getTariff());
|
|
|
|
|
|
|
|
|
|
} else if ("payment.canceled".equals(eventType)) {
|
|
|
|
|
payment.setStatus(PaymentStatus.CANCELED);
|
|
|
|
|
|
|
|
|
|
Map<String, Object> cancellationDetails = (Map<String, Object>) object.get("cancellation_details");
|
|
|
|
|
if (cancellationDetails != null) {
|
|
|
|
|
String reason = (String) cancellationDetails.get("reason");
|
|
|
|
|
payment.setCancellationReason(reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} 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");
|
|
|
|
|
if (cancellationDetails != null) {
|
|
|
|
|
String reason = (String) cancellationDetails.get("reason");
|
|
|
|
|
payment.setCancellationReason(reason);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-02-18 16:14:17 +07:00
|
|
|
private void activateTariffForUser(User user, Tariff tariff) {
|
|
|
|
|
TariffInfo tariffInfo = new TariffInfo();
|
|
|
|
|
tariffInfo.setStatus(TariffStatus.ACTIVE);
|
|
|
|
|
tariffInfo.setStartTariff(LocalDateTime.now());
|
|
|
|
|
tariffInfo.setEndTariff(LocalDateTime.now().plusMonths(1));
|
|
|
|
|
tariffInfo.setTokens(tariff.getTokens());
|
|
|
|
|
tariffInfo.setTariff(tariff);
|
|
|
|
|
|
|
|
|
|
tariffInfoService.linkUserTariffInfo(user, tariffInfoService.addTariffInfo(tariffInfo));
|
|
|
|
|
}
|
|
|
|
|
}
|