我已经到处寻找答案,但还没有找到解决方案。我使用Stripe向用户收费,但是付款不应该硬编码,因为价格根据回答的最不正确的问题而变化。我想要做的就是抓住总价格'在确认页面(HTML)上给出并向Stripe收费(使用Node)。
我目前正在进行令牌化工作,并且当金额被硬编码时收费成功,但我需要更改费用金额。有没有人知道Stripe(www.stripe.com)是否可以这样做?
我的app.js文件(部分):
// charge route
app.post('/charge', (req, res) => {
const amount = 2500; <-- needs to change to not be hardcoded
stripe.customers.create({
email: "random-email@gmail.com",
source: req.body.mytoken
})
.then(customer => {
stripe.charges.create({
amount,
description:'item desc',
currency:'usd',
customer:customer.id
})})
.then(charge => res.send('success'));
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
更新
我还想更新用户&#39;来自输入表单的电子邮件信息,而不是像现在这样的硬编码:email: "random-email@gmail.com"
第二次更新
条纹形式:
<div class="" style="margin-top: 60px;">
<h2 class="quote-info">Estimated total: $<span id="new_text"></span> USD</h2>
</div>
<!-- Payment form -->
<form action="/charge" method="post" id="payment-form">
<div class="form-row">
<label for="card-element">
Credit or debit card
</label>
<div id="card-element">
<!-- a Stripe Element will be inserted here. -->
</div>
<!-- Used to display form errors -->
<div id="card-errors"></div>
</div>
<button>Submit Payment</button>
</form>
在脚本标记的HTML页面底部找到的功能:
function stripeTokenHandler(token) {
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
var formData = JSON.stringify({
mytoken: token.id
});
$.ajax({
type: "POST",
url: "/charge",
data: formData,
success: function(){alert("done")},
dataType: "json",
contentType: "application/json"
});
form.submit();
}
答案 0 :(得分:1)
您需要做的是从您的前端到后端进行通信,并告诉后端收取一定数量的电子邮件+某些电子邮件。这样做的方法是在javascript中从前端向节点后端发出POST请求。以下是您需要做的事情:
This stack overflow回答提供了一个使用此方法从前端向后端发送数据的快速示例。
答案 1 :(得分:1)
您需要从Express中的表单中检索POST数据。
基于您当前实施的阻力最小的路径。
首先,您需要确保将总金额从表单传递到后端:
function stripeTokenHandler(token) {
var form = document.getElementById('payment-form');
var token = document.createElement('input');
token.setAttribute('type', 'hidden');
token.setAttribute('name', 'stripeToken');
token.setAttribute('value', token.id);
var totalAmount = document.createElement('input');
token.setAttribute('type', 'hidden');
token.setAttribute('name', 'totalAmount');
token.setAttribute('value', $('#new_text').innerHTML);
// or, prefereably, grab this directly from the var you're using to update the #new_text span's value
form.appendChild(token);
form.appendChild(totalAmount);
// You could also just use the variables here (i.e. token.id) instead of grabbing it from the inputs generated above
var formData = JSON.stringify({
mytoken: token.value,
totalAmount: totalAmount.value
});
// You're not actually referencing the form here, so you don't technically need to append the inputs above.
// I've only included them for consistency. You're setting the values to be submitted to /charge when you set
// the variable formData above and then passing it via the data property of $.ajax below.
$.ajax({
type: "POST",
url: "/charge",
data: formData,
success: function(){alert("done")},
dataType: "json",
contentType: "application/json"
});
form.submit();
}
然后你应该能够做到这一点:
app.post('/charge', (req, res) => {
const amount = req.param('totalAmount');
...
在这种情况下,您不会在表单中添加隐藏的输入,而是将表单的action
更新为:
action="/charge/TOTAL_AMOUNT/EMAIL"
然后修改您的Express路线以引用此值likeo:
app.get('/charge/:totalAmount/:email', (req, res) => {
const amount = req.params.totalAmount;
const email = req.params.email;
...
<强> Scotch.io also has a great explanation of handling POST parameters in Express. 强>