【发布时间】:2019-08-31 06:34:27
【问题描述】:
我正在构建一个允许用户注册和支付活动费用的 Stripe 自定义结帐集成。注册选项非常简单,仅包含 4 个类别 - 价格相同。
我已经成功地将类别标题传递给 Stripe Checkout 弹出窗口中的 description 参数,但是在将相同的描述参数传递给 Stripe API 时遇到了麻烦。
测试充电成功后,Stripe 会返回带有 'description: null' 的对象。
将字符串添加到描述参数会正确返回字符串,但是我希望它返回表单中设置的类别标题。
索引.HTML
<script>
var checkoutHandler = StripeCheckout.configure({
key: "pk_test_stripeTestKey",
locale: "auto",
});
// var button = document.getElementById("submitButton");
// button.addEventListener("click", function(ev) {
function checkout(ev) {
var description = $('#categoryDesc').val();
checkoutHandler.open({
image: "/cj-stripe-icon.jpg",
name: 'The EVENT 2019',
description: description, //$("#categoryDesc").val() + " - " + "$130",
amount: 13000,
token: handleToken
});
};
// });
function handleToken(token) {
fetch("/charge", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(token)
})
.then(response => {
if (!response.ok)
throw response;
return response.json();
})
.then(output => {
console.log("Purchase succeeded:", output);
submitToAPI(event);
document.getElementById("shop").innerHTML = "<h2>Thank you for registering!</h2><h3>You should receive an email confirmation shortly.</h3>";
})
.catch(err => {
console.log("Purchase failed:", err);
})
}
</script>
APP.JS
onst express = require("express");
const stripe = require("stripe")(keySecret);
const bodyParser = require("body-parser");
const app = express();
app.use(express.static("public")); // serves content from the public directory
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.post("/charge", (req, res) => {
let amount = 13000;
stripe.customers.create({
email: req.body.email,
card: req.body.id
})
.then(customer =>
stripe.charges.create({
amount,
currency: "usd",
description: req.body.description,
customer: customer.id
}))
.then(charge => res.send(charge))
.catch(err => {
console.log("Error:", err);
res.status(500).send({error: "Purchase Failed"});
});
});
【问题讨论】:
标签: javascript node.js express stripe-payments