我正在尝试使用reactJS和expressJS创建一个条纹支付应用程序,出现此错误:
代理错误:无法将来自本地主机的请求/付款代理:3000到https://localhost:5000/
有关更多信息,请参见https://nodejs.org/api/errors.html#errors_common_system_errors(EPROTO)
在package.json文件中,我将代理设置为-
"proxy": "https://localhost:5000"
在我的react组件中-
const onToken = token => {
axios({
url: "payment",
method: "post",
data: {
amount: priceForStripe,
token: token
}
})
.then(response => {
alert("succesful payment");
})
.catch(error => {
console.log("Payment Error: ", error);
alert(
"There was an issue with your payment! Please make sure you use the provided credit card."
);
});
};
在我的server.js中-
const stripe = require("stripe")("sk_test_...");
app.post("/payment", (req, res) => {
const body = {
source: req.body.token.id,
amount: req.body.amount,
currency: "usd"
};
stripe.charges.create(body, (stripeErr, stripeRes) => {
if (stripeErr) {
res.status(500).send({ error: stripeErr });
} else {
res.status(200).send({ success: stripeRes });
}
});
});
每当我提交付款时,我都遇到错误-
我尝试了所有与here链接的方法,但无法解决该问题。我衷心感谢任何人解释该问题的任何解决方案。
答案 0 :(得分:1)
如@CherryDT所述,首先我将代理设置为"proxy": "http://localhost:5000"
。然后按照@Greg M的建议更改后端代码-
app.post("/payment", (req, res) => {
stripe.customers
.create({
email: req.body.email, // customer email, which user need to enter while making payment
source: req.body.token.id // token for the given card
})
.then(customer =>
stripe.charges.create({
// charge the customer
amount: req.body.amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
})
)
.then(charge => res.status(200).send({ success: "success" }));
});
就是这样。我的付款方式效果很好。
答案 1 :(得分:0)
我认为代理错误是红色鲱鱼。真正的问题是服务器上的解析,导致500。
看起来by default是Axios为您编码的json(但您应仔细检查请求)。要在Express中访问JSON编码的请求正文数据,您需要使用body-parser
middleware。
有关示例,请参见以下答案:How do I consume the JSON POST data in an Express application
答案 2 :(得分:0)
由于后端可以正常工作而没有条纹,因此500错误表明出现条纹是问题所在。
这与您在条带费用.create请求正文中发送的信息有关。我认为您缺少了customer.id。
这篇文章arjunphp.com/node-stripe-express-js显示charges.create请求为
{ amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
}
答案 3 :(得分:0)
我正在从安德烈(Andre)那里进行确切的反应。我的解决方案是启动后端服务器。
因此,从同一课程中遇到此问题的任何人都可以尝试上面的答案或:
npm start
或
yarn start