import axios from 'axios' import { useEffect, useState } from 'react' import authApi from '../../apis/auth.api' import kakaopayApi from '../../apis/kakaopay.api' import reservationApi from '../../apis/reservation.api' import { useAuth } from '../../context/auth_context' import catchErrors from '../../utils/catchErrors' import styles from './PaymentPage.module.scss' const Payment = ({ location }) => { const { user } = useAuth() const [guestInfo, setGuestInfo] = useState({ name: "", email: "", birth: "", phoneNumber: "", password: "", rePassword: "" }) const [errorMsg, setErrorMsg] = useState({ errorName: false, errorEmail: false, errorBirthday: false, errorMbnum: false, errorPassword: false, }) const [guestID, setGuestID] = useState() const [userInfo, setUserInfo] = useState({ nickname: "", email: "", birth: "", phoneNumber: "" }) const [ticketInfo, setTicketInfo] = useState({ ...location.state }) const [element, setElement] = useState() const [error, setError] = useState("") const [startTime, setStartTime] = useState(""); const [number, setNumber] = useState(null); const [loading, setLoading] = useState(false); const [mbError, setMbError] = useState(false); const [confirmMb, setConfirmMb] = useState(false); useEffect(() => { if (user.role === "member") { getUserInfo() } }, []) const getUserInfo = async () => { try { setError("") const response = await axios.post(`/api/auth/getuserinfo`, { id: user.id }) setUserInfo(response.data) } catch (error) { catchErrors(error, setError) } } const handleChangeGuest = (e) => { setGuestInfo({ ...guestInfo, [e.target.name]: String(e.target.value) }) if (e.target.name === "birth" || e.target.name === "phoneNumber") { setGuestInfo({ ...guestInfo, [e.target.name]: String(e.target.value) }) } } //인증번호 const handleOnClickMbnum = async (e) => { e.preventDefault(); try { setStartTime(""); setError(""); setLoading(true) const phone = guestInfo.phoneNumber; const message = await authApi.confirmMbnum(phone); if (message.isSuccess) { setMbError("보냄"); setStartTime(message.startTime); } } catch (error) { catchErrors(error, setError); } finally { setLoading(false); } } const handleOnChangeMb = (e) => { setNumber(String(e.target.value)); } const handleOnClickMbConfirm = async (e) => { e.preventDefault(); try { setError(""); setLoading(true); const confirmNum = { userMbnum: guestInfo.phoneNumber, number: number, startTime: startTime }; const message = await authApi.confirmNum(confirmNum); setMbError(message); if (message === "성공") { setConfirmMb(true); } } catch (error) { catchErrors(error, setError); } finally { setLoading(false); } } //비밀번호 확인 const validationPw = () => { if (guestInfo.password !== guestInfo.rePassword) return false; else return true; } const handleClickGuest = async () => { try { setError("") setLoading(true); let validPw = validationPw(); if (confirmMb) { if (validPw) { const response = await authApi.saveGuestInfo(guestInfo); if (response.id) { setGuestID(response.id) alert("비회원 정보가 저장되었습니다.") } else { setErrorMsg(response); alert("형식에 맞게 다시 작성해주세요"); } } else throw new Error("비밀번호가 일치하지 않습니다."); } else throw new Error("핸드폰 번호를 인증해주세요."); } catch (error) { catchErrors(error, setError) } } const kakaoBtnClick = () => { setElement(

'카카오페이'

를 선택하셨습니다.

결제하기를 눌러 결제를 이어가주세요.

) setTicketInfo({ ...ticketInfo, payment: "카카오페이" }) } const reservationComplete = async () => { try { setError("") if (user.role === "member") { const response = await reservationApi.save({ userType: "member", user: userInfo.id, ...ticketInfo, }) const responsekakao = await kakaopayApi.approveReq({ cid: 'TC0ONETIME', partner_order_id: 'butter_studio', partner_user_id: '000000' + guestID, item_name: ticketInfo.title, item_code: ticketInfo.movieId, 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/paymentfail', cancel_url: 'http://localhost:3000/paymentfail', }) if (response && responsekakao) { localStorage.setItem('tid', responsekakao.tid) window.location.href = responsekakao.redirect_url } } else { if (guestID) { const response = await reservationApi.save({ userType: "guest", user: guestID, ...ticketInfo, }) const responsekakao = await kakaopayApi.approveReq({ cid: 'TC0ONETIME', partner_order_id: 'butter_studio', partner_user_id: '000000' + guestID, item_name: ticketInfo.title, item_code: ticketInfo.movieId, 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/paymentfail', cancel_url: 'http://localhost:3000/paymentfail', }) if (response && responsekakao) { localStorage.setItem('tid', responsekakao.tid) window.location.href = responsekakao.redirect_url } } else { alert("비회원 정보를 모두 입력 후 비회원 정보 저장 버튼을 눌러주세요.") } } } catch (error) { catchErrors(error, setError) } } return (

결제하기

{user.role === "member" ?
회원정보

※ 회원정보 변경은 마이페이지에서 가능합니다.

:
비회원예매 정보입력
{errorMsg.errorName &&

이름을 입력해주세요

}
{errorMsg.errorEmail &&

이메일을 입력해주세요

}
{errorMsg.errorBirthday &&

숫자 6자리를 입력해주세요.

}
{errorMsg.errorMbnum &&

-없이 숫자 11자리를 입력해주세요.

}
{(mbError === "재전송") &&

유효시간이 만료되었습니다. 재전송해주세요.

} {(mbError === "보냄") &&

5분이내에 입력해주세요.

} {(mbError === "성공") &&

인증되었습니다.

} {(mbError === "실패") &&

인증번호를 다시 입력해주세요.

}
{errorMsg.errorPassword &&

8~11자리 사이로 입력해주세요.

}

※ 비회원 정보 오기입 시 예매 내역 확인/취소 및 티켓 발권이 어려울 수 있으니 다시 한번 확인해 주시기 바랍니다.

}
결제방법
{element}
영화포스터
{ticketInfo.title}
{ticketInfo.cinema}
{ticketInfo.time}
{ticketInfo.selectedTheater}관 {ticketInfo.selectedSeats.map(el => String.fromCharCode(parseInt(el.split('-')[0]) + 64) + el.split('-')[1]) + ' '}
성인: {ticketInfo.adult}명
청소년: {ticketInfo.youth}명
경로우대: {ticketInfo.senior}명
총 결제금액: {ticketInfo.totalFee}원
) } export default Payment