generated from pricelees/issue-pr-template
refactor: 결제 도메인(Service, Client) 로깅 추가
This commit is contained in:
parent
351069a572
commit
f5e9212c01
@ -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,85 +17,127 @@ 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,
|
||||||
totalAmount = approveResponse.totalAmount,
|
totalAmount = approveResponse.totalAmount,
|
||||||
reservation = reservation,
|
reservation = reservation,
|
||||||
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,
|
||||||
cancelAmount = cancelInfo.cancelAmount,
|
cancelAmount = cancelInfo.cancelAmount,
|
||||||
approvedAt = approvedAt,
|
approvedAt = approvedAt,
|
||||||
canceledAt = cancelInfo.canceledAt
|
canceledAt = cancelInfo.canceledAt
|
||||||
)
|
)
|
||||||
|
|
||||||
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,
|
||||||
cancelReason = cancelReason,
|
cancelReason = cancelReason,
|
||||||
cancelAmount = payment.totalAmount,
|
cancelAmount = payment.totalAmount,
|
||||||
approvedAt = payment.approvedAt,
|
approvedAt = payment.approvedAt,
|
||||||
canceledAt = canceledAt
|
canceledAt = canceledAt
|
||||||
)
|
)
|
||||||
|
|
||||||
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,11 +15,12 @@ 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,
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
private const val CONFIRM_URL: String = "/v1/payments/confirm"
|
private const val CONFIRM_URL: String = "/v1/payments/confirm"
|
||||||
@ -32,16 +33,19 @@ class TossPaymentClient(
|
|||||||
logPaymentInfo(paymentRequest)
|
logPaymentInfo(paymentRequest)
|
||||||
|
|
||||||
return tossPaymentClient.post()
|
return tossPaymentClient.post()
|
||||||
.uri(CONFIRM_URL)
|
.uri(CONFIRM_URL)
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.body(paymentRequest)
|
.body(paymentRequest)
|
||||||
.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 {
|
||||||
@ -49,47 +53,49 @@ class TossPaymentClient(
|
|||||||
val param = Map.of<String, String>("cancelReason", cancelRequest.cancelReason)
|
val param = Map.of<String, String>("cancelReason", cancelRequest.cancelReason)
|
||||||
|
|
||||||
return tossPaymentClient.post()
|
return tossPaymentClient.post()
|
||||||
.uri(CANCEL_URL, cancelRequest.paymentKey)
|
.uri(CANCEL_URL, cancelRequest.paymentKey)
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.body(param)
|
.body(param)
|
||||||
.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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user