-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.html
261 lines (247 loc) · 9.01 KB
/
sample.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!-- https://developers.portone.io/docs/ko/auth/guide/3 -->
<!DOCTYPE html>
<html lang="en">
<head>
<!-- jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<!-- iamport.payment.js -->
<script type="text/javascript" src="https://cdn.iamport.kr/v1/iamport.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
var IMP = window.IMP;
IMP.init("impXXXXXXXX");
async function getUserId() {
try {
const res = await axios({
url: "/user",
method: "post",
});
return res.data.id;
} catch(err) {
console.log(err);
}
}
async function getMerchantUid(user_id, amount) {
try {
const res = await axios({
url: "/order",
method: "post",
headers: { "Content-Type": "application/json" },
data: { amount, user_id },
});
return res.data.merchant_uid;
} catch(err) {
console.log(err);
}
}
async function getCustomerUid(pg, user_id, period) {
try {
const res = await axios({
url: "/card",
method: "post",
headers: { "Content-Type": "application/json" },
data: { pg, period, user_id },
});
return res.data.customer_uid;
} catch(err) {
console.log(err);
}
}
async function requestPay() {
const amount = 1004;
const userId = await getUserId();
const merchantUid = await getMerchantUid(userId, amount);
console.log({ merchantUid });
IMP.request_pay(
{
pg: "nice.{상점 ID}", // https://developers.portone.io/docs/ko/pg/payment-gateway/nice
pay_method: "card",
merchant_uid: merchantUid,
name: "당근 10kg",
amount,
buyer_email: "Iamport@chai.finance",
buyer_name: "포트원 기술지원팀",
buyer_tel: "010-1234-5678",
buyer_addr: "서울특별시 강남구 삼성동",
buyer_postcode: "123-456",
m_redirect_url: "https://d4e7-222-106-224-225.ngrok-free.app/portone-webhook", // https://developers.portone.io/docs/ko/auth/guide/4/redirect
},
// https://developers.portone.io/docs/ko/auth/guide/6
async requestPayResponse => {
const { success, error_msg, ...restUid } = requestPayResponse;
if (!success) {
alert(`결제에 실패하였습니다. 에러 내용: ${error_msg}`);
return;
}
// 이전 단계에서 구현한 결제정보 사후 검증 API 호출
const res = await axios({
url: "/payments/complete",
method: "post",
headers: { "Content-Type": "application/json" },
data: { ...restUid },
});
console.log({ ...res.data });
switch (res.data.status) {
case "vbankIssued":
// 가상계좌 발급 시 로직
alert(res.data.message);
break;
case "success":
// 결제 성공 시 로직
alert(res.data.message);
break;
}
}
);
}
async function requestBillingKeyofTosspay() {
const period = 60;
const userId = await getUserId();
const customerUid = await getCustomerUid("tosspay", userId, period);
console.log({ customerUid });
IMP.request_pay(
{
pg: "tosspayments.{MID}", // https://developers.portone.io/docs/ko/pg/payment-gateway/newtoss/readme#3-%EA%B2%B0%EC%A0%9C-%EC%9A%94%EC%B2%AD%ED%95%98%EA%B8%B0, 비인증 결제창 요청
pay_method: "card", // "card"만 지원됩니다.
name: "최초인증결제",
amount: 0, // 실제 승인은 발생되지 않고 오직 빌링키만 발급됩니다.
customer_uid: customerUid, // 필수 입력.
buyer_email: "test@portone.io",
buyer_name: "포트원",
buyer_tel: "02-1234-1234",
m_redirect_url: "https://d4e7-222-106-224-225.ngrok-free.app/portone-webhook", // https://developers.portone.io/docs/ko/auth/guide/4/redirect
customer_id: userId, // 가맹점이 회원에게 부여한 고유 ID
},
// https://developers.portone.io/docs/ko/auth/guide-1/bill/pg
async rsp => {
console.log({ ...rsp });
const { success, error_msg, ...restUid } = rsp;
if (rsp.success) {
const res = await axios({
url: "/billings/complete",
method: "post",
headers: { "Content-Type": "application/json" },
data: { user_id: userId, ...restUid },
});
console.log({ ...res.data });
alert("빌링키 발급 성공");
} else {
alert("빌링키 발급 실패");
}
}
);
}
async function requestPayWithBillingKey(pg, currency) {
const { data: { user_id, customer_uid } } = await axios({
url: "/billings",
method: "get",
params: {
pg,
}
});
const res = await axios({
url: "/billings",
method: "post",
headers: { "Content-Type": "application/json" },
data: { user_id, customer_uid, currency },
});
console.log({ ...res.data });
if (res.data.status === "success") {
alert("빌링키 결제 성공");
} else {
alert("빌링키 결제 실패");
}
}
async function requestBillingKeyOfPaypal() {
const period = 60;
const userId = await getUserId();
const merchantUid = await getMerchantUid(userId, 0);
const customerUid = await getCustomerUid("paypal", userId, period);
console.log({ customerUid });
// https://developers.portone.io/docs/ko/pg/payment-gateway/rt
IMP.loadUI(
"paypal-rt",
{
pg: "paypal_v2", // https://developers.portone.io/docs/ko/pg/payment-gateway/rt#loadui-%EC%9A%94%EC%B2%AD-%EA%B0%9D%EC%B2%B4
name: "카카오페이로 등록",
pay_method: "paypal",
merchant_uid: merchantUid, // 주문 번호
customer_uid: customerUid, // [필수 입력] 빌링키와 1:1 매핑되는 UUID
customer_id: userId, // 빌링키 발급 수단을 특정하는 고유 번호
notice_url: "https://d4e7-222-106-224-225.ngrok-free.app/portone-webhook",
custom_data: {
merchant_uid: merchantUid,
customer_uid: customerUid
}
},
async rsp => {
console.log({ ...rsp });
const { imp_uid, merchant_uid, customer_uid } = rsp;
if (merchant_uid === merchantUid && customer_uid === customerUid) {
const res = await axios({
url: "/billings/complete",
method: "post",
headers: { "Content-Type": "application/json" },
data: { user_id: userId, ...rsp, status: 'paid' },
});
console.log({ ...res.data });
alert("빌링키 발급 성공");
} else {
alert("빌링키 발급 실패");
}
}
);
}
async function schedulePay() {
const { data: { user_id, customer_uid } } = await axios({
url: "/billings",
method: "get",
params: {
pg: "tosspay"
}
});
const res = await axios({
url: "/payments/schedule",
method: "post",
headers: { "Content-Type": "application/json" },
data: { user_id, customer_uid },
});
console.log({ ...res.data });
if (res.data.status === "success") {
alert("빌링키 예약 성공");
} else {
alert("빌링키 예약 실패");
}
}
</script>
<meta charset="UTF-8">
<title>Sample Payment</title>
</head>
<body>
<table>
<th></th>
<th>PG사</th>
<tr>
<th>인증결제</th>
<th>nice</th>
<td><button onclick="requestPay()">결제 요청</button></td>
</tr>
<tr>
<th rowspan="2">비 인증결제</th>
<th>tosspay</th>
<td><button onclick="requestBillingKeyofTosspay()">빌링키 요청</button></td>
<td><button onclick="requestPayWithBillingKey('tosspay', 'KRW')">결제 요청</button></td>
<td><button onclick="schedulePay()">1분 후 결제 예약</button></td>
</tr>
<tr>
<th>paypal</th>
<td><button onclick="requestBillingKeyOfPaypal()">빌링키 요청</button></td>
<td><button onclick="requestPayWithBillingKey('paypal', 'USD')">결제 요청</button></td>
</tr>
</table>
<!-- https://developers.portone.io/docs/ko/pg/payment-gateway/rt#%ED%8E%98%EC%9D%B4%ED%8C%94-rt-%EB%B2%84%ED%8A%BC%EC%9D%B4-%EB%B3%B4%EC%9D%B4%EC%A7%80-%EC%95%8A%EC%9D%84-%EB%95%8C -->
<div class="portone-ui-container" data-portone-ui-type="paypal-rt">
<!-- 3. 여기에 페이팔 버튼이 생성됩니다. -->
</div>
</body>
</html>