-
Notifications
You must be signed in to change notification settings - Fork 0
Fix: 결제 처리 에러 수정 #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Fix: 결제 처리 에러 수정 #119
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import com.catchtable.notification.event.ReservationVisitedEvent; | ||
| import com.catchtable.notification.event.VacancyEvent; | ||
| import com.catchtable.payment.entity.Payment; | ||
| import com.catchtable.payment.entity.PaymentStatus; | ||
| import com.catchtable.payment.repository.PaymentRepository; | ||
| import com.catchtable.payment.service.PaymentService; | ||
| import com.catchtable.remain.entity.StoreRemain; | ||
|
|
@@ -124,6 +125,16 @@ public String createReservationFromAi( | |
| currentUserId, remainId, member, couponId); | ||
|
|
||
| // Payment 레코드 생성 (결제창 호출을 위해 orderId 필요) | ||
| // 같은 reservation_id에 잔존 payment(FAILED/PENDING) 있으면 UNIQUE 위반 → 결제 재시도 시나리오에서 충돌 | ||
| // INSERT 전 기존 row 정리해 idempotent 보장. PAID 상태면 비즈니스 예외. | ||
| paymentRepository.findByReservation_Id(saved.getId()).ifPresent(existing -> { | ||
| if (existing.getStatus() == PaymentStatus.PAID) { | ||
| throw new CustomException(ErrorCode.PAYMENT_ALREADY_PAID); | ||
| } | ||
| paymentRepository.delete(existing); | ||
| paymentRepository.flush(); | ||
| }); | ||
|
|
||
| String orderId = "CATCH-" + saved.getId() + "-" + System.currentTimeMillis(); | ||
| Payment payment = Payment.builder() | ||
| .reservation(saved) | ||
|
|
@@ -221,6 +232,16 @@ public ReservationCreateResponseDto create(Long userId, ReservationCreateRequest | |
| Reservation saved = createReservationCore( | ||
| userId, request.remainId(), request.member(), request.couponId()); | ||
|
|
||
| // 같은 reservation_id에 잔존 payment(FAILED/PENDING) 있으면 UNIQUE 위반. | ||
| // INSERT 전 기존 row 정리해 idempotent 보장. PAID 상태면 비즈니스 예외. | ||
| paymentRepository.findByReservation_Id(saved.getId()).ifPresent(existing -> { | ||
| if (existing.getStatus() == PaymentStatus.PAID) { | ||
| throw new CustomException(ErrorCode.PAYMENT_ALREADY_PAID); | ||
| } | ||
| paymentRepository.delete(existing); | ||
| paymentRepository.flush(); | ||
| }); | ||
|
Comment on lines
+237
to
+243
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| // ConfirmedEvent는 결제 완료 시점(PaymentService.confirmPayment)에서 발행한다. | ||
| String orderId = "CATCH-" + saved.getId() + "-" + System.currentTimeMillis(); | ||
| Payment payment = Payment.builder() | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createReservationCore메서드는 항상 새로운Reservation엔티티를 생성하고 저장하므로,saved.getId()는 매번 새롭게 발급되는 고유한 ID입니다.\n\n따라서 새로 생성된saved.getId()를 조건으로paymentRepository.findByReservation_Id(saved.getId())를 조회하면 항상 결과가 비어있게 되므로(Optional.empty()),ifPresent내부의 로직은 결코 실행되지 않습니다.\n\n#### 💡 권장 해결 방안\n결제 재시도 시나리오나 중복 예약을 방지하고자 한다면, 새로운 예약을 생성하기 전에 기존에 동일한 사용자(userId)와 예약 시간대(remainId)로 생성된PENDING상태의 예약이 존재하는지 먼저 조회해야 합니다.\n\n만약 존재한다면 해당 예약의 결제 정보를 재사용하거나 예외를 던지도록 수정하는 것이 올바른 멱등성(Idempotency) 보장 방식입니다.