2023-06-22 17:41:26 +00:00
|
|
|
|
|
|
|
import React, {useState, useEffect, useCallback} from 'react';
|
|
|
|
import {ActivityIndicator, Platform, Alert, ProgressBarAndroid, Dimensions, StyleSheet, View, Text,} from 'react-native';
|
2022-02-28 09:41:54 +00:00
|
|
|
import WebView from 'react-native-webview';
|
|
|
|
import {connect, useDispatch} from 'react-redux';
|
|
|
|
import {createStructuredSelector} from 'reselect';
|
|
|
|
import {ScreenComponent} from "../components/ScreenComponent";
|
2023-06-22 17:41:26 +00:00
|
|
|
import {readUser} from "../webservice/AuthApi";
|
|
|
|
import queryString from 'query-string';
|
|
|
|
import I18n from "react-native-i18n";
|
|
|
|
import {bindActionCreators} from "redux";
|
|
|
|
import { envoieUserWalletToCardAction, getCommissionUserWalletToCardAction } from "../webservice/EnvoieUserApi"
|
2023-10-05 06:09:30 +00:00
|
|
|
import { fetchActivePaySubscription } from '../redux/insurance/insurance.actions';
|
2023-06-22 17:41:26 +00:00
|
|
|
//import { payment_transaction } from '../webservice/agent/RetraitCarteVersWalletApi';
|
2023-10-05 06:09:30 +00:00
|
|
|
import {USER_CARD_WALLET, USER_PAY_INSURANCE} from '../redux/types/NanoSanteKeyType'
|
2022-02-28 09:41:54 +00:00
|
|
|
|
|
|
|
const WebviewScreen = ({
|
|
|
|
navigation,
|
2023-06-22 17:41:26 +00:00
|
|
|
getCommissionUserWalletToCardAction,
|
|
|
|
envoieUserWalletToCardAction,
|
2023-10-05 06:09:30 +00:00
|
|
|
fetchActivePaySubscription,
|
|
|
|
saveTransaction
|
2023-06-22 17:41:26 +00:00
|
|
|
|
|
|
|
}) => {
|
2022-02-28 09:41:54 +00:00
|
|
|
let webviewRef = null;
|
|
|
|
const dispatch = useDispatch();
|
2023-10-05 06:09:30 +00:00
|
|
|
const [hasRedirected, setHasRedirected] = useState(false);
|
|
|
|
const [constRedirect, setCountRedirect ] = useState(0)
|
2022-02-28 09:41:54 +00:00
|
|
|
|
2023-10-05 06:09:30 +00:00
|
|
|
const {url, requestBody, transactionType, subscription} = navigation.state.params;
|
2023-06-22 17:41:26 +00:00
|
|
|
|
|
|
|
//useEffect(
|
|
|
|
// useCallback(() => {
|
|
|
|
// const onBackPress = () => {
|
|
|
|
// if (backButtonEnabled) {
|
|
|
|
// webviewRef?.goBack();
|
|
|
|
// } else {
|
|
|
|
// navigation.goBack();
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// };
|
|
|
|
// BackHandler.addEventListener('hardwareBackPress', onBackPress);
|
|
|
|
// return () =>
|
|
|
|
// BackHandler.removeEventListener(
|
|
|
|
// 'hardwareBackPress',
|
|
|
|
// onBackPress,
|
|
|
|
// );
|
|
|
|
// }, []);
|
2023-10-05 06:09:30 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
//useCallback(() => {
|
|
|
|
if(saveTransaction.result !== null) {
|
2023-06-22 17:41:26 +00:00
|
|
|
Alert.alert(
|
|
|
|
I18n.t("SUCCESS"),
|
2023-10-05 06:09:30 +00:00
|
|
|
saveTransaction.result.response,
|
2023-06-22 17:41:26 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
text: I18n.t("OK"), onPress: () => {
|
|
|
|
readUser().then(() => {
|
|
|
|
if (user) {
|
|
|
|
if (user !== undefined) {
|
|
|
|
getCommissionUserWalletToCardAction(user.phone, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
navigation.goBack()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
{cancelable: false}
|
2022-02-28 09:41:54 +00:00
|
|
|
);
|
2023-06-22 17:41:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 06:09:30 +00:00
|
|
|
if(saveTransaction.error !== null) {
|
|
|
|
navigation.goBack();
|
2023-06-22 17:41:26 +00:00
|
|
|
Alert.alert(
|
|
|
|
I18n.t("ERROR_TREATMENT_REQUEST"),
|
2023-10-05 06:09:30 +00:00
|
|
|
saveTransaction.error.data.error,
|
2023-06-22 17:41:26 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
text: I18n.t("OK"), onPress: () => {
|
|
|
|
navigation.goBack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
{cancelable: false}
|
|
|
|
)
|
|
|
|
}
|
2023-10-05 06:09:30 +00:00
|
|
|
}, [saveTransaction]);
|
2022-02-28 09:41:54 +00:00
|
|
|
|
|
|
|
const renderLoader = () => (
|
2023-06-22 17:41:26 +00:00
|
|
|
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
|
|
|
|
{Platform.OS === 'android'
|
|
|
|
?
|
|
|
|
(
|
|
|
|
<>
|
|
|
|
<ProgressBarAndroid/>
|
|
|
|
<Text>{I18n.t('LOADING_DOTS')}</Text>
|
|
|
|
|
|
|
|
</>
|
|
|
|
) :
|
|
|
|
<>
|
|
|
|
<ActivityIndicator size="large" color={'#ccc'}/>
|
|
|
|
<Text>{I18n.t('LOADING_DOTS')}</Text>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
2023-10-05 06:09:30 +00:00
|
|
|
const checkPaymentStatus = (message) => {
|
|
|
|
message = JSON.parse(message);
|
2023-06-22 17:41:26 +00:00
|
|
|
|
2023-10-05 06:09:30 +00:00
|
|
|
let status = message['status'];
|
|
|
|
let transaction_id = message['transaction_id'];
|
2023-06-22 17:41:26 +00:00
|
|
|
|
2023-10-05 06:09:30 +00:00
|
|
|
if(status === "0") {
|
|
|
|
navigation.goBack()
|
|
|
|
Alert.alert(I18n.t('PAYMENT_ERROR'), I18n.t('PAYMENT_COULD_NOT_MADE'), [{text:"Ok",onPress:()=>{}}])
|
|
|
|
}
|
|
|
|
else if(status === "1") {
|
|
|
|
// navigation.goBack()
|
|
|
|
if(transactionType === "USER_CARD_WALLET" ){
|
2023-06-22 17:41:26 +00:00
|
|
|
envoieUserWalletToCardAction({
|
|
|
|
payment_transaction_id: transaction_id,
|
|
|
|
...requestBody
|
|
|
|
}, true)
|
2023-10-05 06:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(transactionType === "USER_PAY_INSURANCE") {
|
|
|
|
fetchActivePaySubscription(subscription.id, {
|
|
|
|
payment_transaction_id: transaction_id,
|
|
|
|
...requestBody
|
|
|
|
}, true)
|
2023-06-22 17:41:26 +00:00
|
|
|
}
|
2023-10-05 06:09:30 +00:00
|
|
|
// Marquez la redirection comme déjà effectuée
|
|
|
|
setHasRedirected(true);
|
2023-06-22 17:41:26 +00:00
|
|
|
|
2023-10-05 06:09:30 +00:00
|
|
|
// Retournez dans l'application
|
|
|
|
navigation.goBack();
|
|
|
|
|
2023-06-22 17:41:26 +00:00
|
|
|
}
|
|
|
|
};
|
2022-02-28 09:41:54 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ScreenComponent>
|
|
|
|
<View style={styles.contain}>
|
|
|
|
<WebView
|
|
|
|
source={{uri: url}}
|
|
|
|
style={styles.webview}
|
|
|
|
ref={ref => {
|
|
|
|
webviewRef = ref;
|
|
|
|
}}
|
|
|
|
javaScriptEnabled
|
|
|
|
domStorageEnabled
|
2023-10-05 06:09:30 +00:00
|
|
|
onMessage={({nativeEvent}) => {
|
|
|
|
checkPaymentStatus(nativeEvent.data);
|
2022-02-28 09:41:54 +00:00
|
|
|
}}
|
2023-06-22 17:41:26 +00:00
|
|
|
renderLoading={() => (
|
|
|
|
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center', marginTop: -42}}>
|
|
|
|
{Platform.OS === 'android'
|
|
|
|
?
|
|
|
|
(
|
|
|
|
<>
|
|
|
|
<ProgressBarAndroid/>
|
|
|
|
<Text>{I18n.t('LOADING_DOTS')}</Text>
|
|
|
|
|
|
|
|
</>
|
|
|
|
) :
|
|
|
|
<>
|
|
|
|
<ActivityIndicator size="large" color={'#ccc'}/>
|
|
|
|
<Text>{I18n.t('LOADING_DOTS')}</Text>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
</View>
|
|
|
|
|
|
|
|
)}
|
2022-02-28 09:41:54 +00:00
|
|
|
startInLoadingState
|
|
|
|
/>
|
2023-10-05 06:09:30 +00:00
|
|
|
|
2022-02-28 09:41:54 +00:00
|
|
|
</View>
|
2023-06-22 17:41:26 +00:00
|
|
|
|
|
|
|
</ScreenComponent>
|
|
|
|
|
2022-02-28 09:41:54 +00:00
|
|
|
);
|
|
|
|
};
|
2023-06-22 17:41:26 +00:00
|
|
|
const mapStateToProps = state => ({
|
2023-10-05 06:09:30 +00:00
|
|
|
saveTransaction: state.envoieUserWalletToCardReducer
|
2023-06-22 17:41:26 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators({
|
|
|
|
//payment_transaction,
|
|
|
|
envoieUserWalletToCardAction,
|
2023-10-05 06:09:30 +00:00
|
|
|
getCommissionUserWalletToCardAction,
|
|
|
|
fetchActivePaySubscription
|
2023-06-22 17:41:26 +00:00
|
|
|
}, dispatch);
|
2022-02-28 09:41:54 +00:00
|
|
|
|
2023-06-22 17:41:26 +00:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(WebviewScreen);
|
2022-02-28 09:41:54 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
contain: {
|
|
|
|
flex: 1,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
paddingLeft: 20,
|
|
|
|
paddingRight: 20,
|
|
|
|
},
|
|
|
|
contentModal: {
|
|
|
|
width: '100%',
|
|
|
|
borderRadius: 8,
|
|
|
|
padding: 8,
|
|
|
|
},
|
|
|
|
item: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'center',
|
|
|
|
paddingVertical: 20,
|
|
|
|
},
|
|
|
|
contentAction: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'flex-end',
|
|
|
|
paddingTop: 24,
|
|
|
|
},
|
|
|
|
webview: {
|
|
|
|
height: Dimensions.get('window').height,
|
|
|
|
width: Dimensions.get('window').width,
|
|
|
|
},
|
|
|
|
});
|