- This is the html:
<body ng-controller="stripeController">
<form action="/stripe" method="POST" id="payment-form">
<span class="payment-errors"></span>
<div class="from-row">
<lebel>
<span>Price:{{getTotal}}$</span>
<input type="hidden" data-stripe="number">
</lebel>
</div>
<div class="form-row">
<label>
<span>Card Number</span>
<input type="number" size="20" data-stripe="number">
</label>
</div>
<div class="form-row">
<label>
<span>Expiration (MM/YY)</span>
<input type="text" size="2" data-stripe="exp_month">
</label>
<span> / </span>
<input type="text" size="2" data-stripe="exp_year">
</div>
<div class="form-row">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc">
</label>
</div>
<input type="submit" class="submit" value="Submit Payment">
</form>
</body>
这是 Controller 代码:
app.controller('stripeController', function($scope, $http) { $http.get('/api/me').success(function (response) { $scope.userInfo = response; $scope.isCartEmpty = true; $scope.userCart = []; if($scope.userInfo.cart.length>0){ $scope.isCartEmpty= false; } console.log($scope.isCartEmpty); for(var i=0;i<$scope.userInfo.cart.length;i++){ $scope.userCart[i] = $scope.userInfo.cart[i].productId } var y = 0; $scope.getTotal =0; for(i=0;i<$scope.userCart.length;i++) { $http.get('/api/product/' + $scope.userCart[i]).success(function (response) { $scope.userInfo.cart[y].name = response.name; $scope.userInfo.cart[y].price = response.price; $scope.getTotal+= response.price; y++; }); } });
});
这是node.js post 函数:
router.post('/stripe', function(req, res){ //Need to check if the cart empty than re-direct to catalog. var stripe = require("stripe")( "sk_test_mypass" ); stripe.charges.create({ amount: ????? * 100, currency: "usd", source: req.body.stripeToken, // obtained with Stripe.js description: "Test Charge" }, function(err, charge) { if(err){ console.log("Error"); } console.log("Successfully bought product!"); }); });
-我有 1 个问题: 在node.js函数中我放置了“??????”我怎样才能从 html 形式。当我输入 req.body.getTotal 时,我没有得到数据...
最佳答案
您需要将其发送到 hidden field ,但最好在服务器端根据购物车中的产品 ID 和数量计算总数,这样客户端就没有机会更改金额。
关于html - 我如何将值从 Angular 形式转移到服务器nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42069951/