@@ -5,12 +5,15 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import ru.soune.nocopy.dto.search.GlobalSearchStartResponse;
|
||||||
import ru.soune.nocopy.entity.payment.Payment;
|
import ru.soune.nocopy.entity.payment.Payment;
|
||||||
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.service.payment.PaymentService;
|
import ru.soune.nocopy.service.payment.PaymentService;
|
||||||
|
import ru.soune.nocopy.service.register.AuthService;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -21,6 +24,8 @@ public class PaymentController {
|
|||||||
|
|
||||||
private final PaymentService paymentService;
|
private final PaymentService paymentService;
|
||||||
|
|
||||||
|
private final AuthService authService;
|
||||||
|
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
public ResponseEntity<?> createPayment(@RequestParam String email, @RequestParam Long tariffId,
|
public ResponseEntity<?> createPayment(@RequestParam String email, @RequestParam Long tariffId,
|
||||||
@RequestParam String operationType, @RequestParam String operationUuid) {
|
@RequestParam String operationType, @RequestParam String operationUuid) {
|
||||||
@@ -54,9 +59,16 @@ public class PaymentController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/auto-renewal/disable")
|
@PostMapping("/auto-renewal/disable")
|
||||||
public ResponseEntity<?> disableAutoRenewal(@RequestParam String email) {
|
public ResponseEntity<?> disableAutoRenewal(@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||||
try {
|
try {
|
||||||
paymentService.disableAutoRenewal(email);
|
if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||||
|
Map<String, Object> errorData = new HashMap<>();
|
||||||
|
errorData.put("token", tokenHeader);
|
||||||
|
|
||||||
|
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||||
|
}
|
||||||
|
|
||||||
|
paymentService.disableAutoRenewal(authService.useUserAuthToken(tokenHeader));
|
||||||
return ResponseEntity.ok(Map.of("message", "Auto-renewal disabled"));
|
return ResponseEntity.ok(Map.of("message", "Auto-renewal disabled"));
|
||||||
} catch (UserNotFoundException e) {
|
} catch (UserNotFoundException e) {
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||||
@@ -65,10 +77,17 @@ public class PaymentController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/methods/{paymentMethodId}")
|
@DeleteMapping("/methods/{paymentMethodId}")
|
||||||
public ResponseEntity<?> deletePaymentMethod(@RequestParam String email,
|
public ResponseEntity<?> deletePaymentMethod(@RequestHeader(value = "Authorization", required = false) String tokenHeader,
|
||||||
@PathVariable String paymentMethodId) {
|
@PathVariable String paymentMethodId) {
|
||||||
try {
|
try {
|
||||||
paymentService.deletePaymentMethod(email, paymentMethodId);
|
if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||||
|
Map<String, Object> errorData = new HashMap<>();
|
||||||
|
errorData.put("token", tokenHeader);
|
||||||
|
|
||||||
|
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||||
|
}
|
||||||
|
|
||||||
|
paymentService.deletePaymentMethod(authService.useUserAuthToken(tokenHeader), paymentMethodId);
|
||||||
return ResponseEntity.ok(Map.of("message", "Payment method deleted"));
|
return ResponseEntity.ok(Map.of("message", "Payment method deleted"));
|
||||||
} catch (UserNotFoundException e) {
|
} catch (UserNotFoundException e) {
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||||
@@ -80,9 +99,17 @@ public class PaymentController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/methods")
|
@GetMapping("/methods")
|
||||||
public ResponseEntity<?> getPaymentMethods(@RequestParam String email) {
|
public ResponseEntity<?> getPaymentMethods(@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||||
try {
|
try {
|
||||||
return ResponseEntity.ok(paymentService.getUserPaymentMethods(email));
|
if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||||
|
Map<String, Object> errorData = new HashMap<>();
|
||||||
|
errorData.put("token", tokenHeader);
|
||||||
|
|
||||||
|
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok(paymentService.getUserPaymentMethods(
|
||||||
|
authService.useUserAuthToken(tokenHeader)));
|
||||||
} catch (UserNotFoundException e) {
|
} catch (UserNotFoundException e) {
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||||
.body(Map.of("error", e.getMessage()));
|
.body(Map.of("error", e.getMessage()));
|
||||||
|
|||||||
@@ -90,9 +90,8 @@ public class PaymentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<PaymentMethodDTO> getUserPaymentMethods(String email) {
|
public List<PaymentMethodDTO> getUserPaymentMethods(Long userId) {
|
||||||
User user = userRepository.findByEmail(email);
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
if (user == null) throw new UserNotFoundException(email);
|
|
||||||
|
|
||||||
return paymentMethodRepository.findByUserAndActiveTrue(user)
|
return paymentMethodRepository.findByUserAndActiveTrue(user)
|
||||||
.stream()
|
.stream()
|
||||||
@@ -107,10 +106,8 @@ public class PaymentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deletePaymentMethod(String email, String paymentMethodId) {
|
public void deletePaymentMethod(Long userId, String paymentMethodId) {
|
||||||
User user = userRepository.findByEmail(email);
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
if (user == null) throw new UserNotFoundException(email);
|
|
||||||
|
|
||||||
PaymentMethod pm = paymentMethodRepository.findByPaymentMethodId(paymentMethodId)
|
PaymentMethod pm = paymentMethodRepository.findByPaymentMethodId(paymentMethodId)
|
||||||
.orElseThrow(() -> new RuntimeException("Payment method not found"));
|
.orElseThrow(() -> new RuntimeException("Payment method not found"));
|
||||||
|
|
||||||
@@ -123,9 +120,8 @@ public class PaymentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void disableAutoRenewal(String email) {
|
public void disableAutoRenewal(Long userId) {
|
||||||
User user = userRepository.findByEmail(email);
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
if (user == null) throw new UserNotFoundException(email);
|
|
||||||
|
|
||||||
TariffInfo tariffInfo = user.getActiveTariffInfo();
|
TariffInfo tariffInfo = user.getActiveTariffInfo();
|
||||||
if (tariffInfo != null) {
|
if (tariffInfo != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user