diff --git a/src/components/ImmediateCreditsStages/CardRegistration.js b/src/components/ImmediateCreditsStages/CardRegistration.js index 3846be1..9543f7c 100644 --- a/src/components/ImmediateCreditsStages/CardRegistration.js +++ b/src/components/ImmediateCreditsStages/CardRegistration.js @@ -113,7 +113,7 @@ const CardRegistration = ({ borrowerData, setStage, setPaymentLink, setPaymentDe setPaymentDetails(jsonedResponse); - // setStage(13); + setStage(13); setIsLoading(false); } catch (error) { @@ -327,7 +327,9 @@ const CardRegistration = ({ borrowerData, setStage, setPaymentLink, setPaymentDe type="submit" className="sign-btn cu-btn" disabled={ - workPlace.length < 3 || workPosition.length < 3 || address.length < 3 ? true : false + workPlace.length < 3 || workPosition.length < 3 || address.length < 3 || isLoading + ? true + : false } onClick={onSubmit}>
diff --git a/src/components/ImmediateCreditsStages/PaymentDetails.js b/src/components/ImmediateCreditsStages/PaymentDetails.js index fd71947..27333cd 100644 --- a/src/components/ImmediateCreditsStages/PaymentDetails.js +++ b/src/components/ImmediateCreditsStages/PaymentDetails.js @@ -1,5 +1,5 @@ // IMPORT MODULES -import React, { useState, useContext } from 'react'; +import React, { useState, useContext, useEffect } from 'react'; import { LanguageContext } from '../../backend/LanguageContext'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; @@ -7,6 +7,7 @@ import { zodResolver } from '@hookform/resolvers/zod'; // IMPORT IMAGES import next from '../../icons/next.svg'; import next_reverse from '../../icons/next-reverse.svg'; +import CardDetails from './CardDetails'; // import arrow from "../../icons/arrow.svg"; const schema = z.object({ @@ -14,25 +15,92 @@ const schema = z.object({ // surname: z.string().min(1), }); -const PaymentDetails = ({ - setStage, - setPaymentCard, - paymentCard, - setPaymentCv, - paymentCv, - setPaymentCardDuration, - paymentCardDuration, -}) => { +const PaymentDetails = ({ setStage, paymentDetails, borrowerData, setSumbitCardDetails }) => { const { locale } = useContext(LanguageContext); + const [invalidData, setInvalidData] = useState(false); + const [timeLeft, setTimeLeft] = useState(paymentDetails['remaining-time']); + + const [paymentName, setPaymentName] = useState(''); + const [paymentCard, setPaymentCard] = useState(''); + const [paymentCv, setPaymentCv] = useState(''); + const [paymentCardDurationYear, setPaymentCardDurationYear] = useState(''); + const [paymentCardDurationMonth, setPaymentCardDurationMonth] = useState(''); + const handlePaymentCard = (e) => { setPaymentCard(e.target.value); }; + const handlePaymentName = (e) => { + setPaymentName(e.target.value.toUpperCase()); + }; const handlePaymentCv = (e) => { setPaymentCv(e.target.value); }; - const handlePaymentCardDuration = (e) => { - setPaymentCardDuration(e.target.value); + const handlePaymentCardDurationYear = (e) => { + setPaymentCardDurationYear(e.target.value); + }; + const handlePaymentCardDurationMonth = (e) => { + setPaymentCardDurationMonth(e.target.value); + }; + + useEffect(() => { + const interval = setInterval(() => { + if (timeLeft > 0) { + setTimeLeft(timeLeft - 1); + } + }, 1000); + + return () => clearInterval(interval); + }, [timeLeft]); + + const sendPaymentRequest = async () => { + try { + const headers = { + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', + }; + + const formData = new URLSearchParams(); + formData.append('app', 'sanlykarz'); + formData.append('id', borrowerData.idNo); + formData.append('md-order', paymentDetails['md-order']); + formData.append('card-number', paymentCard.toString()); + formData.append( + 'card-expiry', + '20' + paymentCardDurationYear.toString() + paymentCardDurationMonth.toString(), + ); + formData.append('name-on-card', paymentName); + formData.append('card-cvc', paymentCv.toString()); + + const response = await fetch('https://shahsyotag.halkbank.gov.tm/api/v1/submit-card', { + method: 'POST', + headers: headers, + body: formData.toString(), + }); + + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + + const jsonedResponse = await response.json(); + + if (jsonedResponse.status === 'other-error') { + setInvalidData(true); + } else if (jsonedResponse.status === 'ok') { + setSumbitCardDetails(jsonedResponse); + setInvalidData(false); + } else { + setInvalidData(false); + } + + console.log(jsonedResponse); + + // setStage(13); + + // setIsLoading(false); + } catch (error) { + console.error(error.toString()); + // setError(true); + } }; const { @@ -44,9 +112,13 @@ const PaymentDetails = ({ }); const onSubmit = (data) => { - console.log(data); + sendPaymentRequest(); }; + if (timeLeft <= 0) { + setStage(12); + } + return (
@@ -59,7 +131,32 @@ const PaymentDetails = ({ ? 'Loan calculator' : 'Karzyň kalkulýatory'} +
+

Сумма к оплате: {paymentDetails['amount-info']}

+

+ Оставшееся время: {Math.floor((timeLeft % 3600) / 60)}:{timeLeft % 60} +

+
+
+ + + {/* {errors.code && {errors.code.message}} */} +
+ {paymentDetails['is-cvc-required'] && ( +
+ + + {/* {errors.code && {errors.code.message}} */} +
+ )} +
-
-
+ {invalidData && ( +
+

Ошибкаб проверьте данные

+
+ )}
-