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; @Service @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(paymentResponse.orderId, paymentResponse.paymentKey, paymentResponse.totalAmount, reservation, paymentResponse.approvedAt); PaymentEntity saved = paymentRepository.save(paymentEntity); return ReservationPaymentResponse.from(saved); } @Transactional(readOnly = true) public Optional findPaymentByReservationId(Long reservationId) { return paymentRepository.findByReservationId(reservationId); } public void saveCanceledPayment(PaymentCancel.Response cancelInfo, OffsetDateTime approvedAt, String paymentKey) { canceledPaymentRepository.save(new CanceledPaymentEntity( 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(); // 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다. CanceledPaymentEntity canceled = cancelPayment(paymentKey, "고객 요청", OffsetDateTime.now()); return new PaymentCancel.Request(paymentKey, canceled.getCancelAmount(), canceled.getCancelReason()); } private CanceledPaymentEntity cancelPayment(String paymentKey, String cancelReason, OffsetDateTime canceledAt) { PaymentEntity paymentEntity = paymentRepository.findByPaymentKey(paymentKey) .orElseThrow(() -> throwPaymentNotFoundByPaymentKey(paymentKey)); paymentRepository.delete(paymentEntity); return canceledPaymentRepository.save(new CanceledPaymentEntity(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); } }