探索React Native Payments:实现无缝移动支付体验
import React from 'react';
import { Text, View, Button } from 'react-native';
import { usePaymentRequest } from 'react-native-payments';
const PaymentScreen = () => {
const paymentRequest = {
currency: "USD",
total: { label: "Demo Total", amount: { currency: "USD", value: "100.00" } },
countryCode: "US",
supportedNetworks: ["visa", "mastercard"],
};
const { canMakePayments, requestPayment } = usePaymentRequest(paymentRequest);
const handlePaymentRequest = async () => {
try {
const token = await requestPayment();
// 使用token处理支付
console.log("支付成功,支付令牌: ", token);
} catch (err) {
console.error("支付失败: ", err);
}
};
return (
<View>
<Text>支付屏幕</Text>
<Button
title="请求支付"
disabled={!canMakePayments}
onPress={handlePaymentRequest}
/>
</View>
);
};
export default PaymentScreen;
这个代码实例展示了如何在React Native应用中集成移动支付功能。它使用了react-native-payments
库中的usePaymentRequest
钩子来请求支付。用户点击按钮后,会触发handlePaymentRequest
函数,该函数尝试发起支付请求,并在成功或失败后打印相关信息。
评论已关闭