diff --git a/src/main/java/org/kakaoshare/backend/domain/funding/entity/FundingDetail.java b/src/main/java/org/kakaoshare/backend/domain/funding/entity/FundingDetail.java index 30f5c9d31..53d271e2e 100644 --- a/src/main/java/org/kakaoshare/backend/domain/funding/entity/FundingDetail.java +++ b/src/main/java/org/kakaoshare/backend/domain/funding/entity/FundingDetail.java @@ -71,13 +71,6 @@ public FundingDetail(final Member member, this.rate = calculateRate(this.amount); } - public void increaseAmountAndRate(final Long amount) { - if (amount != null) { - this.rate += calculateRate(amount); - this.amount += amount; - } - } - public void partialCancel(final Long amount) { this.rate -= calculateRate(amount); this.amount -= amount; diff --git a/src/main/java/org/kakaoshare/backend/domain/funding/exception/FundingDetailErrorCode.java b/src/main/java/org/kakaoshare/backend/domain/funding/exception/FundingDetailErrorCode.java index b30e4a71c..26719947f 100644 --- a/src/main/java/org/kakaoshare/backend/domain/funding/exception/FundingDetailErrorCode.java +++ b/src/main/java/org/kakaoshare/backend/domain/funding/exception/FundingDetailErrorCode.java @@ -6,7 +6,8 @@ @Getter public enum FundingDetailErrorCode implements ErrorCode { - NOT_FOUND(HttpStatus.NOT_FOUND, "기여한 펀딩 내역을 찾을 수 없습니다."); + NOT_FOUND(HttpStatus.NOT_FOUND, "기여한 펀딩 내역을 찾을 수 없습니다."), + INVALID_CANCEL_AMOUNT(HttpStatus.BAD_REQUEST, "환불 금액은 기여 금액보다 클 수 없습니다."); private final HttpStatus httpStatus; private final String message; diff --git a/src/main/java/org/kakaoshare/backend/domain/payment/service/PaymentService.java b/src/main/java/org/kakaoshare/backend/domain/payment/service/PaymentService.java index f8a37e6ae..38cf690db 100644 --- a/src/main/java/org/kakaoshare/backend/domain/payment/service/PaymentService.java +++ b/src/main/java/org/kakaoshare/backend/domain/payment/service/PaymentService.java @@ -74,6 +74,7 @@ import java.util.Objects; import java.util.function.Function; +import static org.kakaoshare.backend.domain.funding.exception.FundingDetailErrorCode.INVALID_CANCEL_AMOUNT; import static org.kakaoshare.backend.domain.funding.exception.FundingErrorCode.INVALID_ATTRIBUTE_AMOUNT; import static org.kakaoshare.backend.domain.funding.exception.FundingErrorCode.INVALID_STATUS; import static org.kakaoshare.backend.domain.member.exception.MemberErrorCode.NOT_FOUND; @@ -170,7 +171,7 @@ public PaymentFundingSuccessResponse approveFunding(final String providerId, final Funding funding = findFundingById(fundingOrderDetail.fundingId()); final Member member = findMemberByProviderId(providerId); final Long amount = payment.getTotalPrice(); - saveOrReflectFundingDetail(payment, funding, member, amount); + saveFundingDetail(payment, funding, member); funding.increaseAccumulateAmount(amount); // TODO: 5/10/24 결제 후 목표 금액 달성 시 @@ -224,12 +225,19 @@ public void cancelFundingDetail(final String providerId, final PaymentFundingDetailCancelRequest paymentFundingCancelRequest) { final Long fundingDetailId = paymentFundingCancelRequest.fundingDetailId(); final FundingDetail fundingDetail = findFundingDetailById(fundingDetailId); + final Long amount = paymentFundingCancelRequest.amount(); + validateCancelAmount(fundingDetail, amount); validateAlreadyCanceled(fundingDetail, FundingDetail::canceled); validateMemberFundingDetail(providerId, fundingDetail); - final Long amount = paymentFundingCancelRequest.amount(); refundFundingDetails(amount, fundingDetail); } + private void validateCancelAmount(final FundingDetail fundingDetail, final Long amount) { + if (fundingDetail.getAmount() < amount) { + throw new FundingDetailException(INVALID_CANCEL_AMOUNT); + } + } + private Order findOrderByPaymentId(final Long paymentId) { return orderRepository.findByPaymentId(paymentId) .orElseThrow(() -> new OrderException(OrderErrorCode.NOT_FOUND)); @@ -480,11 +488,8 @@ private FundingDetail findFundingDetailById(final Long fundingDetailId) { .orElseThrow(() -> new FundingDetailException(FundingDetailErrorCode.NOT_FOUND)); } - private void saveOrReflectFundingDetail(final Payment payment, final Funding funding, final Member member, final Long amount) { - fundingDetailRepository.findByFundingAndMember(funding, member) - .ifPresentOrElse( - fundingDetail -> fundingDetail.increaseAmountAndRate(amount), - () -> fundingDetailRepository.save(new FundingDetail(member, funding, payment)) - ); + private void saveFundingDetail(final Payment payment, final Funding funding, final Member member) { + final FundingDetail fundingDetail = new FundingDetail(member, funding, payment); + fundingDetailRepository.save(fundingDetail); } } diff --git a/src/test/java/org/kakaoshare/backend/domain/payment/service/PaymentServiceTest.java b/src/test/java/org/kakaoshare/backend/domain/payment/service/PaymentServiceTest.java index dfe838114..240ce5457 100644 --- a/src/test/java/org/kakaoshare/backend/domain/payment/service/PaymentServiceTest.java +++ b/src/test/java/org/kakaoshare/backend/domain/payment/service/PaymentServiceTest.java @@ -30,7 +30,11 @@ import org.kakaoshare.backend.domain.payment.dto.kakaopay.Amount; import org.kakaoshare.backend.domain.payment.dto.preview.PaymentPreviewRequest; import org.kakaoshare.backend.domain.payment.dto.preview.PaymentPreviewResponse; -import org.kakaoshare.backend.domain.payment.dto.ready.request.*; +import org.kakaoshare.backend.domain.payment.dto.ready.request.PaymentFundingReadyRequest; +import org.kakaoshare.backend.domain.payment.dto.ready.request.PaymentGiftReadyItem; +import org.kakaoshare.backend.domain.payment.dto.ready.request.PaymentGiftReadyReceiver; +import org.kakaoshare.backend.domain.payment.dto.ready.request.PaymentGiftReadyRequest; +import org.kakaoshare.backend.domain.payment.dto.ready.request.PaymentReadyProductDto; import org.kakaoshare.backend.domain.payment.dto.ready.response.KakaoPayReadyResponse; import org.kakaoshare.backend.domain.payment.dto.ready.response.PaymentReadyResponse; import org.kakaoshare.backend.domain.payment.dto.success.request.PaymentSuccessRequest; @@ -49,13 +53,19 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.time.LocalDateTime; -import java.util.*; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.kakaoshare.backend.fixture.BrandFixture.STARBUCKS; import static org.kakaoshare.backend.fixture.FundingFixture.SAMPLE_FUNDING; -import static org.kakaoshare.backend.fixture.MemberFixture.*; +import static org.kakaoshare.backend.fixture.MemberFixture.HAN; +import static org.kakaoshare.backend.fixture.MemberFixture.KAKAO; +import static org.kakaoshare.backend.fixture.MemberFixture.KIM; import static org.kakaoshare.backend.fixture.ProductFixture.CAKE; import static org.kakaoshare.backend.fixture.ProductFixture.COFFEE; import static org.mockito.ArgumentMatchers.any; @@ -361,7 +371,6 @@ public void approveFunding() throws Exception { final Funding funding = SAMPLE_FUNDING.생성(1L, creator, cake); doReturn(fundingOrderDetail).when(redisUtils).remove(orderDetailsKey, FundingOrderDetail.class); doReturn(Optional.of(funding)).when(fundingRepository).findById(funding.getFundingId()); - doReturn(Optional.empty()).when(fundingDetailRepository).findByFundingAndMember(funding, contributor); doReturn(Optional.of(contributor)).when(memberRepository).findMemberByProviderId(providerId); final ProductSummaryResponse productSummaryResponse = ProductSummaryResponse.from(cake);