diff --git a/src/main/java/com/example/silverbridgeX_user/payment/converter/PaymentConverter.java b/src/main/java/com/example/silverbridgeX_user/payment/converter/PaymentConverter.java index 50afd23..8a7e962 100644 --- a/src/main/java/com/example/silverbridgeX_user/payment/converter/PaymentConverter.java +++ b/src/main/java/com/example/silverbridgeX_user/payment/converter/PaymentConverter.java @@ -23,14 +23,24 @@ public static Payment toKakaoPay(String tid, String sid, User user) { } - public static PaymentDto.KakaoPayStatus toKakaoPayStatus(boolean isLogExist, PaymentDto.KakaoSubscribeStatusResponse kakaoSubscribeStatusResponse) { - String status = kakaoSubscribeStatusResponse.getStatus(); + public static PaymentDto.KakaoPayStatus toKakaoPayStatus(boolean isLogExist, + PaymentDto.KakaoSubscribeStatusResponse kakaoSubscribeStatusResponse) { + String status; + if (!isLogExist) { + // 결제 시도 이력이 없으면 무조건 INACTIVE + status = "INACTIVE"; + } else { + // 결제 시도 이력이 있으면 실제 상태 조회 + status = kakaoSubscribeStatusResponse.getStatus(); + } String last_approved_at; - if(kakaoSubscribeStatusResponse.getLast_approved_at() == null || kakaoSubscribeStatusResponse.getLast_approved_at().isEmpty()) { + if (kakaoSubscribeStatusResponse.getLast_approved_at() == null + || kakaoSubscribeStatusResponse.getLast_approved_at().isEmpty()) { last_approved_at = kakaoSubscribeStatusResponse.getCreated_at(); + } else { + last_approved_at = kakaoSubscribeStatusResponse.getLast_approved_at(); } - else last_approved_at = kakaoSubscribeStatusResponse.getLast_approved_at(); return PaymentDto.KakaoPayStatus.builder() .isLogExist(isLogExist) diff --git a/src/main/java/com/example/silverbridgeX_user/payment/repository/PaymentRepository.java b/src/main/java/com/example/silverbridgeX_user/payment/repository/PaymentRepository.java index 6d60a54..e636ad3 100644 --- a/src/main/java/com/example/silverbridgeX_user/payment/repository/PaymentRepository.java +++ b/src/main/java/com/example/silverbridgeX_user/payment/repository/PaymentRepository.java @@ -16,5 +16,5 @@ public interface PaymentRepository extends JpaRepository { List findAllWithMemberAndSidNotNull(); @Query("SELECT p FROM Payment p WHERE p.user.id = :userId ORDER BY p.createdAt DESC") - Optional getLatestKakaoPayInfo(@Param("userId") Long userId); + Optional findTopByUserIdOrderByIdDesc(@Param("userId") Long userId); } diff --git a/src/main/java/com/example/silverbridgeX_user/payment/service/PaymentService.java b/src/main/java/com/example/silverbridgeX_user/payment/service/PaymentService.java index bc4e4d2..dc31e82 100644 --- a/src/main/java/com/example/silverbridgeX_user/payment/service/PaymentService.java +++ b/src/main/java/com/example/silverbridgeX_user/payment/service/PaymentService.java @@ -16,11 +16,13 @@ import java.util.Map; import java.util.Optional; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +@Slf4j @Service @RequiredArgsConstructor public class PaymentService { @@ -74,15 +76,15 @@ public PaymentDto.KakaoReadyResponse kakaoPayReady(Long userId) { @Transactional public PaymentDto.KakaoApproveResponse approveResponse(String pgToken, Long userId) { - Payment payment = paymentRepository.getLatestKakaoPayInfo(userId) + Payment payment = paymentRepository.findTopByUserIdOrderByIdDesc(userId) .orElseThrow(() -> GeneralException.of(ErrorCode.TID_NOT_EXIST)); String tid = payment.getTid(); Map parameters = new HashMap<>(); parameters.put("cid", cid); parameters.put("tid", tid); - parameters.put("partner_order_id", "ORDER_ID"); - parameters.put("partner_user_id", "USER_ID"); + parameters.put("partner_order_id", String.valueOf(System.currentTimeMillis())); + parameters.put("partner_user_id", String.valueOf(userId)); parameters.put("pg_token", pgToken); try { @@ -145,7 +147,7 @@ public PaymentDto.KakaoApproveResponse approveSubscribeResponse(String sid, Long @Transactional public PaymentDto.KakaoSubscribeCancelResponse subscribeCancelResponse(Long userId) { - Payment kakaoPay = paymentRepository.getLatestKakaoPayInfo(userId) + Payment kakaoPay = paymentRepository.findTopByUserIdOrderByIdDesc(userId) .orElseThrow(() -> new GeneralException(ErrorCode.TID_NOT_EXIST)); String sid = kakaoPay.getSid(); @@ -192,7 +194,7 @@ public PaymentDto.KakaoSubscribeStatusResponse subscribeStatusResponse(String si @Transactional(readOnly = true) public Payment getKakaoPayInfo(Long userId) { - Payment kakaoPay = paymentRepository.getLatestKakaoPayInfo(userId) + Payment kakaoPay = paymentRepository.findTopByUserIdOrderByIdDesc(userId) .orElseThrow(() -> new GeneralException(ErrorCode.TID_NOT_EXIST)); return kakaoPay; @@ -233,7 +235,7 @@ public void savePayInfo(Long userId, PaymentDto.KakaoApproveResponse kakaoApprov @Transactional public void cancelPay(Long userId) { - Payment kakaoPay = paymentRepository.getLatestKakaoPayInfo(userId) + Payment kakaoPay = paymentRepository.findTopByUserIdOrderByIdDesc(userId) .orElseThrow(() -> new GeneralException(ErrorCode.TID_NOT_EXIST)); paymentRepository.delete(kakaoPay); @@ -247,21 +249,25 @@ public PaymentDto.KakaoPayStatus getSubscribeStatus(Long userId) { // 2. 유저가 활성화 상태가 아니면 → 구독 X 상태로 리턴 if (!user.isSubscribeActive()) { + log.info("유저가 활성화 상태가 아니면 → 구독 X 상태로 리턴"); return PaymentConverter.toKakaoPayStatus(false, new PaymentDto.KakaoSubscribeStatusResponse()); } // 3. 최신 결제 정보 조회 - Optional optionalKakaoPay = paymentRepository.getLatestKakaoPayInfo(userId); + log.info("최신 결제 정보 조회"); + Optional optionalKakaoPay = paymentRepository.findTopByUserIdOrderByIdDesc(userId); // 4. 결제 정보가 없거나, sid가 없으면 → 구독 비활성화 처리 후 구독 X 상태 리턴 if (optionalKakaoPay.isEmpty() || optionalKakaoPay.get().getSid() == null || optionalKakaoPay.get().getSid() .isEmpty()) { + log.info("결제 정보가 없거나, sid가 없으면 → 구독 비활성화 처리 후 구독 X 상태 리턴"); // 추후 결제 취소가 안되었는데 이전 sid가 없다 -> 이전 정기결제 실패 -> 재시도 로직 수가 에정 user.disableSubscription(); return PaymentConverter.toKakaoPayStatus(false, new PaymentDto.KakaoSubscribeStatusResponse()); } // 5. 결제 정보가 있고 sid도 있으면 → 구독 O 상태 리턴 + log.info("결제 정보가 있고 sid도 있으면 → 구독 O 상태 리턴"); Payment kakaoPay = optionalKakaoPay.get(); PaymentDto.KakaoSubscribeStatusResponse kakaoSubscribeStatusResponse = subscribeStatusResponse( kakaoPay.getSid());