【问题标题】:Error being thrown when dealing with Stripe using @stripe/stripe-react-native使用 @stripe/stripe-react-native 处理 Stripe 时抛出错误
【发布时间】:2021-11-08 22:38:25
【问题描述】:

我已将 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'
    }
})

}

我改编自找到的 Node.js 版本 here

【问题讨论】:

  • 您使用的是哪个版本的stripe-react-native
  • @joachimwedin 我们在 Expo SDK 42 上使用 "@stripe/stripe-react-native": "^0.2.1"
  • 你改编的代码被称为服务器端的,不能与 Stripe 的 React Native SDK 一起使用。您想适应 stripe.createPaymentMethod() 调用 (stripe.com/docs/js/payment_methods/create_payment_method)。
  • 这里有一个关于如何使用createPaymentMethod 的快速示例,供从这里的 Tipsi 库迁移的人使用 (github.com/stripe/stripe-react-native/blob/…),以防万一
  • @karbi 前几天我也在 Reddit 上发布了这个帖子,收到了同样的回复,这让我感觉好多了,谢谢。如果您想要 Stack 积分,请回复此帖子,我将接受它

标签: javascript node.js react-native expo stripe-payments


【解决方案1】:

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

相反,您希望调用与 stripe.createPaymentMethod() 等效的 React Native(请参阅 JS 库 ref)。在 React Native 库文档中有一个关于如何执行此操作的简单示例 here(它最初是为从 Tipsi 库迁移的人编写的,但它也应该适用于您的用例)。

【讨论】:

    猜你喜欢
    • 2014-05-11
    • 2017-04-26
    • 2017-05-12
    • 1970-01-01
    • 2021-05-28
    • 2022-12-28
    • 2021-09-07
    • 2015-07-01
    • 2021-05-22
    相关资源
    最近更新 更多