11package com .threestar .trainus .domain .payment .controller ;
22
3+ import org .springframework .beans .factory .annotation .Value ;
34import org .springframework .http .HttpStatus ;
45import org .springframework .http .ResponseEntity ;
6+ import org .springframework .web .bind .annotation .GetMapping ;
57import org .springframework .web .bind .annotation .PathVariable ;
68import org .springframework .web .bind .annotation .PostMapping ;
79import org .springframework .web .bind .annotation .RequestBody ;
810import org .springframework .web .bind .annotation .RequestMapping ;
11+ import org .springframework .web .bind .annotation .RequestParam ;
912import org .springframework .web .bind .annotation .RestController ;
1013
11- import com .threestar .trainus .domain .payment .dto .CancelPaymentRequest ;
12- import com .threestar .trainus .domain .payment .dto .ConfirmPaymentRequest ;
14+ import com .threestar .trainus .domain .payment .dto .CancelPaymentRequestDto ;
15+ import com .threestar .trainus .domain .payment .dto .ConfirmPaymentRequestDto ;
1316import com .threestar .trainus .domain .payment .dto .PaymentClient ;
1417import com .threestar .trainus .domain .payment .dto .PaymentRequestDto ;
1518import com .threestar .trainus .domain .payment .dto .PaymentResponseDto ;
16- import com .threestar .trainus .domain .payment .dto .SaveAmountRequest ;
19+ import com .threestar .trainus .domain .payment .dto .SaveAmountRequestDto ;
1720import com .threestar .trainus .domain .payment .dto .TossPaymentResponseDto ;
21+ import com .threestar .trainus .domain .payment .dto .failure .FailurePaymentResponseDto ;
22+ import com .threestar .trainus .domain .payment .dto .failure .PaymentFailureHistoryPageDto ;
23+ import com .threestar .trainus .domain .payment .dto .failure .PaymentFailurePageWrapperDto ;
24+ import com .threestar .trainus .domain .payment .dto .success .PaymentSuccessHistoryPageDto ;
25+ import com .threestar .trainus .domain .payment .dto .success .PaymentSuccessPageWrapperDto ;
26+ import com .threestar .trainus .domain .payment .dto .success .SuccessfulPaymentResponseDto ;
27+ import com .threestar .trainus .domain .payment .mapper .PaymentMapper ;
1828import com .threestar .trainus .domain .payment .service .PaymentService ;
1929import com .threestar .trainus .global .unit .BaseResponse ;
30+ import com .threestar .trainus .global .unit .PagedResponse ;
2031
32+ import io .swagger .v3 .oas .annotations .Operation ;
33+ import io .swagger .v3 .oas .annotations .tags .Tag ;
2134import jakarta .servlet .http .HttpSession ;
2235import lombok .RequiredArgsConstructor ;
2336import lombok .extern .slf4j .Slf4j ;
2437
38+ @ Tag (name = "결제 API" , description = "결제, 검증, 취소 API" )
2539@ Slf4j
2640@ RestController
2741@ RequestMapping ("/api/v1/payments" )
@@ -31,48 +45,94 @@ public class PaymentController {
3145 private final PaymentClient tossPaymentClient ;
3246 private final PaymentService paymentService ;
3347
48+ @ Value ("${spring.page.size.limit}" )
49+ private int pageSizeLimit ;
50+
3451 @ PostMapping ("/prepare" )
52+ @ Operation (summary = "결제 준비" , description = "실제 결제 전 최종 가격 적용 후 결제 준비" )
3553 public ResponseEntity <BaseResponse <PaymentResponseDto >> preparePayment (HttpSession session ,
3654 @ RequestBody PaymentRequestDto request ) {
3755 Long userId = (Long )session .getAttribute ("LOGIN_USER" );
3856 PaymentResponseDto response = paymentService .preparePayment (request , userId );
39- //여기에서 쿠폰 가격 정해서 답변 내보내고 이를 통해 아래 호출
4057 return BaseResponse .ok ("결제 정보 준비 완료" , response , HttpStatus .OK );
4158 }
4259
4360 @ PostMapping ("/saveAmount" )
44- public ResponseEntity <BaseResponse <Void >> saveAmount (HttpSession session , @ RequestBody SaveAmountRequest request ) {
45- session .setAttribute (request .getOrderId (), request .getAmount ());
61+ @ Operation (summary = "결제 검증 데이터 저장" , description = "결제 무결성 검증을 위한 데이터 저장" )
62+ public ResponseEntity <BaseResponse <Void >> saveAmount (HttpSession session ,
63+ @ RequestBody SaveAmountRequestDto request ) {
64+ session .setAttribute (request .orderId (), request .amount ());
4665 return BaseResponse .ok ("Payment temp save Successful" , null , HttpStatus .OK );
4766 }
4867
4968 @ PostMapping ("/verifyAmount" )
69+ @ Operation (summary = "결제 검증 데이터 확인" , description = "결제 무결성 검증을 위한 데이터 확인" )
5070 public ResponseEntity <BaseResponse <Void >> verifyAmount (HttpSession session ,
51- @ RequestBody SaveAmountRequest request ) {
52- Integer amount = (Integer )session .getAttribute (request .getOrderId ());
53- log .info ("amount = {}" , amount );
54-
71+ @ RequestBody SaveAmountRequestDto request ) {
72+ Integer amount = (Integer )session .getAttribute (request .orderId ());
5573 try {
56- if (amount == null || !amount .equals (request .getAmount ())) {
74+ if (amount == null || !amount .equals (request .amount ())) {
5775 return BaseResponse .error ("결제 금액 정보가 유효하지 않습니다" , null , HttpStatus .BAD_REQUEST );
5876 }
5977 return BaseResponse .ok ("Payment is valid" , null , HttpStatus .OK );
6078 } finally {
61- session .removeAttribute (request .getOrderId ());
79+ session .removeAttribute (request .orderId ());
6280 }
6381 }
6482
6583 @ PostMapping ("/confirm" )
66- public ResponseEntity <BaseResponse <Void >> confirm (@ RequestBody ConfirmPaymentRequest request ) {
84+ @ Operation (summary = "결제 진행" , description = "결제 진행" )
85+ public ResponseEntity <BaseResponse <SuccessfulPaymentResponseDto >> confirm (
86+ @ RequestBody ConfirmPaymentRequestDto request ) {
6787 TossPaymentResponseDto tossResponse = tossPaymentClient .confirmPayment (request );
68- paymentService .processConfirm (tossResponse );
69- return BaseResponse .ok ("결제 성공" , null , HttpStatus .OK );
88+ SuccessfulPaymentResponseDto payResult = paymentService .processConfirm (tossResponse );
89+ return BaseResponse .ok ("결제 성공" , payResult , HttpStatus .OK );
7090 }
7191
7292 @ PostMapping ("/cancel" )
73- public ResponseEntity <BaseResponse <Void >> cancel (@ RequestBody CancelPaymentRequest request ) {
93+ @ Operation (summary = "결제 취소" , description = "결제 취소" )
94+ public ResponseEntity <BaseResponse <FailurePaymentResponseDto >> cancel (
95+ @ RequestBody CancelPaymentRequestDto request ) {
7496 TossPaymentResponseDto tossResponse = tossPaymentClient .cancelPayment (request );
75- paymentService .processCancel (tossResponse );
76- return BaseResponse .ok ("결제 취소 성공" , null , HttpStatus .OK );
97+ FailurePaymentResponseDto payResult = paymentService .processCancel (tossResponse , request .cancelReason ());
98+ return BaseResponse .ok ("결제 취소 성공" , payResult , HttpStatus .OK );
99+ }
100+
101+ @ GetMapping ("/view/success" )
102+ @ Operation (summary = "완료 결제 조회" , description = "완료된 결제 내역 조회" )
103+ public ResponseEntity <PagedResponse <PaymentSuccessPageWrapperDto >> readAll (HttpSession session ,
104+ @ RequestParam ("page" ) int page ,
105+ @ RequestParam ("pageSize" ) int pageSize ) {
106+ Long userId = (Long )session .getAttribute ("LOGIN_USER" );
107+ int correctPage = Math .max (page , 1 );
108+ int correctPageSize = Math .max (1 , Math .min (pageSize , pageSizeLimit ));
109+ PaymentSuccessHistoryPageDto paymentSuccessHistoryPageDto = paymentService .viewAllSuccessTransaction (userId ,
110+ correctPage , correctPageSize );
111+ PaymentSuccessPageWrapperDto payments = PaymentMapper .toPaymentSuccessPageWrapperDto (
112+ paymentSuccessHistoryPageDto );
113+ return PagedResponse .ok ("성공 결제 조회 성공" , payments , paymentSuccessHistoryPageDto .count (), HttpStatus .OK );
77114 }
115+
116+ @ GetMapping ("/view/cancel" )
117+ @ Operation (summary = "취소 결제 조회" , description = "취소된 결제 내역 조회" )
118+ public ResponseEntity <PagedResponse <PaymentFailurePageWrapperDto >> readAllFailure (HttpSession session ,
119+ @ RequestParam ("page" ) int page ,
120+ @ RequestParam ("pageSize" ) int pageSize ) {
121+ Long userId = (Long )session .getAttribute ("LOGIN_USER" );
122+ int correctPage = Math .max (page , 1 );
123+ int correctPageSize = Math .max (1 , Math .min (pageSize , pageSizeLimit ));
124+ PaymentFailureHistoryPageDto paymentFailureHistoryPageDto = paymentService .viewAllFailureTransaction (userId ,
125+ correctPage , correctPageSize );
126+ PaymentFailurePageWrapperDto payments = PaymentMapper .toPaymentFailurePageWrapperDto (
127+ paymentFailureHistoryPageDto );
128+ return PagedResponse .ok ("취소 결제 조회 성공" , payments , paymentFailureHistoryPageDto .count (), HttpStatus .OK );
129+ }
130+
131+ @ GetMapping ("/{paymentKey}" )
132+ @ Operation (summary = "상세 결제 조회" , description = "상세 결제 내역 조회" )
133+ public ResponseEntity <BaseResponse <TossPaymentResponseDto >> readDetailPayment (
134+ @ PathVariable ("paymentKey" ) String paymentKey ) {
135+ return BaseResponse .ok ("상세 결제 조회 완료" , tossPaymentClient .viewDetailPayment (paymentKey ), HttpStatus .OK );
136+ }
137+
78138}
0 commit comments