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

[POM-84] feat: 주문 거절 시 결제 취소 #30

Merged
merged 11 commits into from
Sep 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public enum ExceptionMessage {
NULL_PAYOUT_OBJECT("지급 관련 값은 필수입니다."),
NULL_SALES_OBJECT("매출 관련 값은 필수입니다."),
SALES_DATE_IS_AFTER_NOW("매출 일은 생성 날짜와 같거나 그보다 빨라야 합니다."),
PAYOUT_DATE_IS_BEFORE_NOW("지급 일은 생성 날짜와 같거나 그보다 늦어야 합니다.");
PAYOUT_DATE_IS_BEFORE_NOW("지급 일은 생성 날짜와 같거나 그보다 늦어야 합니다."),
NO_PAYMENT("해당 결제 건이 존재하지 않습니다");

private final String message;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public record ReceiveOrderRequest(
String customerPhoneNumber,
LocalDateTime reservationTime,
Long storeId,
PaymentCreateRequest paymentRequest
PaymentCreateRequest payment
) {

public static Order getEntity(ReceiveOrderRequest request) {
Expand All @@ -29,16 +29,16 @@ public static Order getEntity(ReceiveOrderRequest request) {
.customerPhoneNumber(new PhoneNumber(request.customerPhoneNumber()))
.reservationTime(request.reservationTime())
.storeId(request.storeId())
.paymentId(request.paymentRequest.id())
.paymentId(request.payment.id())
.build();
}

public Payment toPaymentEntity() {
return Payment.builder()
.id(this.paymentRequest.id())
.amount(this.paymentRequest.amount())
.status(this.paymentRequest.status())
.provider(this.paymentRequest.provider())
.id(this.payment.id())
.amount(this.payment.amount())
.status(this.payment.status())
.provider(this.payment.provider())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.ray.pominowner.orders.domain.Order;
import com.ray.pominowner.orders.domain.OrderStatus;
import com.ray.pominowner.orders.repository.OrderRepository;
import com.ray.pominowner.payment.service.PaymentService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -18,6 +19,8 @@ public class OrderService {

private final OrderRepository orderRepository;

private final PaymentService paymentService;

public Order receiveOrder(Order order) {
return orderRepository.save(order);
}
Expand All @@ -42,7 +45,9 @@ public Order reject(Long orderId) {
Order rejectedOrder = Order.of(order,
OrderStatus.REJECTED,
"재고 소진");

orderRepository.save(rejectedOrder);
paymentService.canceled(order.getPaymentId());

return rejectedOrder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package com.ray.pominowner.payment.dto;

import com.ray.pominowner.payment.domain.PGType;
import com.ray.pominowner.payment.domain.Payment;
import com.ray.pominowner.payment.domain.PaymentStatus;

public record PaymentCreateRequest(Long id, int amount, PaymentStatus status, PGType provider) {

public Payment toEntity() {
return new Payment(id, amount, status, provider);
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package com.ray.pominowner.payment.service;

import com.ray.pominowner.payment.domain.Payment;
import com.ray.pominowner.payment.domain.PaymentStatus;
import com.ray.pominowner.payment.repository.PaymentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.ray.pominowner.global.util.ExceptionMessage.NO_PAYMENT;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Transactional
public class PaymentService {

private final PaymentRepository paymentRepository;

@Transactional
public Payment create(Payment payment) {
return paymentRepository.save(payment);
}


public Payment canceled(Long paymentId) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메소드이름이 동사 과거형인게 조금 부자연스러워 보여요. 현재형으로 바꾸거나 목적어를 추가하는 건 어떠신가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Payment payment = paymentRepository.findById(paymentId)
.orElseThrow(() -> new IllegalArgumentException(NO_PAYMENT.getMessage()));

Payment canceled = Payment.builder()
.id(paymentId)
.amount(payment.getAmount())
.provider(payment.getProvider())
.status(PaymentStatus.CANCELLED)
.build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

도메인 내부에서 작업하는게 좋아보이네요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return paymentRepository.save(canceled);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import com.ray.pominowner.orders.domain.Order;
import com.ray.pominowner.orders.domain.OrderStatus;
import com.ray.pominowner.orders.repository.OrderRepository;
import com.ray.pominowner.payment.domain.PGType;
import com.ray.pominowner.payment.domain.Payment;
import com.ray.pominowner.payment.domain.PaymentStatus;
import com.ray.pominowner.payment.service.PaymentService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -23,6 +27,9 @@ class OrderServiceTest {
@InjectMocks
private OrderService orderService;

@Mock
private PaymentService paymentService;

@Mock
private OrderRepository orderRepository;

Expand All @@ -41,6 +48,7 @@ void setup() {
.totalPrice(30000)
.customerPhoneNumber(new PhoneNumber("01012345678"))
.storeId(1L)
.paymentId(1L)
.build();
}

Expand Down Expand Up @@ -78,8 +86,11 @@ void successAcceptOrder() {
@DisplayName("주문 거절을 성공한다")
void successRejectOrder() {
// given
Payment canceled = new Payment(1L, 1000, PaymentStatus.CANCELLED, PGType.TOSS);

given(orderRepository.save(order)).willReturn(order);
given(orderRepository.findById(1L)).willReturn(Optional.ofNullable(order));
given(paymentService.canceled(order.getPaymentId())).willReturn(canceled);

// when
Order rejectedOrder = orderService.reject(order.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.BDDMockito.given;

@ExtendWith(MockitoExtension.class)
Expand All @@ -37,4 +40,34 @@ public void successCreatePayment() {
assertThat(createdPayment).isEqualTo(payment);
}

@Test
@DisplayName("Payment 취소에 성공한다.")
public void successCancelPayment() {
// given
Payment saved = new Payment(1L, 1000, PaymentStatus.COMPLETE, PGType.TOSS);
Payment canceled = new Payment(1L, 1000, PaymentStatus.CANCELLED, PGType.TOSS);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status의 cancelled 와 변수명 canceled을 통일하면 좋을 것 같아요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


given(paymentRepository.findById(saved.getId())).willReturn(Optional.of(saved));
given(paymentRepository.save(canceled)).willReturn(canceled);

// when
Payment payment = paymentService.canceled(saved.getId());

// then
assertThat(canceled).isEqualTo(payment);
}

@Test
@DisplayName("PaymentId가 존재하지 않는 경우 취소에 실패한다.")
public void failCancelPayment() {
// given
Payment saved = new Payment(1L, 1000, PaymentStatus.COMPLETE, PGType.TOSS);

given(paymentRepository.findById(saved.getId())).willThrow(IllegalArgumentException.class);

// when, then
assertThatThrownBy(() -> paymentService.canceled(saved.getId()))
.isInstanceOf(IllegalArgumentException.class);
}

}