Skip to content

Commit

Permalink
feat: create payment history
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang1221 committed Sep 2, 2024
1 parent 6e83f68 commit 45f3846
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/main/java/co/orange/ddanzi/service/PaymentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -27,6 +29,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@RequiredArgsConstructor
@Service
Expand All @@ -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;
Expand All @@ -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())
Expand All @@ -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);
}

Expand All @@ -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())
Expand All @@ -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);
}

Expand Down

0 comments on commit 45f3846

Please sign in to comment.