Skip to content
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

[HOTFIX, FEAT] 펀딩 기여 시 중첩되는 현상 해결, 기여한 펀딩 취소 금액 검증 로직 추가 #312

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 결제 후 목표 금액 달성 시
Expand Down Expand Up @@ -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);
YeaChan05 marked this conversation as resolved.
Show resolved Hide resolved
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));
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading