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

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

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

Jiwon Yoon's avatar
Jiwon Yoon committed
27
    async function saveGuestReservation() {
28
        try {
29
            setError("")
Jiwon Yoon's avatar
Jiwon Yoon committed
30
            const response = await axios.get(`/api/auth/guestinfo/${user.id}`);
Jiwon Yoon's avatar
Jiwon Yoon committed
31
            const response2 = await reservationApi.findOneReservation()
32
            const response3 = await reservationApi.saveTid({ tid: localStorage.getItem('tid')})
Jiwon Yoon's avatar
Jiwon Yoon committed
33
34
35
36
37
38
39
40
41
            if (response.data || response2) {
                const responseEmail = await axios.post('/api/email/send', {
                    reservationData: response2.map(el => { return { "row": el.row, "col": el.col } }),
                    userData: { ...response.data },
                    cinema: "Butter Studio 조치원",
                    title: response2[0].title,
                    theater: response2[0].theater.theaterName,
                    time: response2[0].timetable.date.split('T')[0] + ' ' + moment(response2[0].timetable.start_time).format('HH:mm')
                })
42
                localStorage.removeItem('tid')
Jiwon Yoon's avatar
Jiwon Yoon committed
43
            }
44
45
46
47
48
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
49
    async function saveUserReservation() {
50
        try {
51
            setError("")
52
53
54
            const response = await axios.post(`/api/auth/getuserinfo`, {
                id: user.id
            })
55
56
57
58
59
60
61
62
63
64
65
66
67
            const response2 = await reservationApi.findOneReservation()
            const response3 = await reservationApi.saveTid({ tid: localStorage.getItem('tid')})
            if (response.data || response2) {
                const responseEmail = await axios.post('/api/email/send', {
                    reservationData: response2.map(el => { return { "row": el.row, "col": el.col } }),
                    userData: { ...response.data },
                    cinema: "Butter Studio 조치원",
                    title: response2[0].title,
                    theater: response2[0].theater.theaterName,
                    time: response2[0].timetable.date.split('T')[0] + ' ' + moment(response2[0].timetable.start_time).format('HH:mm')
                })
                localStorage.removeItem('tid')
            }
68
69
70
71
72
        } catch (error) {
            catchErrors(error, setError)
        }
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
73
74
    async function approveKakaopay(tid) {
        try {
75
            setError("")
Jiwon Yoon's avatar
Jiwon Yoon committed
76
77
            const urlParams = new URLSearchParams(window.location.search);
            const pg_token = urlParams.get('pg_token');
78
            const response = await kakaopayApi.approveSuccess({
Jiwon Yoon's avatar
Jiwon Yoon committed
79
80
81
82
83
84
85
                '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
86
87
88
89
90
        } catch (error) {
            catchErrors(error, setError)
        }
    }

91
    return (
92
93
94
95
96
97
98
99
100
101
102
103
        <div className="container text-center py-5">
            <h3 className="my-3 text-white">예매가 정상적으로 완료되었습니다.</h3>
            {user.role === "member"
                ?
                <a href="/mypage">
                    <button className="btn btn-warning mx-1">마이페이지</button>
                </a>
                :
                <a href="/">
                    <button className="btn btn-warning mx-1">홈으로</button>
                </a>
            }
104
105
106
107
108
        </div>
    )
}

export default PaymentCompletePage