diff --git a/src/pages/shop/section/productList/components/ModalBuy.tsx b/src/pages/shop/section/productList/components/ModalBuy.tsx index 69ead53..976283c 100644 --- a/src/pages/shop/section/productList/components/ModalBuy.tsx +++ b/src/pages/shop/section/productList/components/ModalBuy.tsx @@ -45,6 +45,8 @@ const ModalBuy = ({ isOpen, setIsOpen, configId }: ModalBuyProps) => { }); const [price, setPrice] = useState(0); const [isInfoOpen, setIsInfoOpen] = useState(false); + const [needsShahkarVerification, setNeedsShahkarVerification] = useState(false); + const [nationalCode, setNationalCode] = useState(""); const payablePrice = toNumber( price === 0 ? data?.data?.result?.price || 0 : price ); @@ -99,6 +101,8 @@ const ModalBuy = ({ isOpen, setIsOpen, configId }: ModalBuyProps) => { setFieldValue("count", 1); setFieldValue("discount", ""); setPrice(0); + setNeedsShahkarVerification(false); + setNationalCode(""); } }, [isOpen, setFieldValue]); const mutation = useMutation({ @@ -190,10 +194,23 @@ const ModalBuy = ({ isOpen, setIsOpen, configId }: ModalBuyProps) => { return; } - console.log(formik.values); - toast.promise(mutation.mutateAsync({ ...formik.values, status: "buy" }), { - loading: "درحال انتقال به درگاه پرداخت...", + if (needsShahkarVerification && nationalCode.trim().length !== 10) { + toast.error("لطفا کد ملی ۱۰ رقمی را به‌درستی وارد کنید"); + return; + } + + const payload = { + ...formik.values, + status: "buy", + ...(needsShahkarVerification ? { nationalCode: nationalCode.trim() } : {}), + }; + + toast.promise(mutation.mutateAsync(payload), { + loading: needsShahkarVerification + ? "درحال احراز هویت شاهکار..." + : "درحال انتقال به درگاه پرداخت...", success: (res: AxiosResponse) => { + setNeedsShahkarVerification(false); window.location.href = res?.data?.result?.payment?.action; return `${res.data.alert}`; }, @@ -226,16 +243,26 @@ const ModalBuy = ({ isOpen, setIsOpen, configId }: ModalBuyProps) => { requiresAdvancedVerificationFromApi || (err.response?.status === 422 && errorMessage.includes("احراز هویت پیشرفته")); - - if (isAdvancedVerificationError) { + + const requiresShahkarVerificationFromApi = + isResponseObject && + "requires_shahkar_verification" in responseData && + Boolean( + (responseData as { requires_shahkar_verification?: boolean }) + .requires_shahkar_verification + ); + + if (requiresShahkarVerificationFromApi) { + setNeedsShahkarVerification(true); + } else if (isAdvancedVerificationError) { goToAdvancedVerification(); } - + if (err.response?.status === 500) { dispatch(setIsPaymentGateway(false)); navigate("/noactive-payment"); } - + return errorMessage; }, }); @@ -331,6 +358,24 @@ const ModalBuy = ({ isOpen, setIsOpen, configId }: ModalBuyProps) => { {advancedVerificationMessage} )} + {needsShahkarVerification && ( +
+ + برای خریدهای با مبلغ بالا، احراز هویت شاهکار (تطبیق شماره + موبایل حساب شما با کد ملی) الزامی است. لطفا کد ملی خود را + وارد کنید. + + + setNationalCode(e.target.value.replace(/[^0-9]/g, "")) + } + /> +
+ )} {data?.data?.result?.max_config === 0 ? ( <>