Skip to content

Commit 4b12bf7

Browse files
authored
[feat] 토스페이먼츠 위젯 적용 (v2) (#284)
* [fix] : checkstyle 적용 * [fix] : checkstyle 적용 * [fix] : 엔티티, DTO 수정 * [fix] : 서비스 로직 수정 * [fix] : 로그 제거 * [db] : Payment, V2_Order 엔티티 - flyway 관련 마이그래이션 파일 추가 * [db] : Payment, V2_Order 엔티티 - flyway 관련 마이그래이션 파일 수정
1 parent d1b8a7c commit 4b12bf7

8 files changed

Lines changed: 196 additions & 15 deletions

File tree

backend/src/main/java/com/back/api/notification/controller/NotificationController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ApiResponse<List<NotificationResponseDto>> getNotifications(
3333
List<NotificationResponseDto> notifications =
3434
notificationService.getNotifications(userId);
3535

36-
return ApiResponse.ok("알림 목록을 불러왔습니다",notifications);
36+
return ApiResponse.ok("알림 목록을 불러왔습니다", notifications);
3737
}
3838

3939
/**
@@ -45,7 +45,7 @@ public ApiResponse<UnreadCountResponseDto> getUnreadCount(
4545
) {
4646
Long userId = httpRequestContext.getUserId();
4747
long count = notificationService.getUnreadCount(userId);
48-
return ApiResponse.ok("읽지 않은 알림수",new UnreadCountResponseDto(count));
48+
return ApiResponse.ok("읽지 않은 알림수", new UnreadCountResponseDto(count));
4949
}
5050

5151
/**
@@ -59,7 +59,7 @@ public ApiResponse<Void> markAsRead(
5959
Long userId = httpRequestContext.getUserId();
6060
notificationService.markAsRead(notificationId, userId);
6161

62-
return ApiResponse.ok("개별 알림을 읽음 처리 하였습니다.",null);
62+
return ApiResponse.ok("개별 알림을 읽음 처리 하였습니다.", null);
6363
}
6464

6565
/**
@@ -70,6 +70,6 @@ public ApiResponse<Void> markAsRead(
7070
public ApiResponse<Void> markAllAsRead() {
7171
Long userId = httpRequestContext.getUserId();
7272
notificationService.markAllAsRead(userId);
73-
return ApiResponse.ok("모든 알림을 읽음 처리 하였습니다.",null);
73+
return ApiResponse.ok("모든 알림을 읽음 처리 하였습니다.", null);
7474
}
7575
}

backend/src/main/java/com/back/api/payment/order/controller/OrderController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ApiResponse<OrderResponseDto> createOrder(@RequestBody OrderRequestDto or
2727
Long userId = httpRequestContext.getUser().getId();
2828

2929
OrderResponseDto orderResponseDto = orderService.createOrder(orderRequestDto, userId);
30-
return ApiResponse.ok("주문이 생성되었습니다",orderResponseDto);
30+
return ApiResponse.ok("주문이 생성되었습니다", orderResponseDto);
3131
}
3232

3333
@PostMapping("/v2/orders/prepare")
@@ -37,6 +37,6 @@ public ApiResponse<V2_OrderResponseDto> v2_createOrder(@RequestBody OrderRequest
3737

3838
V2_OrderResponseDto orderResponseDto = orderService.v2_createOrder(orderRequestDto, userId);
3939

40-
return ApiResponse.ok("주문이 생성되었습니다",orderResponseDto);
40+
return ApiResponse.ok("주문이 생성되었습니다", orderResponseDto);
4141
}
4242
}
Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,57 @@
11
package com.back.api.payment.payment.dto.response;
22

3+
import java.time.LocalDateTime;
4+
5+
import com.back.domain.payment.order.entity.V2_Order;
6+
import com.back.domain.seat.entity.SeatGrade;
7+
8+
import lombok.extern.slf4j.Slf4j;
9+
10+
@Slf4j
311
public record V2_PaymentConfirmResponse(
412
String orderId,
5-
boolean success
13+
boolean success,
14+
long amount,
15+
LocalDateTime paidAt,
16+
String paymentMethod,
17+
long ticketId,
18+
String eventTitle,
19+
String eventPlace,
20+
LocalDateTime eventDate,
21+
String seatCode,
22+
SeatGrade seatGrade
623
) {
24+
/**
25+
* V2_Order로부터 응답 객체 생성
26+
* - 변수 추출로 중복 접근 방지 (getTicket() 1번만 호출)
27+
* - N+1 쿼리 방지를 위해 order는 fetch join 필수
28+
*/
29+
public static V2_PaymentConfirmResponse from(V2_Order order, boolean success) {
30+
var ticket = order.getTicket();
31+
var event = ticket.getEvent();
32+
var seat = ticket.getSeat();
33+
var payment = order.getPayment();
34+
35+
return new V2_PaymentConfirmResponse(
36+
order.getOrderId(),
37+
success,
38+
order.getAmount(),
39+
ticket.getCreateAt(),
40+
payment.getMethod(),
41+
ticket.getId(),
42+
event.getTitle(),
43+
event.getPlace(),
44+
event.getEventDate(),
45+
seat.getSeatCode(),
46+
seat.getGrade()
47+
);
48+
}
49+
50+
/**
51+
* 간단한 응답 생성 (orderId와 성공 여부만)
52+
* - 결제 확인 중복 요청 등에서 사용
53+
*/
754
public V2_PaymentConfirmResponse(String orderId, boolean success) {
8-
this.orderId = orderId;
9-
this.success = success;
55+
this(orderId, success, 0L, null, null, 0L, null, null, null, null, null);
1056
}
1157
}

