/*
* 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.
*/
import * as React from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
NativeEventEmitter,
NativeModules,
TextInput,
BackHandler,
type NativeEventSubscription,
Animated,
Dimensions,
ScrollView,
} from 'react-native';
// import { Picker } from '@react-native-picker/picker';
import CheckBox from '@react-native-community/checkbox';
import HyperSdkReact, {
HyperFragmentView,
HyperServiceInstance,
} from 'hyper-sdk-react';
import HyperAPIUtils from './API';
import HyperUtils from './Utils';
import { setOrderDetails, setActiveInstance } from './DataStore';
import { Picker } from '@react-native-picker/picker';
class ProcessScreen extends React.Component<{ navigation: any; route: any }> {
state = {
pickerSelected: 'flyer',
upiSdkPresent: false,
saveToLocker: false,
shouldLink: false,
resultText: '',
animation: new Animated.Value(0),
isPayloadGenerated: false,
hideWidget: false,
ppPayload: '',
};
navigation: any;
eventListener: any;
isPopupVisible: boolean;
statusBarLight: boolean;
merchantId: string;
clientId: string;
merchantKeyId: string;
privateKey: string;
service: string;
customerId: string;
mobile: string;
email: string;
apiKey: string;
amount: string;
orderId: string;
clientAuthToken: string;
orderDetails: {};
signature: string;
// nbTxn
nbTxnBank: string;
// cardTxn
cardNumber: string;
cardToken: string;
cardNetwork: string;
expMonth: string;
expYear: string;
cvv: string;
authType: string;
// upiTxn
upiTxnApp: string;
vpa: string;
// createWallet
walletName: string;
//linkWallet
walletId: string;
otp: string;
// walletTxn
directWalletToken: string;
sdkPresent: string;
walletMobile: string;
flyerPayload: string;
hyperService: HyperServiceInstance | undefined;
backHandler: NativeEventSubscription | undefined;
doProcess = (payload: string) => {
if (this.hyperService) {
this.hyperService.process(payload);
} else {
HyperSdkReact.process(payload);
}
};
constructor(props: { navigation: any; route: any }, context: any) {
super(props, context);
this.navigation = props.navigation;
this.isPopupVisible = false;
this.statusBarLight = false;
const params = props.route.params;
this.merchantId = params.merchantId;
this.clientId = params.clientId;
this.merchantKeyId = params.merchantKeyId;
this.privateKey = params.privateKey;
this.service = params.service;
this.customerId = params.customerId;
this.mobile = params.mobile;
this.email = params.email;
this.apiKey = params.apiKey;
this.amount = params.amount;
this.hyperService = params.instance;
this.orderId = '';
this.clientAuthToken = '';
this.orderDetails = {};
this.signature = '';
this.nbTxnBank = 'NB_SBI';
this.cardNumber = '';
this.cardToken = '';
this.cardNetwork = '';
this.expMonth = '';
this.expYear = '';
this.cvv = '';
this.authType = '';
this.upiTxnApp = 'net.one97.paytm';
this.vpa = '';
this.walletName = 'PAYTM';
this.directWalletToken = '';
this.walletId = '';
this.otp = '';
this.sdkPresent = '';
this.walletMobile = '';
this.flyerPayload = JSON.stringify({});
if (this.service === 'pp') {
this.state.pickerSelected = 'paymentPage';
}
}
componentDidMount() {
setActiveInstance(this.hyperService);
const eventEmitter = new NativeEventEmitter(NativeModules.HyperSdkReact);
let eventGroup = HyperSdkReact.HyperEvent;
if (this.hyperService) {
eventGroup = this.hyperService.getHyperEventString();
}
this.eventListener = eventEmitter.addListener(eventGroup, (resp) => {
// HyperUtils.alertCallbackResponse('ProcessScreen', resp);
this.setState({ resultText: resp });
});
this.backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
if (this.isPopupVisible) {
this.handleClose();
return true;
}
if (this.hyperService) {
return !this.hyperService.isNull() && this.hyperService.onBackPressed();
}
return !HyperSdkReact.isNull() && HyperSdkReact.onBackPressed();
});
}
UpdateOrderData = () => {
const [text, onChangeText] = React.useState('');
const [number, onChangeNumber] = React.useState(this.amount);
return (
{
let orderDetailsNew = {};
if (text !== '') {
orderDetailsNew = {
merchant_id: this.merchantId,
customer_id: this.customerId,
order_id: this.orderId,
amount: number,
mobile_number: this.mobile,
customer_email: this.email,
timestamp: HyperUtils.getTimestamp(),
offer_details: {
offer_applied: String(text !== ''),
offer_code: text,
amount: number,
},
};
} else {
orderDetailsNew = {
merchant_id: this.merchantId,
customer_id: this.customerId,
order_id: this.orderId,
amount: number,
mobile_number: this.mobile,
customer_email: this.email,
timestamp: HyperUtils.getTimestamp(),
};
}
let signNew = '';
HyperAPIUtils.generateSign(
this.privateKey,
JSON.stringify(orderDetailsNew)
)
.then((resp) => {
signNew = resp;
// HyperUtils.showCopyAlert(
// 'Payload signed',
// this.signature
// );
})
.catch((err) => {
console.warn(err);
HyperUtils.showCopyAlert(
'Sign failed',
err?.message ?? String(err)
);
});
var payload = HyperUtils.generateProcessPayloadPP(
'updateOrder',
this.clientId,
this.merchantId,
JSON.stringify(orderDetailsNew),
signNew,
this.merchantKeyId,
this.statusBarLight
);
this.doProcess(JSON.stringify(payload));
}}
/>
);
};
componentWillUnmount() {
setActiveInstance(undefined);
this.eventListener.remove();
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;
};
toggleVisibility = () => {
this.setState({ hideWidget: !this.state.hideWidget });
};
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.orderId = HyperUtils.generateOrderId();
// console.warn('merchantId:', this.merchantId);
// console.warn('orderId:', this.orderId);
if (this.service === 'ec') {
HyperAPIUtils.generateOrder(
this.orderId,
this.amount,
this.customerId,
this.mobile,
this.email,
this.apiKey
)
.then((resp) => {
console.log(resp);
HyperUtils.showCopyAlert('OrderID', this.orderId);
this.clientAuthToken =
HyperUtils.getClientAuthToken(resp);
// console.warn('clientAuthToken:', this.clientAuthToken);
})
.catch((err) => {
console.error(err);
});
} else {
HyperUtils.showCopyAlert('OrderID', this.orderId);
}
}}
/>
{this.service === 'ec' ? (
{
this.setState({ pickerSelected: val });
console.log(val, index);
}}
>
) : (
{
this.setState({ pickerSelected: val });
console.log(val, index);
}}
>
)}
{this.state.pickerSelected === 'getPM' ? (
{
var payload = HyperUtils.generatePaymentMethodsPayload();
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'cardList' ? (
{
var payload = HyperUtils.generateListCardsPayload(
this.clientAuthToken
);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'getUPI' ? (
{
var payload = HyperUtils.generateGetUPIAppsPayload(
this.orderId
);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'flyer' ? (
{
this.setState({ isPayloadGenerated: true });
}}
/>
) : null}
{this.state.pickerSelected === 'listWallet' ? (
{
var payload = HyperUtils.generateListWalletsPayload(
this.clientAuthToken
);
console.log(payload);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'isDeviceReady' ? (
{
this.sdkPresent = text;
}}
defaultValue={this.sdkPresent}
/>
{
var payload: {} = HyperUtils.generateDeviceReadyPayload(
this.sdkPresent
);
console.log(payload);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'nbTxn' ? (
{
this.nbTxnBank = text;
}}
defaultValue={this.nbTxnBank}
/>
{
var payload: {} = HyperUtils.generateNBTxnPayload(
this.orderId,
this.clientAuthToken,
this.nbTxnBank
);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'cardTxn' ? (
{
this.cardNumber = text;
}}
defaultValue={this.cardNumber}
/>
{
this.cardToken = text;
}}
defaultValue={this.cardToken}
/>
{
this.expMonth = text;
}}
defaultValue={this.expMonth}
/>
{
this.expYear = text;
}}
defaultValue={this.expYear}
/>
{
this.cardNetwork = text;
}}
defaultValue={this.cardNetwork}
/>
{
this.cvv = text;
}}
defaultValue={this.cvv}
/>
{
this.authType = text;
}}
defaultValue={this.authType}
/>
{
console.log(toggle);
this.setState({ saveToLocker: toggle });
}}
text="Save to locker"
/>
{
var payload: {} = HyperUtils.generateCardTxnPayload(
this.orderId,
this.clientAuthToken,
this.cardNetwork,
this.cardToken,
this.cardNumber,
this.expMonth,
this.expYear,
this.cvv,
this.authType,
this.state.saveToLocker
);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'upiTxn' ? (
{
this.upiTxnApp = text;
}}
defaultValue={this.upiTxnApp}
/>
{
this.vpa = text;
}}
defaultValue={this.vpa}
/>
{
console.log(toggle);
this.setState({ upiSdkPresent: toggle });
}}
text="UPI SDK Present"
/>
{
var payload: {} = HyperUtils.generateUPIIntentTxnPayload(
this.orderId,
this.clientAuthToken,
this.upiTxnApp,
this.vpa,
this.state.upiSdkPresent
);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'createWallet' ? (
{
this.walletName = text;
}}
defaultValue={this.walletName}
/>
{
var payload: {} = HyperUtils.generateCreateWalletPayload(
this.walletName,
this.clientAuthToken
);
console.log(payload);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'linkWallet' ? (
{
this.walletId = text;
}}
defaultValue={this.walletId}
/>
{
this.otp = text;
}}
defaultValue={this.otp}
/>
{
this.walletName = text;
}}
defaultValue={this.walletName}
/>
{
var payload: {} = HyperUtils.generateLinkWalletPayload(
this.walletName,
this.walletId,
this.otp,
this.clientAuthToken
);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'walletTxn' ? (
{
this.walletName = text;
}}
defaultValue={this.walletName}
/>
{
this.directWalletToken = text;
}}
defaultValue={this.directWalletToken}
/>
{
this.sdkPresent = text;
}}
defaultValue={this.sdkPresent}
/>
{
this.walletMobile = text;
}}
defaultValue={this.walletMobile}
/>
{
console.log(toggle);
this.setState({ shouldLink: toggle });
}}
text="Should Link"
/>
{
var payload: {} = HyperUtils.generateWalletTxnPayload(
this.orderId,
this.clientAuthToken,
this.walletName,
this.directWalletToken,
this.sdkPresent
);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'delinkWallet' ? (
{
this.walletName = text;
}}
defaultValue={this.walletName}
/>
{
this.walletId = text;
}}
defaultValue={this.walletId}
/>
{
var payload: {} = HyperUtils.generateDeLinkWalletPayload(
this.walletName,
this.walletId,
this.clientAuthToken
);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.state.pickerSelected === 'deleteCard' ? (
{
this.cardToken = text;
}}
defaultValue={this.cardToken}
/>
{
var payload: {} = HyperUtils.generateDeleteCardPayload(
this.cardToken,
this.clientAuthToken
);
console.log(payload);
this.doProcess(JSON.stringify(payload));
}}
/>
) : null}
{this.service === 'pp' ? (
{
let x: {
merchant_id: String;
customer_id: String;
order_id: String;
amount: String;
mobile_number: String;
customer_email: String;
timestamp: String;
features: any;
} = {
merchant_id: this.merchantId,
customer_id: this.customerId,
order_id: this.orderId,
amount: this.amount,
mobile_number: this.mobile,
customer_email: this.email,
timestamp: HyperUtils.getTimestamp(),
features: { paymentWidget: { enable: true } },
};
this.orderDetails = x;
setOrderDetails(x);
HyperAPIUtils.generateSign(
this.privateKey,
JSON.stringify(this.orderDetails)
)
.then((resp) => {
this.signature = resp;
HyperUtils.showCopyAlert(
'Payload signed',
this.signature
);
})
.catch((err) => {
console.warn(err);
HyperUtils.showCopyAlert(
'Sign failed',
err?.message ?? String(err)
);
});
}}
/>
{
var payload = HyperUtils.generateProcessPayloadPP(
this.state.pickerSelected,
this.clientId,
this.merchantId,
JSON.stringify(this.orderDetails),
this.signature,
this.merchantKeyId,
this.statusBarLight
);
this.doProcess(JSON.stringify(payload));
}}
/>
{!this.hyperService && (
{
var payload = HyperUtils.generateProcessPayloadPP(
this.state.pickerSelected,
this.clientId,
this.merchantId,
JSON.stringify(this.orderDetails),
this.signature,
this.merchantKeyId,
this.statusBarLight
);
HyperSdkReact.processWithActivity(
JSON.stringify(payload)
);
}}
/>
)}
{
this.setState({
isPayloadGenerated: true,
ppPayload: JSON.stringify(
HyperUtils.generateProcessPayloadPP(
this.state.pickerSelected,
this.clientId,
this.merchantId,
JSON.stringify(this.orderDetails),
this.signature,
this.merchantKeyId,
this.statusBarLight
)
),
});
}}
/>
) : null}
{/* {
HyperSdkReact.isInitialised().then((init: boolean) => {
// console.warn('isInitialised:', init);
HyperUtils.showCopyAlert('isInitialised', init + '');
});
}}
/> */}
{
if (this.hyperService) {
this.hyperService.terminate();
} else {
HyperSdkReact.terminate();
}
}}
/>
{/* {
this.handleOpen();
}}
/>
{this.state.isPayloadGenerated ? (
) : null}
/> */}
{
this.toggleVisibility();
}}
/>
{
this.navigation.navigate('OfferScreen');
}}
/>
{this.state.isPayloadGenerated ? (
) : null}
{/*
{
const data = {
requestId: 'testid',
service: 'in.juspay.hyperpay',
payload: {
action: 'updateDetails',
hidePaymentWidget: false,
},
};
this.doProcess(JSON.stringify(data));
}}
/>
*/}
);
}
}
const styles = StyleSheet.create({
root: {
flex: 1,
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
containerNew: {
height: 50,
width: '100%',
// backgroundColor: 'yellow',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
containerSon: {
height: 'auto',
// width: 'auto',
flex: 0,
backgroundColor: 'white',
},
newWala: {
height: 'auto',
// width: 'auto',
flex: 1,
backgroundColor: 'white',
},
containerSon2: {
height: 'auto',
// width: '100%',
flex: 1,
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'center',
},
horizontal: {
flexDirection: 'row',
},
horizontal2: {
flexDirection: 'row',
height: 103,
width: '100%',
position: 'absolute',
bottom: 0,
},
horizontal2Gone: {
display: 'none',
},
input: {
borderColor: 'black',
borderWidth: 1,
margin: 2,
},
input2: {
borderColor: 'black',
color: 'black',
borderWidth: 1,
margin: 2,
// placeholderTextColor: 'black',
},
button: {
backgroundColor: 'blue',
paddingVertical: 12,
paddingHorizontal: 25,
borderRadius: 25,
marginVertical: 12,
},
button2: {
backgroundColor: 'blue',
borderRadius: 25,
width: 200,
height: 40,
alignItems: 'center',
justifyContent: 'center',
},
button3: {
backgroundColor: '#FF3269',
borderRadius: 25,
width: 200,
height: 40,
alignItems: 'center',
justifyContent: 'center',
},
buttonText: {
color: 'white',
fontSize: 18,
},
editText: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
paddingVertical: 12,
paddingHorizontal: 25,
borderRadius: 25,
marginVertical: 12,
marginEnd: 20,
},
checkbox: {
width: 40,
height: 40,
borderColor: 'gray',
borderWidth: 1,
paddingVertical: 12,
borderRadius: 20,
marginVertical: 12,
},
picker: {
width: 250,
},
pickerContainer: {
borderColor: 'gray',
borderWidth: 1,
borderRadius: 12,
},
centerAligned: {
alignItems: 'center',
justifyContent: 'center',
},
marginEnd: {
marginEnd: 20,
},
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,
},
textArea: {
borderColor: 'gray',
height: 150,
justifyContent: 'flex-start',
minHeight: 40,
borderWidth: 1,
paddingVertical: 12,
paddingHorizontal: 25,
borderRadius: 12,
marginVertical: 12,
marginHorizontal: 12,
},
});
const CustomButton = (props: any) => {
return (
{props.title}
);
};
const CustomButton2 = (props: any) => {
return (
{props.title}
);
};
// const CustomButton3 = (props: any) => {
// return (
//
// {props.title}
//
// );
// };
const CustomCheckBox = (props: any) => {
return (
{props.text}
);
};
export default ProcessScreen;