kakaopay.controller.js 1.45 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
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
import fetch from 'node-fetch'
import config from "../config.js";

const success = (req, res) => {
    console.log('kakaopay success route')
    console.log('req body:', req.body)
    return res.json({
        message: 'Success'
    })
}

const fail = (req, res) => {
    console.log('kakaopay fail route')
    console.log('req body:', req.body)
    return res.json({
        message: 'Failed'
    })
}

const cancel = (req, res) => {
    console.log('kakaopay cancel route')
    console.log('req body:', req.body)
    return res.json({
        message: 'Canceled'
    })
}

const singleTest = async (req, res) => {
    console.log("asdaf")
    console.log(req.body)
    const item = req.body
    const data = []
    for (let property in item) {
        let encodedKey = encodeURIComponent(property);
        let encodedValue = encodeURIComponent(item[property]);
        data.push(encodedKey + "=" + encodedValue);
    }
    const bodyData = data.join('&') // encode data (application/x-www-form-urlencoded)
    const response = await fetch('https://kapi.kakao.com/v1/payment/ready', {
        method: 'POST',
        headers: {
            'Authorization': `KakaoAK ${config.kakaoAdminKey}`,
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        },
        body: bodyData,
Jiwon Yoon's avatar
Jiwon Yoon committed
46
    })  
Jiwon Yoon's avatar
Jiwon Yoon committed
47
48
49
50
51
    const resp = await response.json()
    console.log(resp)
    res.json({redirect_url: resp.next_redirect_pc_url})
}

kusang96's avatar
kusang96 committed
52
export default { success, fail, cancel, singleTest }