import { ChangeEvent, useState } from 'react'; import { PATHS } from '@/shared/constants/paths'; import { useLocation } from 'react-router'; import { useNavigate } from '@/shared/lib/hooks/use-navigate'; import { IMAGE_ROOT } from '@/shared/constants/common'; import { HeaderType } from '@/entities/common/model/types'; import { useExtensionKeyinApplyMutation } from '@/entities/additional-service/api/use-extension-keyin-apply-mutation'; import { useSetHeaderTitle, useSetHeaderType, useSetFooterMode, useSetOnBack } from '@/widgets/sub-layout/use-sub-layout'; import { overlay } from 'overlay-kit'; import { Dialog } from '@/shared/ui/dialogs/dialog'; import { useStore } from '@/shared/model/store'; export const KeyInPaymentRequestPage = () => { const { navigate } = useNavigate(); const location = useLocation(); const userMid = useStore.getState().UserStore.mid; const [mid, setMid] = useState(userMid || ''); const [goodsName, setGoodsName] = useState(''); const [amount, setAmount] = useState(0); const [buyerName, setBuyerName] = useState(''); const [email, setEmail] = useState(''); const [phoneNumber, setPhoneNumber] = useState(''); const [cardNo1, setCardNo1] = useState(''); const [cardNo2, setCardNo2] = useState(''); const [cardNo3, setCardNo3] = useState(''); const [cardNo4, setCardNo4] = useState(''); const [cardExpirationMonth, setCardExpirationMonth] = useState(''); const [cardExpirationYear, setCardExpirationYear] = useState(''); const [instmntMonth, setInstmntMonth] = useState('00'); const [moid, setMoid] = useState(''); const { mutateAsync: keyInApply } = useExtensionKeyinApplyMutation(); useSetHeaderTitle('KEY-IN 결제'); useSetHeaderType(HeaderType.LeftArrow); useSetFooterMode(false); useSetOnBack(() => { navigate(PATHS.additionalService.keyInPayment.list); }); const resetForm = () => { setGoodsName(''); setAmount(0); setBuyerName(''); setEmail(''); setPhoneNumber(''); setCardNo1(''); setCardNo2(''); setCardNo3(''); setCardNo4(''); setCardExpirationMonth(''); setCardExpirationYear(''); setInstmntMonth('00'); setMoid(''); }; const callKeyInPaymentRequest = () => { const cardNo = `${cardNo1}${cardNo2}${cardNo3}${cardNo4}`; const cardExpirationDate = `${cardExpirationMonth}${cardExpirationYear}`; let keyInApplyParams = { mid: mid, goodsName: goodsName, amount: amount, buyerName: buyerName, email: email, phoneNumber: phoneNumber, cardNo: cardNo, cardExpirationDate: cardExpirationDate, instmntMonth: instmntMonth, moid: moid, }; keyInApply(keyInApplyParams).then((rs) => { console.log('결제 응답:', rs); if (rs.status) { // 성공: 화면 유지 & 입력 내용 초기화 showSuccessDialog(); resetForm(); } else { // 실패: 화면 유지 & 입력 내용 유지 showErrorDialog('결제에 실패했습니다. 입력 내용을 확인해주세요.'); } }).catch((error) => { console.error('결제 실패:', error); showErrorDialog(error?.message || '결제 요청 중 오류가 발생했습니다'); }); }; const showSuccessDialog = () => { overlay.open(({ isOpen, close, unmount }) => { return ( ); }); }; const showErrorDialog = (errorMessage: string) => { overlay.open(({ isOpen, close, unmount }) => { return ( ); }); }; const isValidPhoneNumber = (phone: string) => { const phoneRegex = /^01[0|1|6|7|8|9][0-9]{7,8}$/; return phoneRegex.test(phone); }; const isValidEmail = (email: string) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; const isValidCardNumber = () => { return cardNo1.length === 4 && cardNo2.length === 4 && cardNo3.length === 4 && cardNo4.length === 4; }; const isValidCardExpiration = () => { if (cardExpirationMonth.length !== 2 || cardExpirationYear.length !== 2) { return false; } const month = parseInt(cardExpirationMonth); return month >= 1 && month <= 12; }; const isFormValid = () => { return ( mid.trim() !== '' && goodsName.trim() !== '' && amount > 0 && buyerName.trim() !== '' && isValidEmail(email) && isValidPhoneNumber(phoneNumber) && isValidCardNumber() && isValidCardExpiration() ); }; const onClickToRequest = () => { callKeyInPaymentRequest(); }; return ( <>
가맹점 *
상품명 *
) => setGoodsName(e.target.value)} />
상품가격 *
) => { const onlyNumbers = e.target.value.replace(/[^0-9]/g, ''); // 숫자만 남김 setAmount(onlyNumbers === '' ? 0 : parseInt(onlyNumbers, 10)); }} inputMode="numeric" // 모바일 키보드 숫자 전용 pattern="[0-9]*" // 브라우저 기본 숫자만 유효하도록 />
구매자명 *
) => setBuyerName(e.target.value)} />
구매자 이메일 *
) => setEmail(e.target.value)} className={email && !isValidEmail(email) ? 'error' : ''} />
구매자 전화번호 *
) => { const onlyNumbers = e.target.value.replace(/[^0-9]/g, ''); setPhoneNumber(onlyNumbers); }} className={phoneNumber && !isValidPhoneNumber(phoneNumber) ? 'error' : ''} inputMode="numeric" pattern="[0-9]*" maxLength={11} />
카드번호 *
) => { const onlyNumbers = e.target.value.replace(/[^0-9]/g, ''); if (onlyNumbers.length <= 4) setCardNo1(onlyNumbers); }} inputMode="numeric" pattern="[0-9]*" maxLength={4} placeholder="1234" />
) => { const onlyNumbers = e.target.value.replace(/[^0-9]/g, ''); if (onlyNumbers.length <= 4) setCardNo2(onlyNumbers); }} inputMode="numeric" pattern="[0-9]*" maxLength={4} placeholder="5678" />
) => { const onlyNumbers = e.target.value.replace(/[^0-9]/g, ''); if (onlyNumbers.length <= 4) setCardNo3(onlyNumbers); }} inputMode="numeric" pattern="[0-9]*" maxLength={4} placeholder="9012" />
) => { const onlyNumbers = e.target.value.replace(/[^0-9]/g, ''); if (onlyNumbers.length <= 4) setCardNo4(onlyNumbers); }} inputMode="numeric" pattern="[0-9]*" maxLength={4} placeholder="3456" />
유효기간(월/년)*
) => { const onlyNumbers = e.target.value.replace(/[^0-9]/g, ''); if (onlyNumbers.length <= 2) setCardExpirationMonth(onlyNumbers); }} inputMode="numeric" pattern="[0-9]*" maxLength={2} placeholder='MM' style={{ flex: 1 }} /> / ) => { const onlyNumbers = e.target.value.replace(/[^0-9]/g, ''); if (onlyNumbers.length <= 2) setCardExpirationYear(onlyNumbers); }} inputMode="numeric" pattern="[0-9]*" maxLength={2} placeholder='YY' style={{ flex: 1 }} />
할부 기간*
주문번호
) => setMoid(e.target.value)} />
) }