I've looked all over for answers, but haven't found a solution. I'm using Stripe to charge users, however, the payment shouldn't be hardcoded since the pricing changes depending on the variest questions answered. What I'm wanting to do is grab the 'total price' given on the confirmation page (HTML) and charge that amount to Stripe (using Node).
I currently have the tokenization working and the charge is successful when the amount is hardcoded, but I need the charge amount to change. Does anyone know if this is possible with Stripe (www.stripe.com)?
my app.js file (portion):
// 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}`);
});
UPDATE
I also want to update the users' email information from the input form rather than it being hardcoded like it currently is on this line: email: "random-email@gmail.com"
Second Update
Stripe form:
<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>
Function found at the bottom of HTML page in a script tag:
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();
}