I'm using node and stripe to handle payments. I've got a /charge
route setup that takes a bunch of parameters from the front end and renders a receipt.
I'm wondering how i redirect from a POST
route to a GET
route in order to prevent errors when someone tries to reload the page after the POST
route has loaded?
These are my routes so far. I have a payment button at /
that sends a POST request to /charge
.
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/charge', function(req, res, next) {
var stripeToken = req.body.stripeToken;
var stripeEmail = req.body.stripeEmail;
var amount = 3999;
var description = "Product description";
var charge = stripe.charges.create({
amount: amount, // amount in cents, again
currency: "gbp",
source: stripeToken,
receipt_email: stripeEmail,
description: description
}, function(err, charge) {
if (err && err.type === 'StripeCardError') {
// The card has been declined
} else {
res.render('charge', {
title: 'Charge',
descrption: charge.description,
total: (charge.amount / 100) + charge.currency.toUpperCase(),
trans_id: charge.id
});
}
});
});
As i have it now, the /charge
route errors really badly, and breaks the app if the page is reloaded after submitting. This is due to the token only being valid on first try, so the charge
object is empty on all attempts after this.
What's the best way to handle this issue? Redirect to a new page? How do i get the charge
values to persist on to this new page? Can i redirect to the homepage on refresh??
Any advice on this would be appreciated!