PaymentController.java
5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.ecommerce.payment.controller;
import com.ecommerce.payment.model.dto.PaymentRequest;
import com.ecommerce.payment.model.dto.PaymentResponse;
import com.ecommerce.payment.service.PaymentService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/payments")
@RequiredArgsConstructor
public class PaymentController {
private final PaymentService paymentService;
@PostMapping
public ResponseEntity<PaymentResponse> createPayment(@Valid @RequestBody PaymentRequest request) {
PaymentResponse response = paymentService.createPayment(request);
return ResponseEntity.ok(response);
}
@GetMapping("/{paymentId}")
public ResponseEntity<PaymentResponse> getPayment(@PathVariable String paymentId) {
PaymentResponse response = paymentService.getPayment(paymentId);
return ResponseEntity.ok(response);
}
@GetMapping("/gateway/{gatewayPaymentId}")
public ResponseEntity<PaymentResponse> getPaymentByGatewayId(@PathVariable String gatewayPaymentId) {
PaymentResponse response = paymentService.getPaymentByGatewayId(gatewayPaymentId);
return ResponseEntity.ok(response);
}
@GetMapping("/order/{orderId}")
public ResponseEntity<List<PaymentResponse>> getPaymentsByOrder(@PathVariable String orderId) {
List<PaymentResponse> payments = paymentService.getPaymentsByOrder(orderId);
return ResponseEntity.ok(payments);
}
@GetMapping
public ResponseEntity<Map<String, Object>> getAllPayments(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size,
@RequestParam(defaultValue = "createdAt") String sortBy,
@RequestParam(defaultValue = "desc") String sortDirection) {
Sort sort = sortDirection.equalsIgnoreCase("desc") ?
Sort.by(sortBy).descending() : Sort.by(sortBy).ascending();
Pageable pageable = PageRequest.of(page, size, sort);
Page<PaymentResponse> payments = paymentService.getAllPayments(pageable);
Map<String, Object> response = new HashMap<>();
response.put("payments", payments.getContent());
response.put("currentPage", payments.getNumber());
response.put("totalItems", payments.getTotalElements());
response.put("totalPages", payments.getTotalPages());
return ResponseEntity.ok(response);
}
@GetMapping("/user/{userId}")
public ResponseEntity<Map<String, Object>> getPaymentsByUser(
@PathVariable Long userId,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());
Page<PaymentResponse> payments = paymentService.getPaymentsByUser(userId, pageable);
Map<String, Object> response = new HashMap<>();
response.put("payments", payments.getContent());
response.put("currentPage", payments.getNumber());
response.put("totalItems", payments.getTotalElements());
response.put("totalPages", payments.getTotalPages());
return ResponseEntity.ok(response);
}
@GetMapping("/status/{status}")
public ResponseEntity<Map<String, Object>> getPaymentsByStatus(
@PathVariable String status,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());
Page<PaymentResponse> payments = paymentService.getPaymentsByStatus(status, pageable);
Map<String, Object> response = new HashMap<>();
response.put("payments", payments.getContent());
response.put("currentPage", payments.getNumber());
response.put("totalItems", payments.getTotalElements());
response.put("totalPages", payments.getTotalPages());
return ResponseEntity.ok(response);
}
@PostMapping("/{paymentId}/confirm")
public ResponseEntity<PaymentResponse> confirmPayment(@PathVariable String paymentId) {
PaymentResponse response = paymentService.confirmPayment(paymentId);
return ResponseEntity.ok(response);
}
@PostMapping("/{paymentId}/cancel")
public ResponseEntity<PaymentResponse> cancelPayment(
@PathVariable String paymentId,
@RequestBody Map<String, String> request) {
String reason = request.get("reason");
PaymentResponse response = paymentService.cancelPayment(paymentId, reason);
return ResponseEntity.ok(response);
}
@GetMapping("/statistics")
public ResponseEntity<Map<String, Object>> getPaymentStatistics(
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
Map<String, Object> statistics = paymentService.getPaymentStatistics(startDate, endDate);
return ResponseEntity.ok(statistics);
}
@GetMapping("/health")
public ResponseEntity<Map<String, String>> healthCheck() {
Map<String, String> response = new HashMap<>();
response.put("status", "UP");
response.put("service", "payment-service");
response.put("timestamp", LocalDateTime.now().toString());
return ResponseEntity.ok(response);
}
}