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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}