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 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
class PaymentService(
private val paymentRepository: PaymentRepository,
private val canceledPaymentRepository: CanceledPaymentRepository
) {
@Transactional
public class PaymentService {
private final PaymentRepository paymentRepository;
private final CanceledPaymentRepository canceledPaymentRepository;
public PaymentService(PaymentRepository paymentRepository, CanceledPaymentRepository canceledPaymentRepository) {
this.paymentRepository = paymentRepository;
this.canceledPaymentRepository = canceledPaymentRepository;
}
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 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()
@Transactional(readOnly = true)
public Optional<PaymentEntity> findPaymentByReservationId(Long reservationId) {
return paymentRepository.findByReservationId(reservationId);
fun findPaymentByReservationId(reservationId: Long): Optional<PaymentEntity> {
return paymentRepository.findByReservationId(reservationId)
}
public void saveCanceledPayment(PaymentCancel.Response cancelInfo, OffsetDateTime approvedAt, String paymentKey) {
canceledPaymentRepository.save(new CanceledPaymentEntity(null,
paymentKey, cancelInfo.cancelReason, cancelInfo.cancelAmount, approvedAt, cancelInfo.canceledAt));
fun saveCanceledPayment(cancelInfo: PaymentCancel.Response, approvedAt: OffsetDateTime, paymentKey: String) {
canceledPaymentRepository.save<CanceledPaymentEntity?>(CanceledPaymentEntity(null,
paymentKey, cancelInfo.cancelReason, cancelInfo.cancelAmount, approvedAt, cancelInfo.canceledAt))
}
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();
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
// 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다.
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) {
PaymentEntity paymentEntity = paymentRepository.findByPaymentKey(paymentKey)
.orElseThrow(() -> throwPaymentNotFoundByPaymentKey(paymentKey));
paymentRepository.delete(paymentEntity);
private fun cancelPayment(paymentKey: String, cancelReason: String, canceledAt: OffsetDateTime): CanceledPaymentEntity {
val paymentEntity = paymentRepository.findByPaymentKey(paymentKey)
.orElseThrow<RoomescapeException?>(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) })
paymentRepository.delete(paymentEntity)
return canceledPaymentRepository.save(new CanceledPaymentEntity(null, paymentKey, cancelReason, paymentEntity.getTotalAmount(),
paymentEntity.getApprovedAt(), canceledAt));
return canceledPaymentRepository.save<CanceledPaymentEntity>(CanceledPaymentEntity(null, paymentKey, cancelReason, paymentEntity.totalAmount,
paymentEntity.approvedAt, canceledAt))
}
public void updateCanceledTime(String paymentKey, OffsetDateTime canceledAt) {
CanceledPaymentEntity canceledPayment = canceledPaymentRepository.findByPaymentKey(paymentKey)
.orElseThrow(() -> throwPaymentNotFoundByPaymentKey(paymentKey));
canceledPayment.setCanceledAt(canceledAt);
fun updateCanceledTime(paymentKey: String, canceledAt: OffsetDateTime) {
val canceledPayment = canceledPaymentRepository.findByPaymentKey(paymentKey)
.orElseThrow<RoomescapeException?>(Supplier { throwPaymentNotFoundByPaymentKey(paymentKey) })
canceledPayment.canceledAt = canceledAt
}
private RoomescapeException throwPaymentNotFoundByPaymentKey(String paymentKey) {
return new RoomescapeException(
private fun throwPaymentNotFoundByPaymentKey(paymentKey: String?): RoomescapeException {
return RoomescapeException(
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 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
)