1+ package com .ururulab .ururu .payment .domain .entity ;
2+
3+ import com .ururulab .ururu .global .common .entity .BaseEntity ;
4+ import com .ururulab .ururu .member .domain .entity .Member ;
5+ import com .ururulab .ururu .order .domain .entity .Order ;
6+ import com .ururulab .ururu .payment .domain .entity .enumerated .PayMethod ;
7+ import com .ururulab .ururu .payment .domain .entity .enumerated .PaymentStatus ;
8+ import com .ururulab .ururu .payment .domain .policy .PaymentPolicy ;
9+ import jakarta .persistence .*;
10+ import lombok .AccessLevel ;
11+ import lombok .Getter ;
12+ import lombok .NoArgsConstructor ;
13+
14+ import java .time .ZoneId ;
15+ import java .time .ZonedDateTime ;
16+
17+ @ Entity
18+ @ Getter
19+ @ Table (name = "payment" )
20+ @ NoArgsConstructor (access = AccessLevel .PROTECTED )
21+ public class Payment extends BaseEntity {
22+
23+ @ Id
24+ @ GeneratedValue (strategy = GenerationType .IDENTITY )
25+ private Long id ;
26+
27+ @ ManyToOne (fetch = FetchType .LAZY )
28+ @ JoinColumn (name = "member_id" , nullable = false )
29+ private Member member ;
30+
31+ @ ManyToOne (fetch = FetchType .LAZY )
32+ @ JoinColumn (name = "order_id" , nullable = false )
33+ private Order order ;
34+
35+ @ Column (name = "payment_key" , length = PaymentPolicy .PAYMENT_KEY_MAX_LENGTH )
36+ private String paymentKey ;
37+
38+ @ Column (name = "total_amount" , nullable = false )
39+ private Integer totalAmount ;
40+
41+ @ Column (nullable = false )
42+ private Integer amount ;
43+
44+ @ Column (nullable = false )
45+ private Integer point ;
46+
47+ @ Enumerated (EnumType .STRING )
48+ @ Column (name = "pay_method" )
49+ private PayMethod payMethod ;
50+
51+ @ Enumerated (EnumType .STRING )
52+ @ Column (nullable = false )
53+ private PaymentStatus status ;
54+
55+ @ Column (name = "request_at" )
56+ private ZonedDateTime requestAt ;
57+
58+ @ Column (name = "paid_at" )
59+ private ZonedDateTime paidAt ;
60+
61+ @ Column (name = "cancelled_at" )
62+ private ZonedDateTime cancelledAt ;
63+
64+ public static Payment create (
65+ Member member ,
66+ Order order ,
67+ Integer totalAmount ,
68+ Integer amount ,
69+ Integer point
70+ ) {
71+ if (member == null ) {
72+ throw new IllegalArgumentException (PaymentPolicy .MEMBER_REQUIRED );
73+ }
74+ if (order == null ) {
75+ throw new IllegalArgumentException (PaymentPolicy .ORDER_REQUIRED );
76+ }
77+ if (totalAmount == null ) {
78+ throw new IllegalArgumentException (PaymentPolicy .TOTAL_AMOUNT_REQUIRED );
79+ }
80+ if (totalAmount < PaymentPolicy .MIN_AMOUNT ) {
81+ throw new IllegalArgumentException (PaymentPolicy .TOTAL_AMOUNT_MIN );
82+ }
83+ if (totalAmount > PaymentPolicy .MAX_AMOUNT ) {
84+ throw new IllegalArgumentException (PaymentPolicy .TOTAL_AMOUNT_MAX );
85+ }
86+
87+ if (amount == null ) {
88+ throw new IllegalArgumentException (PaymentPolicy .AMOUNT_REQUIRED );
89+ }
90+ if (amount < PaymentPolicy .MIN_AMOUNT ) {
91+ throw new IllegalArgumentException (PaymentPolicy .AMOUNT_MIN );
92+ }
93+ if (amount > PaymentPolicy .MAX_AMOUNT ) {
94+ throw new IllegalArgumentException (PaymentPolicy .AMOUNT_MAX );
95+ }
96+ if (point == null ) {
97+ throw new IllegalArgumentException (PaymentPolicy .POINT_REQUIRED );
98+ }
99+ if (point < PaymentPolicy .MIN_POINT ) {
100+ throw new IllegalArgumentException (PaymentPolicy .POINT_MIN );
101+ }
102+ if (!totalAmount .equals (amount + point )) {
103+ throw new IllegalArgumentException (PaymentPolicy .AMOUNT_MISMATCH );
104+ }
105+
106+ Payment payment = new Payment ();
107+ payment .member = member ;
108+ payment .order = order ;
109+ payment .totalAmount = totalAmount ;
110+ payment .amount = amount ;
111+ payment .point = point ;
112+ payment .status = PaymentStatus .PENDING ;
113+ payment .requestAt = ZonedDateTime .now (ZoneId .of ("Asia/Seoul" ));
114+
115+ return payment ;
116+ }
117+
118+ public void updatePaymentInfo (String paymentKey , PayMethod payMethod , Integer paidAmount ) {
119+ if (this .status == PaymentStatus .PAID ) {
120+ throw new IllegalStateException (PaymentPolicy .CANNOT_UPDATE_PAID );
121+ }
122+ if (this .status == PaymentStatus .REFUNDED ) {
123+ throw new IllegalStateException (PaymentPolicy .CANNOT_UPDATE_REFUNDED );
124+ }
125+ if (paymentKey == null || paymentKey .trim ().isEmpty ()) {
126+ throw new IllegalArgumentException (PaymentPolicy .PAYMENT_KEY_REQUIRED );
127+ }
128+ if (payMethod == null ) {
129+ throw new IllegalArgumentException (PaymentPolicy .PAY_METHOD_REQUIRED );
130+ }
131+ if (!this .amount .equals (paidAmount )) {
132+ throw new IllegalArgumentException (PaymentPolicy .PAYMENT_AMOUNT_MISMATCH );
133+ }
134+
135+ this .paymentKey = paymentKey ;
136+ this .payMethod = payMethod ;
137+ }
138+
139+ public void markAsPaid (ZonedDateTime approvedAt ) {
140+ if (approvedAt == null ) {
141+ throw new IllegalArgumentException (PaymentPolicy .APPROVED_AT_REQUIRED );
142+ }
143+ if (this .status == PaymentStatus .PAID ) {
144+ throw new IllegalStateException (PaymentPolicy .ALREADY_PAID );
145+ }
146+ if (this .status == PaymentStatus .REFUNDED ) {
147+ throw new IllegalStateException (PaymentPolicy .ALREADY_REFUNDED );
148+ }
149+
150+ this .status = PaymentStatus .PAID ;
151+ this .paidAt = approvedAt ;
152+ }
153+
154+ public void markAsRefunded (ZonedDateTime cancelledAt ) {
155+ if (cancelledAt == null ) {
156+ throw new IllegalArgumentException (PaymentPolicy .CANCELLED_AT_REQUIRED );
157+ }
158+ if (this .status != PaymentStatus .PAID ) {
159+ throw new IllegalStateException (PaymentPolicy .NOT_PAID );
160+ }
161+
162+ this .status = PaymentStatus .REFUNDED ;
163+ this .cancelledAt = cancelledAt ;
164+ }
165+
166+ public void markAsFailed () {
167+ if (this .status == PaymentStatus .PAID ) {
168+ throw new IllegalStateException (PaymentPolicy .CANNOT_FAIL_PAID );
169+ }
170+
171+ this .status = PaymentStatus .FAILED ;
172+ }
173+
174+ public boolean isPaid () {
175+ return this .status == PaymentStatus .PAID ;
176+ }
177+
178+ public boolean isRefundable () {
179+ return this .status == PaymentStatus .PAID ;
180+ }
181+
182+ public boolean isCancellable () {
183+ return this .status == PaymentStatus .PENDING ;
184+ }
185+ }
0 commit comments