PaymentPage.js 11.4 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
import axios from 'axios'
import { useEffect, useState } from 'react'
Jiwon Yoon's avatar
Jiwon Yoon committed
3
import authApi from '../../apis/auth.api'
한규민's avatar
한규민 committed
4
import reservationApi from '../../apis/reservation.api'
Jiwon Yoon's avatar
Jiwon Yoon committed
5
6
7
8
9
import { useAuth } from '../../context/auth_context'
import catchErrors from '../../utils/catchErrors'
import styles from './PaymentPage.module.scss'

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

    useEffect(() => {
24
        if (user.role === "member") {
Jiwon Yoon's avatar
Jiwon Yoon committed
25
26
27
28
29
30
            getUserInfo()
        }
    }, [])

    async function getUserInfo() {
        try {
Kim, Subin's avatar
Kim, Subin committed
31
            setError("")
Jiwon Yoon's avatar
Jiwon Yoon committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
            const response = await axios.post(`/api/auth/getuserinfo`, {
                id: user.id
            })
            setUserInfo(response.data)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

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

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

58
59
60
61
62
63
64
65
66
67
    function kakaoBtnClick() {
        setElement(
            <div className="text-center">
                <p className=" font-weight-bold" style={{ display: 'inline', color: "#FEDC00" }}>'카카오페이'</p><p style={{ display: 'inline' }}>를 선택하셨습니다. </p>
                <p>결제하기를 눌러 결제를 이어가주세요.</p>
            </div>
        )
        setTicketInfo({ ...ticketInfo, payment: "카카오페이" })
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
68
69
    async function reservationComplete() {
        try {
Kim, Subin's avatar
Kim, Subin committed
70
            setError("")
71
            if (user.role === "member") {
한규민's avatar
한규민 committed
72
                const response = await reservationApi.save({
Jiwon Yoon's avatar
Jiwon Yoon committed
73
74
75
76
77
                    userType: "member",
                    user: userInfo.id,
                    ...ticketInfo,
                    timetable: 1
                })
78
79
                const responsekakao = await axios.post('/api/kakaopay/test/single', {
                    cid: 'TC0ONETIME',
Jiwon Yoon's avatar
Jiwon Yoon committed
80
81
                    partner_order_id: 'butter_studio',
                    partner_user_id: '000000'+ (userInfo.id || guestInfo.id),
82
83
84
85
86
87
88
89
90
91
92
                    item_name: ticketInfo.title,
                    quantity: ticketInfo.adult + ticketInfo.youth + ticketInfo.senior,
                    total_amount: ticketInfo.totalFee,
                    vat_amount: 0,
                    tax_free_amount: 0,
                    approval_url: 'http://localhost:3000/paymentcomplete',
                    fail_url: 'http://localhost:3000/ticket',
                    cancel_url: 'http://localhost:3000/ticket',
                })
                if (response && responsekakao) {
                    window.location.href = responsekakao.data.redirect_url
93
                }
Jiwon Yoon's avatar
Jiwon Yoon committed
94
95
            } else {
                if (guestID) {
한규민's avatar
한규민 committed
96
                    const response = await reservationApi.save({
Jiwon Yoon's avatar
Jiwon Yoon committed
97
98
99
                        userType: "guest",
                        user: guestID,
                        ...ticketInfo,
Jiwon Yoon's avatar
Jiwon Yoon committed
100
                        timetableId: 1
Jiwon Yoon's avatar
Jiwon Yoon committed
101
                    })
102
103
                    const responsekakao = await axios.post('/api/kakaopay/test/single', {
                        cid: 'TC0ONETIME',
Jiwon Yoon's avatar
Jiwon Yoon committed
104
105
                        partner_order_id: 'butter_studio',
                        partner_user_id: '000000'+ guestID,
106
                        item_name: ticketInfo.title,
Jiwon Yoon's avatar
Jiwon Yoon committed
107
                        item_code: ticketInfo.movieId,
108
109
110
111
112
113
114
115
                        quantity: ticketInfo.adult + ticketInfo.youth + ticketInfo.senior,
                        total_amount: ticketInfo.totalFee,
                        vat_amount: 0,
                        tax_free_amount: 0,
                        approval_url: 'http://localhost:3000/paymentcomplete',
                        fail_url: 'http://localhost:3000/ticket',
                        cancel_url: 'http://localhost:3000/ticket',
                    })
Jiwon Yoon's avatar
Jiwon Yoon committed
116
                    if (response||responsekakao) {
Jiwon Yoon's avatar
Jiwon Yoon committed
117
                        localStorage.setItem('tid',responsekakao.data.tid)
118
                        window.location.href = responsekakao.data.redirect_url
119
                    }
Jiwon Yoon's avatar
Jiwon Yoon committed
120
                } else {
121
                    alert("비회원 정보를 모두 입력 후 비회원 정보 저장 버튼을 눌러주세요.")
Jiwon Yoon's avatar
Jiwon Yoon committed
122
123
124
125
126
127
128
129
130
131
                }
            }
        } catch (error) {
            catchErrors(error, setError)
        }
    }


    return (
        <div className="container" style={{ color: "white" }}>
Jiwon Yoon's avatar
Jiwon Yoon committed
132
            {console.log(ticketInfo)}
Jiwon Yoon's avatar
Jiwon Yoon committed
133
134
135
136
137
138
139
            <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">
140
                    {user.role === "member"
Jiwon Yoon's avatar
Jiwon Yoon committed
141
142
                        ?
                        <div>
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
                            <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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
                        </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>
Jiwon Yoon's avatar
Jiwon Yoon committed
187
                                <input type="password" name="password" placeholder="비밀번호" onChange={handleChangeGuest} required style={{ width: "178px" }} />
Jiwon Yoon's avatar
Jiwon Yoon committed
188
189
190
191
192
193
194
195
196
197
                            </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>
198
199
200
201
202
                    <img src="/images/naverpay_button.png" style={{ width: "150px" }} />
                    <button onClick={kakaoBtnClick} style={{ backgroundColor: "black", border: '0' }}>
                        <img src="/images/payment_icon_yellow_medium.png" style={{ width: "130px" }} />
                    </button>
                    {element}
Jiwon Yoon's avatar
Jiwon Yoon committed
203
                    <div className="my-5">
204
                        <button className="btn btn-warning" type="button" style={{ width: "100%" }} onClick={reservationComplete}>결제하기</button>
Jiwon Yoon's avatar
Jiwon Yoon committed
205
206
207
208
209
210
211
212
213
214
215
216
                    </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.adult}</div>
217
218
219
                            <div>청소년: {ticketInfo.youth}</div>
                            <div>경로우대: {ticketInfo.senior}</div>
                            <div className="mt-2"> 결제금액: {ticketInfo.totalFee}</div>
Jiwon Yoon's avatar
Jiwon Yoon committed
220
221
222
223
224
225
226
227
228
229
                        </div>
                    </div>
                </div>
            </div>

        </div>
    )
}

export default Payment