Skip to content

Commit f30b964

Browse files
committed
feature: 결제 도메인 및 관련 컴포넌트 추가 (Payment 엔티티, 서비스, 리포지토리 등)
1 parent 6d990fd commit f30b964

6 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.loopers.domain.payment;
2+
3+
import com.loopers.domain.BaseEntity;
4+
import com.loopers.domain.money.Money;
5+
import com.loopers.support.error.CoreException;
6+
import com.loopers.support.error.ErrorType;
7+
import jakarta.persistence.AttributeOverride;
8+
import jakarta.persistence.Column;
9+
import jakarta.persistence.Embedded;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.EnumType;
12+
import jakarta.persistence.Enumerated;
13+
import jakarta.persistence.Table;
14+
import java.util.UUID;
15+
import lombok.AccessLevel;
16+
import lombok.NoArgsConstructor;
17+
import org.springframework.util.StringUtils;
18+
19+
@Entity
20+
@Table(name = "payment")
21+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
22+
public class Payment extends BaseEntity {
23+
24+
@Column(name = "ref_user_id", nullable = false)
25+
private Long userId;
26+
27+
@Column(name = "ref_order_id", nullable = false)
28+
private Long orderId;
29+
30+
@Column(name = "transaction_id", nullable = false, unique = true)
31+
private String transactionId;//멱등키
32+
33+
@Column(name = "card_type", nullable = false)
34+
private String cardType;
35+
36+
@Column(name = "card_no", nullable = false)
37+
private String cardNo;
38+
39+
@Embedded
40+
@AttributeOverride(name = "value", column = @Column(name = "amount"))
41+
private Money amount;
42+
43+
@Enumerated(EnumType.STRING)
44+
@Column(nullable = false)
45+
private PaymentStatus status;
46+
47+
@Column(name = "pg_txn_id")
48+
private String pgTxnId;
49+
50+
public Payment(Long orderId, Long userId, Money amount, String cardType, String cardNo) {
51+
validateConstructor(orderId, userId, amount, cardType, cardNo);
52+
53+
this.orderId = orderId;
54+
this.userId = userId;
55+
this.amount = amount;
56+
this.cardType = cardType;
57+
this.cardNo = cardNo;
58+
this.status = PaymentStatus.READY;
59+
60+
this.transactionId = UUID.randomUUID().toString();
61+
}
62+
63+
private void validateConstructor(Long orderId, Long userId, Money amount, String cardType, String cardNo) {
64+
if (orderId == null) {
65+
throw new CoreException(ErrorType.BAD_REQUEST, "주문 정보는 필수입니다.");
66+
}
67+
if (userId == null) {
68+
throw new CoreException(ErrorType.BAD_REQUEST, "사용자 정보는 필수입니다.");
69+
}
70+
if (amount == null) {
71+
throw new CoreException(ErrorType.BAD_REQUEST, "결제 금액은 필수입니다.");
72+
}
73+
if (!StringUtils.hasText(cardType)) {
74+
throw new CoreException(ErrorType.BAD_REQUEST, "카드 종류는 필수입니다.");
75+
}
76+
if (!StringUtils.hasText(cardNo)) {
77+
throw new CoreException(ErrorType.BAD_REQUEST, "카드 번호는 필수입니다.");
78+
}
79+
}
80+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.loopers.domain.payment;
2+
3+
public interface PaymentRepository {
4+
5+
Payment save(Payment payment);
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.loopers.domain.payment;
2+
3+
import com.loopers.domain.order.Order;
4+
import com.loopers.domain.user.User;
5+
import jakarta.transaction.Transactional;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.stereotype.Component;
8+
9+
@RequiredArgsConstructor
10+
@Component
11+
public class PaymentService {
12+
13+
private final PaymentRepository paymentRepository;
14+
15+
@Transactional
16+
public Payment createPendingPayment(User user, Order order, String cardType, String cardNo) {
17+
Payment payment = new Payment(
18+
order.getId(),
19+
user.getId(),
20+
order.getTotalAmount(),
21+
cardType,
22+
cardNo
23+
);
24+
25+
return paymentRepository.save(payment);
26+
}
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.loopers.domain.payment;
2+
3+
public enum PaymentStatus {
4+
READY,
5+
PAID,
6+
CANCELLED,
7+
FAILED
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.loopers.infrastructure.payment;
2+
3+
import com.loopers.domain.payment.Payment;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface PaymentJpaRepository extends JpaRepository<Payment, Long> {
7+
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.loopers.infrastructure.payment;
2+
3+
import com.loopers.domain.payment.Payment;
4+
import com.loopers.domain.payment.PaymentRepository;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.stereotype.Component;
7+
8+
@RequiredArgsConstructor
9+
@Component
10+
public class PaymentRepositoryImpl implements PaymentRepository {
11+
12+
private final PaymentJpaRepository paymentJpaRepository;
13+
14+
@Override
15+
public Payment save(Payment payment) {
16+
return paymentJpaRepository.save(payment);
17+
}
18+
}

0 commit comments

Comments
 (0)