PaymentPage.js 10.1 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
import axios from 'axios'
import { useEffect, useState } from 'react'
3
import { useHistory } from 'react-router-dom'
Jiwon Yoon's avatar
Jiwon Yoon committed
4
5
6
7
8
9
import Kakaopay from '../../components/Kakaopay'
import { useAuth } from '../../context/auth_context'
import catchErrors from '../../utils/catchErrors'
import styles from './PaymentPage.module.scss'

const Payment = ({ location }) => {
10
    const history = useHistory();
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
    const [ticketInfo, setTicketInfo] = useState({ ...location.state })
    const [error, setError] = useState("")
13
14
15
16
17
18
    const [userInfo, setUserInfo] = useState({
        nickname: "",
        email: "",
        birth: "",
        phoneNumber: ""
    })
Jiwon Yoon's avatar
Jiwon Yoon committed
19
20
21
22
23
24
    const [guestInfo, setGuestInfo] = useState({})
    const [guestID, setGuestID] = useState()
    const { user } = useAuth()

    useEffect(() => {
        console.log(user.id)
25
        if (user.role === "member") {
Jiwon Yoon's avatar
Jiwon Yoon committed
26
27
28
29
30
31
32
33
34
            getUserInfo()
        }
    }, [])

    async function getUserInfo() {
        try {
            const response = await axios.post(`/api/auth/getuserinfo`, {
                id: user.id
            })
35
            console.log(response.data)
Jiwon Yoon's avatar
Jiwon Yoon committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
            setUserInfo(response.data)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    function handleChangeGuest(e) {
        setGuestInfo({ ...guestInfo, [e.target.name]: String(e.target.value) })
    }

    async function handleClickGuest() {
        try {
            const response = await axios.post('/api/auth/guest/save', {
                ...guestInfo
            })
            setGuestID(response.data.id)
52
            alert("비회원 정보가 저장되었습니다.")
Jiwon Yoon's avatar
Jiwon Yoon committed
53
54
55
56
57
58
59
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    async function reservationComplete() {
        try {
60
            if (user.id > 0) {
Jiwon Yoon's avatar
Jiwon Yoon committed
61
62
63
64
65
66
67
                const response = await axios.post(`/api/reservation/save`, {
                    userType: "member",
                    user: userInfo.id,
                    ...ticketInfo,
                    payment: "카카오페이",
                    timetable: 1
                })
68
69
70
71
72
73
74
75
76
77
                if (response.data) {
                    const responseEmail = await axios.post('/api/email/send', {
                        ...ticketInfo,
                        ...userInfo,
                        ...guestInfo,
                    })
                    console.log("이메일전송완료===", responseEmail.data)
                    alert("예매가 완료되었습니다.")
                    history.push('/')
                }
Jiwon Yoon's avatar
Jiwon Yoon committed
78
79
80
81
82
83
84
85
86
            } else {
                if (guestID) {
                    const response = await axios.post(`/api/reservation/save`, {
                        userType: "guest",
                        user: guestID,
                        ...ticketInfo,
                        payment: "카카오페이",
                        timetable: 1
                    })
87
88
89
90
91
92
93
94
95
                    if (response.data) {
                        const responseEmail = await axios.post('/api/email/send', {
                            ...ticketInfo,
                            ...userInfo,
                            ...guestInfo,
                        })
                        console.log("이메일전송완료===", responseEmail.data)
                    }
                    alert("예매가 완료되었습니다.")
Jiwon Yoon's avatar
Jiwon Yoon committed
96
                    console.log("비회원예매완료===", response.data)
97
                    history.push('/')
Jiwon Yoon's avatar
Jiwon Yoon committed
98
                } else {
99
                    alert("비회원 정보를 모두 입력 후 비회원 정보 저장 버튼을 눌러주세요.")
Jiwon Yoon's avatar
Jiwon Yoon committed
100
101
                }
            }
102

Jiwon Yoon's avatar
Jiwon Yoon committed
103
104
105
106
107
108
109
110
111
112
        } catch (error) {
            catchErrors(error, setError)
        }
    }


    return (
        <div className="container" style={{ color: "white" }}>
            {console.log(ticketInfo)}
            {console.log(userInfo)}
113
            {/* {console.log(guestInfo)} */}
Jiwon Yoon's avatar
Jiwon Yoon committed
114
115
116
117
118
119
120
            <div className="row justify-content-center my-5">
                <div className="col-sm-4 ">
                    <h3 className="py-2 text-white text-center" style={{ border: "3px solid #000000", borderBottom: "3px solid #FEDC00" }}>결제하기</h3>
                </div>
            </div>
            <div className="row justify-content-center">
                <div className="col-sm-8 text-center">
121
                    {user.role === "member"
Jiwon Yoon's avatar
Jiwon Yoon committed
122
123
                        ?
                        <div>
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
                            <h5 className="mb-4 p-2" style={{ backgroundColor: "white", color: "black" }}>회원정보</h5>
                            <div className="my-1">
                                <label className={styles.labelStyle}>이름</label>
                                <input type="text" name="name" placeholder="이름" value={userInfo.nickname} />
                            </div>
                            <div className="my-1">
                                <label className={styles.labelStyle}>이메일</label>
                                <input type="email" name="email" placeholder="이메일" value={userInfo.email} />
                            </div>
                            <div className="my-1">
                                <label className={styles.labelStyle}>생년월일</label>
                                <input type="number" name="birth" placeholder="생년월일" maxLength="6" value={userInfo.birth} />
                            </div>
                            <div className="my-1">
                                <label className={styles.labelStyle}>휴대폰 번호</label>
                                <input type="number" name="phoneNumber" placeholder="휴대폰 번호" maxLength="11" value={userInfo.phoneNumber} />
                            </div>
                            <div className="m-2">
                                <p className={`text-muted ${styles.warningText}`}>
                                     회원정보 변경은 마이페이지에서 가능합니다.
                                </p>
                            </div>
Jiwon Yoon's avatar
Jiwon Yoon committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
                        </div>
                        :
                        <div>
                            <h5 className="mb-4 p-2" style={{ backgroundColor: "white", color: "black" }}>비회원예매 정보입력</h5>
                            <div className="my-1">
                                <label className={styles.labelStyle}>이름</label>
                                <input type="text" name="name" placeholder="이름" onChange={handleChangeGuest} required />
                            </div>
                            <div className="my-1">
                                <label className={styles.labelStyle}>이메일</label>
                                <input type="email" name="email" placeholder="이메일" onChange={handleChangeGuest} required />
                            </div>
                            <div className="my-1">
                                <label className={styles.labelStyle}>생년월일</label>
                                <input type="number" name="birth" placeholder="생년월일" onChange={handleChangeGuest} maxLength="6" required />
                            </div>
                            <div className="my-1">
                                <label className={styles.labelStyle}>휴대폰 번호</label>
                                <input type="number" name="phoneNumber" placeholder="휴대폰 번호" onChange={handleChangeGuest} maxLength="11" required />
                            </div>
                            <div className="my-1">
                                <label className={styles.labelStyle}>비밀번호</label>
                                <input type="password" name="guestPassword" placeholder="비밀번호" onChange={handleChangeGuest} required style={{ width: "178px" }} />
                            </div>
                            <div className="m-2">
                                <p className={`text-muted ${styles.warningText}`}>
                                     비회원 정보 오기입  예매 내역 확인/취소  티켓 발권이 어려울  있으니 다시 한번 확인해 주시기 바랍니다.
                                </p>
                            </div>
                            <button className="btn btn-warning mb-3" type="button" style={{ width: "100%" }} onClick={handleClickGuest}>비회원 정보 저장</button>
                        </div>
                    }
                    <h5 className="my-4 p-2" style={{ backgroundColor: "white", color: "black" }}>결제방법</h5>
179
                    <img src="/images/naverpay_button.png" style={{width:"150px"}} />
Jiwon Yoon's avatar
Jiwon Yoon committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
                    <Kakaopay ticketInfo={ticketInfo} setTicketInfo={setTicketInfo} />
                    <div className="my-5">
                        <button className="btn btn-warning" type="button" style={{ width: "100%" }} onClick={reservationComplete}>결제완료</button>
                    </div>
                </div>

                <div className="col-sm-4">
                    <div className="text-center rounded-3 p-3" style={{ backgroundColor: "#252525" }}>
                        <img style={{ maxHeight: "10rem" }} src={`https://image.tmdb.org/t/p/original${ticketInfo.poster_path}`} alt="영화포스터" />
                        <h5 className="my-3">{ticketInfo.title}</h5>
                        <div>{ticketInfo.cinema}</div>
                        <div>{ticketInfo.time}</div>
                        <div className="mb-3">{ticketInfo.selectedTheater} {ticketInfo.selectedSeats.map(el => String.fromCharCode(parseInt(el.split('-')[0]) + 65) + el.split('-')[1]) + ' '}</div>
                        <div className="rounded-3 p-3" style={{ backgroundColor: '#404040' }}>
                            <div>청소년: {ticketInfo.teenager}</div>
                            <div>성인: {ticketInfo.adult}</div>
                            <div>경로우대: {ticketInfo.elderly}</div>
                            <div> 결제금액: {ticketInfo.teenager * 7000 + ticketInfo.adult * 8000 + ticketInfo.elderly * 6000}</div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    )
}

export default Payment