kakaopay.controller.js 2.67 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
3
import axios from 'axios'
import config from "../config/app.config.js";

4
const success = async (req, res) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    try {
        // const { cid, tid, partner_order_id, partner_user_id, pg_token } = 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('&')
        const response = await axios.post('https://kapi.kakao.com/v1/payment/approve', bodyData, {
            headers: {
                'Authorization': `KakaoAK ${config.kakaoAdminKey}`,
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
            },
        })
        const resp = response.data
        console.log('resp', resp)
23
        res.json({ ...resp })
Jiwon Yoon's avatar
Jiwon Yoon committed
24
25
26
    } catch (error) {
        console.log(error)
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
29
30
31
32
33
34
}

const fail = (req, res) => {
    return res.json({
        message: 'Failed'
    })
}

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const cancel = async  (req, res) => {
    try {
        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('&')
        const response = await axios.post('https://kapi.kakao.com/v1/payment/cancel', bodyData, {
            headers: {
                'Authorization': `KakaoAK ${config.kakaoAdminKey}`,
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
            },
        })
        const resp = response.data
        res.json(resp)
    } catch (error) {
        console.log(error)
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
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
}

const singleTest = async (req, res) => {
    try {
        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('&')
        const response = await axios.post('https://kapi.kakao.com/v1/payment/ready', bodyData, {
            headers: {
                'Authorization': `KakaoAK ${config.kakaoAdminKey}`,
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
            },
        })
        const resp = response.data
        res.json({ redirect_url: resp.next_redirect_pc_url })
    } catch (error) {
        console.log(error)
    }
}

export default { success, fail, cancel, singleTest }