要使用意图流从 react-native 发起 UPI 支付,您只需使用此处react-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;