backend/src/main/java/com/back/api/payment/payment/service/PaymentService.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,12 @@ public V2_PaymentConfirmResponse v2_confirmPayment(
141141

142142
// OrderService가 order의 정합성(주문자/주문상태/amount) 보장
143143
V2_Order order = orderService.v2_getOrderForPayment(orderId, userId, clientAmount);
144-
log.info("[v2 결제 디버깅] - 결제 승인 메서드: 결제 서비스 로그");
145-
log.info("[v2 결제 디버깅] - orderId : {}", orderId);
146-
log.info("[v2 결제 디버깅] - paymentKey : {}", paymentKey);
147-
log.info("[v2 결제 디버깅] - userId : {}", userId);
144+
145+
// log.info("[v2 결제 디버깅] - 결제 승인 메서드: 결제 서비스 로그");
146+
// log.info("[v2 결제 디버깅] - orderId : {}", orderId);
147+
// log.info("[v2 결제 디버깅] - paymentKey : {}", paymentKey);
148+
// log.info("[v2 결제 디버깅] - userId : {}", userId);
149+
148150
V2_PaymentConfirmRequest request = new V2_PaymentConfirmRequest(orderId, paymentKey, order.getAmount());
149151

150152
TossPaymentResponse result = tossPaymentService.confirmPayment(request);
@@ -169,6 +171,8 @@ public V2_PaymentConfirmResponse v2_confirmPayment(
169171
)
170172
);
171173

174+
order.setPayment(savedPayment);
175+
172176
// Order status PENDING -> PAID, paymentKey DB 저장 (주문 상태 업테이트)
173177
order.markPaid(result.paymentKey());
174178

@@ -199,6 +203,6 @@ public V2_PaymentConfirmResponse v2_confirmPayment(
199203
)
200204
);
201205

202-
return new V2_PaymentConfirmResponse(orderId, true);
206+
return V2_PaymentConfirmResponse.from(order, true);
203207
}
204208
}

backend/src/main/java/com/back/domain/payment/order/entity/V2_Order.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.UUID;
66

7+
import com.back.domain.payment.payment.entity.Payment;
78
import com.back.domain.ticket.entity.Ticket;
89
import com.back.global.entity.BaseEntity;
910

@@ -22,12 +23,14 @@
2223
import lombok.Builder;
2324
import lombok.Getter;
2425
import lombok.NoArgsConstructor;
26+
import lombok.Setter;
2527

