|
7 | 7 | </head> |
8 | 8 | <body> |
9 | 9 | <h1>아임포트 결제 테스트 페이지</h1> |
10 | | - <button id="payment-button">결제하기</button> |
| 10 | + <button id="kakao-button">카카오페이 결제</button> |
| 11 | + <button id="tosspay-button">네이버페이 결제</button> |
11 | 12 |
|
12 | 13 | <script> |
13 | 14 | const IMP = window.IMP; |
14 | | - IMP.init("<%= impKey %>"); |
| 15 | + IMP.init("<%= impKey %>"); // 서버에서 impKey 주입 |
15 | 16 |
|
16 | | - document.getElementById("payment-button").addEventListener("click", () => { |
| 17 | + // 공통 결제 요청 함수 |
| 18 | + function requestPayment(pgName) { |
17 | 19 | IMP.request_pay( |
18 | 20 | { |
19 | | - pg: "kakaopay", // 테스트용 PG사 선택 |
| 21 | + pg: pgName, // kakaopay 또는 naverpay |
20 | 22 | pay_method: "card", |
21 | 23 | merchant_uid: "order_no_" + new Date().getTime(), // 주문번호 |
22 | 24 | name: "테스트 상품", |
23 | | - amount: 1200, // 테스트 결제 금액 |
| 25 | + amount: 1200, // 금액 |
24 | 26 | |
25 | 27 | buyer_name: "테스트 사용자", |
26 | 28 | buyer_tel: "010-1234-5678", |
27 | 29 | }, |
28 | 30 | function (rsp) { |
29 | 31 | if (rsp.success) { |
30 | | - // 결제 성공 시 백엔드 검증 요청 |
31 | 32 | console.log("결제 성공", rsp); |
32 | 33 |
|
33 | 34 | fetch("http://localhost:3000/api/payments/complete", { |
|
38 | 39 | body: JSON.stringify({ |
39 | 40 | impUid: rsp.imp_uid, |
40 | 41 | merchantUid: rsp.merchant_uid, |
41 | | - productId: 1, // productId |
42 | | - userId: 3, // userId |
43 | | - requestId: 1, // requestId |
| 42 | + productId: 1, |
| 43 | + userId: 3, |
| 44 | + requestId: 1, |
44 | 45 | }), |
45 | 46 | }) |
46 | 47 | .then((res) => res.json()) |
47 | 48 | .then((data) => { |
48 | 49 | console.log("백엔드 검증 결과:", data); |
49 | | - console.log(rsp.merchant_uid); |
50 | | - alert("결제 및 검증 성공", rsp.merchant_uid); |
| 50 | + alert("결제 및 검증 성공: " + rsp.merchant_uid); |
51 | 51 | }) |
52 | 52 | .catch((err) => { |
53 | 53 | console.error(err); |
|
59 | 59 | } |
60 | 60 | } |
61 | 61 | ); |
| 62 | + } |
| 63 | +
|
| 64 | + // 버튼 이벤트 등록 |
| 65 | + document.getElementById("kakao-button").addEventListener("click", () => { |
| 66 | + requestPayment("kakaopay"); |
| 67 | + }); |
| 68 | +
|
| 69 | + document.getElementById("tosspay-button").addEventListener("click", () => { |
| 70 | + requestPayment("tosspay"); |
62 | 71 | }); |
63 | 72 | </script> |
64 | 73 | </body> |
|
0 commit comments