【发布时间】: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