You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native集成Stripe调用presentPaymentSheet导致应用崩溃求助

React Native + Django Stripe支付调起崩溃问题修复方案
    1. StripeProvider缺少必填参数
      你当前的StripeProvider只传了publishableKey,两个关键参数缺失:
    • 必须添加stripeVersion参数,值要和后端创建Ephemeral Key时指定的版本完全一致,也就是2020-08-27,版本不匹配会导致临时密钥校验失败,调起支付时原生层直接崩溃
    • iOS端必须传入merchantIdentifier参数,值为你苹果开发者后台配置的支付商户ID,否则iOS环境调起支付会直接闪退
      修正后的StripeProvider示例:
<StripeProvider
    publishableKey="pk_test_Dt4ZBItXSZT1EzmOd8yCxonL"
    stripeVersion="2020-08-27"
    merchantIdentifier="merchant.com.你的商户名" // 仅iOS需要,安卓可正常兼容
>
    1. initPaymentSheet缺少必填参数且未做错误校验
      当前initPaymentSheet没有传入必填的merchantDisplayName参数(商户展示名,支付弹窗会显示),且你没有判断init返回的错误,只要Promise没被拒就把loading设为true,实际上如果init返回了错误对象,后续调用present必然崩溃。
      修正后的init逻辑:
initPaymentSheet({
    customerId: res.customer,
    customerEphemeralKeySecret: res.ephemeralKey,
    paymentIntentClientSecret: res.paymentIntent,
    merchantDisplayName: "你的电商平台名称" // 必填参数
}).then((initRes) => {
    // 必须判断初始化是否有错误
    if (!initRes.error) {
        setLoading(true)
    } else {
        console.log("支付初始化失败", initRes.error)
    }
})
    1. presentPaymentSheet参数传递错误
      你调用presentPaymentSheet时多余传入了{ clientSecret }参数,Stripe React Native SDK的presentPaymentSheet不需要额外传参,所有必要参数已经在initPaymentSheet时完成配置,多余传参会导致原生层参数解析失败直接崩溃。
      修正后的openPaymentSheet逻辑:
const openPaymentSheet = async () => {
    // 不要传任何参数
    const { error } = await presentPaymentSheet();
    if (error) {
        Alert.alert(`Error code: ${error.code}`, error.message);
    } else {
        Alert.alert('Success', 'Your order is confirmed!');
    }
};
    1. useEffect依赖项缺失
      当前useEffect的依赖数组为空,如果页面挂载时token或subtotal还未加载完成(比如用户信息是异步获取的),调用initializePaymentSheet时传入的是undefined,会导致后端返回错误,初始化失败后你依然可能把loading设为true,点击按钮就崩溃。
      修正useEffect依赖:
useEffect(() => {
    // 确保token和subtotal都存在再初始化
    if (token && subtotal) {
        initializePaymentSheet(token, subtotal)
    }
}, [token, subtotal])
    1. 接口返回异常未做兜底
      你调用fetchPaymentSheetData后没有判断返回结果是否合法,如果接口报错、网络异常返回的res为undefined,访问res.paymentIntent会抛出异常,后续初始化逻辑直接失效。建议增加返回值校验:
fetchPaymentSheetData(token, subtotal)
.then((res) => {
    // 增加返回值校验
    if (!res || !res.paymentIntent || !res.ephemeralKey || !res.customer) {
        console.log("支付参数获取失败")
        return
    }
    setClientSecret(res.paymentIntent)
    // 后续init逻辑
})

内容的提问来源于stack exchange,提问作者Felipe Paz Martinez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 02:15:02