-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.js
More file actions
109 lines (93 loc) · 3.57 KB
/
temp.js
File metadata and controls
109 lines (93 loc) · 3.57 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
var express = require('express');
var mysql = require('mysql');
const bodyParser = require('body-parser');
const request = require('request');
const cors = require('cors');
const router = express.Router();
const app = express();
var db = mysql.createConnection({
host:'localhost',
user:'root',
password:'network629',
database:'test'
});
db.connect();
app.use(cors());
// app.use(bodyParser.urlencoded({ extended: false }));
router.get('/', cors(), (req, res) => { res.send('cors!') });
app.get('/', function (req, res) {
db.query(`SELECT * FROM topic`, function(error,topics){
var option = {
method: "POST",
uri: 'https://kapi.kakao.com/v1/payment/ready',
form: {
cid: "TC0ONETIME",
partner_order_id: "partner_order_id",
partner_user_id: "partner_user_id",
item_name: "초코파이",
quantity: 1,
total_amount: 2200,
vat_amount: 200,
tax_free_amount: 0,
approval_url: "http://localhost:3000/auth",
fail_url: "http://localhost:3000/fail",
cancel_url: "http://localhost:3000/cancel"
},
headers: {
'Authorization': 'KakaoAK a1355d29b2de8657244d9548bf1a4ea9',
'Content-Type': 'application/json;charset=UTF-8', //'application/x-www-form-urlencoded;charset=utf-8'
},
};
request(option, function (err, response) {
if (err) throw err;
//console.log(typeof(response.body)); // string type 확인
var object = response.body.split(','); // next_redirect_pc_url 부분 커팅
var temp_tid = object[0].split(':');
tid = temp_tid[1].slice(1, -1);
// console.log(tid); // tid 확인
var address = object[4].split(':');
var temp_url = address[2].slice(0, -1);
var next_redirect_pc_url = 'https:' + temp_url;
//console.log(next_redirect_pc_url); // 주소 확인
res.redirect(next_redirect_pc_url);
});
});
});
app.get('/auth', function (req, res) {
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
//console.log(fullUrl); // 요청된 url 확인
console.log(req.query.pg_token); // pg_token 확인
var t = tid;
console.log(t);
var option = {
method: "POST",
uri: 'https://kapi.kakao.com/v1/payment/approve',
form: {
cid: "TC0ONETIME",
tid: t,
partner_order_id: "partner_order_id",
partner_user_id: "partner_user_id",
pg_token: req.query.pg_token,
},
headers: {
'Authorization': 'KakaoAK a1355d29b2de8657244d9548bf1a4ea9',
'Content-Type': 'application/json;charset=UTF-8', //'application/x-www-form-urlencoded;charset=utf-8'
},
};
request(option, function (err, response) {
if (err) throw err;
res.redirect('/success');
});
});
app.get('/success', function(req, res){
res.send('결제가 완료되었습니다.');
})
app.get('/fail', function(req, res){
res.send('결제 실패입니다. 다시 결제 요청해주세요.');
});
app.get('/cancel', function(req, res){
res.send('결제가 취소되었습니다.');
});
app.listen(3000, function () {
console.log('서버 실행');
});