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.*;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/payments")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class PaymentController {
|
|
|
|
|
|
|
|
|
|
private final PaymentService paymentService;
|
|
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|