2026-02-18 16:14:17 +07:00
|
|
|
package ru.soune.nocopy.controller;
|
|
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2026-02-19 14:15:59 +07:00
|
|
|
import org.springframework.http.HttpStatus;
|
2026-02-18 18:42:55 +07:00
|
|
|
import org.springframework.http.ResponseEntity;
|
2026-02-18 16:14:17 +07:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
2026-03-18 16:08:37 +07:00
|
|
|
import ru.soune.nocopy.dto.search.GlobalSearchStartResponse;
|
2026-02-18 16:14:17 +07:00
|
|
|
import ru.soune.nocopy.entity.payment.Payment;
|
2026-02-19 15:06:48 +07:00
|
|
|
import ru.soune.nocopy.exception.PaymentNotFoundException;
|
2026-02-18 18:42:55 +07:00
|
|
|
import ru.soune.nocopy.exception.TariffNotFoundException;
|
|
|
|
|
import ru.soune.nocopy.exception.UserNotFoundException;
|
2026-02-18 16:14:17 +07:00
|
|
|
import ru.soune.nocopy.service.payment.PaymentService;
|
2026-03-18 16:08:37 +07:00
|
|
|
import ru.soune.nocopy.service.register.AuthService;
|
2026-02-18 16:14:17 +07:00
|
|
|
|
2026-03-18 16:08:37 +07:00
|
|
|
import java.util.HashMap;
|
2026-02-18 16:14:17 +07:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/payments")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class PaymentController {
|
|
|
|
|
|
|
|
|
|
private final PaymentService paymentService;
|
|
|
|
|
|
2026-03-18 16:08:37 +07:00
|
|
|
private final AuthService authService;
|
|
|
|
|
|
2026-02-18 16:14:17 +07:00
|
|
|
@PostMapping("/create")
|
2026-02-19 14:15:59 +07:00
|
|
|
public ResponseEntity<?> createPayment(@RequestParam String email, @RequestParam Long tariffId,
|
|
|
|
|
@RequestParam String operationType, @RequestParam String operationUuid) {
|
2026-02-18 18:42:55 +07:00
|
|
|
try {
|
|
|
|
|
Payment payment = paymentService.createPayment(email, tariffId, operationType, operationUuid);
|
2026-02-19 14:15:59 +07:00
|
|
|
|
2026-02-18 18:42:55 +07:00
|
|
|
return ResponseEntity.ok(payment);
|
2026-02-19 14:15:59 +07:00
|
|
|
} catch (TariffNotFoundException e) {
|
|
|
|
|
return ResponseEntity
|
|
|
|
|
.status(HttpStatus.NOT_FOUND)
|
|
|
|
|
.body(Map.of("error", "Tariff not found", "message", e.getMessage()));
|
|
|
|
|
} catch (UserNotFoundException e) {
|
|
|
|
|
return ResponseEntity
|
|
|
|
|
.status(HttpStatus.NOT_FOUND)
|
|
|
|
|
.body(Map.of("error", "User not found", "message", e.getMessage()));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity
|
|
|
|
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body(Map.of("error", "Internal server error", "message", e.getMessage()));
|
2026-02-18 18:42:55 +07:00
|
|
|
}
|
2026-02-18 16:14:17 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("webhook/yookassa")
|
|
|
|
|
public void handleYooKassaNotification(@RequestBody Map<String, Object> notification) {
|
|
|
|
|
try {
|
|
|
|
|
paymentService.handlePaymentNotification(notification);
|
2026-03-03 17:51:51 +07:00
|
|
|
log.info("Notification from yookassa: " + notification);
|
2026-02-19 15:06:48 +07:00
|
|
|
} catch (PaymentNotFoundException e) {
|
2026-02-18 16:14:17 +07:00
|
|
|
log.error("Error processing payment notification", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-18 15:27:43 +07:00
|
|
|
|
2026-03-18 16:26:47 +07:00
|
|
|
@PostMapping("/auto-renewal")
|
|
|
|
|
public ResponseEntity<?> disableAutoRenewal(@RequestParam(value = "renewal") Boolean renewal,
|
|
|
|
|
@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
2026-03-18 15:27:43 +07:00
|
|
|
try {
|
2026-03-18 16:08:37 +07:00
|
|
|
if (tokenHeader == null || tokenHeader.isBlank()) {
|
|
|
|
|
Map<String, Object> errorData = new HashMap<>();
|
|
|
|
|
errorData.put("token", tokenHeader);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok().body(Map.of("error", errorData));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 16:26:47 +07:00
|
|
|
paymentService.changeAutoRenewal(authService.useUserAuthToken(tokenHeader), renewal);
|
2026-03-18 15:27:43 +07:00
|
|
|
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}")
|
2026-03-18 16:08:37 +07:00
|
|
|
public ResponseEntity<?> deletePaymentMethod(@RequestHeader(value = "Authorization", required = false) String tokenHeader,
|
2026-03-18 15:27:43 +07:00
|
|
|
@PathVariable String paymentMethodId) {
|
|
|
|
|
try {
|
2026-03-18 16:08:37 +07:00
|
|
|
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);
|
2026-03-18 15:27:43 +07:00
|
|
|
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")
|
2026-03-18 16:08:37 +07:00
|
|
|
public ResponseEntity<?> getPaymentMethods(@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
2026-03-18 15:27:43 +07:00
|
|
|
try {
|
2026-03-18 16:08:37 +07:00
|
|
|
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)));
|
2026-03-18 15:27:43 +07:00
|
|
|
} catch (UserNotFoundException e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
|
|
|
|
.body(Map.of("error", e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-18 16:14:17 +07:00
|
|
|
}
|