【问题标题】:Open non-standard protocol url in browser from react-native从 react-native 在浏览器中打开非标准协议 url
【发布时间】:2020-12-17 11:46:03
【问题描述】:

我必须使用 Chrome 或 Android 中的任何默认浏览器从我的 ReactNative 代码打开如下 URL。

upi://pay?pa=starbucks.payu@indus&pn=sample@okicici&am=5.00&tn=note&mc=&tr=1234567890

但是,当我打开此 URL 时出现错误:

可能的未处理承诺拒绝......没有找到要处理的活动 意图。

我的代码可以打开下面给出的链接:

let upi_url = "upi://pay?pa=starbucks.payu@indus&pn=sample@okicici&am=5.00&tn=note&mc=&tr=1234567890";
Linking.openURL(upi_url)

如果我传递https://www.google.com 之类的 URL,它就可以正常工作。但是,我有一个 QR 码阅读器应用程序,当我单击其中的打开网站按钮时,它能够读取上述 URL,然后使用浏览器打开它。只是想弄清楚要走的方向。

【问题讨论】:

  • 您确定这是一个有效的网址吗?
  • 这是印度的一种支付协议。并且会自动让 Android 显示所有支持的应用程序,例如 Google Pay、PhonePe 等……如果我通过 QR 码阅读器应用程序打开 URL,就会出现这种行为。它打开 chrome,然后重定向到相关的应用程序,与在 Gmaps 中打开的 google maps url 相同。

标签: react-native linker upi


【解决方案1】:

要使用意图流从 react-native 发起 UPI 支付,您只需使用来自 herereact-native-upi-pay 包。

以下是工作代码:

import React, { useState } from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
  Button,
  TextInput
} from 'react-native';



import upi_pay from 'react-native-upi-pay';

const App: () => React$Node = () => {

  const [ payment, setPayment] = useState({  status : "Not Set",
                                             txnId : "000000"
                                         });
  const [ response, setResponse] = useState({});
  const [ upiId, setUpiId ] = useState("starbucks.payu@indus");

  
                                             

  const pay = async() => {
    
    upi_pay.initializePayment({
      vpa: upiId, // or can be john@ybl or mobileNo@upi
      payeeName: 'SOME+PRIVATE+LIMITED',
      amount: '1', //the actual amount
      transactionRef: 'aasf-332-aoei-fn'
    },success,failure);

    

  }

  const success = (data) => {
    let status = { status : "SUCCESS", txnId : data['txnId']};
    setPayment(status);
    setResponse(data);
  }

  const failure = (data) => {
    let status = { status : "FAILURE", txnId : data['txnId']};
    setPayment(status);
    setResponse(data);
  }

  const handleUPIChange = (id)=>{      
      setUpiId(id);      
  }

  
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>

          <View style={styles.body}>
            <View style={styles.sectionContainer}>
              <Text style={styles.footer}>UPI INTENT FLOW</Text>
              <TextInput
                value={upiId}
                onChangeText={(upiId) => handleUPIChange(upiId)}
                placeholder={'UPI Id'}
                style={styles.input}
              />
              <Button title="pay" onPress={ ()=> pay() } />                               
            </View>
            <View style={styles.sectionContainer}>
            <Text>Result:</Text>
            <Text style={styles.sectionTitle}> 
               
               {  JSON.stringify(payment) }                                         
            </Text>
            <View style={styles.divider}></View>
            
            <Text>Output:</Text>
            <Text style={styles.sectionTitle}> 
               
               {  JSON.stringify(response) }
               
                          
            </Text>
            </View>
            
          </View>
        </ScrollView>
      </SafeAreaView>
    </>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: "#C3C3C3"
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: "white",
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
    
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: "black",
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: "black",
  },
  highlight: {
    fontWeight: '700',
  },
  footer: {
    color: "#3b3b3b",
    fontSize: 20,
    fontWeight: 'bold',
    padding: 4,    
    textAlign: 'left',
  },
  input: {
    borderColor: "black",
    borderWidth: 1,
    borderRadius: 4,
    paddingHorizontal: 10,
    paddingVertical: 5,
    marginBottom: 10 
  },
  divider: {
    borderBottomWidth: 2,
    borderBottomColor: "#c3c3c3",
    marginVertical: 10

  }
  
});

export default App;

【讨论】:

    猜你喜欢
    • 2016-06-02
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 2020-11-27
    相关资源
    最近更新 更多