diff --git a/src/main/java/com/moabam/api/application/payment/PaymentMapper.java b/src/main/java/com/moabam/api/application/payment/PaymentMapper.java index c5bf6c56..3495adad 100644 --- a/src/main/java/com/moabam/api/application/payment/PaymentMapper.java +++ b/src/main/java/com/moabam/api/application/payment/PaymentMapper.java @@ -19,7 +19,7 @@ public static Payment toPayment(Long memberId, Product product) { .memberId(memberId) .product(product) .order(order) - .amount(product.getPrice()) + .totalAmount(product.getPrice()) .build(); } } diff --git a/src/main/java/com/moabam/api/application/product/ProductMapper.java b/src/main/java/com/moabam/api/application/product/ProductMapper.java index 32369eb2..b847d9c1 100644 --- a/src/main/java/com/moabam/api/application/product/ProductMapper.java +++ b/src/main/java/com/moabam/api/application/product/ProductMapper.java @@ -35,7 +35,7 @@ public static PurchaseProductResponse toPurchaseProductResponse(Payment payment) return PurchaseProductResponse.builder() .paymentId(payment.getId()) .orderName(payment.getOrder().getName()) - .price(payment.getAmount()) + .price(payment.getTotalAmount()) .build(); } } diff --git a/src/main/java/com/moabam/api/domain/payment/Payment.java b/src/main/java/com/moabam/api/domain/payment/Payment.java index d3f36285..9c1f4dea 100644 --- a/src/main/java/com/moabam/api/domain/payment/Payment.java +++ b/src/main/java/com/moabam/api/domain/payment/Payment.java @@ -52,18 +52,17 @@ public class Payment { @JoinColumn(name = "product_id", updatable = false, nullable = false) private Product product; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "coupon_id") - private Coupon coupon; - @Column(name = "coupon_wallet_id") private Long couponWalletId; @Embedded private Order order; - @Column(name = "amount", nullable = false) - private int amount; + @Column(name = "total_amount", nullable = false) + private int totalAmount; + + @Column(name = "discount_amount", nullable = false) + private int discountAmount; @Column(name = "payment_key") private String paymentKey; @@ -83,14 +82,14 @@ public class Payment { private LocalDateTime approvedAt; @Builder - public Payment(Long memberId, Product product, Coupon coupon, Long couponWalletId, Order order, int amount, - PaymentStatus status) { + public Payment(Long memberId, Product product, Long couponWalletId, Order order, int totalAmount, + int discountAmount, PaymentStatus status) { this.memberId = requireNonNull(memberId); this.product = requireNonNull(product); - this.coupon = coupon; this.couponWalletId = couponWalletId; this.order = requireNonNull(order); - this.amount = validateAmount(amount); + this.totalAmount = validateAmount(totalAmount); + this.discountAmount = validateAmount(discountAmount); this.status = requireNonNullElse(status, PaymentStatus.READY); } @@ -104,7 +103,7 @@ private int validateAmount(int amount) { public void validateInfo(Long memberId, int amount) { validateByMember(memberId); - validateByAmount(amount); + validateByTotalAmount(amount); } public void validateByMember(Long memberId) { @@ -113,8 +112,8 @@ public void validateByMember(Long memberId) { } } - private void validateByAmount(int amount) { - if (this.amount != amount) { + private void validateByTotalAmount(int amount) { + if (this.totalAmount != amount) { throw new BadRequestException(INVALID_PAYMENT_INFO); } } @@ -124,9 +123,9 @@ public boolean isCouponApplied() { } public void applyCoupon(Coupon coupon, Long couponWalletId) { - this.coupon = coupon; this.couponWalletId = couponWalletId; - this.amount = Math.max(MIN_AMOUNT, this.amount - coupon.getPoint()); + this.discountAmount = coupon.getPoint(); + this.totalAmount = Math.max(MIN_AMOUNT, this.totalAmount - coupon.getPoint()); } public void request(String orderId) { diff --git a/src/test/java/com/moabam/api/domain/payment/PaymentTest.java b/src/test/java/com/moabam/api/domain/payment/PaymentTest.java index 8a2dfffb..b9ca3d9c 100644 --- a/src/test/java/com/moabam/api/domain/payment/PaymentTest.java +++ b/src/test/java/com/moabam/api/domain/payment/PaymentTest.java @@ -21,7 +21,7 @@ void validate_amount_exception() { .memberId(1L) .product(bugProduct()) .order(order(bugProduct())) - .amount(-1000); + .totalAmount(-1000); assertThatThrownBy(paymentBuilder::build) .isInstanceOf(BadRequestException.class) @@ -44,8 +44,8 @@ void success() { payment.applyCoupon(coupon, couponWalletId); // then - assertThat(payment.getAmount()).isEqualTo(BUG_PRODUCT_PRICE - 1000); - assertThat(payment.getCoupon()).isEqualTo(coupon); + assertThat(payment.getTotalAmount()).isEqualTo(BUG_PRODUCT_PRICE - 1000); + assertThat(payment.getDiscountAmount()).isEqualTo(coupon.getPoint()); assertThat(payment.getCouponWalletId()).isEqualTo(couponWalletId); } @@ -61,7 +61,7 @@ void discount_amount_greater() { payment.applyCoupon(coupon, couponWalletId); // then - assertThat(payment.getAmount()).isZero(); + assertThat(payment.getTotalAmount()).isZero(); } } diff --git a/src/test/java/com/moabam/support/fixture/PaymentFixture.java b/src/test/java/com/moabam/support/fixture/PaymentFixture.java index 4bff8364..a46e4b7b 100644 --- a/src/test/java/com/moabam/support/fixture/PaymentFixture.java +++ b/src/test/java/com/moabam/support/fixture/PaymentFixture.java @@ -24,7 +24,7 @@ public static Payment payment(Product product) { .memberId(1L) .product(product) .order(order(product)) - .amount(product.getPrice()) + .totalAmount(product.getPrice()) .build(); } @@ -32,10 +32,10 @@ public static Payment paymentWithCoupon(Product product, Coupon coupon, Long cou return Payment.builder() .memberId(1L) .product(product) - .coupon(coupon) .couponWalletId(couponWalletId) .order(order(product)) - .amount(product.getPrice()) + .totalAmount(product.getPrice()) + .discountAmount(coupon.getPoint()) .build(); }