Skip to content

Commit

Permalink
refactor: 결제를 확인하는 로직과 결과를 갱신하는 로직을 분리한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
pushedrumex committed Jan 28, 2024
1 parent 31880d8 commit 9433655
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
package com.clova.anifriends.domain.payment.repository;

import com.clova.anifriends.domain.payment.Payment;
import com.clova.anifriends.domain.payment.vo.PaymentStatus;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

public interface PaymentRepository extends JpaRepository<Payment, Long> {

Optional<Payment> findByOrderId(String orderId);

@Modifying
@Transactional
@Query("update Payment p set p.status = :status where p.paymentId = :paymentId")
void updatePaymentStatus(
@Param("paymentId") Long paymentId,
@Param("status") PaymentStatus status
);

@Modifying
@Transactional
@Query("update Payment p set p.paymentKey = :paymentKey, p.status = :status where p.paymentId = :paymentId")
void updatePaymentKeyAndStatus(
@Param("paymentId") Long paymentId,
@Param("paymentKey") String paymentKey,
@Param("status") PaymentStatus status
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.clova.anifriends.domain.payment.service;

import static com.clova.anifriends.domain.payment.vo.PaymentStatus.ABORTED;
import static com.clova.anifriends.domain.payment.vo.PaymentStatus.DONE;

import com.clova.anifriends.domain.payment.Payment;
import com.clova.anifriends.domain.payment.dto.response.PaymentResponse;
import com.clova.anifriends.domain.payment.exception.PaymentBadRequestException;
Expand All @@ -18,7 +21,6 @@ public class PaymentService {
private final PaymentRepository paymentRepository;
private final PaymentClient paymentClient;

@Transactional(noRollbackFor = {PaymentFailException.class, ExternalApiException.class})
public PaymentResponse paySuccess(String orderId, String paymentKey, Integer amount) {
Payment payment = getPayment(orderId);
validatePaymentStatus(payment);
Expand All @@ -27,14 +29,13 @@ public PaymentResponse paySuccess(String orderId, String paymentKey, Integer amo
try {
paymentClient.confirmPayment(orderId, paymentKey, amount);
} catch (PaymentFailException | ExternalApiException exception) {
payment.fail();
paymentRepository.updatePaymentStatus(payment.getPaymentId(), ABORTED);
throw exception;
}

payment.updatePaymentKey(paymentKey);
payment.success();
paymentRepository.updatePaymentKeyAndStatus(payment.getPaymentId(), paymentKey, DONE);

return new PaymentResponse(payment.getStatus(), null);
return new PaymentResponse(DONE, null);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.clova.anifriends.domain.applicant.repository.ApplicantRepository;
import com.clova.anifriends.domain.chat.repository.ChatMessageRepository;
import com.clova.anifriends.domain.chat.repository.ChatRoomRepository;
import com.clova.anifriends.domain.donation.repository.DonationRepository;
import com.clova.anifriends.domain.notification.repository.ShelterNotificationRepository;
import com.clova.anifriends.domain.notification.repository.VolunteerNotificationRepository;
import com.clova.anifriends.domain.payment.repository.PaymentRepository;
import com.clova.anifriends.domain.recruitment.repository.RecruitmentRepository;
import com.clova.anifriends.domain.review.repository.ReviewRepository;
import com.clova.anifriends.domain.shelter.repository.ShelterRepository;
Expand Down Expand Up @@ -53,4 +55,10 @@ public abstract class BaseRepositoryTest {

@Autowired
protected VolunteerNotificationRepository volunteerNotificationRepository;

@Autowired
protected PaymentRepository paymentRepository;

@Autowired
protected DonationRepository donationRepository;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.clova.anifriends.domain.payment.repository;

import static org.assertj.core.api.Assertions.assertThat;

import com.clova.anifriends.base.BaseRepositoryTest;
import com.clova.anifriends.domain.donation.Donation;
import com.clova.anifriends.domain.donation.support.fixture.DonationFixture;
import com.clova.anifriends.domain.payment.Payment;
import com.clova.anifriends.domain.payment.vo.PaymentStatus;
import com.clova.anifriends.domain.shelter.Shelter;
import com.clova.anifriends.domain.shelter.support.ShelterFixture;
import com.clova.anifriends.domain.volunteer.Volunteer;
import com.clova.anifriends.domain.volunteer.support.VolunteerFixture;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

class PaymentRepositoryTest extends BaseRepositoryTest {

@Nested
@DisplayName("updatePaymentStatus 실행 시")
class UpdatePaymentStatusTest {

@Test
@DisplayName("성공")
void updatePaymentStatus() {
// given
Shelter shelter = ShelterFixture.shelter();
Volunteer volunteer = VolunteerFixture.volunteer();
Donation donation = DonationFixture.donation(shelter, volunteer);
Payment payment = new Payment(donation);

shelterRepository.save(shelter);
volunteerRepository.save(volunteer);
paymentRepository.save(payment);

// when
paymentRepository.updatePaymentStatus(payment.getPaymentId(), PaymentStatus.ABORTED);

// then
entityManager.flush();
entityManager.clear();

Payment updatedPayment = paymentRepository.findById(payment.getPaymentId()).get();
assertThat(updatedPayment.getStatus()).isEqualTo(PaymentStatus.ABORTED);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ void paySuccess() {
// then
verify(paymentClient, times(1)).confirmPayment(orderId, paymentKey, amount);
assertThat(result).usingRecursiveComparison().isEqualTo(expected);
assertThat(payment.getStatus()).isEqualTo(PaymentStatus.DONE);
assertThat(payment.getPaymentKey()).isEqualTo(paymentKey);
}

@Test
Expand Down

0 comments on commit 9433655

Please sign in to comment.