PaymentCompletePage.js 3 KB
Newer Older
1
2
import axios from 'axios'
import { useEffect, useState } from 'react'
Kim, Subin's avatar
Kim, Subin committed
3
import { useAuth } from '../context/auth_context'
4
import catchErrors from '../utils/catchErrors'
한규민's avatar
한규민 committed
5
import reservationApi from '../apis/reservation.api'
6
7
8
9

const PaymentCompletePage = () => {
    const { user } = useAuth()
    const [error, setError] = useState()
Jiwon Yoon's avatar
Jiwon Yoon committed
10
11
    const [success, setSuccess] = useState(false)
    const [paymentData, setPaymentData] = useState()
Jiwon Yoon's avatar
Jiwon Yoon committed
12

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

Jiwon Yoon's avatar
Jiwon Yoon committed
25
    async function saveGuestReservation() {
26
        try {
Jiwon Yoon's avatar
Jiwon Yoon committed
27
            const response = await axios.get(`/api/auth/guestinfo/${user.id}`);
Jiwon Yoon's avatar
Jiwon Yoon committed
28
29
30
            const response2 = await reservationApi.findOneReservation()
            console.log("예매내역=====",response2)
            // if (response.data||response2) {
Jiwon Yoon's avatar
Jiwon Yoon committed
31
            //     const responseEmail = await axios.post('/api/email/send', {
Jiwon Yoon's avatar
Jiwon Yoon committed
32
            //         reservationData: response2,
Jiwon Yoon's avatar
Jiwon Yoon committed
33
34
35
36
37
38
39
40
            //         userData: { ...response.data },
            //         cinema: "Butter Studio 조치원",
            //         title: "더 수어사이드 스쿼드",
            //         theater: "1",
            //         time: "2021/07/21 10:00"
            //     })
            //     console.log(responseEmail.data)
            // }
41
42
43
44
45
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
46
    async function saveUserReservation() {
47
48
49
50
        try {
            const response = await axios.post(`/api/auth/getuserinfo`, {
                id: user.id
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
51

Jiwon Yoon's avatar
Jiwon Yoon committed
52
53
54
55
56
57
58
59
            // if (response.data) {
            //     const responseEmail = await axios.post('/api/email/send', {
            //         ...response2.data,
            //         ...response.data,

            //     })
            //     console.log(responseEmail.data)
            // }
60
61
62
63
64
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
65
66
    async function approveKakaopay(tid) {
        try {
Jiwon Yoon's avatar
Jiwon Yoon committed
67
68
69
70
71
72
73
74
75
76
            const urlParams = new URLSearchParams(window.location.search);
            const pg_token = urlParams.get('pg_token');
            const response = await axios.post(`/api/kakaopay/success`, {
                'tid': tid,
                cid: 'TC0ONETIME',
                partner_order_id: 'butter_studio',
                partner_user_id: '000000' + user.id,
                pg_token: pg_token
            })
            setPaymentData(response.data)
Jiwon Yoon's avatar
Jiwon Yoon committed
77
78
79
80
81
        } catch (error) {
            catchErrors(error, setError)
        }
    }

82
83
84
85
    return (
        <div className="text-center">
            <h3>예매가 정상적으로 완료되었습니다.</h3>
            <button>홈으로</button>
Kim, Subin's avatar
Kim, Subin committed
86
            {user.role === "member" ? <button>마이페이지</button> : <></>}
87
88
89
90
91
        </div>
    )
}

export default PaymentCompletePage