refactor: PaymentService 코틀린 전환 및 저장 로직 수정

This commit is contained in:
이상진 2025-07-16 14:14:05 +09:00
parent 55d4460f31
commit 3ca58b2dae
2 changed files with 81 additions and 84 deletions

View File

@ -1,82 +1,84 @@
package roomescape.payment.business; package roomescape.payment.business
import java.time.OffsetDateTime; import org.springframework.http.HttpStatus
import java.util.Optional; import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.http.HttpStatus; import roomescape.common.exception.ErrorType
import org.springframework.stereotype.Service; import roomescape.common.exception.RoomescapeException
import org.springframework.transaction.annotation.Transactional; import roomescape.payment.infrastructure.persistence.CanceledPaymentEntity
import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository
import roomescape.common.exception.ErrorType; import roomescape.payment.infrastructure.persistence.PaymentEntity
import roomescape.common.exception.RoomescapeException; import roomescape.payment.infrastructure.persistence.PaymentRepository
import roomescape.payment.infrastructure.persistence.CanceledPaymentEntity; import roomescape.payment.web.PaymentApprove
import roomescape.payment.infrastructure.persistence.CanceledPaymentRepository; import roomescape.payment.web.PaymentCancel
import roomescape.payment.infrastructure.persistence.PaymentEntity; import roomescape.payment.web.ReservationPaymentResponse
import roomescape.payment.infrastructure.persistence.PaymentRepository; import roomescape.payment.web.toReservationPaymentResponse
import roomescape.payment.web.PaymentApprove; import roomescape.reservation.domain.Reservation
import roomescape.payment.web.PaymentCancel; import java.time.OffsetDateTime
import roomescape.payment.web.ReservationPaymentResponse; import java.util.*
import roomescape.reservation.domain.Reservation; import java.util.function.Supplier
@Service @Service
@Transactional class PaymentService(
public class PaymentService { private val paymentRepository: PaymentRepository,
private val canceledPaymentRepository: CanceledPaymentRepository
) {
private final PaymentRepository paymentRepository; @Transactional
private final CanceledPaymentRepository canceledPaymentRepository; fun savePayment(
paymentResponse: PaymentApprove.Response,
public PaymentService(PaymentRepository paymentRepository, CanceledPaymentRepository canceledPaymentRepository) { reservation: Reservation
this.paymentRepository = paymentRepository; ): ReservationPaymentResponse = PaymentEntity(
this.canceledPaymentRepository = canceledPaymentRepository; orderId = paymentResponse.orderId,
} paymentKey = paymentResponse.paymentKey,
totalAmount = paymentResponse.totalAmount,
public ReservationPaymentResponse savePayment(PaymentApprove.Response paymentResponse, reservation = reservation,
Reservation reservation) { approvedAt = paymentResponse.approvedAt
PaymentEntity paymentEntity = new PaymentEntity(null, paymentResponse.orderId, paymentResponse.paymentKey, ).also {
paymentResponse.totalAmount, reservation, paymentResponse.approvedAt); paymentRepository.save(it)
PaymentEntity saved = paymentRepository.save(paymentEntity); }.toReservationPaymentResponse()
return ReservationPaymentResponse.from(saved);
}
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Optional<PaymentEntity> findPaymentByReservationId(Long reservationId) { fun findPaymentByReservationId(reservationId: Long): Optional<PaymentEntity> {
return paymentRepository.findByReservationId(reservationId); return paymentRepository.findByReservationId(reservationId)
} }
public void saveCanceledPayment(PaymentCancel.Response cancelInfo, OffsetDateTime approvedAt, String paymentKey) { fun saveCanceledPayment(cancelInfo: PaymentCancel.Response, approvedAt: OffsetDateTime, paymentKey: String) {
canceledPaymentRepository.save(new CanceledPaymentEntity(null, canceledPaymentRepository.save<CanceledPaymentEntity?>(CanceledPaymentEntity(null,
paymentKey, cancelInfo.cancelReason, cancelInfo.cancelAmount, approvedAt, cancelInfo.canceledAt)); paymentKey, cancelInfo.cancelReason, cancelInfo.cancelAmount, approvedAt, cancelInfo.canceledAt))
} }
public PaymentCancel.Request cancelPaymentByAdmin(Long reservationId) { fun cancelPaymentByAdmin(reservationId: Long): PaymentCancel.Request {
String paymentKey = findPaymentByReservationId(reservationId) val paymentKey = findPaymentByReservationId(reservationId)
.orElseThrow(() -> new RoomescapeException(ErrorType.PAYMENT_NOT_POUND, .orElseThrow<RoomescapeException?>(java.util.function.Supplier {
String.format("[reservationId: %d]", reservationId), HttpStatus.NOT_FOUND)) RoomescapeException(roomescape.common.exception.ErrorType.PAYMENT_NOT_POUND,
.getPaymentKey(); kotlin.String.format("[reservationId: %d]", reservationId), org.springframework.http.HttpStatus.NOT_FOUND)
})!!
.paymentKey
// 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다. // 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다.
CanceledPaymentEntity canceled = cancelPayment(paymentKey, "고객 요청", OffsetDateTime.now()); val canceled = cancelPayment(paymentKey, "고객 요청", OffsetDateTime.now())
return new PaymentCancel.Request(paymentKey, canceled.getCancelAmount(), canceled.getCancelReason()); return PaymentCancel.Request(paymentKey, canceled.cancelAmount, canceled.cancelReason)
} }
private CanceledPaymentEntity cancelPayment(String paymentKey, String cancelReason, OffsetDateTime canceledAt) { private fun cancelPayment(paymentKey: String, cancelReason: String, canceledAt: OffsetDateTime): CanceledPaymentEntity {
PaymentEntity paymentEntity = paymentRepository.findByPaymentKey(paymentKey) val paymentEntity = paymentRepository.findByPaymentKey(paymentKey)
.orElseThrow(() -> throwPaymentNotFoundByPaymentKey(paymentKey)); .orElseThrow<RoomescapeException?>(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) })
paymentRepository.delete(paymentEntity); paymentRepository.delete(paymentEntity)
return canceledPaymentRepository.save(new CanceledPaymentEntity(null, paymentKey, cancelReason, paymentEntity.getTotalAmount(), return canceledPaymentRepository.save<CanceledPaymentEntity>(CanceledPaymentEntity(null, paymentKey, cancelReason, paymentEntity.totalAmount,
paymentEntity.getApprovedAt(), canceledAt)); paymentEntity.approvedAt, canceledAt))
} }
public void updateCanceledTime(String paymentKey, OffsetDateTime canceledAt) { fun updateCanceledTime(paymentKey: String, canceledAt: OffsetDateTime) {
CanceledPaymentEntity canceledPayment = canceledPaymentRepository.findByPaymentKey(paymentKey) val canceledPayment = canceledPaymentRepository.findByPaymentKey(paymentKey)
.orElseThrow(() -> throwPaymentNotFoundByPaymentKey(paymentKey)); .orElseThrow<RoomescapeException?>(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) })
canceledPayment.setCanceledAt(canceledAt); canceledPayment.canceledAt = canceledAt
} }
private RoomescapeException throwPaymentNotFoundByPaymentKey(String paymentKey) { private fun throwPaymentNotFoundByPaymentKey(paymentKey: String?): RoomescapeException {
return new RoomescapeException( return RoomescapeException(
ErrorType.PAYMENT_NOT_POUND, String.format("[paymentKey: %s]", paymentKey), ErrorType.PAYMENT_NOT_POUND, String.format("[paymentKey: %s]", paymentKey),
HttpStatus.NOT_FOUND); HttpStatus.NOT_FOUND)
} }
} }

View File

@ -53,18 +53,13 @@ data class ReservationPaymentResponse(
val totalAmount: Long, val totalAmount: Long,
val reservation: ReservationResponse, val reservation: ReservationResponse,
val approvedAt: OffsetDateTime val approvedAt: OffsetDateTime
) { )
companion object {
@JvmStatic fun PaymentEntity.toReservationPaymentResponse(): ReservationPaymentResponse = ReservationPaymentResponse(
fun from(saved: PaymentEntity): ReservationPaymentResponse { id = this.id!!,
return ReservationPaymentResponse( orderId = this.orderId,
saved.id!!, paymentKey = this.paymentKey,
saved.orderId, totalAmount = this.totalAmount,
saved.paymentKey, reservation = ReservationResponse.from(this.reservation),
saved.totalAmount, approvedAt = this.approvedAt
ReservationResponse.from(saved.reservation), )
saved.approvedAt
)
}
}
}