From 3ad420190da36cfb6a880b728835537e1a30e2ef Mon Sep 17 00:00:00 2001 From: persi Date: Mon, 30 Jun 2025 14:50:12 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=EA=B2=B0=EC=A0=9C=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=EA=B0=80=20=EC=97=86=EA=B1=B0=EB=82=98,=20sid?= =?UTF-8?q?=EA=B0=80=20=EC=97=86=EC=9C=BC=EB=A9=B4=20=EA=B5=AC=EB=8F=85=20?= =?UTF-8?q?=EB=B9=84=ED=99=9C=EC=84=B1=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../payment/repository/PaymentRepository.java | 5 +--- .../payment/service/PaymentService.java | 27 +++++++++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) 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 623f422..6d60a54 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 @@ -8,10 +8,7 @@ import org.springframework.data.repository.query.Param; public interface PaymentRepository extends JpaRepository { - - boolean existsByUserId(Long userId); - - Optional findByTid(String tid); + Optional findTopByTidOrderByIdDesc(String tid); boolean existsByTid(String tid); 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 2eefb86..6984f0c 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 @@ -14,6 +14,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; @@ -214,7 +215,7 @@ public void saveTid(Long userId, String tid) { @Transactional public void saveSid(PaymentDto.KakaoApproveResponse kakaoApproveResponse) { - Payment kakaoPay = paymentRepository.findByTid(kakaoApproveResponse.getTid()) + Payment kakaoPay = paymentRepository.findTopByTidOrderByIdDesc(kakaoApproveResponse.getTid()) .orElseThrow(() -> new GeneralException(ErrorCode.TID_NOT_EXIST)); kakaoPay.updateSid(kakaoApproveResponse.getSid()); @@ -240,26 +241,36 @@ public void cancelPay(Long userId) { paymentRepository.delete(kakaoPay); } - @Transactional(readOnly = true) + @Transactional public PaymentDto.KakaoPayStatus getSubscribeStatus(Long userId) { + // 1. 유저 조회 User user = userRepository.findById(userId) .orElseThrow(() -> new GeneralException(ErrorCode.USER_NOT_FOUND)); + // 2. 유저가 활성화 상태가 아니면 → 구독 X 상태로 리턴 if (!user.isSubscribeActive()) { return PaymentConverter.toKakaoPayStatus(false, new PaymentDto.KakaoSubscribeStatusResponse()); } - Payment kakaoPay = getKakaoPayInfo(userId); + // 3. 최신 결제 정보 조회 + Optional optionalKakaoPay = paymentRepository.getLatestKakaoPayInfo(userId); - if (kakaoPay.getSid() != null && !kakaoPay.getSid().isEmpty()) { - PaymentDto.KakaoSubscribeStatusResponse kakaoSubscribeStatusResponse = subscribeStatusResponse( - kakaoPay.getSid()); - return PaymentConverter.toKakaoPayStatus(true, kakaoSubscribeStatusResponse); + // 4. 결제 정보가 없거나, sid가 없으면 → 구독 비활성화 처리 후 구독 X 상태 리턴 + if (optionalKakaoPay.isEmpty() || optionalKakaoPay.get().getSid() == null || optionalKakaoPay.get().getSid() + .isEmpty()) { + user.disableSubscription(); + return PaymentConverter.toKakaoPayStatus(false, new PaymentDto.KakaoSubscribeStatusResponse()); } - return PaymentConverter.toKakaoPayStatus(false, new PaymentDto.KakaoSubscribeStatusResponse()); + // 5. 결제 정보가 있고 sid도 있으면 → 구독 O 상태 리턴 + Payment kakaoPay = optionalKakaoPay.get(); + PaymentDto.KakaoSubscribeStatusResponse kakaoSubscribeStatusResponse = subscribeStatusResponse( + kakaoPay.getSid()); + + return PaymentConverter.toKakaoPayStatus(true, kakaoSubscribeStatusResponse); } + @Transactional public void regularPayment() { List kakaoPayList = paymentRepository.findAllWithMemberAndSidNotNull(); From 783ef4fb306f43fa90e1e17b9190d0ea7451890d Mon Sep 17 00:00:00 2001 From: persi Date: Mon, 30 Jun 2025 14:54:44 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=EA=B2=B0=EC=A0=9C=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=EA=B0=80=20=EC=97=86=EA=B1=B0=EB=82=98,=20sid?= =?UTF-8?q?=EA=B0=80=20=EC=97=86=EC=9C=BC=EB=A9=B4=20=EA=B5=AC=EB=8F=85=20?= =?UTF-8?q?=EB=B9=84=ED=99=9C=EC=84=B1=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../silverbridgeX_user/payment/service/PaymentService.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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 6984f0c..af5870b 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 @@ -72,7 +72,7 @@ public PaymentDto.KakaoReadyResponse kakaoPayReady(Long userId) { } } - @Transactional(readOnly = true) + @Transactional public PaymentDto.KakaoApproveResponse approveResponse(String pgToken, Long userId) { Payment payment = paymentRepository.getLatestKakaoPayInfo(userId) .orElseThrow(() -> GeneralException.of(ErrorCode.TID_NOT_EXIST)); @@ -218,9 +218,7 @@ public void saveSid(PaymentDto.KakaoApproveResponse kakaoApproveResponse) { Payment kakaoPay = paymentRepository.findTopByTidOrderByIdDesc(kakaoApproveResponse.getTid()) .orElseThrow(() -> new GeneralException(ErrorCode.TID_NOT_EXIST)); - kakaoPay.updateSid(kakaoApproveResponse.getSid()); - - paymentRepository.save(kakaoPay); + kakaoPay.updateSid(kakaoApproveResponse.getSid()); // jpa에서 영속 상태의 엔티티는 setter 호출만해도 dirty checking 반영됨 } @Transactional @@ -258,6 +256,7 @@ public PaymentDto.KakaoPayStatus getSubscribeStatus(Long userId) { // 4. 결제 정보가 없거나, sid가 없으면 → 구독 비활성화 처리 후 구독 X 상태 리턴 if (optionalKakaoPay.isEmpty() || optionalKakaoPay.get().getSid() == null || optionalKakaoPay.get().getSid() .isEmpty()) { + // 추후 결제 취소가 안되었는데 이전 sid가 없다 -> 이전 정기결제 실패 -> 재시도 로직 수가 에정 user.disableSubscription(); return PaymentConverter.toKakaoPayStatus(false, new PaymentDto.KakaoSubscribeStatusResponse()); }