I'm writing a simple server to handle payment transactions with node js. I took getting started on heroku project template and added "checkout" route logic. When I deployed it on localhost - it worked fine for me. But after I deployed it to heroku server I'm getting "Cannot POST /checkout" error. My index.js code is the following:
const express = require('express'), bodyParser = require('body-parser');
const path = require('path')
const PORT = process.env.PORT || 5000
var router = express.Router();
var checkout = require('./routes/checkout');
var braintree = require('braintree');
const app = express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index'))
.use(bodyParser.urlencoded({extended: true}))
.post('/checkout', (req, res) => {
console.log(req.body.paymentMethodNonce);
//res.send(req.body);
var gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: "merchantId",
publicKey: "publicKey",
privateKey: "privateKey"
});
// Use the payment method nonce here
//console.log(req.body);
var nonceFromTheClient = req.body.paymentMethodNonce;
var amount = req.body.amount;
//Create a new transaction for $10
var newTransaction = gateway.transaction.sale({
//amount:'10.00',
amount: amount,
//paymentMethodNonce: "fake-valid-nonce",
paymentMethodNonce: nonceFromTheClient,
options: {
// This option requests the funds from the transaction
// once it has been authorized successfully
submitForSettlement: true
}
}, function(error, result) {
if (result) {
res.send(result);
} else {
res.status(500).send(error);
}
});
})
.listen(PORT, () => console.log(`Listening on ${ PORT }`))