pricelees 9f8ee8cc02 [#18] 코드 정리 및 일부 컨벤션 통일 (#19)
<!-- 제목 양식 -->
<!-- [이슈번호] 작업 요약 (예시: [#10] Gitea 템플릿 생성) -->

## 📝 관련 이슈 및 PR

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

##  작업 내용
<!-- 어떤 작업을 했는지 알려주세요! -->
- 기존 자바와의 호환성을 위해 사용하던 \@Jvm.. 어노테이션 및 팩토리 메서드 제거
- 기존에 get, find, save 등으로 산재되어 있던 메서드 컨벤션 통일
- 일부 API endpoint 수정
- 테이블 이름 단수 -> 복수 수정

추가적으로 개선이 필요한 점은 있지만, 이는 기능 개선 과정에서 수정할 예정

## 🧪 테스트
<!-- 어떤 테스트를 생각했고 진행했는지 알려주세요! -->
각 작업 마다 전체 테스트 수행 및 정상 동작 확인

## 📚 참고 자료 및 기타
<!-- 참고한 자료, 또는 논의할 사항이 있다면 알려주세요! -->

Reviewed-on: #19
Co-authored-by: pricelees <priceelees@gmail.com>
Co-committed-by: pricelees <priceelees@gmail.com>
2025-07-22 09:05:31 +00:00

109 lines
4.3 KiB
Kotlin

package roomescape.payment.business
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import roomescape.common.exception.ErrorType
import roomescape.common.exception.RoomescapeException
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(
paymentResponse: PaymentApproveResponse,
reservation: ReservationEntity
): PaymentCreateResponse = PaymentEntity(
orderId = paymentResponse.orderId,
paymentKey = paymentResponse.paymentKey,
totalAmount = paymentResponse.totalAmount,
reservation = reservation,
approvedAt = paymentResponse.approvedAt
).also {
paymentRepository.save(it)
}.toCreateResponse()
@Transactional(readOnly = true)
fun isReservationPaid(
reservationId: Long
): Boolean = paymentRepository.existsByReservationId(reservationId)
@Transactional
fun createCanceledPayment(
cancelInfo: PaymentCancelResponse,
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 createCanceledPaymentByReservationId(reservationId: Long): PaymentCancelRequest {
val paymentKey: String = paymentRepository.findPaymentKeyByReservationId(reservationId)
?: throw RoomescapeException(
ErrorType.PAYMENT_NOT_FOUND,
"[reservationId: $reservationId]",
HttpStatus.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 paymentEntity: PaymentEntity = paymentRepository.findByPaymentKey(paymentKey)
?.also { paymentRepository.delete(it) }
?: throw RoomescapeException(
ErrorType.PAYMENT_NOT_FOUND,
"[paymentKey: $paymentKey]",
HttpStatus.NOT_FOUND
)
return CanceledPaymentEntity(
paymentKey = paymentKey,
cancelReason = cancelReason,
cancelAmount = paymentEntity.totalAmount,
approvedAt = paymentEntity.approvedAt,
canceledAt = canceledAt
).also {
canceledPaymentRepository.save(it)
}
}
@Transactional
fun updateCanceledTime(
paymentKey: String,
canceledAt: OffsetDateTime
) {
canceledPaymentRepository.findByPaymentKey(paymentKey)?.let {
it.canceledAt = canceledAt
} ?: throw RoomescapeException(
ErrorType.PAYMENT_NOT_FOUND,
"[paymentKey: $paymentKey]",
HttpStatus.NOT_FOUND
)
}
}