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

#140 [FEAT] 결제하기 api: 결제 금액 검증 로직 추가 #154

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.tattour.server.domain.order.facade.dto.request.UpdateOrderStatusReq;
import org.tattour.server.domain.order.facade.dto.response.ReadOrderHistoryListRes;
import org.tattour.server.domain.order.facade.dto.response.ReadUserOrderHistoryListRes;
import org.tattour.server.domain.order.model.OrderAmount;
import org.tattour.server.domain.order.model.OrderHistory;
import org.tattour.server.domain.order.model.OrderStatus;
import org.tattour.server.domain.order.model.OrderedProduct;
Expand Down Expand Up @@ -88,13 +89,18 @@ private StickerOrderInfo getCartStickersOrderInfo(List<Cart> carts) {
@Override
@Transactional
public void order(PurchaseRequest purchaseRequest, CreateOrderReq orderReq) {
// todo: totalAmount 비교하고 다름 감지 로직 추가하기 (vo OrderAmount)
User user = userProvider.readUserById(orderReq.getUserId());
StickerOrderInfo stickerOrderInfo = getStickerOrderInfo(user, purchaseRequest);
OrderAmount orderAmount = OrderAmount.calculate(
stickerOrderInfo,
orderReq.getTotalAmount(),
orderReq.getShippingFee());

OrderHistory orderHistory = orderService.saveOrder(
OrderHistory.builder()
.productAmount(orderReq.getProductAmount())
.shippingFee(orderReq.getShippingFee())
.totalAmount(orderReq.getTotalAmount())
.productAmount(orderAmount.getProductAmount())
.shippingFee(orderAmount.getShippingFee())
.totalAmount(orderAmount.getTotalAmount())
.recipientName(orderReq.getRecipientName())
.contact(orderReq.getContact())
.mailingAddress(orderReq.getMailingAddress())
Expand All @@ -103,7 +109,6 @@ public void order(PurchaseRequest purchaseRequest, CreateOrderReq orderReq) {
.user(user)
.build());

StickerOrderInfo stickerOrderInfo = getStickerOrderInfo(user, purchaseRequest);
List<OrderedProduct> orderedProducts = orderService.saveOrderedProducts(orderHistory,
stickerOrderInfo);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.tattour.server.domain.order.model;

import lombok.Getter;
import org.tattour.server.domain.sticker.provider.vo.StickerOrderInfo;
import org.tattour.server.global.exception.BusinessException;
import org.tattour.server.global.exception.ErrorType;

@Getter
public class OrderAmount {

private final int productAmount;
private final int shippingFee;
private final int totalAmount;

private OrderAmount(StickerOrderInfo stickerOrderInfo, int requestTotalAmount,
int shippingFee) {
int totalAmount = stickerOrderInfo.calculateTotalAmount(shippingFee);
validate(requestTotalAmount, totalAmount);

this.productAmount = stickerOrderInfo.calculateProductAmount();
this.shippingFee = shippingFee;
this.totalAmount = totalAmount;
}

public static OrderAmount calculate(StickerOrderInfo stickerOrderInfo, int requestTotalAmount,
int shippingFee) {
return new OrderAmount(stickerOrderInfo, requestTotalAmount, shippingFee);
}

private void validate(int requestTotalAmount, int totalAmount) {
if (isTotalAmountNotMatch(requestTotalAmount, totalAmount)) {
throw new BusinessException(ErrorType.ORDER_AMOUNT_NOT_MATCH_EXCEPTION);
}
}

private boolean isTotalAmountNotMatch(int requestTotalAmount, int totalAmount) {
return requestTotalAmount != totalAmount;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.tattour.server.domain.sticker.provider.vo;

import java.util.Map;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -9,9 +10,27 @@
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class StickerOrderInfo {

private Map<Sticker, Integer> stickerOrderInfos;

public static StickerOrderInfo of(Map<Sticker, Integer> stickerOrderInfo) {
return new StickerOrderInfo(stickerOrderInfo);
}

public int calculateProductAmount() {
return stickerOrderInfos
.entrySet()
.stream()
.mapToInt(entry -> getFinalProductPrice(entry.getKey()) * entry.getValue())
.sum();
}

private int getFinalProductPrice(Sticker sticker) {
return Objects.isNull(sticker.getDiscountPrice())
? sticker.getPrice() : sticker.getDiscountPrice();
}

public int calculateTotalAmount(int shippingFee) {
return calculateProductAmount() + shippingFee;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum ErrorType {
VALIDATION_INPUT_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),
VALIDATION_WRONG_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 타입이 입력되었습니다."),
INVALID_ARGUMENT_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 인자가 입력되었습니다."),
INVALID_ORDERSHEET_ARGUMENT_EXCEPTION(HttpStatus.BAD_REQUEST, "stickerId와 count를 둘 다 제공하거나 제공하지 말아야 합니다."),
INVALID_ORDERSHEET_ARGUMENT_EXCEPTION(HttpStatus.BAD_REQUEST,
"stickerId와 count를 둘 다 제공하거나 제공하지 말아야 합니다."),
INVALID_IMAGE_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 이미지 파일입니다."),
INVALID_CUSTOM_IMAGE_COUNT(HttpStatus.BAD_REQUEST, "이미지 파일이 1개 이상이어야합니다."),
INVALID_MULTIPART_EXTENSION_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 파일 확장자입니다."),
Expand Down Expand Up @@ -66,6 +67,7 @@ public enum ErrorType {
ALREADY_EXIST_USER_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 유저 이름입니다."),
ALREADY_EXIST_PRODUCTLIKED_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 좋아요한 상품입니다."),
ALREADY_CANCELED_ORDER_HISTORY_EXCEPTION(HttpStatus.CONFLICT, "이미 취소 처리된 결제 내역입니다."),
ORDER_AMOUNT_NOT_MATCH_EXCEPTION(HttpStatus.CONFLICT, "주문 요청의 금액과 실제 결제 금액이 일치하지 않습니다."),

/**
* 500 INTERNAL SERVER ERROR
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.tattour.server.global.util;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapping;
Expand All @@ -17,7 +16,6 @@
import org.tattour.server.domain.order.provider.vo.OrderAmountDetailRes;
import org.tattour.server.domain.order.provider.vo.OrderHistoryInfo;
import org.tattour.server.domain.order.provider.vo.UserOrderHistoryInfo;
import org.tattour.server.domain.sticker.model.Sticker;
import org.tattour.server.domain.sticker.provider.vo.StickerLikedInfo;
import org.tattour.server.domain.sticker.provider.vo.StickerOrderInfo;
import org.tattour.server.domain.user.model.ProductLiked;
Expand Down Expand Up @@ -82,26 +80,14 @@ default List<OrderSheetStickerRes> toOrderSheetStickerRes(StickerOrderInfo stick
.collect(Collectors.toList());
}

default OrderAmountDetailRes toOrderAmountRes(StickerOrderInfo stickerOrderInfo, int shippingFee) {
int productAmount = stickerOrderInfo.getStickerOrderInfos()
.entrySet()
.stream()
.mapToInt(entry -> calculateProductAmount(entry.getKey(), entry.getValue()))
.sum();
int totalAmount = calculateTotalAmount(productAmount, shippingFee);
default OrderAmountDetailRes toOrderAmountRes(StickerOrderInfo stickerOrderInfo,
int shippingFee) {
int productAmount = stickerOrderInfo.calculateProductAmount();
int totalAmount = stickerOrderInfo.calculateTotalAmount(shippingFee);

return OrderAmountDetailRes.of(totalAmount, productAmount, shippingFee);
}

private int calculateProductAmount(Sticker sticker, int count) {
int price = Objects.isNull(sticker.getDiscountPrice()) ? sticker.getPrice() : sticker.getDiscountPrice();
return price * count;
}

private int calculateTotalAmount(int productAmount, int shippingFee) {
return productAmount + shippingFee;
}


// Custom
CreateCustomSummaryRes toCustomApplySummaryInfo(Custom custom);
Expand Down