pricelees 38ef207c47 [#5]: 공통 기능 코틀린 마이그레이션 및 패키지 분리 (#6)
<!-- 제목 양식 -->
<!-- [이슈번호] 작업 요약 (예시: [#10] Gitea 템플릿 생성) -->

## 📝 관련 이슈 및 PR

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

##  작업 내용
<!-- 어떤 작업을 했는지 알려주세요! -->
1. 추후 모듈 분리를 위해 패키지를 조금 더 직관적으로 분리하고자 했음. 기존 system 패키지 내부에 있는 auth는 ../auth로, dto 및 exception은 common/.. 하위로 이동함.

2. 이동한 클래스들은 모두 코틀린으로 전환하였고, 일부 기능까지 수정하려 했으나 추후에 한 번에 수정하는게 낫다고 판단함.

3. 다음 PR에서는 현재까지 코틀린으로 변환된 클래스를 대상으로 808c6675 에 있는 새로운 응답 객체를 적용할 예정

## 🧪 테스트
<!-- 어떤 테스트를 생각했고 진행했는지 알려주세요! -->
기존의 API 테스트가 SpringbootTest를 사용하여 속도가 상당히 느림. 808c6675 의 테스트를 진행하며 작성해둔 MockMvcTest의 틀을 바탕으로, 코틀린으로 전환된 클래스를 대상으로 MockMvc로 전환할 예정.

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

Reviewed-on: #6
Co-authored-by: pricelees <priceelees@gmail.com>
Co-committed-by: pricelees <priceelees@gmail.com>
2025-07-14 05:05:47 +00:00

83 lines
3.6 KiB
Java

package roomescape.payment.service;
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.payment.domain.CanceledPayment;
import roomescape.payment.domain.Payment;
import roomescape.payment.domain.repository.CanceledPaymentRepository;
import roomescape.payment.domain.repository.PaymentRepository;
import roomescape.payment.dto.request.PaymentCancelRequest;
import roomescape.payment.dto.response.PaymentCancelResponse;
import roomescape.payment.dto.response.PaymentResponse;
import roomescape.payment.dto.response.ReservationPaymentResponse;
import roomescape.reservation.domain.Reservation;
import roomescape.common.exception.ErrorType;
import roomescape.common.exception.RoomescapeException;
@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(PaymentResponse paymentResponse, Reservation reservation) {
Payment payment = new Payment(paymentResponse.orderId(), paymentResponse.paymentKey(),
paymentResponse.totalAmount(), reservation, paymentResponse.approvedAt());
Payment saved = paymentRepository.save(payment);
return ReservationPaymentResponse.from(saved);
}
@Transactional(readOnly = true)
public Optional<Payment> findPaymentByReservationId(Long reservationId) {
return paymentRepository.findByReservationId(reservationId);
}
public void saveCanceledPayment(PaymentCancelResponse cancelInfo, OffsetDateTime approvedAt, String paymentKey) {
canceledPaymentRepository.save(new CanceledPayment(
paymentKey, cancelInfo.cancelReason(), cancelInfo.cancelAmount(), approvedAt, cancelInfo.canceledAt()));
}
public PaymentCancelRequest cancelPaymentByAdmin(Long reservationId) {
String paymentKey = findPaymentByReservationId(reservationId)
.orElseThrow(() -> new RoomescapeException(ErrorType.PAYMENT_NOT_POUND,
String.format("[reservationId: %d]", reservationId), HttpStatus.NOT_FOUND))
.getPaymentKey();
// 취소 시간은 현재 시간으로 일단 생성한 뒤, 결제 취소 완료 후 해당 시간으로 변경합니다.
CanceledPayment canceled = cancelPayment(paymentKey, "고객 요청", OffsetDateTime.now());
return new PaymentCancelRequest(paymentKey, canceled.getCancelAmount(), canceled.getCancelReason());
}
private CanceledPayment cancelPayment(String paymentKey, String cancelReason, OffsetDateTime canceledAt) {
Payment payment = paymentRepository.findByPaymentKey(paymentKey)
.orElseThrow(() -> throwPaymentNotFoundByPaymentKey(paymentKey));
paymentRepository.delete(payment);
return canceledPaymentRepository.save(new CanceledPayment(paymentKey, cancelReason, payment.getTotalAmount(),
payment.getApprovedAt(), canceledAt));
}
public void updateCanceledTime(String paymentKey, OffsetDateTime canceledAt) {
CanceledPayment 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);
}
}