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 Order toEntity() {
Expand All @@ -29,15 +29,16 @@ public Order toEntity() {
.customerPhoneNumber(new PhoneNumber(this.customerPhoneNumber()))
.reservationTime(this.reservationTime())
.storeId(this.storeId())
.paymentId(this.paymentId())
.paymentId(this.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 @@ -2,6 +2,6 @@

public enum OrderStatus {

CONFIRMING, COOKING, READY, CANCELLED, REJECTED
CONFIRMING, COOKING, READY, CANCELED, REJECTED

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,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.service.PaymentService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.util.List;
Expand All @@ -19,6 +20,8 @@ public class OrderService {

private final OrderRepository orderRepository;

private final PaymentService paymentService;

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

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

return rejectedOrder;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/ray/pominowner/payment/domain/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public Payment(Long id, int amount, PaymentStatus status, PGType provider) {
this.provider = provider;
}

public Payment canceled() {
return Payment.builder()
.id(this.id)
.amount(this.amount)
.status(PaymentStatus.CANCELED)
.provider(this.provider)
.build();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public enum PaymentStatus {

COMPLETE,
CANCELLED
CANCELED

}
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
Expand Up @@ -6,16 +6,27 @@
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 cancel(Long paymentId) {
Payment payment = paymentRepository.findById(paymentId)
.orElseThrow(() -> new IllegalArgumentException(NO_PAYMENT.getMessage()));

Payment canceled = payment.canceled();

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 Down Expand Up @@ -79,8 +86,11 @@ void successAcceptOrder() {
@DisplayName("주문 거절을 성공한다")
void successRejectOrder() {
// given
Payment canceled = new Payment(1L, 1000, PaymentStatus.CANCELED, PGType.TOSS);

given(orderRepository.save(order)).willReturn(order);
given(orderRepository.findById(1L)).willReturn(Optional.ofNullable(order));
given(paymentService.cancel(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.CANCELED, PGType.TOSS);

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

// when
Payment payment = paymentService.cancel(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.cancel(saved.getId()))
.isInstanceOf(IllegalArgumentException.class);
}

}