[#30] 코드 구조 개선 #31

Merged
pricelees merged 31 commits from refactor/#30 into main 2025-08-06 10:16:08 +00:00
6 changed files with 192 additions and 113 deletions
Showing only changes of commit b865dc01f6 - Show all commits

View File

@ -1,17 +1,13 @@
package roomescape.payment.business package roomescape.payment.business
import com.github.f4b6a3.tsid.TsidFactory
import io.github.oshai.kotlinlogging.KotlinLogging 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.common.config.next import roomescape.payment.implement.PaymentFinder
import roomescape.payment.exception.PaymentErrorCode import roomescape.payment.implement.PaymentWriter
import roomescape.payment.exception.PaymentException
import roomescape.payment.infrastructure.client.PaymentApproveResponse import roomescape.payment.infrastructure.client.PaymentApproveResponse
import roomescape.payment.infrastructure.persistence.CanceledPaymentEntity import roomescape.payment.infrastructure.persistence.CanceledPaymentEntity
import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository
import roomescape.payment.infrastructure.persistence.PaymentEntity import roomescape.payment.infrastructure.persistence.PaymentEntity
import roomescape.payment.infrastructure.persistence.PaymentRepository
import roomescape.payment.web.PaymentCancelRequest import roomescape.payment.web.PaymentCancelRequest
import roomescape.payment.web.PaymentCancelResponse import roomescape.payment.web.PaymentCancelResponse
import roomescape.payment.web.PaymentCreateResponse import roomescape.payment.web.PaymentCreateResponse
@ -23,108 +19,70 @@ private val log = KotlinLogging.logger {}
@Service @Service
class PaymentService( class PaymentService(
private val tsidFactory: TsidFactory, private val paymentFinder: PaymentFinder,
private val paymentRepository: PaymentRepository, private val paymentWriter: PaymentWriter
private val canceledPaymentRepository: CanceledPaymentRepository,
) { ) {
@Transactional @Transactional
fun createPayment( fun createPayment(
approveResponse: PaymentApproveResponse, approvedPaymentInfo: PaymentApproveResponse,
reservation: ReservationEntity, reservation: ReservationEntity,
): PaymentCreateResponse { ): PaymentCreateResponse {
log.debug { "[PaymentService.createPayment] 결제 정보 저장 시작: request=$approveResponse, reservationId=${reservation.id}" } log.info { "[PaymentService.createPayment] 시작: paymentKey=${approvedPaymentInfo.paymentKey}, reservationId=${reservation.id}" }
val payment = PaymentEntity(
_id = tsidFactory.next(), val created: PaymentEntity = paymentWriter.create(
orderId = approveResponse.orderId, paymentKey = approvedPaymentInfo.paymentKey,
paymentKey = approveResponse.paymentKey, orderId = approvedPaymentInfo.orderId,
totalAmount = approveResponse.totalAmount, totalAmount = approvedPaymentInfo.totalAmount,
reservation = reservation, approvedAt = approvedPaymentInfo.approvedAt,
approvedAt = approveResponse.approvedAt reservation = reservation
) )
return paymentRepository.save(payment) return created.toCreateResponse()
.toCreateResponse() .also { log.info { "[PaymentService.createPayment] 완료: paymentKey=${it.paymentKey}, reservationId=${reservation.id}, paymentId=${it.id}" } }
.also { log.info { "[PaymentService.createPayment] 결제 정보 저장 완료: paymentId=${it.id}, reservationId=${reservation.id}" } }
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
fun isReservationPaid(reservationId: Long): Boolean { fun existsByReservationId(reservationId: Long): Boolean {
log.debug { "[PaymentService.isReservationPaid] 예약 결제 여부 확인 시작: reservationId=$reservationId" } log.info { "[PaymentService.isReservationPaid] 시작: reservationId=$reservationId" }
return paymentRepository.existsByReservationId(reservationId) return paymentFinder.existsPaymentByReservationId(reservationId)
.also { log.info { "[PaymentService.isReservationPaid] 예약 결제 여부 확인 완료: reservationId=$reservationId, isPaid=$it" } } .also { log.info { "[PaymentService.isReservationPaid] 완료: reservationId=$reservationId, isPaid=$it" } }
} }
@Transactional @Transactional
fun createCanceledPayment( fun createCanceledPayment(
cancelInfo: PaymentCancelResponse, canceledPaymentInfo: PaymentCancelResponse,
approvedAt: OffsetDateTime, approvedAt: OffsetDateTime,
paymentKey: String, paymentKey: String,
): CanceledPaymentEntity { ): CanceledPaymentEntity {
log.debug { log.info { "[PaymentService.createCanceledPayment] 시작: paymentKey=$paymentKey" }
"[PaymentService.createCanceledPayment] 결제 취소 정보 저장 시작: paymentKey=$paymentKey" +
", cancelInfo=$cancelInfo" val created: CanceledPaymentEntity = paymentWriter.createCanceled(
} cancelReason = canceledPaymentInfo.cancelReason,
val canceledPayment = CanceledPaymentEntity( cancelAmount = canceledPaymentInfo.cancelAmount,
_id = tsidFactory.next(), canceledAt = canceledPaymentInfo.canceledAt,
paymentKey = paymentKey,
cancelReason = cancelInfo.cancelReason,
cancelAmount = cancelInfo.cancelAmount,
approvedAt = approvedAt, approvedAt = approvedAt,
canceledAt = cancelInfo.canceledAt paymentKey = paymentKey
) )
return canceledPaymentRepository.save(canceledPayment) return created.also {
.also { log.info { "[PaymentService.createCanceledPayment] 완료: paymentKey=${paymentKey}, canceledPaymentId=${it.id}" }
log.info {
"[PaymentService.createCanceledPayment] 결제 취소 정보 생성 완료: canceledPaymentId=${it.id}" +
", paymentKey=${paymentKey}, amount=${cancelInfo.cancelAmount}, canceledAt=${it.canceledAt}"
}
} }
} }
@Transactional @Transactional
fun createCanceledPaymentByReservationId(reservationId: Long): PaymentCancelRequest { fun createCanceledPayment(reservationId: Long): PaymentCancelRequest {
log.debug { "[PaymentService.createCanceledPaymentByReservationId] 예약 삭제 & 결제 취소 정보 저장 시작: reservationId=$reservationId" } log.info { "[PaymentService.createCanceledPaymentByReservationId] 시작: reservationId=$reservationId" }
val paymentKey: String = paymentRepository.findPaymentKeyByReservationId(reservationId)
?: run {
log.warn { "[PaymentService.createCanceledPaymentByReservationId] 예약 조회 실패: reservationId=$reservationId" }
throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND)
}
val canceled: CanceledPaymentEntity = cancelPayment(paymentKey) val payment: PaymentEntity = paymentFinder.findByReservationId(reservationId)
val canceled: CanceledPaymentEntity = paymentWriter.createCanceled(
return PaymentCancelRequest(paymentKey, canceled.cancelAmount, canceled.cancelReason) payment = payment,
.also { log.info { "[PaymentService.createCanceledPaymentByReservationId] 예약 ID로 결제 취소 완료: reservationId=$reservationId" } } cancelReason = "예약 취소",
} canceledAt = OffsetDateTime.now(),
private fun cancelPayment(
paymentKey: String,
cancelReason: String = "고객 요청",
canceledAt: OffsetDateTime = OffsetDateTime.now(),
): CanceledPaymentEntity {
log.debug { "[PaymentService.cancelPayment] 결제 취소 정보 저장 시작: paymentKey=$paymentKey" }
val payment: PaymentEntity = paymentRepository.findByPaymentKey(paymentKey)
?.also {
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(
_id = tsidFactory.next(),
paymentKey = paymentKey,
cancelReason = cancelReason,
cancelAmount = payment.totalAmount,
approvedAt = payment.approvedAt,
canceledAt = canceledAt
) )
return canceledPaymentRepository.save(canceledPayment) return PaymentCancelRequest(canceled.paymentKey, canceled.cancelAmount, canceled.cancelReason)
.also { log.info { "[PaymentService.cancelPayment] 결제 취소 정보 저장 완료: canceledPaymentId=${it.id}" } } .also { log.info { "[PaymentService.createCanceledPaymentByReservationId] 완료: reservationId=$reservationId, paymentKey=${it.paymentKey}" } }
} }
@Transactional @Transactional
@ -132,18 +90,10 @@ class PaymentService(
paymentKey: String, paymentKey: String,
canceledAt: OffsetDateTime, canceledAt: OffsetDateTime,
) { ) {
log.debug { "[PaymentService.updateCanceledTime] 취소 시간 업데이트 시작: paymentKey=$paymentKey, canceledAt=$canceledAt" } log.info { "[PaymentService.updateCanceledTime] 시작: paymentKey=$paymentKey, canceledAt=$canceledAt" }
canceledPaymentRepository.findByPaymentKey(paymentKey)
?.apply { this.canceledAt = canceledAt } paymentFinder.findCanceledByKey(paymentKey).apply { this.canceledAt = canceledAt }
?.also {
log.info { log.info { "[PaymentService.updateCanceledTime] 완료: paymentKey=$paymentKey, canceledAt=$canceledAt" }
"[PaymentService.updateCanceledTime] 취소 시간 업데이트 완료: paymentKey=$paymentKey" +
", canceledAt=$canceledAt"
}
}
?: run {
log.warn { "[PaymentService.updateCanceledTime] 결제 정보 조회 실패: paymentKey=$paymentKey" }
throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND)
}
} }
} }

View File

@ -0,0 +1,48 @@
package roomescape.payment.implement
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.stereotype.Component
import roomescape.payment.exception.PaymentErrorCode
import roomescape.payment.exception.PaymentException
import roomescape.payment.infrastructure.persistence.CanceledPaymentEntity
import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository
import roomescape.payment.infrastructure.persistence.PaymentEntity
import roomescape.payment.infrastructure.persistence.PaymentRepository
private val log: KLogger = KotlinLogging.logger {}
@Component
class PaymentFinder(
private val paymentRepository: PaymentRepository,
private val canceledPaymentRepository: CanceledPaymentRepository,
) {
fun existsPaymentByReservationId(reservationId: Long): Boolean {
log.debug { "[PaymentFinder.existsPaymentByReservationId] 시작: reservationId=$reservationId" }
return paymentRepository.existsByReservationId(reservationId)
.also { log.debug { "[PaymentFinder.existsPaymentByReservationId] 완료: reservationId=$reservationId, isExist=$it" } }
}
fun findByReservationId(reservationId: Long): PaymentEntity {
log.debug { "[PaymentFinder.findByReservationId] 시작: reservationId=$reservationId" }
return paymentRepository.findByReservationId(reservationId)
?.also { log.debug { "[PaymentFinder.findByReservationId] 완료: reservationId=$reservationId" } }
?: run {
log.warn { "[PaymentFinder.findByReservationId] 실패: reservationId=$reservationId" }
throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND)
}
}
fun findCanceledByKey(paymentKey: String): CanceledPaymentEntity {
log.debug { "[PaymentFinder.findCanceledByKey] 시작: paymentKey=$paymentKey" }
return canceledPaymentRepository.findByPaymentKey(paymentKey)
?.also { log.debug { "[PaymentFinder.findCanceledByKey] 완료: canceledPaymentId=${it.id}" } }
?: run {
log.warn { "[PaymentFinder.findCanceledByKey] 실패: paymentKey=$paymentKey" }
throw PaymentException(PaymentErrorCode.CANCELED_PAYMENT_NOT_FOUND)
}
}
}

