From d52e30df4f8cd7a9832a6fd3e55e0103b0fcd610 Mon Sep 17 00:00:00 2001 From: pricelees Date: Wed, 16 Jul 2025 14:52:23 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20PaymentService=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20\@Transactional=20=EB=AA=85?= =?UTF-8?q?=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 읽고 null이 아니면 필드 조회 -> 필드를 쿼리로 바로 조회 - 단순한 존재 여부 판단 -> find => exists로 수정 --- .../payment/business/PaymentService.kt | 90 ++++++++++++------- .../ReservationWithPaymentService.java | 2 +- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/src/main/java/roomescape/payment/business/PaymentService.kt b/src/main/java/roomescape/payment/business/PaymentService.kt index fdf283ae..02f2d06b 100644 --- a/src/main/java/roomescape/payment/business/PaymentService.kt +++ b/src/main/java/roomescape/payment/business/PaymentService.kt @@ -15,15 +15,12 @@ import roomescape.payment.web.ReservationPaymentResponse import roomescape.payment.web.toReservationPaymentResponse import roomescape.reservation.domain.Reservation import java.time.OffsetDateTime -import java.util.* -import java.util.function.Supplier @Service class PaymentService( private val paymentRepository: PaymentRepository, private val canceledPaymentRepository: CanceledPaymentRepository ) { - @Transactional fun savePayment( paymentResponse: PaymentApprove.Response, @@ -39,46 +36,73 @@ class PaymentService( }.toReservationPaymentResponse() @Transactional(readOnly = true) - fun findPaymentByReservationId(reservationId: Long): Optional { - return paymentRepository.findByReservationId(reservationId) - } + fun isReservationPaid( + reservationId: Long + ): Boolean = paymentRepository.existsByReservationId(reservationId) - fun saveCanceledPayment(cancelInfo: PaymentCancel.Response, approvedAt: OffsetDateTime, paymentKey: String) { - canceledPaymentRepository.save(CanceledPaymentEntity(null, - paymentKey, cancelInfo.cancelReason, cancelInfo.cancelAmount, approvedAt, cancelInfo.canceledAt)) - } + @Transactional + fun saveCanceledPayment( + cancelInfo: PaymentCancel.Response, + approvedAt: OffsetDateTime, + paymentKey: String + ): CanceledPaymentEntity = CanceledPaymentEntity( + paymentKey = paymentKey, + cancelReason = cancelInfo.cancelReason, + cancelAmount = cancelInfo.cancelAmount, + approvedAt = approvedAt, + canceledAt = cancelInfo.canceledAt + ).also { canceledPaymentRepository.save(it) } + + @Transactional fun cancelPaymentByAdmin(reservationId: Long): PaymentCancel.Request { - val paymentKey = findPaymentByReservationId(reservationId) - .orElseThrow(java.util.function.Supplier { - RoomescapeException(roomescape.common.exception.ErrorType.PAYMENT_NOT_POUND, - kotlin.String.format("[reservationId: %d]", reservationId), org.springframework.http.HttpStatus.NOT_FOUND) - })!! - .paymentKey + val paymentKey: String = paymentRepository.findPaymentKeyByReservationId(reservationId) + ?: throw RoomescapeException( + ErrorType.PAYMENT_NOT_POUND, + "[reservationId: $reservationId]", + HttpStatus.NOT_FOUND + ) // 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다. - val canceled = cancelPayment(paymentKey, "고객 요청", OffsetDateTime.now()) + val canceled: CanceledPaymentEntity = cancelPayment(paymentKey) return PaymentCancel.Request(paymentKey, canceled.cancelAmount, canceled.cancelReason) } - private fun cancelPayment(paymentKey: String, cancelReason: String, canceledAt: OffsetDateTime): CanceledPaymentEntity { - val paymentEntity = paymentRepository.findByPaymentKey(paymentKey) - .orElseThrow(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) }) - paymentRepository.delete(paymentEntity) + private fun cancelPayment( + paymentKey: String, + cancelReason: String = "고객 요청", + canceledAt: OffsetDateTime = OffsetDateTime.now() + ): CanceledPaymentEntity { + val paymentEntity: PaymentEntity = paymentRepository.findByPaymentKey(paymentKey) + ?.also { paymentRepository.delete(it) } + ?: throw RoomescapeException( + ErrorType.PAYMENT_NOT_POUND, + "[paymentKey: $paymentKey]", + HttpStatus.NOT_FOUND + ) - return canceledPaymentRepository.save(CanceledPaymentEntity(null, paymentKey, cancelReason, paymentEntity.totalAmount, - paymentEntity.approvedAt, canceledAt)) + return CanceledPaymentEntity( + paymentKey = paymentKey, + cancelReason = cancelReason, + cancelAmount = paymentEntity.totalAmount, + approvedAt = paymentEntity.approvedAt, + canceledAt = canceledAt + ).also { + canceledPaymentRepository.save(it) + } } - fun updateCanceledTime(paymentKey: String, canceledAt: OffsetDateTime) { - val canceledPayment = canceledPaymentRepository.findByPaymentKey(paymentKey) - .orElseThrow(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) }) - canceledPayment.canceledAt = canceledAt - } - - private fun throwPaymentNotFoundByPaymentKey(paymentKey: String?): RoomescapeException { - return RoomescapeException( - ErrorType.PAYMENT_NOT_POUND, String.format("[paymentKey: %s]", paymentKey), - HttpStatus.NOT_FOUND) + @Transactional + fun updateCanceledTime( + paymentKey: String, + canceledAt: OffsetDateTime + ) { + canceledPaymentRepository.findByPaymentKey(paymentKey)?.let { + it.canceledAt = canceledAt + } ?: throw RoomescapeException( + ErrorType.PAYMENT_NOT_POUND, + "[paymentKey: $paymentKey]", + HttpStatus.NOT_FOUND + ) } } diff --git a/src/main/java/roomescape/reservation/service/ReservationWithPaymentService.java b/src/main/java/roomescape/reservation/service/ReservationWithPaymentService.java index edec7bae..853e6e30 100644 --- a/src/main/java/roomescape/reservation/service/ReservationWithPaymentService.java +++ b/src/main/java/roomescape/reservation/service/ReservationWithPaymentService.java @@ -47,7 +47,7 @@ public class ReservationWithPaymentService { @Transactional(readOnly = true) public boolean isNotPaidReservation(Long reservationId) { - return paymentService.findPaymentByReservationId(reservationId).isEmpty(); + return !paymentService.isReservationPaid(reservationId); } public void updateCanceledTime(String paymentKey, OffsetDateTime canceledAt) {