Skip to content

Commit ec4e912

Browse files
authored
fix: 결제 페이지 이탈 시 나이스페이 결제 레이어 정리 (#422)
1 parent e116a15 commit ec4e912

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/pages/payment/Purchase.jsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from "axios";
22
import Cookies from "js-cookie";
3-
import { useEffect, useState, useContext } from "react";
3+
import { useCallback, useEffect, useRef, useState, useContext } from "react";
44
import { useLocation, useNavigate, useParams } from "react-router-dom";
55

66
import OnOffBtn from "../../components/button/OnOffBtn";
@@ -71,6 +71,7 @@ const Purchase = () => {
7171
const [buttonEnabled, setButtonEnabled] = useState(false);
7272

7373
const [isLoading, setIsLoading] = useState(false);
74+
const [isPayModalOpen, setIsPayModalOpen] = useState(false);
7475

7576
const { id } = useParams();
7677
const location = useLocation();
@@ -87,6 +88,26 @@ const Purchase = () => {
8788
// NICEPAY SDK 로딩 상태
8889
const { ready: nicepayReady, requestPay } = useNicepay();
8990

91+
const closeNicepayLayer = useCallback(() => {
92+
document.getElementById("newDivLayer")?.remove(); // 나이스페이가 body 밑에 직접 만든 div
93+
document.querySelectorAll("iframe").forEach((el) => el.remove());
94+
setIsPayModalOpen(false);
95+
}, []);
96+
97+
useEffect(() => {
98+
return () => {
99+
closeNicepayLayer();
100+
};
101+
}, [closeNicepayLayer]);
102+
103+
useEffect(() => {
104+
const onPopState = () => {
105+
if (isPayModalOpen) closeNicepayLayer();
106+
};
107+
window.addEventListener("popstate", onPopState);
108+
return () => window.removeEventListener("popstate", onPopState);
109+
}, [isPayModalOpen, closeNicepayLayer]);
110+
90111
useEffect(() => {
91112
if (name.length > 0 && name.trim() !== "") {
92113
setNameValid(true);
@@ -278,6 +299,8 @@ const Purchase = () => {
278299
"podo_payment_request",
279300
JSON.stringify(requestBody)
280301
);
302+
303+
setIsPayModalOpen(true);
281304

282305
requestPay({
283306
clientId: import.meta.env.VITE_NICEPAY_CLIENT_KEY,
@@ -292,11 +315,13 @@ const Purchase = () => {
292315
// ❗ '결제 요청을 취소'는 에러 취급 X → 조용히 종료(재시도 가능)
293316
if (msg.includes("결제 요청을 취소")) {
294317
setIsLoading(false);
318+
setIsPayModalOpen(false);
295319
return;
296320
}
297321
console.log("결제창 에러:", msg);
298322
alert(`결제창 에러: ${msg || "알 수 없는 오류"}`);
299323
setIsLoading(false);
324+
setIsPayModalOpen(false);
300325
},
301326
...vbankOptions,
302327
});

0 commit comments

Comments
 (0)