Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions src/main/java/com/kt/controller/payment/PaymentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public ResponseEntity<?> confirmPayment(@RequestBody PaymentTossConfirmRequest r
) {
String orderId = request.orderId().split("-")[0];
Long orderIdLong = Long.parseLong(orderId);
String paymentKey = request.paymentKey();

try {
String url = tossPaymentsProperties.getApiUrl() + "/confirm";
Expand Down Expand Up @@ -124,12 +125,29 @@ public ResponseEntity<?> confirmPayment(@RequestBody PaymentTossConfirmRequest r

if (response.getStatusCode() == HttpStatus.OK) {
Map<String, Object> tossPayment = response.getBody();
Long paymentId = paymentFacade.completePayment(tossPayment, currentUser.getId(), orderIdLong);
try {
Long paymentId = paymentFacade.completePayment(tossPayment, currentUser.getId(), orderIdLong);

Map<String, Object> responseBody = new HashMap<>(tossPayment);
responseBody.put("paymentId", paymentId);
Map<String, Object> responseBody = new HashMap<>(tossPayment);
responseBody.put("paymentId", paymentId);

return ResponseEntity.ok(responseBody);

} catch (Exception e) {
System.out.println("결제 실패sdlkjfnmalskdmfklamsdklfmaklsdmfklamsdflkmasdklf");

cancelTossPayment(paymentKey, "시스템 오류로 인한 자동 취소");

orderService.rollback(orderIdLong);

return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of(
"code", ErrorCode.PAYMENT_CANCEL_FAILED,
"message", "결제는 승인되었으나 처리 중 오류가 발생하여 자동 취소"
));
}

return ResponseEntity.ok(responseBody);
}

return ResponseEntity.ok(response.getBody());
Expand Down Expand Up @@ -224,6 +242,22 @@ public ApiResult<Map<String, String>> getClientKey() {
return ApiResult.ok(Map.of("clientKey", tossPaymentsProperties.getClientKey()));
}

/* 이 코드 service로 빼야하는지 등 검토좀 한번 ㅂ부탁드릴게요!*/
private void cancelTossPayment(String paymentKey, String cancelReason) {
String url = tossPaymentsProperties.getApiUrl() + "/" + paymentKey + "/cancel";
String auth = tossPaymentsProperties.getSecretKey() + ":";
String encodedAuth = java.util.Base64.getEncoder()
.encodeToString(auth.getBytes(StandardCharsets.UTF_8));

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + encodedAuth);
headers.setContentType(MediaType.APPLICATION_JSON);

Map<String, Object> body = Map.of("cancelReason", cancelReason);

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(body, headers);
restTemplate.postForEntity(url, entity, Map.class);
}

}

6 changes: 3 additions & 3 deletions src/main/resources/static/payment-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ <h2 style="color: #666;">주문 처리 완료</h2>

const orderStatus = data.orderStatus;

// ORDERED 상태: 결제 위젯 표시
if (orderStatus === 'ORDERED') {
// PENDING_PAYMENT 상태: 결제 위젯 표시
if (orderStatus === 'PENDING_PAYMENT') {
paymentWidgetArea.style.display = 'block';
await initPaymentWidget(data);
}
Expand All @@ -255,7 +255,7 @@ <h2 style="color: #666;">주문 처리 완료</h2>
await loadPaymentInfo(data.orderId);
}
// CANCELLED, REFUNDED 등: 안내 메시지만 표시
else if (['CANCELLED', 'REFUNDED', 'RETURN_REQUESTED', 'RETURNED', 'REFUND_REQUESTED'].includes(orderStatus)) {
else if (['PENDING_PAYMENT ', 'CANCELLED', 'REFUNDED', 'RETURN_REQUESTED', 'RETURNED', 'REFUND_REQUESTED'].includes(orderStatus)) {
cannotActionArea.style.display = 'block';
const messageElement = document.getElementById('cannot-action-message');

Expand Down
Loading