import React, { useState, useEffect } from 'react'; import { Image, NativeModules, Platform, ScrollView } from 'react-native'; import { Center, VStack } from 'native-base'; import { MainTemplate, WarningModal } from '~components'; import NfcManager from 'react-native-nfc-manager'; import { Button, Text } from '~common-components'; import CryptoJS from 'crypto-js'; import { useFocusEffect, useNavigation, useRoute, NavigationProp, RouteProp, } from '@react-navigation/native'; import { useDispatch, useSelector } from 'react-redux'; import { NFCIcon } from '~assets/icons'; import { NFCCardImage } from '~assets/images'; import { Buffer } from 'buffer'; import { sha1 } from 'react-native-sha1'; import dayjs from 'dayjs'; import { getPaymentInfo } from '~store/actions'; import { RootState } from '~store/reducer'; import { clientRefCode } from '~App'; import { formatAmountForBackend, formatAmountForBackendSecondVersion, } from '~common/format'; import { IPaymentInstallmentRequest, IPaymentPayRequest, } from '~services/interfaces'; import { PaymentServices } from '~services'; import { loaderActions } from '~store/actions/loaderActions/loaderActions'; import { StackNavigationProp } from '@react-navigation/stack'; type RootStackParamList = { NFCPaySecondStep: { amount: string }; NFCPaySuccess: { amount: string; referenceCode: any; tempData: any; }; NFCPayFail: { amount: string; referenceCode: any; tempData: any; }; }; type NFCPaySecondStepRouteProp = RouteProp; type NFCPaySecondStepNavigationProp = StackNavigationProp; export const NFCPaySecondStep = () => { const isIos = Platform.OS === 'ios'; const [isNFCSupported, setIsNFCSupported] = useState(false); const [isModalOpened, setIsModalOpened] = useState(false); const [isNfcWarnModalOpened, setIsNfcWarnModalOpened] = useState(false); const route = useRoute(); const amount = route.params.amount; const dispatch = useDispatch(); const navigation = useNavigation(); // const navigation = useNavigation(); const { NFCReaderModule } = NativeModules; const paymentInfo = useSelector( (state: RootState) => state.payment.paymentInfo, ); const { MERCHANT_OID, USER_NAME } = useSelector( (state: RootState) => state?.auth?.result, ); const nowDay = () => { let date = new Date(); let now = dayjs(date).format('DDMMYYYY') + dayjs().format('HH') + dayjs().format('mm') + dayjs().format('SS'); return now; }; useEffect(() => { const getPaymentInfos = async () => { await dispatch( getPaymentInfo({ MERCHANT_OID, }), ); }; const createHash = async () => { let sx = paymentInfo?.TOKEN; let successUrl = ''; let failUrl = ''; let rnd = nowDay(); let merchantSecret = paymentInfo?.SECRET_KEY; let hashStr = sx + clientRefCode + formatAmountForBackend(amount) + successUrl + failUrl + rnd + merchantSecret; try { let sha1Data = await sha1(hashStr); let hexData = await Buffer.from(sha1Data).toString('hex'); return await Buffer.from(hexData).toString('base64'); } catch (e) { console.log(e); } }; const createPaymentHash = async (selectedAmount: string) => { let sx = paymentInfo?.TOKEN; let successUrl = 'https://nkolaydepo.com/success-yeni-kk-provizyon'; let failUrl = 'https://nkolaydepo.com/fail'; let merchantSecret = paymentInfo?.SECRET_KEY; let rnd = nowDay(); let hashStr = sx + clientRefCode + selectedAmount + successUrl + failUrl + rnd + merchantSecret; try { let sha1String = CryptoJS.SHA1(hashStr); let hexString = CryptoJS.enc.Hex.stringify(sha1String); let base64String = Buffer.from(hexString, 'hex').toString('base64'); //let data = base64String + '|' + rnd; return { hexData: base64String, rnd: rnd }; } catch (e) { console.log('HATA' + e); } }; const readNFC = async () => { const deviceIsSupported = await NfcManager.isSupported(); if (deviceIsSupported) { if (isIos) setIsModalOpened(true); console.log('NFC okuma başladı'); let hash = await createHash(); try { await NFCReaderModule.readNFC( ({ cardNumber, expireDate, firstName, lastName, error, }: { cardNumber: string; expireDate: string; firstName?: string; lastName?: string; error?: boolean; }) => { if (!error) { console.log('NFC kart okundu:', { cardNumber, expireDate, firstName, lastName }); dispatch({ type: loaderActions.OPEN_LOADER }); const EXPIRE_DATE = expireDate.split('/'); const month = EXPIRE_DATE[0]; const year = EXPIRE_DATE[1]; const request: IPaymentInstallmentRequest = { sx: paymentInfo?.TOKEN || '', amount: formatAmountForBackend(amount), cardNumber: cardNumber, hosturl: 'http://localhost:5000/', iscardvalid: 'true', hashData: hash || '', use3D: 'false', }; PaymentServices.paymentInstallment(request) .then(response => { console.log('Ödeme talebi gönderildi:', request); if (response.data.RESPONSE_CODE === 2) { const backendAmount = formatAmountForBackendSecondVersion( `${response.data.PAYMENT_BANK_LIST[0].AUTHORIZATION_AMOUNT}`, ); console.log('Ödeme cevabı alındı: response', response); createPaymentHash(backendAmount).then(resp => { const hex = resp?.hexData ?? ''; const rnd = resp?.rnd ?? ''; const paymentRequest: IPaymentPayRequest = { sx: paymentInfo?.TOKEN || '', clientRefCode: `${clientRefCode}`, successUrl: 'https://nkolaydepo.com/success-yeni-kk-provizyon', failUrl: 'https://nkolaydepo.com/fail', installmentNo: '1', cardHolderName: '', month, year: `20${year}`, cvv: '', amount: backendAmount, // Response'dan dönen Authorization Amount'i bas cardNumber: cardNumber, EncodedValue: response.data.PAYMENT_BANK_LIST[0].EncodedValue, use3D: 'false', transactionType: 'SALES', hosturl: '', rnd: rnd || '', hashData: hex || '', contactless: 'true', environment: 'CEPNPOS', CurrenyCode: '949', userCode: USER_NAME, }; console.log('Ödeme talebi gönderildi:', paymentRequest); PaymentServices.paymentPay(paymentRequest) .then(tempResponse => { dispatch({ type: loaderActions.CLOSE_LOADER }); const is3D = tempResponse.data.USE_3D === "true" if (tempResponse.data.RESPONSE_CODE === 2 && !is3D) { console.log('NFCPay icine girdi if'); navigation.navigate('NFCPaySuccess', { amount: amount, referenceCode: tempResponse.data.REFERENCE_CODE, tempData: tempResponse.data, }); } else { console.log('NFCPay icine girdi else'); navigation.navigate('NFCPayFail', { amount: amount, referenceCode: tempResponse.data.REFERENCE_CODE, tempData: is3D ? { RESPONSE_DATA: "Temazsız Ödeme tutarı limiti aşıldı." } : tempResponse.data, }); } }) .catch(() => dispatch({ type: loaderActions.CLOSE_LOADER }), ); }); } else { console.log('Ödeme cevabı alındı:', response.data); dispatch({ type: loaderActions.CLOSE_LOADER }); setIsNfcWarnModalOpened(true); } }) .catch(() => dispatch({ type: loaderActions.CLOSE_LOADER })); } else { console.log('NFC okuma hatası:', error); setIsModalOpened(false); setIsNfcWarnModalOpened(true); } }, ); } catch (error) { console.error('NFC okuma hatası:', error); setIsModalOpened(false); } } else { setIsNFCSupported(true); } }; getPaymentInfos(); readNFC(); }, [ MERCHANT_OID, NFCReaderModule, amount, dispatch, navigation, paymentInfo?.SECRET_KEY, paymentInfo?.TOKEN, ]); return ( } >
{""}
setIsModalOpened(false)} isOpened={isModalOpened} title={isIos ? "Android Cihaz Gerekli!?" : "Kartınızı Yaklaştırın"} description={ isIos ? "NFC kullanımı için Android cihazınızı kullanınız. iOS cihazlarda NFC deneyimini maalesef şu an gerçekleştiremiyoruz." : "NFC okuma işlemi için kartınızı telefona yaklaştırınız." } footerElemets={ <> {isIos ? (