package se.bilhalsning.controller; import java.util.Map; import java.util.UUID; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import se.bilhalsning.dto.OrderResponse; import se.bilhalsning.entity.Order; import se.bilhalsning.entity.User; import se.bilhalsning.exception.InvalidCredentialsException; import se.bilhalsning.service.OrderService; import se.bilhalsning.service.UserService; @RestController @RequestMapping("/api/payment") public class PaymentController { private final OrderService orderService; private final UserService userService; private final String swishNumber; private final int letterPrice; public PaymentController( OrderService orderService, UserService userService, @Value("${app.payment.swish-number}") String swishNumber, @Value("${app.payment.letter-price}") int letterPrice) { this.orderService = orderService; this.userService = userService; this.swishNumber = swishNumber; this.letterPrice = letterPrice; } @PostMapping("/{orderId}/pay") public ResponseEntity pay(@PathVariable UUID orderId, @AuthenticationPrincipal UserDetails userDetails) { User user = userService.findByEmail(userDetails.getUsername()) .orElseThrow(InvalidCredentialsException::new); Order order = orderService.confirmPayment(orderId, user.getId()); return ResponseEntity.ok(toResponse(order)); } @GetMapping("/swish-info") public ResponseEntity> swishInfo() { return ResponseEntity.ok(Map.of("number", swishNumber, "amount", letterPrice)); } private OrderResponse toResponse(Order order) { return new OrderResponse( order.getId(), order.getPlate(), order.getLetterText(), order.getStatus().getValue(), order.getTrackingId(), order.getAmountPaid(), order.getCreatedAt() ); } }