简体   繁体   中英

Stripe Payment - Network request failed

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM