From 3ca58b2daef205316fc758899f5201de1083f324 Mon Sep 17 00:00:00 2001 From: pricelees Date: Wed, 16 Jul 2025 14:14:05 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20PaymentService=20=EC=BD=94=ED=8B=80?= =?UTF-8?q?=EB=A6=B0=20=EC=A0=84=ED=99=98=20=EB=B0=8F=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../payment/business/PaymentService.kt | 140 +++++++++--------- .../java/roomescape/payment/web/PaymentDTO.kt | 25 ++-- 2 files changed, 81 insertions(+), 84 deletions(-) diff --git a/src/main/java/roomescape/payment/business/PaymentService.kt b/src/main/java/roomescape/payment/business/PaymentService.kt index 27dde99f..fdf283ae 100644 --- a/src/main/java/roomescape/payment/business/PaymentService.kt +++ b/src/main/java/roomescape/payment/business/PaymentService.kt @@ -1,82 +1,84 @@ -package roomescape.payment.business; +package roomescape.payment.business -import java.time.OffsetDateTime; -import java.util.Optional; - -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.persistence.CanceledPaymentEntity; -import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository; -import roomescape.payment.infrastructure.persistence.PaymentEntity; -import roomescape.payment.infrastructure.persistence.PaymentRepository; -import roomescape.payment.web.PaymentApprove; -import roomescape.payment.web.PaymentCancel; -import roomescape.payment.web.ReservationPaymentResponse; -import roomescape.reservation.domain.Reservation; +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.persistence.CanceledPaymentEntity +import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository +import roomescape.payment.infrastructure.persistence.PaymentEntity +import roomescape.payment.infrastructure.persistence.PaymentRepository +import roomescape.payment.web.PaymentApprove +import roomescape.payment.web.PaymentCancel +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 -@Transactional -public class PaymentService { +class PaymentService( + private val paymentRepository: PaymentRepository, + private val canceledPaymentRepository: CanceledPaymentRepository +) { - private final PaymentRepository paymentRepository; - private final CanceledPaymentRepository canceledPaymentRepository; + @Transactional + fun savePayment( + paymentResponse: PaymentApprove.Response, + reservation: Reservation + ): ReservationPaymentResponse = PaymentEntity( + orderId = paymentResponse.orderId, + paymentKey = paymentResponse.paymentKey, + totalAmount = paymentResponse.totalAmount, + reservation = reservation, + approvedAt = paymentResponse.approvedAt + ).also { + paymentRepository.save(it) + }.toReservationPaymentResponse() - public PaymentService(PaymentRepository paymentRepository, CanceledPaymentRepository canceledPaymentRepository) { - this.paymentRepository = paymentRepository; - this.canceledPaymentRepository = canceledPaymentRepository; - } + @Transactional(readOnly = true) + fun findPaymentByReservationId(reservationId: Long): Optional { + return paymentRepository.findByReservationId(reservationId) + } - public ReservationPaymentResponse savePayment(PaymentApprove.Response paymentResponse, - Reservation reservation) { - PaymentEntity paymentEntity = new PaymentEntity(null, paymentResponse.orderId, paymentResponse.paymentKey, - paymentResponse.totalAmount, reservation, paymentResponse.approvedAt); - PaymentEntity saved = paymentRepository.save(paymentEntity); - return ReservationPaymentResponse.from(saved); - } + fun saveCanceledPayment(cancelInfo: PaymentCancel.Response, approvedAt: OffsetDateTime, paymentKey: String) { + canceledPaymentRepository.save(CanceledPaymentEntity(null, + paymentKey, cancelInfo.cancelReason, cancelInfo.cancelAmount, approvedAt, cancelInfo.canceledAt)) + } - @Transactional(readOnly = true) - public Optional findPaymentByReservationId(Long reservationId) { - return paymentRepository.findByReservationId(reservationId); - } + 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 canceled = cancelPayment(paymentKey, "고객 요청", OffsetDateTime.now()) - public void saveCanceledPayment(PaymentCancel.Response cancelInfo, OffsetDateTime approvedAt, String paymentKey) { - canceledPaymentRepository.save(new CanceledPaymentEntity(null, - paymentKey, cancelInfo.cancelReason, cancelInfo.cancelAmount, approvedAt, cancelInfo.canceledAt)); - } + return PaymentCancel.Request(paymentKey, canceled.cancelAmount, canceled.cancelReason) + } - public PaymentCancel.Request cancelPaymentByAdmin(Long reservationId) { - String paymentKey = findPaymentByReservationId(reservationId) - .orElseThrow(() -> new RoomescapeException(ErrorType.PAYMENT_NOT_POUND, - String.format("[reservationId: %d]", reservationId), HttpStatus.NOT_FOUND)) - .getPaymentKey(); - // 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다. - CanceledPaymentEntity canceled = cancelPayment(paymentKey, "고객 요청", OffsetDateTime.now()); + private fun cancelPayment(paymentKey: String, cancelReason: String, canceledAt: OffsetDateTime): CanceledPaymentEntity { + val paymentEntity = paymentRepository.findByPaymentKey(paymentKey) + .orElseThrow(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) }) + paymentRepository.delete(paymentEntity) - return new PaymentCancel.Request(paymentKey, canceled.getCancelAmount(), canceled.getCancelReason()); - } + return canceledPaymentRepository.save(CanceledPaymentEntity(null, paymentKey, cancelReason, paymentEntity.totalAmount, + paymentEntity.approvedAt, canceledAt)) + } - private CanceledPaymentEntity cancelPayment(String paymentKey, String cancelReason, OffsetDateTime canceledAt) { - PaymentEntity paymentEntity = paymentRepository.findByPaymentKey(paymentKey) - .orElseThrow(() -> throwPaymentNotFoundByPaymentKey(paymentKey)); - paymentRepository.delete(paymentEntity); + fun updateCanceledTime(paymentKey: String, canceledAt: OffsetDateTime) { + val canceledPayment = canceledPaymentRepository.findByPaymentKey(paymentKey) + .orElseThrow(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) }) + canceledPayment.canceledAt = canceledAt + } - return canceledPaymentRepository.save(new CanceledPaymentEntity(null, paymentKey, cancelReason, paymentEntity.getTotalAmount(), - paymentEntity.getApprovedAt(), canceledAt)); - } - - public void updateCanceledTime(String paymentKey, OffsetDateTime canceledAt) { - CanceledPaymentEntity canceledPayment = canceledPaymentRepository.findByPaymentKey(paymentKey) - .orElseThrow(() -> throwPaymentNotFoundByPaymentKey(paymentKey)); - canceledPayment.setCanceledAt(canceledAt); - } - - private RoomescapeException throwPaymentNotFoundByPaymentKey(String paymentKey) { - return new RoomescapeException( - ErrorType.PAYMENT_NOT_POUND, String.format("[paymentKey: %s]", paymentKey), - HttpStatus.NOT_FOUND); - } + private fun throwPaymentNotFoundByPaymentKey(paymentKey: String?): RoomescapeException { + return RoomescapeException( + ErrorType.PAYMENT_NOT_POUND, String.format("[paymentKey: %s]", paymentKey), + HttpStatus.NOT_FOUND) + } } diff --git a/src/main/java/roomescape/payment/web/PaymentDTO.kt b/src/main/java/roomescape/payment/web/PaymentDTO.kt index 2d97f96a..8b4b12e2 100644 --- a/src/main/java/roomescape/payment/web/PaymentDTO.kt +++ b/src/main/java/roomescape/payment/web/PaymentDTO.kt @@ -53,18 +53,13 @@ data class ReservationPaymentResponse( val totalAmount: Long, val reservation: ReservationResponse, val approvedAt: OffsetDateTime -) { - companion object { - @JvmStatic - fun from(saved: PaymentEntity): ReservationPaymentResponse { - return ReservationPaymentResponse( - saved.id!!, - saved.orderId, - saved.paymentKey, - saved.totalAmount, - ReservationResponse.from(saved.reservation), - saved.approvedAt - ) - } - } -} +) + +fun PaymentEntity.toReservationPaymentResponse(): ReservationPaymentResponse = ReservationPaymentResponse( + id = this.id!!, + orderId = this.orderId, + paymentKey = this.paymentKey, + totalAmount = this.totalAmount, + reservation = ReservationResponse.from(this.reservation), + approvedAt = this.approvedAt +) \ No newline at end of file