【问题标题】:Stripe Payment - Network request failedStripe Payment - 网络请求失败
【发布时间】:2020-11-11 21:53:26
【问题描述】:

我在 React Native 应用程序中使用 Stripe 进行支付。对于后端,我运行了 NodeJS,它运行良好,这意味着当我通过令牌时,付款成功借记。但是,在本机反应方面,我正在获取客户卡详细信息并创建令牌,然后将此令牌传递给我的 NodeJS 服务器进行付款,但每次都会出现网络错误。

反应原生代码

pay() {
        stripe.createToken({
            card: {
                "number": '4242424242424242',
                "exp_month": 12,
                "exp_year": 2020,
                "cvc": '123',
                "name": "RAM",
               
            }
            }).then((token_object) => {
                
                fetch('https://<IP address>:3000/pay', {
                    method:"POST",
                    headers: {
                        'Content-Type': 'application/json',
                      },
                      body: JSON.stringify(token_object)
                }).then(response => response.json())
                .then(data => {
                  console.log('Success:', data);
                })
                .catch((error) => {
                  console.error('Error:', error.message);
                });
            console.log(token_object.id);
            });
    }

NODEJS 代码

const express = require ('express')
const cors = require('cors')
const stripe = require ('stripe')('sk_test_')
const app = express()
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 3000

app.use(bodyParser.json())
app.use(cors())

app.get('/',(req,res) =>{
    res.send("hello from NodeJS!!!!")
})

app.post('/pay',async (req,res) =>{
    console.log(req.body)
    try {
        const {token} = req.body,
         charge = await stripe.charges.create({
            amount: 15 * 100,
            currency: 'inr',
            description: 'Jewwllwry',
            source: (token),
          });
          console.log("charged",{charge})
          res.send("payment done")
    } catch (error) {
        console.log(error)
    }
})

app.listen(PORT, ()=>{
    console.log("server is running on port" + PORT)
})

【问题讨论】:

  • 如果你移动了console.log(token_object.id);上面的 fetch,你看到正确的值了吗?
  • 是的,令牌在该位置可用。我只是通过向上移动它来检查它。
  • 刚刚编辑了我的问题,但放入了完整的 NodeJS 代码。
  • 好的,那么如果你向节点 api 发送一个 get 请求,它会起作用吗?或者尝试一个基本的帖子,它会返回一个字符串或其他东西,很难说一个解决方案,但一步一步尝试
  • 我尝试了获取请求,但这个也失败了。我现在该怎么办??

标签: node.js react-native stripe-payments fetch-api


【解决方案1】:

当您在本地环境中工作时,请尝试使用 http 而不是 https 发送您的请求。

【讨论】:

  • 是的,在本地环境中。尝试使用 https 和 http 但无济于事
  • 好吧,你是说你在fetch 请求中尝试了http,但它也没有用。当您在请求正文中使用 JSON.stringify() 时。在您的 api 中尝试source: JSON.parse(token)
  • 我使用来自 ipconfig 命令的 IPv4 地址代替 IP。跟这个有关系吗??
  • 尝试使用JSON.parse(token)、https、http。每次获取网络请求失败
  • 删除 response.json() 并在那里记录响应,看看它给你什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 2019-01-06
  • 2018-06-11
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多