代码之家  ›  专栏  ›  技术社区  ›  Mort Raphael Souza

在浏览器中响应本机开放链接并返回应用程序

  •  6
  • Mort Raphael Souza  · 技术社区  · 6 年前

    我在React Native中开发了一个应用程序,它应该与支付网关通信,在完成支付过程(成功或失败)后,我需要向用户显示一个警报。为此,我在 WebView 然后我得到返回的URL onNavigationStateChange 并显示成功或失败消息。

    但是,这种流动 安全 问题必须在 默认设备浏览器 .

    当前代码:

    const BASEURL = 'https://gatewayURL/?ID=';
    let Token = null;
    let paymentAccepted = null;
    let paymentFactorId = null;
    
    class Gateway extends PureComponent {
      static propTypes = {
        dispatch: PropTypes.func,
        navigation: PropTypes.any,
      }
    
      componentWillMount() {
        this.props.dispatch(getPaymentStatus());
      }
    
    
      _onLoad(webViewState) {
        let url = webViewState.url.toString();
        let isResponseValid = url.includes('backFromGateway');
        if(isResponseValid){
          if(this.props.checkedPaymentStatus != 'checked' ){
            setTimeout(() => {
              this.props.dispatch(setPaymentStatus('checked'));
    
              let splitedURL = url.split("/");
              paymentFactorId = splitedURL[splitedURL.length -2];
              if(splitedURL[splitedURL.length - 1] === '0'){
                paymentAccepted = true;
                this.props.dispatch(setGatewayResponse('done', paymentFactorId));
              }
              else {
                paymentAccepted = false;
                this.props.dispatch(setGatewayResponse('rejected', paymentFactorId));
              }
    
    
              this.props.navigation.navigate('BackFromGateway', { title: '' })
            }, 1000);
          }
        }
      }
    
    
      render() {
        const { addNewOrderGatewayToken, checkedPaymentStatus } = this.props;
        token = addNewOrderGatewayToken;
        let view = null;
        if(checkedPaymentStatus !== 'checked'){
          view =  <WebView onNavigationStateChange={this._onLoad.bind(this)} style={styles.container} source={{ uri: `${BASEURL}${token}`  }}/>
        }
        else{
          view = <View></View>
        }
    
        return (
          <View style={styles.container}>
            {view}
          </View>      
        );
      }
    }
    

    知道吗?
    谢谢

    2 回复  |  直到 6 年前
        1
  •  8
  •   blaz    6 年前

    here gatewaylistener

    gatewaylistener://success?id={paymentId} gatewaylistener://error?id={paymentId}

    // setup
    componentDidMount() {
      Linking.getInitialURL().then((url) => {
        if (url) {
          this.handleOpenURL(url)
        }
      }).catch(err => {})
      Linking.addEventListener('url', this.handleOpenURL)
    }
    
    componentWillUnmount() {
      Linking.removeEventListener('url', this.handleOpenURL)
    }
    
    // open your gateway
    async openGateWay = () => {
      const { addNewOrderGatewayToken } = this.props
      const url = `${BASEURL}${addNewOrderGatewayToken}`
      const canOpen = await Linking.canOpenURL(url)
      if (canOpen) {
        this.props.dispatch(setPaymentStatus('checked'))
        Linking.openURL(url)
      }
    }
    
    // handle gateway callbacks
    handleOpenURL = (url) => {
      if (isSucceedPayment(url)) { // your condition
        // handle success payment
      } else {
        // handle failure
      }
    }
    
        2
  •  0
  •   jdnichollsc    5 年前

    InAppBrowser

      getDeepLink (path = '') {
        const scheme = 'my-demo'
        const prefix = Platform.OS === 'android' ? `${scheme}://demo/` : `${scheme}://`
        return prefix + path
      }
    
      async tryDeepLinking () {
        const redirectToURL = `https://proyecto26.github.io/react-native-inappbrowser/`
        const redirectUrl = this.getDeepLink('home')
        const url = `${redirectToURL}?redirect_url=${encodeURIComponent(redirectUrl)}`
        try {
          if (await InAppBrowser.isAvailable()) {
            const result = await InAppBrowser.openAuth(url, redirectUrl)
            await this.sleep(800)
            Alert.alert('Response', JSON.stringify(result))
          } else {
            // You can use Linking directly for iOS < 9
          }
        } catch (error) {
          Alert.alert('Something’s wrong with the app :(')
        }
      }