pricelees 49a808e06b [#20] 도메인별 예외 분리 (#21)
<!-- 제목 양식 -->
<!-- [이슈번호] 작업 요약 (예시: [#10] Gitea 템플릿 생성) -->

## 📝 관련 이슈 및 PR

**PR과 관련된 이슈 번호**
- #20

##  작업 내용
<!-- 어떤 작업을 했는지 알려주세요! -->
- 기존에 공통으로 사용하던 커스텀 예외를 도메인별로 분리
- 새로 정의된 예외는 ErrorCode를 지정할 때 HttpStatusCode를 지정하도록 하여 잘못된 지정에서 오는 휴먼 에러 방지
- 커스텀 예외를 적용하는 과정에서 가독성이 낮다고 느껴지는 일부 함수형 코드는 선언형으로 수정

## 🧪 테스트
<!-- 어떤 테스트를 생각했고 진행했는지 알려주세요! -->
- 테스트 중 일부 미비된 테스트 보완 후 전체 로직 테스트 완료

## 📚 참고 자료 및 기타
<!-- 참고한 자료, 또는 논의할 사항이 있다면 알려주세요! -->
커스텀 예외도 작업이 길어져 로깅은 이후에 진행할 예정

Reviewed-on: #21
Co-authored-by: pricelees <priceelees@gmail.com>
Co-committed-by: pricelees <priceelees@gmail.com>
2025-07-24 02:48:52 +00:00

101 lines
4.0 KiB
Kotlin

package roomescape.payment.business
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import roomescape.payment.exception.PaymentErrorCode
import roomescape.payment.exception.PaymentException
import roomescape.payment.infrastructure.client.PaymentApproveResponse
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.payment.web.PaymentCancelRequest
import roomescape.payment.web.PaymentCancelResponse
import roomescape.payment.web.PaymentCreateResponse
import roomescape.payment.web.toCreateResponse
import roomescape.reservation.infrastructure.persistence.ReservationEntity
import java.time.OffsetDateTime
@Service
class PaymentService(
private val paymentRepository: PaymentRepository,
private val canceledPaymentRepository: CanceledPaymentRepository
) {
@Transactional
fun createPayment(
approveResponse: PaymentApproveResponse,
reservation: ReservationEntity
): PaymentCreateResponse {
val payment = PaymentEntity(
orderId = approveResponse.orderId,
paymentKey = approveResponse.paymentKey,
totalAmount = approveResponse.totalAmount,
reservation = reservation,
approvedAt = approveResponse.approvedAt
)
return paymentRepository.save(payment).toCreateResponse()
}
@Transactional(readOnly = true)
fun isReservationPaid(reservationId: Long): Boolean = paymentRepository.existsByReservationId(reservationId)
@Transactional
fun createCanceledPayment(
cancelInfo: PaymentCancelResponse,
approvedAt: OffsetDateTime,
paymentKey: String
): CanceledPaymentEntity {
val canceledPayment = CanceledPaymentEntity(
paymentKey = paymentKey,
cancelReason = cancelInfo.cancelReason,
cancelAmount = cancelInfo.cancelAmount,
approvedAt = approvedAt,
canceledAt = cancelInfo.canceledAt
)
return canceledPaymentRepository.save(canceledPayment)
}
@Transactional
fun createCanceledPaymentByReservationId(reservationId: Long): PaymentCancelRequest {
val paymentKey: String = paymentRepository.findPaymentKeyByReservationId(reservationId)
?: throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND)
// 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다.
val canceled: CanceledPaymentEntity = cancelPayment(paymentKey)
return PaymentCancelRequest(paymentKey, canceled.cancelAmount, canceled.cancelReason)
}
private fun cancelPayment(
paymentKey: String,
cancelReason: String = "고객 요청",
canceledAt: OffsetDateTime = OffsetDateTime.now()
): CanceledPaymentEntity {
val payment: PaymentEntity = paymentRepository.findByPaymentKey(paymentKey)
?.also { paymentRepository.delete(it) }
?: throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND)
val canceledPayment = CanceledPaymentEntity(
paymentKey = paymentKey,
cancelReason = cancelReason,
cancelAmount = payment.totalAmount,
approvedAt = payment.approvedAt,
canceledAt = canceledAt
)
return canceledPaymentRepository.save(canceledPayment)
}
@Transactional
fun updateCanceledTime(
paymentKey: String,
canceledAt: OffsetDateTime
) {
canceledPaymentRepository.findByPaymentKey(paymentKey)
?.apply { this.canceledAt = canceledAt }
?: throw PaymentException(PaymentErrorCode.PAYMENT_NOT_FOUND)
}
}