View File

@ -0,0 +1,80 @@
package roomescape.payment.implement
import com.github.f4b6a3.tsid.TsidFactory
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.stereotype.Component
import roomescape.common.config.next
import roomescape.payment.infrastructure.persistence.CanceledPaymentEntity
import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository
import roomescape.payment.infrastructure.persistence.PaymentEntity
import roomescape.payment.infrastructure.persistence.PaymentRepository
import roomescape.reservation.infrastructure.persistence.ReservationEntity
import java.time.OffsetDateTime
private val log: KLogger = KotlinLogging.logger {}
@Component
class PaymentWriter(
private val paymentRepository: PaymentRepository,
private val canceledPaymentRepository: CanceledPaymentRepository,
private val tsidFactory: TsidFactory,
) {
fun create(
paymentKey: String,
orderId: String,
totalAmount: Long,
approvedAt: OffsetDateTime,
reservation: ReservationEntity
): PaymentEntity {
log.debug { "[PaymentWriter.create] 시작: paymentKey=${paymentKey}, reservationId=${reservation.id}" }
val payment = PaymentEntity(
_id = tsidFactory.next(),
orderId = orderId,
paymentKey = paymentKey,
totalAmount = totalAmount,
reservation = reservation,
approvedAt = approvedAt
)
return paymentRepository.save(payment)
.also { log.debug { "[PaymentWriter.create] 완료: paymentId=${it.id}, reservationId=${reservation.id}" } }
}
fun createCanceled(
payment: PaymentEntity,
cancelReason: String,
canceledAt: OffsetDateTime,
): CanceledPaymentEntity = createCanceled(
cancelReason = cancelReason,
canceledAt = canceledAt,
cancelAmount = payment.totalAmount,
approvedAt = payment.approvedAt,
paymentKey = payment.paymentKey
)
fun createCanceled(
cancelReason: String,
cancelAmount: Long,
canceledAt: OffsetDateTime,
approvedAt: OffsetDateTime,
paymentKey: String,
): CanceledPaymentEntity {
log.debug { "[PaymentWriter.createCanceled] 시작: paymentKey=$paymentKey cancelAmount=$cancelAmount" }
val canceledPayment = CanceledPaymentEntity(
_id = tsidFactory.next(),
paymentKey = paymentKey,
cancelReason = cancelReason,
cancelAmount = cancelAmount,
approvedAt = approvedAt,
canceledAt = canceledAt
)
return canceledPaymentRepository.save(canceledPayment)
.also {
log.debug { "[PaymentWriter.createCanceled] 완료: paymentKey=${paymentKey}, canceledPaymentId=${it.id}" }
}
}
}

