[英]Stripe and NodeJS - Passing an amount to charge
EDIT: To clarify, the charge does actually process, the only thing I can't do is dynamically set the charge amount. 编辑:澄清一下,费用确实在处理,我唯一不能做的就是动态设置费用金额。
I've worked all day and haven't found any solution. 我已经工作了一整天,还没有找到任何解决方案。 Here is my server.js: 这是我的server.js:
app.post('/charge', function(req, res) {
var stripeToken = req.body.stripeToken;
var amount = 12000;
console.log(req);
stripe.charges.create({
card: stripeToken,
currency: 'usd',
amount: amount
},
function(err, charge) {
if (err) {
res.send(500, err);
} else {
res.send(204);
}
});
});
And here is my dynamic stripe button. 这是我的动态条纹按钮。
$('#calcTransAmount').click(function(event) {
var amount = $('#transAmount').scope().totall;
//console.log(amount);
var holder = document.getElementById('hello');
var script = document.createElement('script');
script.src = 'https://checkout.stripe.com/checkout.js';
script.setAttribute('class', 'stripe-button');
script.setAttribute('data-amount', amount);
script.setAttribute('data-key',"KEY");
script.setAttribute('data-image', "IMG");
script.setAttribute('data-name', "NAME");
script.setAttribute('data-billing-address', "true");
script.setAttribute('data-description', "Purchase");
script.setAttribute('data-locale', "auto");
document.getElementById('btnCont').appendChild(script);
});
The button works with the dynamic amount and everything, now I just can't seem to figure out how the post to /charge
receives the amount and sets it as a variable. 该按钮适用于动态金额和所有内容,现在我似乎似乎无法弄清楚/charge
帖子如何接收金额并将其设置为变量。
This is my req
object: 这是我的req
对象:
{ stripeToken: 'tok_19t9jcLhf04QCVXXXXXXXX',
stripeTokenType: 'card',
stripeEmail: 'me@gmail.com',
stripeBillingName: 'Clark',
stripeBillingAddressCountry: 'United States',
stripeBillingAddressCountryCode: 'US',
stripeBillingAddressZip: 'Zip',
stripeBillingAddressLine1: 'My address',
stripeBillingAddressCity: 'City',
stripeBillingAddressState: 'State' }
And I can't find a way to get my data-amount attribute from the button to the server to charge a dynamic amount. 而且,我找不到从按钮获取我的数据量属性到服务器以收取动态金额的方法。
Thanks 谢谢
Special thanks to @koopajah for the inspiration for the solution. 特别感谢@koopajah为解决方案带来的灵感。
In the form on the client side, in addition to the javascript button, I passed a key value using a hidden input box that would allow my server side to grab the cart total from our DB. 在客户端的表单中,除了javascript按钮外,我还使用了一个隐藏的输入框传递了一个键值,这将允许我的服务器端从数据库中获取购物车总额。
<form action="/charge" method="POST">
<input name="coKey" value ="{{currentUser.coKey}}">
</form>
Then on the server side, I can pull the appropriate key like this: var coKey = req.body.coKey;
然后在服务器端,我可以像这样拉出适当的键: var coKey = req.body.coKey;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.