/* * Copyright (c) Juspay Technologies. * * This source code is licensed under the AGPL 3.0 license found in the * LICENSE file in the root directory of this source tree. */ /* eslint-disable react-native/no-inline-styles */ import * as React from 'react'; import { StyleSheet, View, Text, TextInput, TouchableOpacity, NativeEventEmitter, NativeModules, type EmitterSubscription, type NativeEventSubscription, Animated, Dimensions, BackHandler, ScrollView, Alert, } from 'react-native'; import HyperAPIUtils from './API'; import HyperSdkReact, { HyperServiceInstance } from 'hyper-sdk-react'; import HyperUtils from './Utils'; import merchantConfig from './merchant_config.json'; import customerConfig from './customer_config.json'; import { Picker } from '@react-native-picker/picker'; class HomeScreen extends React.Component<{ navigation: any }> { state = { animation: new Animated.Value(0), pickerSelected: 'pp', hyperInstances: new Map(), currentlySelectedInstance: '', }; navigation: any; instanceListeners = new Map(); backHandler: NativeEventSubscription | undefined; preFetchPayload: {}; signaturePayload: {}; initiatePayload: {}; eventListener: any; isPopupVisible: boolean; merchantId: string; clientId: string; tenantId: string; merchantKeyId: string; signature: string; customerId: string; mobile: string; email: string; apiKey: string; privateKey: string; amount: string; constructor(props: { navigation: any }, context: any) { super(props, context); this.navigation = props.navigation; this.isPopupVisible = false; this.merchantId = merchantConfig.merchantId; this.clientId = merchantConfig.clientId; this.tenantId = ''; this.merchantKeyId = merchantConfig.merchantKeyId; this.signature = ''; this.customerId = customerConfig.customerId; this.mobile = customerConfig.mobile; this.email = customerConfig.email; this.apiKey = merchantConfig.apiKey; this.privateKey = merchantConfig.privateKey; this.amount = customerConfig.amount; this.preFetchPayload = HyperUtils.generatePreFetchPayload( this.clientId, this.state.pickerSelected ); this.signaturePayload = {}; this.initiatePayload = {}; } componentDidMount() { const eventEmitter = new NativeEventEmitter(NativeModules.HyperSdkReact); this.eventListener = eventEmitter.addListener( HyperSdkReact.HyperEvent, (resp) => { HyperUtils.alertCallbackResponse('HomeScreen', resp); } ); this.backHandler = BackHandler.addEventListener('hardwareBackPress', () => { if (this.isPopupVisible) { this.handleClose(); return true; } return false; }); } componentWillUnmount() { this.eventListener.remove(); this.instanceListeners.forEach((listener) => listener.remove()); this.instanceListeners.clear(); this.backHandler?.remove(); } handleOpen = () => { Animated.timing(this.state.animation, { toValue: 1, duration: 300, useNativeDriver: true, }).start(); this.isPopupVisible = true; }; handleClose = () => { Animated.timing(this.state.animation, { toValue: 0, duration: 200, useNativeDriver: true, }).start(); this.isPopupVisible = false; }; render() { const screenHeight = Dimensions.get('window').height; const backdrop = { transform: [ { translateY: this.state.animation.interpolate({ inputRange: [0, 0.01], outputRange: [screenHeight, 0], extrapolate: 'clamp', }), }, ], opacity: this.state.animation.interpolate({ inputRange: [0.01, 0.5], outputRange: [0, 1], extrapolate: 'clamp', }), }; const slideUp = { transform: [ { translateY: this.state.animation.interpolate({ inputRange: [0.01, 1], outputRange: [0, -1 * screenHeight], extrapolate: 'clamp', }), }, ], }; return ( <> { this.handleOpen(); }} /> { this.setState({ pickerSelected: val }); console.log(val, index); }} > { this.preFetchPayload = HyperUtils.generatePreFetchPayload( this.clientId, this.state.pickerSelected ); // console.warn('preFetchPayload:', this.preFetchPayload); HyperSdkReact.preFetch(JSON.stringify(this.preFetchPayload)); }} /> { if (this.tenantId !== '') { HyperSdkReact.createHyperServicesWithTenantId( this.tenantId, this.clientId ); } else { HyperSdkReact.createHyperServices(); } }} /> { const hyperserviceInstance = this.tenantId !== '' ? new HyperServiceInstance(this.tenantId, this.clientId) : new HyperServiceInstance(); const instanceKey = hyperserviceInstance.getHyperEventString(); const hypInsMap = this.state.hyperInstances; hypInsMap.set(instanceKey, hyperserviceInstance); this.setState({ hyperInstances: hypInsMap, currentlySelectedInstance: this.state.currentlySelectedInstance || instanceKey, }); const eventEmitter = new NativeEventEmitter( NativeModules.HyperSdkReact ); this.instanceListeners.set( instanceKey, eventEmitter.addListener(instanceKey, (resp) => { HyperUtils.alertCallbackResponse('Custom HomeScreen', resp); }) ); }} /> { Alert.alert( 'hyperInstances', JSON.stringify(Object.fromEntries(this.state.hyperInstances)) ); }} /> Select HyperService Instance: { this.setState({ currentlySelectedInstance: val }); console.log(val, index); }} > {this.state.hyperInstances && Array.from(this.state.hyperInstances.entries()).map( (val, key) => { return ( ); } )} {this.state.pickerSelected === 'pp' ? ( { this.signaturePayload = { merchant_id: this.merchantId, customer_id: this.customerId, timestamp: HyperUtils.getTimestamp(), }; HyperAPIUtils.generateSign( this.privateKey, JSON.stringify(this.signaturePayload) ) .then((resp) => { this.signature = resp; HyperUtils.showCopyAlert( 'Payload signed', this.signature ); }) .catch((err) => { console.warn(err); HyperUtils.showCopyAlert( 'Sign failed', err?.message ?? String(err) ); }); }} /> ) : null} { this.initiatePayload = this.state.pickerSelected === 'ec' ? HyperUtils.generateECInitiatePayload( this.merchantId, this.clientId, this.customerId ) : HyperUtils.generatePPInitiatePayload( this.clientId, this.merchantId, JSON.stringify(this.signaturePayload), this.signature, this.merchantKeyId ); HyperSdkReact.initiate(JSON.stringify(this.initiatePayload)); }} /> {this.state.currentlySelectedInstance != '' && ( { let hypInstance = this.state.hyperInstances.get( this.state.currentlySelectedInstance )!; this.initiatePayload = this.state.pickerSelected === 'ec' ? HyperUtils.generateECInitiatePayload( this.merchantId, this.clientId, this.customerId ) : HyperUtils.generatePPInitiatePayload( this.clientId, this.merchantId, JSON.stringify(this.signaturePayload), this.signature, this.merchantKeyId ); hypInstance.initiate(JSON.stringify(this.initiatePayload)); }} /> )} { this.navigation.navigate('ProcessScreen', { merchantId: this.merchantId, clientId: this.clientId, customerId: this.customerId, mobile: this.mobile, email: this.email, amount: this.amount, apiKey: this.apiKey, merchantKeyId: this.merchantKeyId, privateKey: this.privateKey, service: this.state.pickerSelected, }); }} /> {this.state.currentlySelectedInstance != '' && ( { this.navigation.navigate('ProcessScreen', { merchantId: this.merchantId, clientId: this.clientId, customerId: this.customerId, mobile: this.mobile, email: this.email, amount: this.amount, apiKey: this.apiKey, merchantKeyId: this.merchantKeyId, privateKey: this.privateKey, service: this.state.pickerSelected, instance: this.state.hyperInstances.get( this.state.currentlySelectedInstance )!, }); }} /> )} { HyperSdkReact.isInitialised().then((init: boolean) => { // console.warn('isInitialised:', init); HyperUtils.showCopyAlert('isInitialised', init + ''); }); }} /> {this.state.currentlySelectedInstance != '' && ( { let hypInstance = this.state.hyperInstances.get( this.state.currentlySelectedInstance )!; hypInstance.isInitialised().then((init: boolean) => { // console.warn('isInitialised:', init); HyperUtils.showCopyAlert('isInitialised', init + ''); }); }} /> )} { HyperSdkReact.terminate(); }} /> {this.state.currentlySelectedInstance != '' && ( { const instanceKey = this.state.currentlySelectedInstance; const hypInstance = this.state.hyperInstances.get(instanceKey)!; hypInstance.terminate(); this.instanceListeners.get(instanceKey)?.remove(); this.instanceListeners.delete(instanceKey); const hypInsMap = this.state.hyperInstances; hypInsMap.delete(instanceKey); this.setState({ hyperInstances: hypInsMap, currentlySelectedInstance: hypInsMap.keys().next().value ?? '', }); }} /> )} TenantId : { this.tenantId = text; }} defaultValue={this.tenantId} /> { this.merchantId = text; }} defaultValue={this.merchantId} /> { this.clientId = text; }} defaultValue={this.clientId} /> { this.customerId = text; }} defaultValue={this.customerId} /> { this.mobile = text; }} defaultValue={this.mobile} /> { this.email = text; }} defaultValue={this.email} /> { this.amount = text; }} defaultValue={this.amount} /> { this.apiKey = text; }} defaultValue={this.apiKey} /> { this.merchantKeyId = text; }} defaultValue={this.merchantKeyId} /> { this.privateKey = text; }} defaultValue={this.privateKey} /> { HyperAPIUtils.createCustomer( this.customerId, this.mobile, this.email, this.apiKey ) .then((resp) => { console.log(resp); HyperUtils.showCopyAlert('createCustomer', resp); }) .catch((err) => { console.error(err); }); }} /> ); } } const styles = StyleSheet.create({ scrollView: { flexGrow: 1, alignItems: 'center', justifyContent: 'center', }, container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, button: { backgroundColor: 'blue', paddingVertical: 12, paddingHorizontal: 25, borderRadius: 25, marginVertical: 12, marginEnd: 12, }, buttonText: { color: 'white', fontSize: 18, }, cover: { backgroundColor: 'rgba(0,0,0,.5)', }, sheet: { position: 'absolute', top: Dimensions.get('window').height, left: 0, right: 0, height: '100%', justifyContent: 'flex-end', }, popup: { backgroundColor: '#FFF', borderTopLeftRadius: 20, borderTopRightRadius: 20, alignItems: 'center', justifyContent: 'center', minHeight: 80, }, horizontal: { flexDirection: 'row', marginHorizontal: 30, alignItems: 'center', }, editText: { height: 40, width: '45%', borderColor: 'gray', borderWidth: 1, paddingVertical: 12, paddingHorizontal: 12, borderRadius: 12, marginVertical: 12, marginEnd: 20, }, longEditText: { height: 40, width: '95%', borderColor: 'gray', borderWidth: 1, paddingVertical: 12, paddingHorizontal: 12, borderRadius: 12, marginVertical: 12, marginEnd: 20, }, picker: { width: 250, }, pickerContainer: { borderColor: 'gray', borderWidth: 1, borderRadius: 12, }, }); const CustomButton = (props: any) => { return ( {props.title} ); }; export default HomeScreen;