React
1、支付商家:Stripe。 React-Native-Stripe android版本是从0.2.0才开始支持。之前的版本只支持Apple Pay,android需要写原生代码。
2、根据React-Native android版本Google Pay示例代码,开发好后,用Debug版本测试,但是测试时是不会支付的,会报支付失败。如果用发行版本测试,会报商户未开通问题,需要先开通商户才能使用发行版本测试。
// Stripe Google Pay,安卓版本最低需要 4.2.0目录:android/build.gradlecom.android.tools.build:gradle:4.2.0目录:android/gradle/wrapper/gradle-wrapper.propertieshttps\://services.gradle/distributions/gradle-6.7.1-bin.zipnpm install @stripe/stripe-react-native后,先不要执行yarn命令先把yarn.lock和package.lock文件删除,不把这两个文件删除,会影响android运行,打包发行版本后可能会因为环境原因造成不知名的闪退。删除文件后,再执行yarn命令。 import { useGooglePay } from '@stripe/stripe-react-native';// Google Payconst { initGooglePay, presentGooglePay } = useGooglePay();const [canUseGooglePay, setCanUseGooglePay] = useState<boolean>(false);// 在需要Google Pay的页面或其他页面初始化时调用useEffect(() => { if (Platform.OS !== 'ios') { initGooglePay({ testEnv: false, // Debug模式时,需要设置为true merchantName: 'Stripe Test', // 商户名称,自定义的 countryCode: 'AU', // 国家代码,这里是澳大利亚 }).then(({ error }) => { if (error) { // 初始化Google Pay失败 } else { // 设置状态为支持谷歌支付 setCanUseGooglePay(true); } }); }}, []); // Google Pay按钮点击触发方法。Google Pay按钮必须要按照谷歌的样式,有黑色边框的图标,从Googe Pay上下载图标放上去就行,不然审核开通商户时不给通过。const onGooglePay = () => { if (!canUseGooglePay) return; const { error } = await presentGooglePay({ clientSecret, // clientSecret获取,和Apple Pay获取clientSecret是一样的,Stripe官网上的Apple Pay开发文档里有 forSetupIntent: false, // 暂时不知道啥意思,官网没有说 currencyCode: 'AUD', // 货币代码,这里是澳元 }); if (error) { // handle error } else { // 支付成功时 }};3、截图Google Pay支付流程,到 Google Pay Business Console 中,在Business profile中填写商户信息,在Google Pay API中,上传支付流程截图,提交Google审核,审核通过后将会开通商户,会有商户ID发送到邮件里,在启动App时,像设置Apple Pay商户ID一样设置Google Pay的商户ID。
React-Native Stripe Google Pay开发