|
| 1 | +package com.loopers.application.payment; |
| 2 | + |
| 3 | +import com.loopers.domain.order.Order; |
| 4 | +import com.loopers.domain.order.repository.OrderRepository; |
| 5 | +import com.loopers.domain.product.repository.ProductRepository; |
| 6 | +import com.loopers.domain.payment.Payment; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.springframework.stereotype.Service; |
| 10 | +import org.springframework.transaction.annotation.Propagation; |
| 11 | +import org.springframework.transaction.annotation.Transactional; |
| 12 | + |
| 13 | +import java.time.LocalDateTime; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +@Slf4j |
| 17 | +@Service |
| 18 | +@RequiredArgsConstructor |
| 19 | +public class PaymentFacade { |
| 20 | + |
| 21 | + private final PaymentService paymentService; |
| 22 | + private final OrderRepository orderRepository; |
| 23 | + private final ProductRepository productRepository; |
| 24 | + |
| 25 | + @Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 26 | + public PaymentInfo requestPayment(String userId, PaymentCommand.RequestPayment command) { |
| 27 | + return paymentService.requestPayment(userId, command); |
| 28 | + } |
| 29 | + |
| 30 | + @Transactional |
| 31 | + public PaymentInfo processCallback(String userId, PaymentCommand.ProcessCallback command) { |
| 32 | + PaymentInfo paymentInfo = paymentService.processCallback(userId, command); |
| 33 | + |
| 34 | + if (paymentInfo.status() == com.loopers.domain.payment.PaymentStatus.SUCCESS) { |
| 35 | + Order order = orderRepository.findByOrderNo(paymentInfo.orderNo()) |
| 36 | + .orElseThrow(() -> new com.loopers.support.error.CoreException( |
| 37 | + com.loopers.support.error.ErrorType.NOT_FOUND, |
| 38 | + "주문을 찾을 수 없습니다: " + paymentInfo.orderNo())); |
| 39 | + order.markAsPaid(); |
| 40 | + orderRepository.save(order); |
| 41 | + log.info("[PaymentFacade] 결제 성공 및 주문 완료 - orderNo: {}", paymentInfo.orderNo()); |
| 42 | + } else if (paymentInfo.status() == com.loopers.domain.payment.PaymentStatus.FAILED) { |
| 43 | + log.warn("[PaymentFacade] 결제 실패 - orderNo: {}", paymentInfo.orderNo()); |
| 44 | + } |
| 45 | + |
| 46 | + return paymentInfo; |
| 47 | + } |
| 48 | + |
| 49 | + @Transactional |
| 50 | + public PaymentInfo syncPaymentStatus(String userId, String transactionKey) { |
| 51 | + PaymentInfo paymentInfo = paymentService.syncPaymentStatus(userId, transactionKey); |
| 52 | + |
| 53 | + if (paymentInfo.status() == com.loopers.domain.payment.PaymentStatus.SUCCESS) { |
| 54 | + Order order = orderRepository.findByOrderNo(paymentInfo.orderNo()) |
| 55 | + .orElseThrow(() -> new com.loopers.support.error.CoreException( |
| 56 | + com.loopers.support.error.ErrorType.NOT_FOUND, |
| 57 | + "주문을 찾을 수 없습니다: " + paymentInfo.orderNo())); |
| 58 | + if (!order.isPaid()) { |
| 59 | + order.markAsPaid(); |
| 60 | + orderRepository.save(order); |
| 61 | + } |
| 62 | + log.info("[PaymentFacade] 동기화 완료 (성공) 및 주문 완료 - orderNo: {}", paymentInfo.orderNo()); |
| 63 | + } else if (paymentInfo.status() == com.loopers.domain.payment.PaymentStatus.FAILED) { |
| 64 | + log.warn("[PaymentFacade] 동기화 완료 (실패) - orderNo: {}", paymentInfo.orderNo()); |
| 65 | + } |
| 66 | + |
| 67 | + return paymentInfo; |
| 68 | + } |
| 69 | + |
| 70 | + @Transactional |
| 71 | + public PaymentInfo cancelPayment(String userId, String orderNo, String reason) { |
| 72 | + Payment payment = paymentService.findByOrderNo(orderNo); |
| 73 | + |
| 74 | + if (payment.getStatus() == com.loopers.domain.payment.PaymentStatus.SUCCESS) { |
| 75 | + throw new com.loopers.support.error.CoreException( |
| 76 | + com.loopers.support.error.ErrorType.BAD_REQUEST, |
| 77 | + "이미 성공한 결제는 취소할 수 없습니다."); |
| 78 | + } |
| 79 | + |
| 80 | + paymentService.cancelPayment(payment.getId(), reason); |
| 81 | + |
| 82 | + Order order = payment.getOrder(); |
| 83 | + if (!order.isPaid()) { |
| 84 | + order.cancel(); |
| 85 | + orderRepository.save(order); |
| 86 | + |
| 87 | + order.getItems().forEach(item -> |
| 88 | + productRepository.increaseStock(item.getProductId(), item.getQuantity())); |
| 89 | + } |
| 90 | + |
| 91 | + log.info("[PaymentFacade] 결제 취소 완료 - orderNo: {}, reason: {}", orderNo, reason); |
| 92 | + return PaymentInfo.from(paymentService.findById(payment.getId())); |
| 93 | + } |
| 94 | + |
| 95 | + @Transactional(readOnly = true) |
| 96 | + public PaymentInfo getPaymentByOrderNo(String orderNo) { |
| 97 | + return paymentService.getPaymentByOrderNo(orderNo); |
| 98 | + } |
| 99 | + |
| 100 | + @Transactional(readOnly = true) |
| 101 | + public List<PaymentInfo> getPendingPaymentsOlderThan(LocalDateTime dateTime) { |
| 102 | + return paymentService.getPendingPaymentsOlderThan(dateTime); |
| 103 | + } |
| 104 | + |
| 105 | + @Transactional(readOnly = true) |
| 106 | + public List<PaymentInfo> getPaymentsRequiringRetry() { |
| 107 | + return paymentService.getPaymentsRequiringRetry(); |
| 108 | + } |
| 109 | +} |
0 commit comments