Stripe Payment - Network request failed

旧街凉风 提交于 2020-08-10 19:17:36

问题


I am using stripe for the payment in react native app. For the backend, I have NodeJS running and it is working fine means when I pass the token, the payment gets successfully debited. However, in react native side, I am getting customer card details and creating token and then passing this token to my NodeJS server for payment but every time it gets network error.

React native Code

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 code

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)
})

回答1:


Try sending your request with http instead of https as you you are working on local env.



来源:https://stackoverflow.com/questions/63032784/stripe-payment-network-request-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!