Advertisement
maneesf

Hyperpay

Sep 8th, 2022
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect, useRef } from 'react'
  2. import { View, StyleSheet, ScrollView, KeyboardAvoidingView, Platform, NativeModules, DeviceEventEmitter } from 'react-native'
  3. import { useDispatch } from 'react-redux'
  4. import { Formik } from 'formik'
  5. import * as yup from 'yup'
  6.  
  7. import Input from '../../components/common/Input'
  8. import AppButton from '../../components/common/AppButton'
  9. import { colors, fonts, dimensions } from '../../constants/constants'
  10. import { fetchCheckoutId, fetchPaymentStatus } from '../../redux/actions/paymentActions'
  11. import { Button } from 'react-native-elements'
  12. var valid = require("card-validator");
  13. import Checkbox from '../../components/common/Checkbox'
  14. import { translate } from '../../components/common/translate'
  15.  
  16. const cardBrand: any = {
  17.     Mastercard: 'MASTER',
  18.     Visa: 'VISA',
  19.     Amex: 'AMEX'
  20. }
  21.  
  22. const NewCard = (props: any) => {
  23.  
  24.     const [checkoutId, setCheckoutId] = useState<string>('')
  25.     const isDefault = useRef<boolean>(false)
  26.     const reference = useRef(false)
  27.     const dispatch = useDispatch()
  28.  
  29.     useEffect(() => {
  30.         if(!checkoutId) generateId()
  31.         const listener = DeviceEventEmitter.addListener('transactionStatus', onSessionConnect)
  32.         return listener.remove.bind(listener)
  33.     }, [])
  34.  
  35.     const generateId = async () => {
  36.         const id = await fetchCheckoutId()
  37.         setCheckoutId(id)
  38.     }
  39.  
  40.     const onSessionConnect = (e: any) => {
  41.         if(!reference.current){
  42.             fetchPaymentStatus(e.checkoutID, dispatch, isDefault.current)
  43.             setCheckoutId('')
  44.             props.navigation.goBack()
  45.         }
  46.         reference.current = true
  47.     }
  48.  
  49.     const initialValues = {
  50.         brand: '',
  51.         fullName: '',
  52.         cardNumber: '',
  53.         expiryDate: '',
  54.         cvv: '',
  55.         default: false
  56.     }
  57.  
  58.     const schema = yup.object().shape({
  59.         fullName: yup.string().required(translate('required')),
  60.  
  61.         expiryDate: yup.string().test('Expiry Date Test',
  62.         translate('invalidExp'),
  63.         value => valid.expirationDate(value).isValid)
  64.         .required(translate('required')),
  65.  
  66.         cvv: yup.string().test('Cvv Test',
  67.         translate('invalidCvv'),
  68.         value => valid.cvv(value).isValid)
  69.         .required(translate('required')),
  70.  
  71.         cardNumber: yup.string().test('Card Test',
  72.         translate('invalidCard'),
  73.         value => valid.number(value).isValid)
  74.         .required(translate('required'))
  75.     })
  76.  
  77.     const dateHandler = (val: any, setFieldValue: Function) => {
  78.         if(val.length === 2){
  79.             setFieldValue('expiryDate', `${val}/`)    
  80.         } else {
  81.             setFieldValue('expiryDate', val)
  82.         }
  83.     }
  84.  
  85.     const saveAddresshandler = async (val: any) => {
  86.         if(!val.brand){
  87.             return
  88.         }
  89.         isDefault.current = val.default
  90.         const [month, year] = val.expiryDate?.split('/')
  91.    
  92.         const paymentParams = {
  93.             checkoutID: checkoutId,
  94.             paymentBrand: cardBrand[val.brand],
  95.             cardNumber: val.cardNumber,
  96.             holderName: val.fullName,
  97.             expiryMonth: month,
  98.             expiryYear: `20${year}`,
  99.             cvv: val.cvv
  100.         }
  101.         if(Platform.OS !== 'ios'){
  102.             NativeModules?.Hyperpay?.transactionPayment(paymentParams, false)
  103.         } else {
  104.             try {
  105.                 const status = await NativeModules?.Hyperpay?.transactionPayment(paymentParams)
  106.                 if(status?.checkoutId){
  107.                     onSessionConnect({ checkoutID: status.checkoutId })
  108.                 }
  109.             } catch(err) {
  110.                 console.log(err)
  111.             }
  112.         }
  113.     }
  114.  
  115.     const onNumberChange = (num: string, fn: Function) => {
  116.         const card = valid.number(`${num}`)
  117.         if(card?.card?.niceType) fn('brand', card.card.niceType)
  118.     }
  119.  
  120.     return (
  121.         <KeyboardAvoidingView style={{ flex: 1}} behavior={(Platform.OS === 'ios') ? 'position' : 'height'}>
  122.             <ScrollView>
  123.                 <View style={styles.backgroundStyle}>
  124.  
  125.                     <View style={styles.formContainer}>
  126.                         <Formik
  127.                             initialValues={initialValues}
  128.                             validationSchema={schema}
  129.                             onSubmit={(value: any) => {
  130.                                 saveAddresshandler(value)
  131.                             }}
  132.                             enableReinitialize={true}
  133.                         >
  134.                             {({ values, handleChange, handleBlur, handleSubmit, errors, touched, setFieldValue }) => (
  135.                                 <>
  136.                                     <View style={styles.inputContainer}>
  137.                                         <Input
  138.                                             value={values.fullName}
  139.                                             placeholder={translate("Full Name*")}
  140.                                             onChange={handleChange('fullName')}
  141.                                             autoCorrect={false}
  142.                                             autoCapitalize="none"
  143.                                             onBlur={handleBlur('fullName')}
  144.                                             errorText={errors.fullName}
  145.                                             touched={touched.fullName}
  146.                                         />
  147.                                     </View>
  148.  
  149.                                     <View style={styles.inputContainer}>
  150.  
  151.                                         <Input
  152.                                             placeholder={translate("Add Card Number*")}
  153.                                             keyboardType='number-pad'
  154.                                             value={values.cardNumber}
  155.                                             onChange={(num: string) => {
  156.                                                 setFieldValue('cardNumber', num)
  157.                                                 onNumberChange(num, setFieldValue)
  158.                                             }}
  159.                                             autoCorrect={false}
  160.                                             autoCapitalize="none"
  161.                                             onBlur={handleBlur('cardNumber')}
  162.                                             errorText={errors.cardNumber}
  163.                                             touched={touched.cardNumber}
  164.                                         />
  165.                                     </View>
  166.  
  167.                                 <View style={{flexDirection: 'row', justifyContent: 'space-between'}}>
  168.                                 <View style={styles.cvvContainer}>
  169.  
  170.                                     <Input
  171.                                         placeholder="MM/YY*"
  172.                                         keyboardType="number-pad"
  173.                                         value={values.expiryDate}
  174.                                         onChange={(val: any) => dateHandler(val, setFieldValue)}
  175.                                         autoCorrect={false}
  176.                                         autoCapitalize="none"
  177.                                         onBlur={handleBlur('expiryDate')}
  178.                                         errorText={errors.expiryDate}
  179.                                         touched={touched.expiryDate}
  180.                                     />
  181.                                     </View>
  182.  
  183.                                     <View style={styles.dateContainer}>
  184.  
  185.                                     <Input
  186.                                         placeholder={translate("CVV*")}
  187.                                         value={values.cvv}
  188.                                         onChange={handleChange('cvv')}
  189.                                         autoCorrect={false}
  190.                                         keyboardType='number-pad'
  191.                                         autoCapitalize="none"
  192.                                         onBlur={handleBlur('cvv')}
  193.                                         errorText={errors.cvv}
  194.                                         touched={touched.cvv}
  195.                                         secureEntry
  196.                                     />
  197.                                     </View>
  198.                                 </View>
  199.                                 <View style={styles.checkboxContainer}>
  200.                                 <Checkbox
  201.                                     checked={values.default}
  202.                                     onChange={(val: boolean) => setFieldValue('default', val)}
  203.                                     label={translate("Set As Default")}
  204.                                 />
  205.                                 </View>
  206.                                     <View style={{marginTop: '10%'}}>
  207.                                         <AppButton
  208.                                             title={translate("Save")}
  209.                                             onPress={handleSubmit}
  210.                                         />
  211.                                         <View style={{marginVertical: '5%'}}>
  212.                                         <Button
  213.                                             title={translate("Cancel")}
  214.                                             titleStyle={{
  215.                                                 fontSize: 20,
  216.                                                 color: colors.redButton
  217.                                             }}
  218.                                             buttonStyle={{
  219.                                                 paddingVertical: 10,
  220.                                                 backgroundColor: '#fff',
  221.                                                 borderRadius: 10,
  222.                                                 borderWidth: 2,
  223.                                                 borderColor: colors.redButton
  224.                                             }}
  225.                                             onPress={() => {
  226.                                                 setCheckoutId('')
  227.                                                 props.navigation.goBack()
  228.                                             }}
  229.                                             />
  230.                                         </View>
  231.                                        
  232.                                     </View>
  233.                                 </>
  234.                             )}
  235.                         </Formik>
  236.  
  237.  
  238.                     </View>
  239.                 </View>
  240.             </ScrollView>
  241.         </KeyboardAvoidingView>
  242.     )
  243. }
  244.  
  245. const styles = StyleSheet.create({
  246.     backgroundStyle: {
  247.         // flex: 1,
  248.         height: dimensions.screenHeight,
  249.         backgroundColor: colors.white,
  250.     },
  251.     formContainer: {
  252.         width: '100%',
  253.         backgroundColor: colors.white,
  254.         borderTopLeftRadius: 15,
  255.         borderTopRightRadius: 15,
  256.         paddingHorizontal: '5%',
  257.         paddingTop: '8%'
  258.     },
  259.     inputContainer: {
  260.         marginBottom: '5%',
  261.         paddingVertical: '1%'
  262.     },
  263.     cvvContainer: {
  264.         marginBottom: '5%',
  265.         width: '48%',
  266.         paddingRight: '2%',
  267.         paddingVertical: '1%'
  268.     },
  269.     dateContainer: {
  270.         marginBottom: '5%',
  271.         width: '48%',
  272.         paddingVertical: '1%'
  273.     },
  274.     label: {
  275.         color: 'rgb(241, 38, 0)',
  276.         fontSize: 14,
  277.         fontFamily: fonts.OpenSansSemiBold,
  278.         paddingVertical: 5
  279.     },
  280.     locationContainer: {
  281.         margin: '1%',
  282.         marginBottom: 20,
  283.         borderColor: 'rgba(0, 0, 0, 0.11)',
  284.         borderWidth: 1,
  285.         borderRadius: 5
  286.     },
  287.     alternative: {
  288.         fontSize: 12,
  289.         fontFamily: fonts.OpenSansRegular,
  290.         color: 'rgb(241, 38, 0)',
  291.  
  292.     },
  293.     checkboxContainer: {
  294.         paddingVertical: 19
  295.     }
  296. })
  297.  
  298. export default NewCard
  299.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement