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.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<PaymentEntity> {
return paymentRepository.findByReservationId(reservationId)
}
fun isReservationPaid(
reservationId: Long
): Boolean = paymentRepository.existsByReservationId(reservationId)
fun saveCanceledPayment(cancelInfo: PaymentCancel.Response, approvedAt: OffsetDateTime, paymentKey: String) {
canceledPaymentRepository.save<CanceledPaymentEntity?>(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<RoomescapeException?>(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<RoomescapeException?>(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>(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<RoomescapeException?>(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
)
}
}

View File

@ -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) {