Add payment processing endpoint

Problem Context

Users submit payment for their orders. The payment gateway charges the user's card and we record the transaction.

Expert
300 points

File Changes (1)

src/main/java/com/example/controller/PaymentController.java MODIFIED
@@ -20 +20 @@
20 20 @RestController
21 21 @RequestMapping("/api/payments")
22 22 public class PaymentController {
23 +
24 + @PostMapping("/process")
25 + public ResponseEntity<PaymentResult> processPayment(@RequestBody PaymentRequest request) {
26 + Order order = orderService.findById(request.getOrderId());
27 +
28 + if (order.getStatus() == OrderStatus.PAID) {
29 + return ResponseEntity.ok(PaymentResult.alreadyPaid());
30 + }
31 +
32 + // Charge the payment gateway
33 + PaymentGatewayResponse response = paymentGateway.charge(
34 + request.getCardToken(),
35 + order.getTotalAmount()
36 + );
37 +
38 + if (response.isSuccessful()) {
39 + order.setStatus(OrderStatus.PAID);
40 + orderService.save(order);
41 +
42 + Payment payment = new Payment();
43 + payment.setOrderId(order.getId());
44 + payment.setAmount(order.getTotalAmount());
45 + payment.setTransactionId(response.getTransactionId());
46 + paymentRepository.save(payment);
47 + }
48 +
49 + return ResponseEntity.ok(PaymentResult.from(response));
50 + }
23 51 }
Login Required: You must be registered to submit reviews and receive AI feedback. Register or login to start reviewing!

Your Review

Tip: Be thorough! Consider security, performance, code quality, and best practices.
Review Tips
  • Look for security vulnerabilities (SQL injection, XSS, etc.)
  • Check for null pointer exceptions and error handling
  • Consider performance implications
  • Evaluate code maintainability and readability
  • Check for proper resource management
  • Look for logic errors or edge cases
Analyzing Your Review
Our AI is carefully evaluating your code review against best practices