@@ -0,0 +1,101 @@
|
||||
package ru.soune.nocopy.service.payment;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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;
|
||||
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.service.tariff.TariffInfoService;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PaymentService {
|
||||
private final PaymentRepository paymentRepository;
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
private final TariffRepository tariffRepository;
|
||||
|
||||
private final TariffInfoRepository tariffInfoRepository;
|
||||
|
||||
private final TariffInfoService tariffInfoService;
|
||||
|
||||
@Transactional
|
||||
public Payment createPayment(String email, Long tariffId, String operationType, String paymentUuid) {
|
||||
User user = userRepository.findByEmail(email);
|
||||
Tariff tariff = tariffRepository.findById(tariffId).orElseThrow();
|
||||
|
||||
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) {
|
||||
String eventType = (String) notification.get("event");
|
||||
Map<String, Object> object = (Map<String, Object>) notification.get("object");
|
||||
String paymentUuid = (String) object.get("id");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user