使用 @stripe/stripe-react-native 处理 Stripe 时抛出错误

Error being thrown when dealing with Stripe using @stripe/stripe-react-native

我已将 Stripe 添加到我的 React-Native/Expo (SDK 42) 应用程序中。我正在尝试按照条纹网站上列出的“创建付款”方法进行操作,但它不断抛出以下错误:

undefined Unable to resolve module stripe from ../../../../.js stripe could not be found within the project.

我试过调用 Stripe,但我仍然不断收到错误消息。

下面是错误所在的代码:

import { CardField, useStripe, StripeProvider, Stripe } from '@stripe/stripe-react-native'

const stripe = require('stripe')('testKey')

const paymentMethod = async() => {
await stripe.paymentMethods.create({
    type: 'card',
    card:{
        number: '4242424242424242',
        exp_month: 9,
        exp_year: 2022,
        cvc: '314'
    }
})

}

我根据 here

找到的 Node.js 版本改编了这个

您在问题中包含的代码旨在通过 Stripe 的 Node 客户端库调用服务器端。它不能适应在客户端与 Stripe 的 React Native SDK 一起工作。

相反,您想调用 stripe.createPaymentMethod() 的 React Native 等价物(参见 JS 库 ref). There's a quick example in the React Native library docs about how to do this here(它最初是为从 Tipsi 库迁移的人编写的,但它也应该适用于你的用例)。