From 45f38467fb32b2631a4199b04420f98de4475287 Mon Sep 17 00:00:00 2001 From: kang Date: Mon, 2 Sep 2024 14:43:55 +0900 Subject: [PATCH] feat: create payment history --- .../orange/ddanzi/service/PaymentService.java | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/co/orange/ddanzi/service/PaymentService.java b/src/main/java/co/orange/ddanzi/service/PaymentService.java index d7e41266..50606dbe 100644 --- a/src/main/java/co/orange/ddanzi/service/PaymentService.java +++ b/src/main/java/co/orange/ddanzi/service/PaymentService.java @@ -7,6 +7,7 @@ import co.orange.ddanzi.common.response.Success; import co.orange.ddanzi.domain.order.Order; import co.orange.ddanzi.domain.order.Payment; +import co.orange.ddanzi.domain.order.PaymentHistory; import co.orange.ddanzi.domain.order.enums.OrderStatus; import co.orange.ddanzi.domain.order.enums.PayStatus; import co.orange.ddanzi.domain.product.Item; @@ -19,6 +20,7 @@ import co.orange.ddanzi.dto.payment.UpdatePaymentResponseDto; import co.orange.ddanzi.global.jwt.AuthUtils; import co.orange.ddanzi.repository.ItemRepository; +import co.orange.ddanzi.repository.PaymentHistoryRepository; import co.orange.ddanzi.repository.PaymentRepository; import co.orange.ddanzi.repository.ProductRepository; import jakarta.transaction.Transactional; @@ -27,6 +29,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; + @Slf4j @RequiredArgsConstructor @Service @@ -36,6 +40,7 @@ public class PaymentService { private final ProductRepository productRepository; private final ItemRepository itemRepository; private final PaymentRepository paymentRepository; + private final PaymentHistoryRepository paymentHistoryRepository; @Autowired OrderService orderService; @@ -55,6 +60,8 @@ public ApiResponse startPayment(CreatePaymentRequestDto requestDto){ item.updateStatus(ItemStatus.IN_TRANSACTION); log.info("Update item status, item_status: {}", item.getStatus()); + createPaymentHistory(buyer, newPayment); + CreatePaymentResponseDto responseDto = CreatePaymentResponseDto.builder() .orderId(newOrder.getId()) .payStatus(newPayment.getPayStatus()) @@ -65,12 +72,12 @@ public ApiResponse startPayment(CreatePaymentRequestDto requestDto){ @Transactional public ApiResponse endPayment(UpdatePaymentRequestDto requestDto){ - + User buyer = authUtils.getUser(); Order order = orderService.getOrderRecord(requestDto.getOrderId()); Payment payment = order.getPayment(); Item item = order.getItem(); Product product = item.getProduct(); - if(isAvailableToChangePayment(payment)){ + if(isAvailableToChangePayment(buyer, payment)){ return ApiResponse.onFailure(Error.PAYMENT_CANNOT_CHANGE, null); } @@ -90,6 +97,8 @@ else if(payment.getPayStatus().equals(PayStatus.PAID)){ product.updateStock(product.getStock() - 1); } + createPaymentHistory(buyer, payment); + UpdatePaymentResponseDto responseDto = UpdatePaymentResponseDto.builder() .orderId(order.getId()) .payStatus(payment.getPayStatus()) @@ -103,8 +112,28 @@ public Integer calculateCharge(Integer salePrice){ return (int) Math.floor(salePrice*0.032); } - private boolean isAvailableToChangePayment(Payment payment){ - User user = authUtils.getUser(); + private void createPaymentHistory(User user, Payment payment){ + PaymentHistory paymentHistory = PaymentHistory.builder() + .buyerId(user.getId()) + .payStatus(payment.getPayStatus()) + .payment(payment) + .createAt(LocalDateTime.now()) + .build(); + paymentHistoryRepository.save(paymentHistory); + } + + private void createPaymentHistoryWithError(User user, Payment payment, String error){ + PaymentHistory paymentHistory = PaymentHistory.builder() + .buyerId(user.getId()) + .payStatus(payment.getPayStatus()) + .payment(payment) + .error(error) + .createAt(LocalDateTime.now()) + .build(); + paymentHistoryRepository.save(paymentHistory); + } + + private boolean isAvailableToChangePayment(User user, Payment payment){ return payment.getOrder().getBuyer().equals(user) && payment.getPayStatus().equals(PayStatus.PENDING); }