您好,我是 NodeJS 新手,我刚刚创建了一个非常简单的 API 来传递 stripe 中的一些数据,只是为了查看 stripe.charges.create
的功能,我想创建一个 API 来传递我的 token 并创建费用,但我真的不知道如何在用户单击付款时获取数据。
这是我的客户端
var handler = StripeCheckout.configure({
key: 'MY publiskKEY',
image: 'img/logo.jpg',
locale: 'auto',
token: function(token) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
$http({
method: 'POST',
url: 'http://localhost:8080/api',
data: {
token: token.id
}
}).success(function(data) {
}).error(function() {
});
console.log(token)
console.log(token.id)
}
});
$scope.stripeForm = function(e) {
handler.open({
name: 'Sample Form',
description: 'Payment',
amount: 1000
});
// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
}
这是我的服务器端
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var stripe = require('stripe')('my publish key');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var router = express.Router();
router.get('/', function(req, res) {
var charge = stripe.charges.create({
amount: 1000,
currency: "usd",
description: "Example charge",
source: 'This is i want to store my token.id',
}, function(err, charge) {
console.log(err, charge)
});
res.json({ token: token });
});
app.use('/api', router);
app.listen(port);
console.log('Magic happens on port ' + port);
抱歉这个菜鸟问题,我只是 NodeJS 的新手。
最佳答案
首先将api
中的get
更改为post
由于您使用的是来自客户端的 post 请求,因此您应该使用 post
现在,您将在 req.body
中获取从客户端发送的数据,因为您使用了 body-parser
将正文附加到请求。
因此,您可以使用您的 token ,如 req.body.token
router.post('/', function(req, res) {
// here you can use your request object
// like req.body.token req.body
console.log(req.body)
console.log(req.body.token)
var charge = stripe.charges.create({
amount: 1000,
currency: "usd",
description: "Example charge",
source: 'This is i want to store my token.id',
}, function(err, charge) {
console.log(err, charge)
});
res.json({ token: token });
});
关于javascript - 将客户端的数据传递到服务器 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44453730/