refactor: 결제 도메인(Service, Client) 로깅 추가

This commit is contained in:
이상진 2025-07-27 23:27:36 +09:00
parent 351069a572
commit f5e9212c01
2 changed files with 115 additions and 66 deletions

View File

@ -1,5 +1,6 @@
package roomescape.payment.business package roomescape.payment.business
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional import org.springframework.transaction.annotation.Transactional
import roomescape.payment.exception.PaymentErrorCode import roomescape.payment.exception.PaymentErrorCode
@ -16,16 +17,19 @@ import roomescape.payment.web.toCreateResponse
import roomescape.reservation.infrastructure.persistence.ReservationEntity import roomescape.reservation.infrastructure.persistence.ReservationEntity
import java.time.OffsetDateTime import java.time.OffsetDateTime
private val log = KotlinLogging.logger {}
@Service @Service
class PaymentService( class PaymentService(
private val paymentRepository: PaymentRepository, private val paymentRepository: PaymentRepository,
private val canceledPaymentRepository: CanceledPaymentRepository private val canceledPaymentRepository: CanceledPaymentRepository,
) { ) {
@Transactional @Transactional
fun createPayment( fun createPayment(
approveResponse: PaymentApproveResponse, approveResponse: PaymentApproveResponse,
reservation: ReservationEntity reservation: ReservationEntity,
): PaymentCreateResponse { ): PaymentCreateResponse {
log.debug { "[PaymentService.createPayment] 결제 정보 저장 시작: request=$approveResponse, reservationId=${reservation.id}" }
val payment = PaymentEntity( val payment = PaymentEntity(
orderId = approveResponse.orderId, orderId = approveResponse.orderId,
paymentKey = approveResponse.paymentKey, paymentKey = approveResponse.paymentKey,
@ -34,18 +38,29 @@ class PaymentService(
approvedAt = approveResponse.approvedAt approvedAt = approveResponse.approvedAt
) )
return paymentRepository.save(payment).toCreateResponse() return paymentRepository.save(payment)
.toCreateResponse()
.also { log.info { "[PaymentService.createPayment] 결제 정보 저장 완료: paymentId=${it.id}, reservationId=${reservation.id}" } }
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
fun isReservationPaid(reservationId: Long): Boolean = paymentRepository.existsByReservationId(reservationId) fun isReservationPaid(reservationId: Long): Boolean {
log.debug { "[PaymentService.isReservationPaid] 예약 결제 여부 확인 시작: reservationId=$reservationId" }
return paymentRepository.existsByReservationId(reservationId)
.also { log.info { "[PaymentService.isReservationPaid] 예약 결제 여부 확인 완료: reservationId=$reservationId, isPaid=$it" } }
}
@Transactional @Transactional
fun createCanceledPayment( fun createCanceledPayment(
cancelInfo: PaymentCancelResponse, cancelInfo: PaymentCancelResponse,
approvedAt: OffsetDateTime, approvedAt: OffsetDateTime,
paymentKey: String paymentKey: String,
): CanceledPaymentEntity { ): CanceledPaymentEntity {
log.debug {
"[PaymentService.createCanceledPayment] 결제 취소 정보 저장 시작: paymentKey=$paymentKey" +
", cancelInfo=$cancelInfo"
}
val canceledPayment = CanceledPaymentEntity( val canceledPayment = CanceledPaymentEntity(
paymentKey = paymentKey, paymentKey = paymentKey,
cancelReason = cancelInfo.cancelReason, cancelReason = cancelInfo.cancelReason,
@ -55,27 +70,44 @@ class PaymentService(
) )
return canceledPaymentRepository.save(canceledPayment) return canceledPaymentRepository.save(canceledPayment)
.also {
log.info {
"[PaymentService.createCanceledPayment] 결제 취소 정보 생성 완료: canceledPaymentId=${it.id}" +
", paymentKey=${paymentKey}, amount=${cancelInfo.cancelAmount}, canceledAt=${it.canceledAt}"
}
}
} }
@Transactional @Transactional
fun createCanceledPaymentByReservationId(reservationId: Long): PaymentCancelRequest { fun createCanceledPaymentByReservationId(reservationId: Long): PaymentCancelRequest {
log.debug { "[PaymentService.createCanceledPaymentByReservationId] 예약 삭제 & 결제 취소 정보 저장 시작: reservationId=$reservationId" }
val paymentKey: String = paymentRepository.findPaymentKeyByReservationId(reservationId) val paymentKey: String = paymentRepository.findPaymentKeyByReservationId(reservationId)
?: throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND) ?: run {
log.warn { "[PaymentService.createCanceledPaymentByReservationId] 예약 조회 실패: reservationId=$reservationId" }
throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND)
}
// 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다.
val canceled: CanceledPaymentEntity = cancelPayment(paymentKey) val canceled: CanceledPaymentEntity = cancelPayment(paymentKey)
return PaymentCancelRequest(paymentKey, canceled.cancelAmount, canceled.cancelReason) return PaymentCancelRequest(paymentKey, canceled.cancelAmount, canceled.cancelReason)
.also { log.info { "[PaymentService.createCanceledPaymentByReservationId] 예약 ID로 결제 취소 완료: reservationId=$reservationId" } }
} }
private fun cancelPayment( private fun cancelPayment(
paymentKey: String, paymentKey: String,
cancelReason: String = "고객 요청", cancelReason: String = "고객 요청",
canceledAt: OffsetDateTime = OffsetDateTime.now() canceledAt: OffsetDateTime = OffsetDateTime.now(),
): CanceledPaymentEntity { ): CanceledPaymentEntity {
log.debug { "[PaymentService.cancelPayment] 결제 취소 정보 저장 시작: paymentKey=$paymentKey" }
val payment: PaymentEntity = paymentRepository.findByPaymentKey(paymentKey) val payment: PaymentEntity = paymentRepository.findByPaymentKey(paymentKey)
?.also { paymentRepository.delete(it) } ?.also {
?: throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND) paymentRepository.delete(it)
log.info { "[PaymentService.cancelPayment] 결제 정보 삭제 완료: paymentId=${it.id}, paymentKey=$paymentKey" }
}
?: run {
log.warn { "[PaymentService.cancelPayment] 결제 정보 조회 실패: paymentKey=$paymentKey" }
throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND)
}
val canceledPayment = CanceledPaymentEntity( val canceledPayment = CanceledPaymentEntity(
paymentKey = paymentKey, paymentKey = paymentKey,
@ -86,15 +118,26 @@ class PaymentService(
) )
return canceledPaymentRepository.save(canceledPayment) return canceledPaymentRepository.save(canceledPayment)
.also { log.info { "[PaymentService.cancelPayment] 결제 취소 정보 저장 완료: canceledPaymentId=${it.id}" } }
} }
@Transactional @Transactional
fun updateCanceledTime( fun updateCanceledTime(
paymentKey: String, paymentKey: String,
canceledAt: OffsetDateTime canceledAt: OffsetDateTime,
) { ) {
log.debug { "[PaymentService.updateCanceledTime] 취소 시간 업데이트 시작: paymentKey=$paymentKey, canceledAt=$canceledAt" }
canceledPaymentRepository.findByPaymentKey(paymentKey) canceledPaymentRepository.findByPaymentKey(paymentKey)
?.apply { this.canceledAt = canceledAt } ?.apply { this.canceledAt = canceledAt }
?: throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND) ?.also {
log.info {
"[PaymentService.updateCanceledTime] 취소 시간 업데이트 완료: paymentKey=$paymentKey" +
", canceledAt=$canceledAt"
}
}
?: run {
log.warn { "[PaymentService.updateCanceledTime] 결제 정보 조회 실패: paymentKey=$paymentKey" }
throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND)
}
} }
} }