View File

@ -1,14 +1,12 @@
package roomescape.payment.infrastructure.persistence package roomescape.payment.infrastructure.persistence
import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
interface PaymentRepository : JpaRepository<PaymentEntity, Long> { interface PaymentRepository : JpaRepository<PaymentEntity, Long> {
fun existsByReservationId(reservationId: Long): Boolean fun existsByReservationId(reservationId: Long): Boolean
@Query("SELECT p.paymentKey FROM PaymentEntity p WHERE p.reservation.id = :reservationId") fun findByReservationId(reservationId: Long): PaymentEntity?
fun findPaymentKeyByReservationId(reservationId: Long): String?
fun findByPaymentKey(paymentKey: String): PaymentEntity? fun findByPaymentKey(paymentKey: String): PaymentEntity?
} }

View File

@ -3,8 +3,6 @@ package roomescape.payment.web
import com.fasterxml.jackson.databind.annotation.JsonDeserialize import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import roomescape.payment.infrastructure.client.PaymentCancelResponseDeserializer import roomescape.payment.infrastructure.client.PaymentCancelResponseDeserializer
import roomescape.payment.infrastructure.persistence.PaymentEntity import roomescape.payment.infrastructure.persistence.PaymentEntity
import roomescape.reservation.web.ReservationRetrieveResponse
import roomescape.reservation.web.toRetrieveResponse
import java.time.OffsetDateTime import java.time.OffsetDateTime
data class PaymentCancelRequest( data class PaymentCancelRequest(
@ -26,15 +24,15 @@ data class PaymentCreateResponse(
val orderId: String, val orderId: String,
val paymentKey: String, val paymentKey: String,
val totalAmount: Long, val totalAmount: Long,
val reservation: ReservationRetrieveResponse, val reservationId: Long,
val approvedAt: OffsetDateTime val approvedAt: OffsetDateTime
) )
fun PaymentEntity.toCreateResponse(): PaymentCreateResponse = PaymentCreateResponse( fun PaymentEntity.toCreateResponse() = PaymentCreateResponse(
id = this.id!!, id = this.id!!,
orderId = this.orderId, orderId = this.orderId,
paymentKey = this.paymentKey, paymentKey = this.paymentKey,
totalAmount = this.totalAmount, totalAmount = this.totalAmount,
reservation = this.reservation.toRetrieveResponse(), reservationId = this.reservation.id!!,
approvedAt = this.approvedAt approvedAt = this.approvedAt
) )

View File

@ -8,8 +8,9 @@ import roomescape.payment.infrastructure.client.PaymentApproveResponse
import roomescape.payment.web.PaymentCancelRequest import roomescape.payment.web.PaymentCancelRequest
import roomescape.payment.web.PaymentCancelResponse import roomescape.payment.web.PaymentCancelResponse
import roomescape.reservation.infrastructure.persistence.ReservationEntity import roomescape.reservation.infrastructure.persistence.ReservationEntity
import roomescape.reservation.web.ReservationCreateResponse
import roomescape.reservation.web.ReservationCreateWithPaymentRequest import roomescape.reservation.web.ReservationCreateWithPaymentRequest
import roomescape.reservation.web.ReservationRetrieveResponse import roomescape.reservation.web.toCreateResponse
import java.time.OffsetDateTime import java.time.OffsetDateTime
private val log = KotlinLogging.logger {} private val log = KotlinLogging.logger {}
@ -22,24 +23,24 @@ class ReservationWithPaymentService(
) { ) {
fun createReservationAndPayment( fun createReservationAndPayment(
request: ReservationCreateWithPaymentRequest, request: ReservationCreateWithPaymentRequest,
paymentInfo: PaymentApproveResponse, approvedPaymentInfo: PaymentApproveResponse,
memberId: Long, memberId: Long,
): ReservationCreateResponse { ): ReservationCreateResponse {
log.info { "[ReservationWithPaymentService.createReservationAndPayment] 예약 & 결제 정보 저장 시작: memberId=$memberId, paymentInfo=$approvedPaymentInfo" } log.info { "[ReservationWithPaymentService.createReservationAndPayment] 예약 & 결제 정보 저장 시작: memberId=$memberId, paymentInfo=$approvedPaymentInfo" }
val reservation: ReservationEntity = reservationService.createConfirmedReservation(request, memberId) val reservation: ReservationEntity = reservationService.createConfirmedReservation(request, memberId)
.also { paymentService.createPayment(approvedPaymentInfo, it) }
return paymentService.createPayment(paymentInfo, reservation) return reservation.toCreateResponse()
.also { log.info { "[ReservationWithPaymentService.createReservationAndPayment] 예약 & 결제 정보 저장 완료: reservationId=${reservation.id}, paymentId=${it.id}" } } .also { log.info { "[ReservationWithPaymentService.createReservationAndPayment] 예약 & 결제 정보 저장 완료: reservationId=${reservation.id}, paymentId=${it.id}" } }
.reservation
} }
fun createCanceledPayment( fun createCanceledPayment(
cancelInfo: PaymentCancelResponse, canceledPaymentInfo: PaymentCancelResponse,
approvedAt: OffsetDateTime, approvedAt: OffsetDateTime,
paymentKey: String, paymentKey: String,
) { ) {
paymentService.createCanceledPayment(cancelInfo, approvedAt, paymentKey) paymentService.createCanceledPayment(canceledPaymentInfo, approvedAt, paymentKey)
} }
fun deleteReservationAndPayment( fun deleteReservationAndPayment(
@ -47,7 +48,7 @@ class ReservationWithPaymentService(
memberId: Long, memberId: Long,
): PaymentCancelRequest { ): PaymentCancelRequest {
log.info { "[ReservationWithPaymentService.deleteReservationAndPayment] 결제 취소 정보 저장 & 예약 삭제 시작: reservationId=$reservationId" } log.info { "[ReservationWithPaymentService.deleteReservationAndPayment] 결제 취소 정보 저장 & 예약 삭제 시작: reservationId=$reservationId" }
val paymentCancelRequest = paymentService.createCanceledPaymentByReservationId(reservationId) val paymentCancelRequest = paymentService.createCanceledPayment(reservationId)
reservationService.deleteReservation(reservationId, memberId) reservationService.deleteReservation(reservationId, memberId)
log.info { "[ReservationWithPaymentService.deleteReservationAndPayment] 결제 취소 정보 저장 & 예약 삭제 완료: reservationId=$reservationId" } log.info { "[ReservationWithPaymentService.deleteReservationAndPayment] 결제 취소 정보 저장 & 예약 삭제 완료: reservationId=$reservationId" }
@ -56,9 +57,13 @@ class ReservationWithPaymentService(
@Transactional(readOnly = true) @Transactional(readOnly = true)
fun isNotPaidReservation(reservationId: Long): Boolean { fun isNotPaidReservation(reservationId: Long): Boolean {
log.debug { "[ReservationWithPaymentService.isNotPaidReservation] 예약 결제 여부 확인: reservationId=$reservationId" } log.info { "[ReservationWithPaymentService.isNotPaidReservation] 시작: reservationId=$reservationId" }
return !paymentService.isReservationPaid(reservationId)
.also { log.info { "[ReservationWithPaymentService.isNotPaidReservation] 결제 여부 확인 완료: reservationId=$reservationId, 결제 여부=${!it}" } } val notPaid: Boolean = !paymentService.existsByReservationId(reservationId)
return notPaid.also {
log.info { "[ReservationWithPaymentService.isNotPaidReservation] 완료: reservationId=$reservationId, 미결제=${notPaid}" }
}
} }
fun updateCanceledTime( fun updateCanceledTime(