|
| 1 | +import React from 'react'; |
| 2 | +import { StyleSheet, Modal, View, Animated, TouchableWithoutFeedback, Text, Alert, Image, Dimensions, Easing, TouchableOpacity, Platform, } from 'react-native'; |
| 3 | +import WebView from 'react-native-webview'; |
| 4 | +import { colors } from './configs'; |
| 5 | +var loader = require('./assets/loader.gif'); |
| 6 | +var borderRadiusDimension = 24 / 896; |
| 7 | +var windowHeight = Dimensions.get('window').height; |
| 8 | +var getRedirectParams = function (url) { |
| 9 | + // initialize result container |
| 10 | + var res = {}; |
| 11 | + // if url has params |
| 12 | + if (url.split('?').length > 1) { |
| 13 | + // get query params in an array |
| 14 | + var params = url.split('?')[1].split('&'); |
| 15 | + // add url params to result |
| 16 | + for (var i = 0; i < params.length; i++) { |
| 17 | + var param = params[i].split('='); |
| 18 | + var val = decodeURIComponent(param[1]).trim(); |
| 19 | + res[param[0]] = String(val); |
| 20 | + } |
| 21 | + } |
| 22 | + // return result |
| 23 | + return res; |
| 24 | +}; |
| 25 | +var FlutterwaveCheckout = function FlutterwaveCheckout(props) { |
| 26 | + var link = props.link, visible = props.visible, onRedirect = props.onRedirect, onAbort = props.onAbort; |
| 27 | + var _a = React.useState(false), show = _a[0], setShow = _a[1]; |
| 28 | + var webviewRef = React.useRef(null); |
| 29 | + var animation = React.useRef(new Animated.Value(0)); |
| 30 | + var animateIn = React.useCallback(function () { |
| 31 | + setShow(true); |
| 32 | + Animated.timing(animation.current, { |
| 33 | + toValue: 1, |
| 34 | + duration: 700, |
| 35 | + easing: Easing["in"](Easing.elastic(0.72)), |
| 36 | + useNativeDriver: false |
| 37 | + }).start(); |
| 38 | + }, []); |
| 39 | + var animateOut = React.useCallback(function () { |
| 40 | + return new Promise(function (resolve) { |
| 41 | + Animated.timing(animation.current, { |
| 42 | + toValue: 0, |
| 43 | + duration: 400, |
| 44 | + useNativeDriver: false |
| 45 | + }).start(function () { |
| 46 | + setShow(false); |
| 47 | + resolve(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }, []); |
| 51 | + var handleReload = React.useCallback(function () { |
| 52 | + if (webviewRef.current) { |
| 53 | + webviewRef.current.reload(); |
| 54 | + } |
| 55 | + }, []); |
| 56 | + var handleAbort = React.useCallback(function (confirmed) { |
| 57 | + if (confirmed === void 0) { confirmed = false; } |
| 58 | + if (!confirmed) { |
| 59 | + Alert.alert('', 'Are you sure you want to cancel this payment?', [ |
| 60 | + { text: 'No' }, |
| 61 | + { |
| 62 | + text: 'Yes, Cancel', |
| 63 | + style: 'destructive', |
| 64 | + onPress: function () { return handleAbort(true); } |
| 65 | + }, |
| 66 | + ]); |
| 67 | + return; |
| 68 | + } |
| 69 | + // remove tx_ref and dismiss |
| 70 | + animateOut().then(onAbort); |
| 71 | + }, [onAbort, animateOut]); |
| 72 | + var handleNavigationStateChange = React.useCallback(function (ev) { |
| 73 | + // cregex to check if redirect has occured on completion/cancel |
| 74 | + var rx = /\/flutterwave\.com\/rn-redirect/; |
| 75 | + // Don't end payment if not redirected back |
| 76 | + if (!rx.test(ev.url)) { |
| 77 | + return; |
| 78 | + } |
| 79 | + // dismiss modal |
| 80 | + animateOut().then(function () { |
| 81 | + if (onRedirect) { |
| 82 | + onRedirect(getRedirectParams(ev.url)); |
| 83 | + } |
| 84 | + }); |
| 85 | + }, [onRedirect]); |
| 86 | + var doAnimate = React.useCallback(function () { |
| 87 | + if (visible === show) { |
| 88 | + return; |
| 89 | + } |
| 90 | + if (visible) { |
| 91 | + return animateIn(); |
| 92 | + } |
| 93 | + animateOut().then(function () { }); |
| 94 | + }, [visible, show, animateOut, animateIn]); |
| 95 | + React.useEffect(function () { |
| 96 | + doAnimate(); |
| 97 | + return function () { }; |
| 98 | + }, [doAnimate]); |
| 99 | + var marginTop = animation.current.interpolate({ |
| 100 | + inputRange: [0, 1], |
| 101 | + outputRange: [windowHeight, 0] |
| 102 | + }); |
| 103 | + var opacity = animation.current.interpolate({ |
| 104 | + inputRange: [0, 0.3, 1], |
| 105 | + outputRange: [0, 1, 1] |
| 106 | + }); |
| 107 | + return (<Modal transparent={true} animated={false} hardwareAccelerated={false} visible={show}> |
| 108 | + <FlutterwaveCheckoutBackdrop onPress={function () { return handleAbort(); }} animation={animation.current}/> |
| 109 | + <Animated.View style={[ |
| 110 | + styles.webviewContainer, |
| 111 | + { |
| 112 | + marginTop: marginTop, |
| 113 | + opacity: opacity |
| 114 | + } |
| 115 | + ]} testID='flw-checkout-dialog'> |
| 116 | + <WebView ref={webviewRef} source={{ uri: link || '' }} style={styles.webview} startInLoadingState={true} scalesPageToFit={true} javaScriptEnabled={true} onNavigationStateChange={handleNavigationStateChange} renderError={function () { return <FlutterwaveCheckoutError hasLink={!!link} onTryAgain={handleReload}/>; }} renderLoading={function () { return <FlutterwaveCheckoutLoader />; }}/> |
| 117 | + </Animated.View> |
| 118 | + </Modal>); |
| 119 | +}; |
| 120 | +var FlutterwaveCheckoutBackdrop = function FlutterwaveCheckoutBackdrop(_a) { |
| 121 | + var animation = _a.animation, onPress = _a.onPress; |
| 122 | + // Interpolation backdrop animation |
| 123 | + var backgroundColor = animation.interpolate({ |
| 124 | + inputRange: [0, 0.3, 1], |
| 125 | + outputRange: [colors.transparent, colors.transparent, 'rgba(0,0,0,0.5)'] |
| 126 | + }); |
| 127 | + return (<TouchableWithoutFeedback testID='flw-checkout-backdrop' onPress={onPress}> |
| 128 | + <Animated.View style={Object.assign({}, styles.backdrop, { backgroundColor: backgroundColor })}/> |
| 129 | + </TouchableWithoutFeedback>); |
| 130 | +}; |
| 131 | +export var FlutterwaveCheckoutError = function (_a) { |
| 132 | + var hasLink = _a.hasLink, onTryAgain = _a.onTryAgain; |
| 133 | + return (<View style={styles.error} testID="flw-checkout-error"> |
| 134 | + {hasLink ? (<> |
| 135 | + <Text style={styles.errorText}> |
| 136 | + An error occurred, please tab below to try again. |
| 137 | + </Text> |
| 138 | + <TouchableOpacity style={styles.errorActionButton} onPress={onTryAgain}> |
| 139 | + <Text style={styles.errorActionButtonText}>Try Again</Text> |
| 140 | + </TouchableOpacity> |
| 141 | + </>) : (<Text style={styles.errorText}> |
| 142 | + An error occurred, please close the checkout dialog and try again. |
| 143 | + </Text>)} |
| 144 | + </View>); |
| 145 | +}; |
| 146 | +var FlutterwaveCheckoutLoader = function () { |
| 147 | + return (<View style={styles.loading} testID="flw-checkout-loader"> |
| 148 | + <Image source={loader} resizeMode="contain" style={styles.loadingImage}/> |
| 149 | + </View>); |
| 150 | +}; |
| 151 | +var styles = StyleSheet.create({ |
| 152 | + errorActionButtonText: { |
| 153 | + textAlign: 'center', |
| 154 | + color: colors.primary, |
| 155 | + fontSize: 16 |
| 156 | + }, |
| 157 | + errorActionButton: { |
| 158 | + paddingHorizontal: 16, |
| 159 | + paddingVertical: 16 |
| 160 | + }, |
| 161 | + errorText: { |
| 162 | + color: colors.secondary, |
| 163 | + textAlign: 'center', |
| 164 | + marginBottom: 32, |
| 165 | + fontSize: 18 |
| 166 | + }, |
| 167 | + error: { |
| 168 | + position: 'absolute', |
| 169 | + left: 0, |
| 170 | + right: 0, |
| 171 | + bottom: 0, |
| 172 | + top: 0, |
| 173 | + backgroundColor: '#ffffff', |
| 174 | + justifyContent: 'center', |
| 175 | + alignItems: 'center', |
| 176 | + paddingHorizontal: 56 |
| 177 | + }, |
| 178 | + backdrop: { |
| 179 | + position: 'absolute', |
| 180 | + left: 0, |
| 181 | + right: 0, |
| 182 | + bottom: 0, |
| 183 | + top: 0 |
| 184 | + }, |
| 185 | + loadingImage: { |
| 186 | + width: 64, |
| 187 | + height: 64, |
| 188 | + resizeMode: 'contain' |
| 189 | + }, |
| 190 | + loading: { |
| 191 | + position: 'absolute', |
| 192 | + top: 0, |
| 193 | + right: 0, |
| 194 | + bottom: 0, |
| 195 | + left: 0, |
| 196 | + backgroundColor: 'rgba(255, 255, 255, 0.3)', |
| 197 | + justifyContent: 'center', |
| 198 | + alignItems: 'center' |
| 199 | + }, |
| 200 | + webviewContainer: { |
| 201 | + top: Platform.select({ ios: 96, android: 64 }), |
| 202 | + flex: 1, |
| 203 | + backgroundColor: '#efefef', |
| 204 | + paddingBottom: Platform.select({ ios: 96, android: 64 }), |
| 205 | + overflow: 'hidden', |
| 206 | + borderTopLeftRadius: windowHeight * borderRadiusDimension, |
| 207 | + borderTopRightRadius: windowHeight * borderRadiusDimension |
| 208 | + }, |
| 209 | + webview: { |
| 210 | + flex: 1, |
| 211 | + backgroundColor: 'rgba(0,0,0,0)' |
| 212 | + } |
| 213 | +}); |
| 214 | +export default FlutterwaveCheckout; |
0 commit comments