refactor: PaymentService 로직 수정 및 \@Transactional 명시

- 읽고 null이 아니면 필드 조회 -> 필드를 쿼리로 바로 조회
- 단순한 존재 여부 판단 -> find => exists로 수정
This commit is contained in:
이상진 2025-07-16 14:52:23 +09:00
parent 4e4956e9dc
commit d52e30df4f
2 changed files with 58 additions and 34 deletions

View File

@ -15,15 +15,12 @@ import roomescape.payment.web.ReservationPaymentResponse
import roomescape.payment.web.toReservationPaymentResponse import roomescape.payment.web.toReservationPaymentResponse
import roomescape.reservation.domain.Reservation import roomescape.reservation.domain.Reservation
import java.time.OffsetDateTime import java.time.OffsetDateTime
import java.util.*
import java.util.function.Supplier
@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 savePayment( fun savePayment(
paymentResponse: PaymentApprove.Response, paymentResponse: PaymentApprove.Response,
@ -39,46 +36,73 @@ class PaymentService(
}.toReservationPaymentResponse() }.toReservationPaymentResponse()
@Transactional(readOnly = true) @Transactional(readOnly = true)
fun findPaymentByReservationId(reservationId: Long): Optional<PaymentEntity> { fun isReservationPaid(
return paymentRepository.findByReservationId(reservationId) reservationId: Long
} ): Boolean = paymentRepository.existsByReservationId(reservationId)
fun saveCanceledPayment(cancelInfo: PaymentCancel.Response, approvedAt: OffsetDateTime, paymentKey: String) { @Transactional
canceledPaymentRepository.save<CanceledPaymentEntity?>(CanceledPaymentEntity(null, fun saveCanceledPayment(
paymentKey, cancelInfo.cancelReason, cancelInfo.cancelAmount, approvedAt, cancelInfo.canceledAt)) 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 { fun cancelPaymentByAdmin(reservationId: Long): PaymentCancel.Request {
val paymentKey = findPaymentByReservationId(reservationId) val paymentKey: String = paymentRepository.findPaymentKeyByReservationId(reservationId)
.orElseThrow<RoomescapeException?>(java.util.function.Supplier { ?: throw RoomescapeException(
RoomescapeException(roomescape.common.exception.ErrorType.PAYMENT_NOT_POUND, ErrorType.PAYMENT_NOT_POUND,
kotlin.String.format("[reservationId: %d]", reservationId), org.springframework.http.HttpStatus.NOT_FOUND) "[reservationId: $reservationId]",
})!! HttpStatus.NOT_FOUND
.paymentKey )
// 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다. // 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다.
val canceled = cancelPayment(paymentKey, "고객 요청", OffsetDateTime.now()) val canceled: CanceledPaymentEntity = cancelPayment(paymentKey)
return PaymentCancel.Request(paymentKey, canceled.cancelAmount, canceled.cancelReason) return PaymentCancel.Request(paymentKey, canceled.cancelAmount, canceled.cancelReason)
} }
private fun cancelPayment(paymentKey: String, cancelReason: String, canceledAt: OffsetDateTime): CanceledPaymentEntity { private fun cancelPayment(
val paymentEntity = paymentRepository.findByPaymentKey(paymentKey) paymentKey: String,
.orElseThrow<RoomescapeException?>(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) }) cancelReason: String = "고객 요청",
paymentRepository.delete(paymentEntity) 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>(CanceledPaymentEntity(null, paymentKey, cancelReason, paymentEntity.totalAmount, return CanceledPaymentEntity(
paymentEntity.approvedAt, canceledAt)) paymentKey = paymentKey,
cancelReason = cancelReason,
cancelAmount = paymentEntity.totalAmount,
approvedAt = paymentEntity.approvedAt,
canceledAt = canceledAt
).also {
canceledPaymentRepository.save(it)
}
} }
fun updateCanceledTime(paymentKey: String, canceledAt: OffsetDateTime) { @Transactional
val canceledPayment = canceledPaymentRepository.findByPaymentKey(paymentKey) fun updateCanceledTime(
.orElseThrow<RoomescapeException?>(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) }) paymentKey: String,
canceledPayment.canceledAt = canceledAt canceledAt: OffsetDateTime
} ) {
canceledPaymentRepository.findByPaymentKey(paymentKey)?.let {
private fun throwPaymentNotFoundByPaymentKey(paymentKey: String?): RoomescapeException { it.canceledAt = canceledAt
return RoomescapeException( } ?: throw RoomescapeException(
ErrorType.PAYMENT_NOT_POUND, String.format("[paymentKey: %s]", paymentKey), ErrorType.PAYMENT_NOT_POUND,
HttpStatus.NOT_FOUND) "[paymentKey: $paymentKey]",
HttpStatus.NOT_FOUND
)
} }
} }

View File

@ -47,7 +47,7 @@ public class ReservationWithPaymentService {
@Transactional(readOnly = true) @Transactional(readOnly = true)
public boolean isNotPaidReservation(Long reservationId) { public boolean isNotPaidReservation(Long reservationId) {
return paymentService.findPaymentByReservationId(reservationId).isEmpty(); return !paymentService.isReservationPaid(reservationId);
} }
public void updateCanceledTime(String paymentKey, OffsetDateTime canceledAt) { public void updateCanceledTime(String paymentKey, OffsetDateTime canceledAt) {