我正在使用 Stripe 构建我的第一个基本 Express 应用程序Checkout
。该应用程序呈现正确,但当我提交表单时,我在两个方面都收到错误stripeToken
and stripeEmail
。我不知道为什么它没有通过。有什么帮助吗?
My app:
const keyPublishable = process.env.PUBLISHABLE_KEY;
const keySecret = process.env.SECRET_KEY;
const app = require("express")();
const stripe = require("stripe")(keySecret);
app.set('view engine', 'ejs')
app.get("/", (req, res) =>
res.render("index.ejs", {keyPublishable}));
app.post("/charge", (req, res) => {
let amount = 500;
stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
})
.then(customer =>
stripe.charges.create({
amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
}))
.then(charge => res.render("charge.ejs"));
});
app.listen(7000);
and my index.ejs
<form action="/charge" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_Gbu2akKhNgGjbKi4LPxOOWqc"
data-amount="500"
data-name="Ojoseyewear"
data-description="Widget"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto">
</script>
</form>
问题是 Stripe 文档示例不包含 bodyParser。
req.Body
只是从表单提交中返回一个对象。如果没有 bodyParser,就无法获取这两个字符串。
将其添加到app.js
你是金色的:
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)