-
Notifications
You must be signed in to change notification settings - Fork 4
feat(point): 포인트 적립, 결제 취소 시 적립금 취소 로직 구현, 결제 취소 5분 제한 로직 추가 #64
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
64fddc2
feat(point): PointHistory, PointWallet 엔티티,레포지토리 추가
bbangjae 898594a
feat(review): 중복 리뷰 생성 방지 검증 추가
bbangjae 6feb432
feat(point): 포인트 타입 취소 추가
bbangjae 8b176e8
feat(point): 포인트 적립, 결제 취소 시 적립금 취소 로직 구현
bbangjae ade4478
feat(point): 포인트 적립, 결제 취소 시 적립금 취소 로직 구현
bbangjae ef836b2
feat(point): 결제 취소 5분 제한 로직 추가
bbangjae ee5cf39
feat(payment): 결제 취소 불가 에러 코드 추가
bbangjae f509c01
yml에서 포인트 적립 값 주입에서 -> 상수로 변경
bbangjae 1718ede
Remove point earning and review settingsyml에서 포인트 적립 값 주입에서 -> 상수로 변경
bbangjae 9992bd8
Merge branch 'dev' into feat/point/aop
bbangjae 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
38 changes: 38 additions & 0 deletions
38
src/main/java/com/sparta/tdd/domain/point/dto/PointRequest.java
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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.sparta.tdd.domain.point.dto; | ||
|
|
||
| import com.sparta.tdd.domain.point.enums.PointType; | ||
| import com.sparta.tdd.domain.user.entity.User; | ||
| import java.util.UUID; | ||
| import lombok.Builder; | ||
|
|
||
| @Builder | ||
| public record PointRequest( | ||
| User user, | ||
| UUID referenceId, | ||
| PointType type, | ||
| Long amount, | ||
| String description | ||
| ) { | ||
|
|
||
| public static PointRequest forPayment(User user, UUID orderId, Long amount, | ||
| String description) { | ||
| return PointRequest.builder() | ||
| .user(user) | ||
| .referenceId(orderId) | ||
| .amount(amount) | ||
| .type(PointType.PAYMENT_EARNED) | ||
| .description(description) | ||
| .build(); | ||
| } | ||
|
|
||
| public static PointRequest forReview(User user, UUID reviewId, Long amount, | ||
| String description) { | ||
| return PointRequest.builder() | ||
| .user(user) | ||
| .referenceId(reviewId) | ||
| .amount(amount) | ||
| .type(PointType.REVIEW_EARNED) | ||
| .description(description) | ||
| .build(); | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/sparta/tdd/domain/point/entity/PointHistory.java
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package com.sparta.tdd.domain.point.entity; | ||
|
|
||
| import com.sparta.tdd.domain.point.dto.PointRequest; | ||
| import com.sparta.tdd.domain.point.enums.PointType; | ||
| import com.sparta.tdd.global.model.BaseEntity; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import java.time.LocalDateTime; | ||
| import java.util.UUID; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Table(name = "p_point_history") | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class PointHistory extends BaseEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.UUID) | ||
| @Column(name = "point_history_id") | ||
| private UUID id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "point_wallet_id") | ||
| private PointWallet wallet; | ||
|
|
||
| @Column(name = "reference_id") | ||
| private UUID referenceId; | ||
|
|
||
| @Column(name = "amount") | ||
| private Long amount; | ||
|
|
||
| @Column(name = "type") | ||
| @Enumerated(EnumType.STRING) | ||
| private PointType type; | ||
|
|
||
| @Column(name = "description") | ||
| private String description; | ||
|
|
||
| @Column(name = "expire_at") | ||
| private LocalDateTime expireAt; | ||
|
|
||
| @Builder | ||
| private PointHistory(PointWallet wallet, UUID referenceId, Long amount, | ||
| PointType type, String description, LocalDateTime expireAt) { | ||
| this.wallet = wallet; | ||
| this.referenceId = referenceId; | ||
| this.amount = amount; | ||
| this.type = type; | ||
| this.description = description; | ||
| this.expireAt = expireAt; | ||
| } | ||
|
|
||
| public static PointHistory create(PointWallet wallet, PointRequest request, | ||
| LocalDateTime expireAt) { | ||
| return PointHistory.builder() | ||
| .wallet(wallet) | ||
| .referenceId(request.referenceId()) | ||
| .amount(request.amount()) | ||
| .type(request.type()) | ||
| .description(request.description()) | ||
| .expireAt(expireAt) | ||
| .build(); | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/sparta/tdd/domain/point/entity/PointWallet.java
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package com.sparta.tdd.domain.point.entity; | ||
|
|
||
| import com.sparta.tdd.domain.user.entity.User; | ||
| import com.sparta.tdd.global.model.BaseEntity; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.OneToOne; | ||
| import jakarta.persistence.Table; | ||
| import java.util.UUID; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Table(name = "p_point_wallet") | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class PointWallet extends BaseEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.UUID) | ||
| @Column(name = "point_wallet_id", nullable = false, updatable = false) | ||
| private UUID id; | ||
|
|
||
| @OneToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "user_id", nullable = false) | ||
| private User user; | ||
|
|
||
| @Column(name = "balance") | ||
| private Long balance; | ||
|
|
||
| @Builder | ||
| public PointWallet(User user) { | ||
| this.user = user; | ||
| this.balance = 0L; | ||
| } | ||
|
|
||
| public void addBalance(Long earnAmount) { | ||
| this.balance += earnAmount; | ||
| } | ||
|
|
||
| public Long subtractBalance(Long amount) { | ||
| if (this.balance < amount) { | ||
| Long currentBalance = this.balance; | ||
| this.balance = 0L; | ||
| return amount - currentBalance; | ||
| } | ||
| this.balance -= amount; | ||
| return amount; | ||
dnjsals45 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/main/java/com/sparta/tdd/domain/point/enums/PointType.java
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.sparta.tdd.domain.point.enums; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum PointType { | ||
| PAYMENT_EARNED("결제 적립"), | ||
| REVIEW_EARNED("리뷰 적립"), | ||
| PAYMENT_CANCELLED("결제 취소"), | ||
| USED("사용"), | ||
| EXPIRED("만료"); | ||
|
|
||
| private final String description; | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/sparta/tdd/domain/point/repository/PointHistoryRepository.java
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.sparta.tdd.domain.point.repository; | ||
|
|
||
| import com.sparta.tdd.domain.point.entity.PointHistory; | ||
| import com.sparta.tdd.domain.point.enums.PointType; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface PointHistoryRepository extends JpaRepository<PointHistory, Long> { | ||
|
|
||
| boolean existsByReferenceIdAndTypeAndDeletedAtIsNull(UUID referenceId, PointType type); | ||
|
|
||
| Optional<PointHistory> findByReferenceIdAndTypeAndDeletedAtIsNull(UUID paymentId, | ||
| PointType pointType); | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/sparta/tdd/domain/point/repository/PointWalletRepository.java
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.sparta.tdd.domain.point.repository; | ||
|
|
||
| import com.sparta.tdd.domain.point.entity.PointWallet; | ||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface PointWalletRepository extends JpaRepository<PointWallet, Long> { | ||
|
|
||
| Optional<PointWallet> findByUserId(Long id); | ||
| } |
85 changes: 85 additions & 0 deletions
85
src/main/java/com/sparta/tdd/domain/point/service/PointService.java
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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package com.sparta.tdd.domain.point.service; | ||
|
|
||
| import com.sparta.tdd.domain.point.dto.PointRequest; | ||
| import com.sparta.tdd.domain.point.entity.PointHistory; | ||
| import com.sparta.tdd.domain.point.entity.PointWallet; | ||
| import com.sparta.tdd.domain.point.enums.PointType; | ||
| import com.sparta.tdd.domain.point.repository.PointHistoryRepository; | ||
| import com.sparta.tdd.domain.point.repository.PointWalletRepository; | ||
| import com.sparta.tdd.domain.user.entity.User; | ||
| import com.sparta.tdd.global.exception.BusinessException; | ||
| import com.sparta.tdd.global.exception.ErrorCode; | ||
| import java.time.LocalDateTime; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PointService { | ||
|
|
||
| private final PointWalletRepository walletRepository; | ||
| private final PointHistoryRepository historyRepository; | ||
|
|
||
| @Transactional | ||
| public void earnPoints(PointRequest request) { | ||
| if (isDuplicateEarn(request.referenceId(), request.type())) { | ||
| return; | ||
| } | ||
|
|
||
| PointWallet wallet = getOrCreateWallet(request.user()); | ||
| wallet.addBalance(request.amount()); | ||
|
|
||
| PointHistory pointHistory = PointHistory.create(wallet, request, | ||
| LocalDateTime.now().plusYears(1)); | ||
|
|
||
| historyRepository.save(pointHistory); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void losePoints(User user, UUID paymentId) { | ||
| PointWallet wallet = findWalletById(user.getId()); | ||
| PointHistory pointHistory = findPointHistory(paymentId); | ||
| Long usedAmount = wallet.subtractBalance(pointHistory.getAmount()); | ||
|
|
||
| PointHistory cancelledHistory = PointHistory.builder() | ||
| .wallet(wallet) | ||
| .referenceId(paymentId) | ||
| .amount(usedAmount) | ||
| .type(PointType.PAYMENT_CANCELLED) | ||
| .description("결제 취소 (결제번호: " + paymentId + ")") | ||
| .expireAt(LocalDateTime.now()) | ||
| .build(); | ||
|
|
||
| historyRepository.save(cancelledHistory); | ||
| } | ||
|
|
||
| private PointWallet findWalletById(Long userId) { | ||
| return walletRepository.findByUserId(userId) | ||
| .orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND)); | ||
| } | ||
|
|
||
| private PointHistory findPointHistory(UUID paymentId) { | ||
| return historyRepository.findByReferenceIdAndTypeAndDeletedAtIsNull(paymentId, | ||
| PointType.PAYMENT_EARNED) | ||
| .orElseThrow(() -> new BusinessException(ErrorCode.PAYMENT_NOT_FOUND)); | ||
| } | ||
|
|
||
|
|
||
| private PointWallet getOrCreateWallet(User user) { | ||
| return walletRepository.findByUserId(user.getId()) | ||
| .orElseGet(() -> walletRepository.save(PointWallet.builder() | ||
| .user(user) | ||
| .build())); | ||
| } | ||
|
|
||
| private boolean isDuplicateEarn(UUID referenceId, PointType type) { | ||
| if (referenceId == null || type == null) { | ||
| return false; | ||
| } | ||
| return historyRepository.existsByReferenceIdAndTypeAndDeletedAtIsNull(referenceId, type); | ||
| } | ||
| } |
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
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.
이 경고 무시 어노테이션은 어떤 문제떄문에 작성해주신걸까요?
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.
order,user가 null 이 발생할 수 있다고 경고가 떠서 제외 시켰습니다!
결제 완료 된 경우는 order, user이 null 발생 할 수 없다고 생각하여 제외 시켰습니다.