2628
@Entity
2729
@Table(name = "v2_orders")
2830
@NoArgsConstructor(access = AccessLevel.PROTECTED)
2931
@AllArgsConstructor
3032
@Getter
33+
@Setter
3134
@Builder
3235
public class V2_Order extends BaseEntity {
3336
@Id
@@ -48,6 +51,10 @@ public class V2_Order extends BaseEntity {
4851
@Column(name = "payment_key", nullable = true)
4952
private String paymentKey; // TossPaymentKey
5053

54+
@JoinColumn(name = "payment_id", nullable = true)
55+
@OneToOne(fetch = FetchType.LAZY)
56+
private Payment payment;
57+
5158
@PrePersist
5259
public void generateOrderId() {
5360
if (this.orderId == null) {

backend/src/main/java/com/back/domain/payment/payment/entity/Payment.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package com.back.domain.payment.payment.entity;
22

3+
import com.back.global.entity.BaseEntity;
4+
35
import jakarta.persistence.Column;
46
import jakarta.persistence.Entity;
57
import jakarta.persistence.EnumType;
68
import jakarta.persistence.Enumerated;
79
import jakarta.persistence.GeneratedValue;
810
import jakarta.persistence.GenerationType;
911
import jakarta.persistence.Id;
12+
import lombok.Getter;
1013
import lombok.NoArgsConstructor;
1114

1215
@Entity
16+
@Getter
1317
@NoArgsConstructor
14-
public class Payment {
18+
public class Payment extends BaseEntity {
1519
@Id
1620
@Column(name = "payment_id", nullable = false)
1721
@GeneratedValue(strategy = GenerationType.IDENTITY)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* =========================================================
2+
* 1. Table: payment
3+
* ========================================================= */
4+
CREATE TABLE IF NOT EXISTS payment (
5+
payment_id BIGSERIAL PRIMARY KEY,
6+
payment_key VARCHAR(255) NOT NULL,
7+
order_id VARCHAR(255) NOT NULL,
8+
amount BIGINT NOT NULL,
9+
method VARCHAR(50) NOT NULL,
10+
status VARCHAR(20) NOT NULL,
11+
12+
created_at TIMESTAMP NULL,
13+
modified_at TIMESTAMP NULL
14+
);
15+
16+
17+
/* =========================================================
18+
* 2. Alter: payment - add created_at (BaseEntity 상속 추가)
19+
* ========================================================= */
20+
ALTER TABLE payment
21+
ADD COLUMN IF NOT EXISTS created_at TIMESTAMP NULL;
22+
23+
24+
/* =========================================================
25+
* 3. Alter: payment - add modified_at (BaseEntity 상속 추가)
26+
* ========================================================= */
27+
ALTER TABLE payment
28+
ADD COLUMN IF NOT EXISTS modified_at TIMESTAMP NULL;
29+
30+
31+
/* =========================================================
32+
* 4. Index: payment_key (조회 성능)
33+
* ========================================================= */
34+
CREATE INDEX IF NOT EXISTS idx_payment_payment_key
35+
ON payment(payment_key);
36+
37+
38+
/* =========================================================
39+
* 5. Index: order_id (조회 성능)
40+
* ========================================================= */
41+
CREATE INDEX IF NOT EXISTS idx_payment_order_id
42+
ON payment(order_id);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* =========================================================
2+
* 1. Table: v2_orders
3+
* ========================================================= */
4+
CREATE TABLE IF NOT EXISTS v2_orders (
5+
v2_order_id VARCHAR(36) PRIMARY KEY,
6+
ticket_id BIGINT NOT NULL,
7+
amount BIGINT NOT NULL,
8+
status VARCHAR(20) NOT NULL DEFAULT 'PENDING',
9+
payment_key VARCHAR(255) NULL,
10+
payment_id BIGINT NULL,
11+
12+
created_at TIMESTAMP NULL,
13+
modified_at TIMESTAMP NULL
14+
);
15+
16+
17+
/* =========================================================
18+
* 2. Alter: v2_orders - add payment_id (신규 컬럼 추가)
19+
* ========================================================= */
20+
ALTER TABLE v2_orders
21+
ADD COLUMN IF NOT EXISTS payment_id BIGINT NULL;
22+
23+
24+
/* =========================================================
25+
* 3. Foreign Key: v2_orders.ticket_id → ticket.ticket_id
26+
* ========================================================= */
27+
DO $$
28+
BEGIN
29+
IF NOT EXISTS (
30+
SELECT 1
31+
FROM pg_constraint
32+
WHERE conname = 'fk_v2_order_ticket'
33+
) THEN
34+
ALTER TABLE v2_orders
35+
ADD CONSTRAINT fk_v2_order_ticket
36+
FOREIGN KEY (ticket_id)
37+
REFERENCES ticket(ticket_id);
38+
END IF;
39+
END $$;
40+
41+
42+
/* =========================================================
43+
* 3. Foreign Key: v2_orders.payment_id → payment.payment_id
44+
* ========================================================= */
45+
DO $$
46+
BEGIN
47+
IF NOT EXISTS (
48+
SELECT 1
49+
FROM pg_constraint
50+
WHERE conname = 'fk_v2_order_payment'
51+
) THEN
52+
ALTER TABLE v2_orders
53+
ADD CONSTRAINT fk_v2_order_payment
54+
FOREIGN KEY (payment_id)
55+
REFERENCES payment(payment_id);
56+
END IF;
57+
END $$;
58+
59+
60+
/* =========================================================
61+
* 4. Index: v2_orders.ticket_id (조회 성능)
62+
* ========================================================= */
63+
CREATE INDEX IF NOT EXISTS idx_v2_orders_ticket_id
64+
ON v2_orders(ticket_id);
65+
66+
67+
/* =========================================================
68+
* 5. Index: v2_orders.payment_id (조회 성능)
69+
* ========================================================= */
70+
CREATE INDEX IF NOT EXISTS idx_v2_orders_payment_id
71+
ON v2_orders(payment_id);
72+
73+
74+
/* =========================================================
75+
* 6. Index: v2_orders.payment_key (조회 성능)
76+
* ========================================================= */
77+
CREATE INDEX IF NOT EXISTS idx_v2_orders_payment_key
78+
ON v2_orders(payment_key);

0 commit comments

Comments
 (0)