@@ -0,0 +1,69 @@
|
|||||||
|
package ru.soune.nocopy.client;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class YooKassaClient {
|
||||||
|
|
||||||
|
@Value("${yookassa.shop-id}")
|
||||||
|
private String shopId;
|
||||||
|
|
||||||
|
@Value("${yookassa.secret-key}")
|
||||||
|
private String secretKey;
|
||||||
|
|
||||||
|
private final HttpClient httpClient = HttpClient.newHttpClient();
|
||||||
|
|
||||||
|
private final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
public boolean createAutoPayment(String paymentMethodId, double amount, String description, String userId) {
|
||||||
|
try {
|
||||||
|
String url = "https://api.yookassa.ru/v3/payments";
|
||||||
|
String auth = shopId + ":" + secretKey;
|
||||||
|
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
Map<String, Object> body = new HashMap<>();
|
||||||
|
body.put("amount", Map.of("value", String.format("%.2f", amount),
|
||||||
|
"currency", "RUB"));
|
||||||
|
body.put("payment_method_id", paymentMethodId);
|
||||||
|
body.put("description", description);
|
||||||
|
body.put("capture", true);
|
||||||
|
body.put("metadata", Map.of("user_id", userId, "auto_payment", "true"));
|
||||||
|
|
||||||
|
String requestBody = mapper.writeValueAsString(body);
|
||||||
|
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(url))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Authorization", "Basic " + encodedAuth)
|
||||||
|
.header("Idempotence-Key", UUID.randomUUID().toString())
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if (response.statusCode() == 200 || response.statusCode() == 201) {
|
||||||
|
log.info("Auto payment created: {}", response.body());
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
log.error("Auto payment failed: {} - {}", response.statusCode(), response.body());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error creating auto payment", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -52,4 +52,40 @@ public class PaymentController {
|
|||||||
log.error("Error processing payment notification", e);
|
log.error("Error processing payment notification", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/auto-renewal/disable")
|
||||||
|
public ResponseEntity<?> disableAutoRenewal(@RequestParam String email) {
|
||||||
|
try {
|
||||||
|
paymentService.disableAutoRenewal(email);
|
||||||
|
return ResponseEntity.ok(Map.of("message", "Auto-renewal disabled"));
|
||||||
|
} catch (UserNotFoundException e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||||
|
.body(Map.of("error", e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/methods/{paymentMethodId}")
|
||||||
|
public ResponseEntity<?> deletePaymentMethod(@RequestParam String email,
|
||||||
|
@PathVariable String paymentMethodId) {
|
||||||
|
try {
|
||||||
|
paymentService.deletePaymentMethod(email, paymentMethodId);
|
||||||
|
return ResponseEntity.ok(Map.of("message", "Payment method deleted"));
|
||||||
|
} catch (UserNotFoundException e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||||
|
.body(Map.of("error", e.getMessage()));
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body(Map.of("error", e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/methods")
|
||||||
|
public ResponseEntity<?> getPaymentMethods(@RequestParam String email) {
|
||||||
|
try {
|
||||||
|
return ResponseEntity.ok(paymentService.getUserPaymentMethods(email));
|
||||||
|
} catch (UserNotFoundException e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||||
|
.body(Map.of("error", e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package ru.soune.nocopy.dto.payment;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class PaymentMethodDTO {
|
||||||
|
private String paymentMethodId;
|
||||||
|
private String lastFour;
|
||||||
|
private String cardType;
|
||||||
|
private String expiryMonth;
|
||||||
|
private String expiryYear;
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package ru.soune.nocopy.entity.payment;
|
||||||
|
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "payment_methods")
|
||||||
|
@Getter @Setter @NoArgsConstructor @AllArgsConstructor
|
||||||
|
public class PaymentMethod {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "user_id", nullable = false)
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
@Column(name = "payment_method_id", nullable = false, unique = true)
|
||||||
|
private String paymentMethodId;
|
||||||
|
|
||||||
|
@Column(name = "last_four")
|
||||||
|
private String lastFour;
|
||||||
|
|
||||||
|
@Column(name = "card_type")
|
||||||
|
private String cardType;
|
||||||
|
|
||||||
|
@Column(name = "expiry_month")
|
||||||
|
private String expiryMonth;
|
||||||
|
|
||||||
|
@Column(name = "expiry_year")
|
||||||
|
private String expiryYear;
|
||||||
|
|
||||||
|
@Column(name = "active")
|
||||||
|
private boolean active = true;
|
||||||
|
|
||||||
|
@Column(name = "created_at")
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
}
|
||||||
@@ -37,4 +37,10 @@ public class TariffInfo {
|
|||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "tariff_id")
|
@JoinColumn(name = "tariff_id")
|
||||||
private Tariff tariff;
|
private Tariff tariff;
|
||||||
|
|
||||||
|
@Column(name = "auto_renewal")
|
||||||
|
private boolean autoRenewal = false;
|
||||||
|
|
||||||
|
@Column(name = "payment_method_id")
|
||||||
|
private String paymentMethodId;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package ru.soune.nocopy.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import ru.soune.nocopy.entity.payment.PaymentMethod;
|
||||||
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface PaymentMethodRepository extends JpaRepository<PaymentMethod, Long> {
|
||||||
|
List<PaymentMethod> findByUserAndActiveTrue(User user);
|
||||||
|
Optional<PaymentMethod> findByPaymentMethodId(String paymentMethodId);
|
||||||
|
}
|
||||||
@@ -1,9 +1,16 @@
|
|||||||
package ru.soune.nocopy.repository;
|
package ru.soune.nocopy.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
import ru.soune.nocopy.entity.tarif.TariffInfo;
|
import ru.soune.nocopy.entity.tarif.TariffInfo;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface TariffInfoRepository extends JpaRepository<TariffInfo, String> {
|
public interface TariffInfoRepository extends JpaRepository<TariffInfo, String> {
|
||||||
|
@Query("SELECT t FROM TariffInfo t WHERE t.autoRenewal = true AND t.endTariff <= :date")
|
||||||
|
List<TariffInfo> findForAutoRenewal(@Param("date") LocalDateTime date);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package ru.soune.nocopy.repository;
|
|||||||
|
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import ru.soune.nocopy.entity.tarif.TariffInfo;
|
||||||
import ru.soune.nocopy.entity.user.User;
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -12,4 +13,5 @@ public interface UserRepository extends JpaRepository<User, Long> {
|
|||||||
boolean existsByPhone(@Size(min = 11, max = 14) String phone);
|
boolean existsByPhone(@Size(min = 11, max = 14) String phone);
|
||||||
List<User> findByCompanyId(String companyId);
|
List<User> findByCompanyId(String companyId);
|
||||||
long countByCompanyId(String companyId);
|
long countByCompanyId(String companyId);
|
||||||
|
User findByPersonalTariffInfo(TariffInfo tariffInfo);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package ru.soune.nocopy.scheduler;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.soune.nocopy.client.YooKassaClient;
|
||||||
|
import ru.soune.nocopy.entity.tarif.Tariff;
|
||||||
|
import ru.soune.nocopy.entity.tarif.TariffInfo;
|
||||||
|
import ru.soune.nocopy.entity.user.User;
|
||||||
|
import ru.soune.nocopy.repository.TariffInfoRepository;
|
||||||
|
import ru.soune.nocopy.repository.UserRepository;
|
||||||
|
import ru.soune.nocopy.service.mail.EmailService;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class PaymentScheduler {
|
||||||
|
|
||||||
|
private final TariffInfoRepository tariffInfoRepository;
|
||||||
|
|
||||||
|
private final EmailService emailService;
|
||||||
|
|
||||||
|
private final YooKassaClient yooKassaClient;
|
||||||
|
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
@Scheduled(cron = "0 0 1 * * *")
|
||||||
|
@Transactional
|
||||||
|
public void processAutoRenewals() {
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
LocalDateTime renewalDate = now.plusDays(1);
|
||||||
|
List<TariffInfo> forRenewal = tariffInfoRepository.findForAutoRenewal(renewalDate);
|
||||||
|
|
||||||
|
for (TariffInfo tariffInfo : forRenewal) {
|
||||||
|
try {
|
||||||
|
if (tariffInfo.getPaymentMethodId() == null) {
|
||||||
|
tariffInfo.setAutoRenewal(false);
|
||||||
|
tariffInfoRepository.save(tariffInfo);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = userRepository.findByPersonalTariffInfo(tariffInfo);
|
||||||
|
boolean success = yooKassaClient.createAutoPayment(
|
||||||
|
tariffInfo.getPaymentMethodId(),
|
||||||
|
tariffInfo.getTariff().getPrice(),
|
||||||
|
"Auto-renewal " + tariffInfo.getTariff().getName(),
|
||||||
|
String.valueOf(user.getId()));
|
||||||
|
|
||||||
|
Tariff tariff = tariffInfo.getTariff();
|
||||||
|
if (success) {
|
||||||
|
tariffInfo.setStartTariff(now);
|
||||||
|
tariffInfo.setEndTariff(now.plusMonths(1));
|
||||||
|
|
||||||
|
tariffInfoRepository.save(tariffInfo);
|
||||||
|
|
||||||
|
emailService.sendAutoRenewalSuccessEmail(user, tariff.getName(), tariff.getPrice(), renewalDate);
|
||||||
|
} else {
|
||||||
|
emailService.sendAutoRenewalFailedEmail(user, tariff.getName(), tariff.getPrice());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error processing auto-renewal for tariffInfo: " + tariffInfo.getId(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,8 @@ import java.io.IOException;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -45,9 +47,6 @@ public class EmailService {
|
|||||||
@Value("${server.baseurl}")
|
@Value("${server.baseurl}")
|
||||||
private String baseUrl;
|
private String baseUrl;
|
||||||
|
|
||||||
@Value("${server.front.port}")
|
|
||||||
private String frontPort;
|
|
||||||
|
|
||||||
public void sendEmail(String from, String sendTo, String subject, String body) throws MessagingException {
|
public void sendEmail(String from, String sendTo, String subject, String body) throws MessagingException {
|
||||||
MimeMessage message = mailSender.createMimeMessage();
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||||
@@ -76,6 +75,35 @@ public class EmailService {
|
|||||||
sendEmail("noreply@no-copy.ru", user.getEmail(), standartAdressFrom, htmlContent);
|
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
|
@Transactional
|
||||||
public EmailVerificationToken createEmailVerificationToken(AuthToken authToken) {
|
public EmailVerificationToken createEmailVerificationToken(AuthToken authToken) {
|
||||||
EmailVerificationToken existingToken = emailVerificationTokenRepository
|
EmailVerificationToken existingToken = emailVerificationTokenRepository
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.soune.ReferralService;
|
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.Payment;
|
||||||
|
import ru.soune.nocopy.entity.payment.PaymentMethod;
|
||||||
import ru.soune.nocopy.entity.payment.PaymentOperationType;
|
import ru.soune.nocopy.entity.payment.PaymentOperationType;
|
||||||
import ru.soune.nocopy.entity.payment.PaymentStatus;
|
import ru.soune.nocopy.entity.payment.PaymentStatus;
|
||||||
import ru.soune.nocopy.entity.tarif.Tariff;
|
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.PaymentNotFoundException;
|
||||||
import ru.soune.nocopy.exception.TariffNotFoundException;
|
import ru.soune.nocopy.exception.TariffNotFoundException;
|
||||||
import ru.soune.nocopy.exception.UserNotFoundException;
|
import ru.soune.nocopy.exception.UserNotFoundException;
|
||||||
import ru.soune.nocopy.repository.PaymentRepository;
|
import ru.soune.nocopy.repository.*;
|
||||||
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 ru.soune.nocopy.service.tariff.TariffInfoService;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -41,6 +42,10 @@ public class PaymentService {
|
|||||||
|
|
||||||
private final ReferralService referralService;
|
private final ReferralService referralService;
|
||||||
|
|
||||||
|
private final PaymentMethodRepository paymentMethodRepository;
|
||||||
|
|
||||||
|
private final YooKassaClient yooKassaClient;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Payment createPayment(String email, Long tariffId, String operationType, String paymentUuid) {
|
public Payment createPayment(String email, Long tariffId, String operationType, String paymentUuid) {
|
||||||
User user = userRepository.findByEmail(email);
|
User user = userRepository.findByEmail(email);
|
||||||
@@ -63,6 +68,72 @@ public class PaymentService {
|
|||||||
return paymentRepository.save(payment);
|
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
|
@Transactional
|
||||||
public void handlePaymentNotification(Map<String, Object> notification) {
|
public void handlePaymentNotification(Map<String, Object> notification) {
|
||||||
String eventType = (String) notification.get("event");
|
String eventType = (String) notification.get("event");
|
||||||
@@ -79,7 +150,16 @@ public class PaymentService {
|
|||||||
|
|
||||||
payment.setStatus(PaymentStatus.SUCCEEDED);
|
payment.setStatus(PaymentStatus.SUCCEEDED);
|
||||||
payment.setCancellationReason(null);
|
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);
|
referralService.onUserAccountRefill(payment.getUser().getId(), intPrice);
|
||||||
} else if ("payment.canceled".equals(eventType)) {
|
} else if ("payment.canceled".equals(eventType)) {
|
||||||
@@ -123,7 +203,7 @@ public class PaymentService {
|
|||||||
new PaymentNotFoundException("Payment not found"));
|
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();
|
String type = tariff.getTariffAccountType();
|
||||||
TariffInfo activeTariffInfo = user.getActiveTariffInfo();
|
TariffInfo activeTariffInfo = user.getActiveTariffInfo();
|
||||||
|
|
||||||
@@ -140,6 +220,8 @@ public class PaymentService {
|
|||||||
tariffInfo.setTokens(tariff.getTokens());
|
tariffInfo.setTokens(tariff.getTokens());
|
||||||
tariffInfo.setBoughtTokens(activeTariffInfo.getBoughtTokens());
|
tariffInfo.setBoughtTokens(activeTariffInfo.getBoughtTokens());
|
||||||
tariffInfo.setTariff(tariff);
|
tariffInfo.setTariff(tariff);
|
||||||
|
activeTariffInfo.setAutoRenewal(autoRenewal);
|
||||||
|
tariffInfo.setPaymentMethodId(paymentMethodId);
|
||||||
|
|
||||||
tariffInfoService.linkUserTariffInfo(user, tariffInfoService.addTariffInfo(tariffInfo));
|
tariffInfoService.linkUserTariffInfo(user, tariffInfoService.addTariffInfo(tariffInfo));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,6 +107,11 @@ searchapi:
|
|||||||
api-key: ${SEARCHAPI_API_KEY:5jyYZC8jSaxhZTwjMUhwtAXi}
|
api-key: ${SEARCHAPI_API_KEY:5jyYZC8jSaxhZTwjMUhwtAXi}
|
||||||
reverse-image-url: "https://searchapi.io/api/v1/search"
|
reverse-image-url: "https://searchapi.io/api/v1/search"
|
||||||
|
|
||||||
|
yookassa:
|
||||||
|
secret-key: test_0Yns_0NHV5GJf6ypJ5HC4NSfnLO8SJkw-1PwrVWsDl4
|
||||||
|
shop-id: 1276731
|
||||||
|
|
||||||
|
|
||||||
app:
|
app:
|
||||||
email:
|
email:
|
||||||
verification:
|
verification:
|
||||||
|
|||||||
@@ -0,0 +1,169 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Подписка успешно продлена</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||||
|
line-height: 1.6;
|
||||||
|
color: #333;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
max-width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
background: white;
|
||||||
|
border-radius: 12px;
|
||||||
|
overflow: hidden;
|
||||||
|
box-shadow: 0 6px 20px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
.header {
|
||||||
|
background: linear-gradient(135deg, #48bb78 0%, #2f855a 100%);
|
||||||
|
color: white;
|
||||||
|
padding: 40px 20px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.logo {
|
||||||
|
font-size: 32px;
|
||||||
|
font-weight: 800;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
letter-spacing: 1px;
|
||||||
|
}
|
||||||
|
.content {
|
||||||
|
padding: 40px 30px;
|
||||||
|
}
|
||||||
|
.greeting {
|
||||||
|
font-size: 18px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.username {
|
||||||
|
color: #2f855a;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.success-container {
|
||||||
|
background: #f0fff4;
|
||||||
|
border: 2px dashed #48bb78;
|
||||||
|
border-radius: 10px;
|
||||||
|
padding: 25px;
|
||||||
|
text-align: center;
|
||||||
|
margin: 30px 0;
|
||||||
|
}
|
||||||
|
.success-icon {
|
||||||
|
font-size: 48px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
.success-message {
|
||||||
|
font-size: 24px;
|
||||||
|
font-weight: 800;
|
||||||
|
color: #2f855a;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
.info-text {
|
||||||
|
color: #666;
|
||||||
|
font-size: 14px;
|
||||||
|
margin: 10px 0;
|
||||||
|
}
|
||||||
|
.details {
|
||||||
|
background: #f8f9fa;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 20px;
|
||||||
|
margin: 25px 0;
|
||||||
|
}
|
||||||
|
.detail-row {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
padding: 10px 0;
|
||||||
|
border-bottom: 1px solid #eee;
|
||||||
|
}
|
||||||
|
.detail-row:last-child {
|
||||||
|
border-bottom: none;
|
||||||
|
}
|
||||||
|
.detail-label {
|
||||||
|
font-weight: 600;
|
||||||
|
color: #555;
|
||||||
|
}
|
||||||
|
.detail-value {
|
||||||
|
color: #333;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
.button {
|
||||||
|
display: inline-block;
|
||||||
|
background: linear-gradient(135deg, #48bb78 0%, #2f855a 100%);
|
||||||
|
color: white;
|
||||||
|
text-decoration: none;
|
||||||
|
padding: 14px 30px;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-weight: 600;
|
||||||
|
margin: 20px 0;
|
||||||
|
}
|
||||||
|
.footer {
|
||||||
|
text-align: center;
|
||||||
|
padding: 25px;
|
||||||
|
color: #666;
|
||||||
|
font-size: 12px;
|
||||||
|
border-top: 1px solid #eee;
|
||||||
|
background: #f9f9f9;
|
||||||
|
}
|
||||||
|
.highlight {
|
||||||
|
color: #2f855a;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="header">
|
||||||
|
<div class="logo">NO COPY</div>
|
||||||
|
<h1>Подписка успешно продлена!</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="content">
|
||||||
|
<p class="greeting">
|
||||||
|
Здравствуйте, <span class="username">{{username}}</span>!
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>Ваша подписка была автоматически продлена. Спасибо, что остаётесь с нами!</p>
|
||||||
|
|
||||||
|
<div class="details">
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">Тариф:</span>
|
||||||
|
<span class="detail-value">{{tariff_name}}</span>
|
||||||
|
</div>
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">Сумма списания:</span>
|
||||||
|
<span class="detail-value">{{price}} ₽</span>
|
||||||
|
</div>
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">Дата продления:</span>
|
||||||
|
<span class="detail-value">{{renewal_date}}</span>
|
||||||
|
</div>
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">Следующее списание:</span>
|
||||||
|
<span class="detail-value highlight">{{next_payment_date}}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style="background: #ebf8ff; border: 1px solid #bee3f8; color: #2c5282; padding: 15px; border-radius: 8px; margin: 25px 0; font-size: 14px;">
|
||||||
|
<strong>💡 Детали подписки:</strong>
|
||||||
|
<ul style="margin-top: 10px; padding-left: 20px;">
|
||||||
|
<li>Доступ к сервису активен до {{next_payment_date}}</li>
|
||||||
|
<li>Вы всегда можете управлять подпиской в личном кабинете</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p class="info-text">
|
||||||
|
Если у вас есть вопросы, наша служба поддержки всегда готова помочь.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="footer">
|
||||||
|
<p>© 2024 NO COPY. Все права защищены.</p>
|
||||||
|
<p>Это письмо отправлено автоматически, пожалуйста, не отвечайте на него.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Автопродление подписки не удалось</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||||
|
line-height: 1.6;
|
||||||
|
color: #333;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
max-width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
background: white;
|
||||||
|
border-radius: 12px;
|
||||||
|
overflow: hidden;
|
||||||
|
box-shadow: 0 6px 20px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
.header {
|
||||||
|
background: linear-gradient(135deg, #f56565 0%, #c53030 100%);
|
||||||
|
color: white;
|
||||||
|
padding: 40px 20px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.logo {
|
||||||
|
font-size: 32px;
|
||||||
|
font-weight: 800;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
letter-spacing: 1px;
|
||||||
|
}
|
||||||
|
.content {
|
||||||
|
padding: 40px 30px;
|
||||||
|
}
|
||||||
|
.greeting {
|
||||||
|
font-size: 18px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.username {
|
||||||
|
color: #c53030;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.code-container {
|
||||||
|
background: #f8f9fa;
|
||||||
|
border: 2px dashed #c53030;
|
||||||
|
border-radius: 10px;
|
||||||
|
padding: 25px;
|
||||||
|
text-align: center;
|
||||||
|
margin: 30px 0;
|
||||||
|
}
|
||||||
|
.verification-code {
|
||||||
|
font-size: 24px;
|
||||||
|
font-weight: 800;
|
||||||
|
color: #333;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
.info-text {
|
||||||
|
color: #666;
|
||||||
|
font-size: 14px;
|
||||||
|
margin: 10px 0;
|
||||||
|
}
|
||||||
|
.warning {
|
||||||
|
background: #fff3cd;
|
||||||
|
border: 1px solid #ffeaa7;
|
||||||
|
color: #856404;
|
||||||
|
padding: 15px;
|
||||||
|
border-radius: 8px;
|
||||||
|
margin: 25px 0;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
.details {
|
||||||
|
background: #f8f9fa;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 20px;
|
||||||
|
margin: 25px 0;
|
||||||
|
}
|
||||||
|
.detail-row {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
padding: 10px 0;
|
||||||
|
border-bottom: 1px solid #eee;
|
||||||
|
}
|
||||||
|
.detail-row:last-child {
|
||||||
|
border-bottom: none;
|
||||||
|
}
|
||||||
|
.detail-label {
|
||||||
|
font-weight: 600;
|
||||||
|
color: #555;
|
||||||
|
}
|
||||||
|
.detail-value {
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
.button {
|
||||||
|
display: inline-block;
|
||||||
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||||
|
color: white;
|
||||||
|
text-decoration: none;
|
||||||
|
padding: 14px 30px;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-weight: 600;
|
||||||
|
margin: 20px 0;
|
||||||
|
}
|
||||||
|
.footer {
|
||||||
|
text-align: center;
|
||||||
|
padding: 25px;
|
||||||
|
color: #666;
|
||||||
|
font-size: 12px;
|
||||||
|
border-top: 1px solid #eee;
|
||||||
|
background: #f9f9f9;
|
||||||
|
}
|
||||||
|
.highlight {
|
||||||
|
color: #c53030;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="header">
|
||||||
|
<div class="logo">NO COPY</div>
|
||||||
|
<h1>Автопродление не удалось</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="content">
|
||||||
|
<p class="greeting">
|
||||||
|
Здравствуйте, <span class="username">{{username}}</span>!
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>К сожалению, автоматическое продление подписки не удалось выполнить.</p>
|
||||||
|
|
||||||
|
<div class="details">
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">Тариф:</span>
|
||||||
|
<span class="detail-value">{{tariff_name}}</span>
|
||||||
|
</div>
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">Сумма:</span>
|
||||||
|
<span class="detail-value">{{price}} ₽</span>
|
||||||
|
</div>
|
||||||
|
<div class="detail-row">
|
||||||
|
<span class="detail-label">Дата попытки:</span>
|
||||||
|
<span class="detail-value">{{attempt_date}}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="warning">
|
||||||
|
<strong>Что делать?</strong>
|
||||||
|
<ul style="margin-top: 10px; padding-left: 20px;">
|
||||||
|
<li>Проверьте достаточно ли средств на карте</li>
|
||||||
|
<li>Убедитесь что карта не заблокирована</li>
|
||||||
|
<li>Если проблема сохраняется, обратитесь в поддержку</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p class="info-text">
|
||||||
|
После успешной оплаты доступ к сервису будет восстановлен автоматически.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="footer">
|
||||||
|
<p>© 2024 NO COPY. Все права защищены.</p>
|
||||||
|
<p>Это письмо отправлено автоматически, пожалуйста, не отвечайте на него.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user