View File

@ -15,9 +15,10 @@ import roomescape.payment.web.PaymentCancelRequest
import roomescape.payment.web.PaymentCancelResponse import roomescape.payment.web.PaymentCancelResponse
import java.util.Map import java.util.Map
private val log: KLogger = KotlinLogging.logger {}
@Component @Component
class TossPaymentClient( class TossPaymentClient(
private val log: KLogger = KotlinLogging.logger {},
private val objectMapper: ObjectMapper, private val objectMapper: ObjectMapper,
tossPaymentClientBuilder: RestClient.Builder, tossPaymentClientBuilder: RestClient.Builder,
) { ) {
@ -38,10 +39,13 @@ class TossPaymentClient(
.retrieve() .retrieve()
.onStatus( .onStatus(
{ status: HttpStatusCode -> status.is4xxClientError || status.is5xxServerError }, { status: HttpStatusCode -> status.is4xxClientError || status.is5xxServerError },
{ req: HttpRequest, res: ClientHttpResponse -> handlePaymentError(res) } { req: HttpRequest, res: ClientHttpResponse -> handlePaymentError(res, "confirm") }
) )
.body(PaymentApproveResponse::class.java) .body(PaymentApproveResponse::class.java)
?: throw PaymentException(PaymentErrorCode.PAYMENT_PROVIDER_ERROR) ?: run {
log.error { "[TossPaymentClient] 응답 변환 오류" }
throw PaymentException(PaymentErrorCode.PAYMENT_PROVIDER_ERROR)
}
} }
fun cancel(cancelRequest: PaymentCancelRequest): PaymentCancelResponse { fun cancel(cancelRequest: PaymentCancelRequest): PaymentCancelResponse {
@ -55,41 +59,43 @@ class TossPaymentClient(
.retrieve() .retrieve()
.onStatus( .onStatus(
{ status: HttpStatusCode -> status.is4xxClientError || status.is5xxServerError }, { status: HttpStatusCode -> status.is4xxClientError || status.is5xxServerError },
{ req: HttpRequest, res: ClientHttpResponse -> handlePaymentError(res) } { req: HttpRequest, res: ClientHttpResponse -> handlePaymentError(res, "cancel") }
) )
.body(PaymentCancelResponse::class.java) .body(PaymentCancelResponse::class.java)
?: throw PaymentException(PaymentErrorCode.PAYMENT_PROVIDER_ERROR) ?: run {
log.error { "[TossPaymentClient] 응답 변환 오류" }
throw PaymentException(PaymentErrorCode.PAYMENT_PROVIDER_ERROR)
}
} }
private fun logPaymentInfo(paymentRequest: PaymentApproveRequest) { private fun logPaymentInfo(paymentRequest: PaymentApproveRequest) {
log.info { log.info {
"결제 승인 요청: paymentKey=${paymentRequest.paymentKey}, orderId=${paymentRequest.orderId}, " + "[TossPaymentClient.confirm] 결제 승인 요청: request: $paymentRequest"
"amount=${paymentRequest.amount}, paymentType=${paymentRequest.paymentType}"
} }
} }
private fun logPaymentCancelInfo(cancelRequest: PaymentCancelRequest) { private fun logPaymentCancelInfo(cancelRequest: PaymentCancelRequest) {
log.info { log.info {
"결제 취소 요청: paymentKey=${cancelRequest.paymentKey}, amount=${cancelRequest.amount}, " + "[TossPaymentClient.cancel] 결제 취소 요청: request: $cancelRequest"
"cancelReason=${cancelRequest.cancelReason}"
} }
} }
private fun handlePaymentError( private fun handlePaymentError(
res: ClientHttpResponse res: ClientHttpResponse,
calledBy: String
): Nothing { ): Nothing {
getErrorCodeByHttpStatus(res.statusCode).also { getErrorCodeByHttpStatus(res.statusCode).also {
logTossPaymentError(res) logTossPaymentError(res, calledBy)
throw PaymentException(it) throw PaymentException(it)
} }
} }
private fun logTossPaymentError(res: ClientHttpResponse): TossPaymentErrorResponse { private fun logTossPaymentError(res: ClientHttpResponse, calledBy: String): TossPaymentErrorResponse {
val body = res.body val body = res.body
val errorResponse = objectMapper.readValue(body, TossPaymentErrorResponse::class.java) val errorResponse = objectMapper.readValue(body, TossPaymentErrorResponse::class.java)
body.close() body.close()
log.error { "결제 실패. response: $errorResponse" } log.error { "[TossPaymentClient.$calledBy] 요청 실패: response: $errorResponse" }
return errorResponse return errorResponse
} }