From edf4d3af24fc9e831ac262c1bdf407f09f7578ac Mon Sep 17 00:00:00 2001 From: pricelees Date: Tue, 7 Oct 2025 22:33:58 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B2=B0=EC=A0=9C=20=EC=99=84=EB=A3=8C?= =?UTF-8?q?=20=EC=9D=B4=ED=9B=84=20=EC=98=88=EC=95=BD=20=EB=B0=8F=20?= =?UTF-8?q?=EA=B2=B0=EC=A0=9C=20=EC=A0=80=EC=9E=A5=EC=9D=84=20=EC=9C=84?= =?UTF-8?q?=ED=95=9C=20=EB=B3=84=EB=8F=84=20=EC=84=9C=EB=B9=84=EC=8A=A4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/OrderPostProcessorService.kt | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 service/src/main/kotlin/com/sangdol/roomescape/order/business/OrderPostProcessorService.kt diff --git a/service/src/main/kotlin/com/sangdol/roomescape/order/business/OrderPostProcessorService.kt b/service/src/main/kotlin/com/sangdol/roomescape/order/business/OrderPostProcessorService.kt new file mode 100644 index 00000000..ccd7c9b9 --- /dev/null +++ b/service/src/main/kotlin/com/sangdol/roomescape/order/business/OrderPostProcessorService.kt @@ -0,0 +1,60 @@ +package com.sangdol.roomescape.order.business + +import com.sangdol.common.persistence.IDGenerator +import com.sangdol.common.persistence.TransactionExecutionUtil +import com.sangdol.roomescape.order.infrastructure.persistence.PostOrderTaskEntity +import com.sangdol.roomescape.order.infrastructure.persistence.PostOrderTaskRepository +import com.sangdol.roomescape.payment.business.PaymentService +import com.sangdol.roomescape.payment.dto.PaymentGatewayResponse +import com.sangdol.roomescape.reservation.business.ReservationService +import io.github.oshai.kotlinlogging.KLogger +import io.github.oshai.kotlinlogging.KotlinLogging +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Propagation +import org.springframework.transaction.annotation.Transactional +import java.time.Instant + +private val log: KLogger = KotlinLogging.logger {} + +@Service +class OrderPostProcessorService( + private val idGenerator: IDGenerator, + private val reservationService: ReservationService, + private val paymentService: PaymentService, + private val postOrderTaskRepository: PostOrderTaskRepository, + private val transactionExecutionUtil: TransactionExecutionUtil +) { + @Transactional(propagation = Propagation.REQUIRES_NEW) + fun processAfterPaymentConfirmation( + reservationId: Long, + paymentResponse: PaymentGatewayResponse + ) { + val paymentKey = paymentResponse.paymentKey + try { + log.info { "[processAfterPaymentConfirmation] 결제 정보 저장 및 예약 확정 처리 시작: reservationId=${reservationId}, paymentKey=${paymentKey}" } + + val paymentCreateResponse = paymentService.savePayment(reservationId, paymentResponse) + reservationService.confirmReservation(reservationId) + + log.info { + "[processAfterPaymentConfirmation] 결제 정보 저장 및 예약 확정 처리 완료: reservationId=${reservationId}, paymentKey=${paymentKey}, paymentId=${paymentCreateResponse.paymentId}, paymentDetailId=${paymentCreateResponse.detailId}" + } + } catch (_: Exception) { + log.warn { "[processAfterPaymentConfirmation] 결제 정보 저장 및 예약 확정 처리 실패. 작업 저장 시작: reservationId=${reservationId}, paymentKey=$paymentKey}" } + + transactionExecutionUtil.withNewTransaction(isReadOnly = false) { + PostOrderTaskEntity( + id = idGenerator.create(), + reservationId = reservationId, + paymentKey = paymentKey, + trial = 1, + nextRetryAt = Instant.now().plusSeconds(30), + ).also { + postOrderTaskRepository.save(it) + } + } + + log.info { "[processAfterPaymentConfirmation] 작업 저장 완료" } + } + } +}