PaymentCompletePage.js 3.51 KB
Newer Older
1
2
3
4
5
import axios from 'axios'
import { useAuth } from '../context/auth_context'
import { useEffect, useState } from 'react'

import catchErrors from '../utils/catchErrors'
한규민's avatar
한규민 committed
6
import reservationApi from '../apis/reservation.api'
7
8
9
10
11


const PaymentCompletePage = () => {
    const { user } = useAuth()
    const [error, setError] = useState()
Jiwon Yoon's avatar
Jiwon Yoon committed
12

13
14
15
16
17
    useEffect(() => {
        if (user.role === "member") {
            getUserInfo()
        } else {
            getGuestInfo()
Jiwon Yoon's avatar
Jiwon Yoon committed
18
19
            const tid = localStorage.getItem('tid')
            approveKakaopay(tid)
20
21
22
23
24
25
        }
    }, [user])

    async function getGuestInfo() {
        try {
            if (user.id > 0) {
한규민's avatar
한규민 committed
26
27
                const response = await axios.get(`/api/auth/guestinfo/${user.id}`);
                const guest = {
28
29
                    userType: "guest",
                    user: user.id
한규민's avatar
한규민 committed
30
31
                };
                const response2 = await reservationApi.findOneReservation(guest);
32
33
34
35
36
37
38
39
40
41
42
                console.log({
                    reservationData: [...response2.data],
                    userData: { ...response.data },
                })
                if (response.data || response2.data) {
                    const responseEmail = await axios.post('/api/email/send', {
                        reservationData: [...response2.data],
                        userData: { ...response.data },
                        cinema: "Butter Studio 조치원",
                        title: "더 수어사이드 스쿼드",
                        theater: "1",
Jiwon Yoon's avatar
Jiwon Yoon committed
43
                        time: "2021/07/21 10:00"
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
                    })
                    console.log(responseEmail.data)
                }

                console.log(response.data)
            }
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    async function getUserInfo() {
        try {
            const response = await axios.post(`/api/auth/getuserinfo`, {
                id: user.id
            })
한규민's avatar
한규민 committed
60
            const member = {
61
62
                userType: "member",
                user: user.id
한규민's avatar
한규민 committed
63
64
            }
            const response2 = await reservationApi.findOneReservation(member);
65
66
67
68
69
            console.log(response2.data)
            if (response.data || response2.data) {
                const responseEmail = await axios.post('/api/email/send', {
                    ...response2.data,
                    ...response.data,
Jiwon Yoon's avatar
Jiwon Yoon committed
70

71
72
73
74
75
76
77
78
                })
                console.log(responseEmail.data)
            }
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
79
80
81
82
    async function approveKakaopay(tid) {
        const urlParams = new URLSearchParams(window.location.search);
        const pg_token = urlParams.get('pg_token');
        try {
Jiwon Yoon's avatar
Jiwon Yoon committed
83
            if (user.id > 0) {
Jiwon Yoon's avatar
Jiwon Yoon committed
84
85
86
87
88
                console.log(user.id)
                const response = await axios.post(`/api/kakaopay/success`, {
                    'tid': tid,
                    cid: 'TC0ONETIME',
                    partner_order_id: 'butter_studio',
Jiwon Yoon's avatar
Jiwon Yoon committed
89
                    partner_user_id: '000000' + user.id,
Jiwon Yoon's avatar
Jiwon Yoon committed
90
91
92
93
94
95
96
97
98
                    pg_token: pg_token
                })
                console.log(response.data)
            }
        } catch (error) {
            catchErrors(error, setError)
        }
    }

99
100
101
102
103
104
105
106
107
108
109
110
    return (
        <div className="text-center">
            <h3>예매가 정상적으로 완료되었습니다.</h3>
            <button>홈으로</button>
            {
                user.role === "member" ? <button>마이페이지</button> : <></>
            }
        </div>
    )
}

export default PaymentCompletePage