diff --git a/src/main/java/com/eatsfine/eatsfine/domain/payment/service/PaymentService.java b/src/main/java/com/eatsfine/eatsfine/domain/payment/service/PaymentService.java index 44d38aa..6cec011 100644 --- a/src/main/java/com/eatsfine/eatsfine/domain/payment/service/PaymentService.java +++ b/src/main/java/com/eatsfine/eatsfine/domain/payment/service/PaymentService.java @@ -22,7 +22,6 @@ import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.client.RestClient; import java.math.BigDecimal; import org.springframework.data.domain.PageRequest; @@ -38,7 +37,7 @@ public class PaymentService { private final PaymentRepository paymentRepository; private final BookingRepository bookingRepository; - private final RestClient tossPaymentClient; + private final TossPaymentService tossPaymentService; @Transactional public PaymentResponseDTO.PaymentRequestResultDTO requestPayment(PaymentRequestDTO.RequestPaymentDTO dto) { @@ -84,19 +83,14 @@ public PaymentResponseDTO.PaymentSuccessResultDTO confirmPayment(PaymentConfirmD // 토스 API 호출 TossPaymentResponse response; try { - response = tossPaymentClient.post() - .uri("/v1/payments/confirm") - .body(dto) - .retrieve() - .body(TossPaymentResponse.class); - - if (response == null || !"DONE".equals(response.status())) { - log.error("Toss Payment Confirmation Failed: Status is not DONE"); - payment.failPayment(); - throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR); - } + response = tossPaymentService.confirm(dto); } catch (Exception e) { - log.error("Toss Payment API Error", e); + payment.failPayment(); + throw e; + } + + if (response == null || !"DONE".equals(response.status())) { + log.error("Toss Payment Confirmation Failed: Status is not DONE"); payment.failPayment(); throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR); } @@ -140,20 +134,10 @@ public PaymentResponseDTO.CancelPaymentResultDTO cancelPayment(String paymentKey .orElseThrow(() -> new PaymentException(PaymentErrorStatus._PAYMENT_NOT_FOUND)); // 토스 결제 취소 API 호출 - TossPaymentResponse response; - try { - response = tossPaymentClient.post() - .uri("/v1/payments/" + paymentKey + "/cancel") - .body(dto) - .retrieve() - .body(TossPaymentResponse.class); - - if (response == null || !"CANCELED".equals(response.status())) { - log.error("Toss Payment Cancel Failed: {}", response); - throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR); - } - } catch (Exception e) { - log.error("Toss Payment Cancel API Error", e); + TossPaymentResponse response = tossPaymentService.cancel(paymentKey, dto); + + if (response == null || !"CANCELED".equals(response.status())) { + log.error("Toss Payment Cancel Failed: {}", response); throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR); } diff --git a/src/main/java/com/eatsfine/eatsfine/domain/payment/service/TossPaymentService.java b/src/main/java/com/eatsfine/eatsfine/domain/payment/service/TossPaymentService.java new file mode 100644 index 0000000..4a434f8 --- /dev/null +++ b/src/main/java/com/eatsfine/eatsfine/domain/payment/service/TossPaymentService.java @@ -0,0 +1,48 @@ +package com.eatsfine.eatsfine.domain.payment.service; + +import com.eatsfine.eatsfine.domain.payment.dto.request.PaymentConfirmDTO; +import com.eatsfine.eatsfine.domain.payment.dto.request.PaymentRequestDTO; +import com.eatsfine.eatsfine.domain.payment.dto.response.TossPaymentResponse; +import com.eatsfine.eatsfine.global.apiPayload.code.status.ErrorStatus; +import com.eatsfine.eatsfine.global.apiPayload.exception.GeneralException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestClient; + +@Slf4j +@Service +public class TossPaymentService { + + private final RestClient tossPaymentClient; + + public TossPaymentService(@Qualifier("tossPaymentClient") RestClient tossPaymentClient) { + this.tossPaymentClient = tossPaymentClient; + } + + public TossPaymentResponse confirm(PaymentConfirmDTO dto) { + try { + return tossPaymentClient.post() + .uri("/v1/payments/confirm") + .body(dto) + .retrieve() + .body(TossPaymentResponse.class); + } catch (Exception e) { + log.error("Toss Payment API Error", e); + throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR); + } + } + + public TossPaymentResponse cancel(String paymentKey, PaymentRequestDTO.CancelPaymentDTO dto) { + try { + return tossPaymentClient.post() + .uri("/v1/payments/{paymentKey}/cancel", paymentKey) + .body(dto) + .retrieve() + .body(TossPaymentResponse.class); + } catch (Exception e) { + log.error("Toss Payment Cancel API Error", e); + throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR); + } + } +}