0

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 }`))
Elena Rubilova
  • 367
  • 4
  • 16

4 Answers4

1

If you are using the free version of Heroku you can only send data not receive.

Zachary
  • 15
  • 3
0

@Elena You can use the static file after the post method

0

issue was solved after redeploying and running the following commands:

heroku login
heroku git:clone -a repository_name

replace source code with the code I had (no changes but excluding node_modules folder)

git add .
git commit -m "added updated code"
git push heroku master
Elena Rubilova
  • 367
  • 4
  • 16
-1
const express = require('express');
const bodyParser = require('body-parser');
var checkout = require('./routes/checkout');
var braintree = require('braintree');
const path = require('path')
const PORT = process.env.PORT || 5000
var router = express.Router();
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));


app.use(express.static(path.join(__dirname, 'public')));
app.set("views", (__dirname + "/views"));
app.set("view engine", "ejs");

app.get('/', (req, res) => res.render('pages/index'))
app.post('/checkout', (req, res) => {

  console.log(req.body.paymentMethodNonce);

  var gateway = braintree.connect({
        environment: braintree.Environment.Sandbox,
        merchantId: "merchantId",
        publicKey: "publicKey",
        privateKey: "privateKey"
  });

    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);
                }
           }
    )
});
Joshua
  • 1