-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpayment.js
More file actions
159 lines (135 loc) · 4.6 KB
/
payment.js
File metadata and controls
159 lines (135 loc) · 4.6 KB
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
IMP.init('imp73337601');
let orderNumber = 1;
let currentDate = new Date().getDate();
const scheduleSeatId = new URLSearchParams(window.location.search).get('schedule-seat-id');
let musicalName;
let musicalPrice;
window.onload = function () {
fetch("https://qquickqqueue.store/api/schedule-seat/" + scheduleSeatId, {
method: "GET",
headers: {
"Content-Type": "application/json",
"ACCESS-TOKEN": getAccessTokenFromCookie()
}
}).then(Response => {
return Response.json();
})
.then(Data => {
const data = Data.data;
musicalName = data.name;
musicalPrice = data.price;
})
}
function requestPay() {
const tokenInfo = parseJwt(getAccessTokenFromCookie());
let merchant_uid = generateMerchantUID();
IMP.request_pay({
pg: "html5_inicis",
pay_method: "card",
merchant_uid: merchant_uid,
name: musicalName,
amount: musicalPrice,
buyer_email: tokenInfo.email,
buyer_name: tokenInfo.name,
buyer_tel: tokenInfo.phoneNumber,
buyer_addr: $("#address").val() + " " + $("#detailAddress").val(),
buyer_postcode: $("#postcode").val()
}, function (rsp) {
if (rsp.success) {
requestCreateTicket()
} else {
var msg = '결제에 실패하였습니다.';
msg += '에러내용 : ' + rsp.error_msg;
alert(msg);
}
});
}
function requestCreateTicket(rsp) {
const requestScheduleSeatId = {
scheduleSeatId: scheduleSeatId
}
fetch("https://qquickqqueue.store/api/tickets", {
method: "POST",
headers: {
"Content-Type": "application/json",
"ACCESS-TOKEN": getAccessTokenFromCookie()
},
body: JSON.stringify(requestScheduleSeatId)
}).then(Response => {
return Response.json();
})
.then(Data => {
var msg = '결제가 완료되었습니다.';
alert(msg);
window.close();
location.href = "mypage.html";
})
}
function parseJwt(token) {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
};
function generateMerchantUID() {
const formattedTimestamp = getFormattedTimestamp();
const paddedOrderNumber = orderNumber.toString().padStart(6, '0');
const merchantUID = currentDate + formattedTimestamp + paddedOrderNumber;
const newDate = new Date().getDate();
if (newDate !== currentDate) {
currentDate = newDate;
orderNumber = 1;
} else {
orderNumber++;
}
return merchantUID;
}
function getFormattedTimestamp() {
const today = new Date();
const hours = String(today.getHours()).padStart(2, '0');
const minutes = String(today.getMinutes()).padStart(2, '0');
const seconds = String(today.getSeconds()).padStart(2, '0');
return hours + minutes + seconds;
}
function getAccessTokenFromCookie() {
const cookies = document.cookie.split(';');
for (const cookie of cookies) {
const [name, value] = cookie.trim().split('=');
if (name === 'access_token') {
return value;
}
}
return null;
}
function postcode() {
new daum.Postcode({
oncomplete: function (data) {
var addr = '';
var extraAddr = '';
if (data.userSelectedType === 'R') {
addr = data.roadAddress;
} else {
addr = data.jibunAddress;
}
if (data.userSelectedType === 'R') {
if (data.bname !== '' && /[동|로|가]$/g.test(data.bname)) {
extraAddr += data.bname;
}
if (data.buildingName !== '' && data.apartment === 'Y') {
extraAddr += (extraAddr !== '' ? ', ' + data.buildingName : data.buildingName);
}
if (extraAddr !== '') {
extraAddr = ' (' + extraAddr + ')';
}
document.getElementById("extraAddress").value = extraAddr;
} else {
document.getElementById("extraAddress").value = '';
}
document.getElementById('postcode').value = data.zonecode;
document.getElementById("address").value = addr;
document.getElementById("detailAddress").focus();
}
}).open();
}