Route is not executed when called after Paypal checkout

Asked

Viewed 31 times

1

Successful route is not activated after Paypal checkout that redirects to /Success

PS: My view is in English.

This is the part that is not being called when accessing /Success

app.get('/success', (req, res) => {
  const payerId = req.query.PayerID;
  const paymentId = req.query.paymentId;

  const execute_payment_json = {
    "payer_id": payerId,
    "transactions": [{
        "amount": {
            "currency": "USD",
            "total": "25.00"
        }
    }]
  };

Here the complete code:

const express = require('express');
const ejs = require('ejs');
const paypal = require('paypal-rest-sdk');
const app = express();

app.set('views', __dirname + '/view/src/index.js');
app.set('view engine', 'jsx');
app.engine('jsx', require('express-react-views').createEngine());
app.get('/', (req, res) => res.render('index'));

// config folder
paypal.configure({
  'mode': 'sandbox', //sandbox or live
  'client_id': 'sample',
  'client_secret': 'sample'
});
app.post('/pay', (req, res) => {
  const create_payment_json = {
    "intent": "sale",
    "payer": {
        "payment_method": "paypal"
    },
    "redirect_urls": {
        "return_url": "http://localhost:3000/success",
        "cancel_url": "http://localhost:3000/cancel"
    },
    "transactions": [{
        "item_list": {
            "items": [{
                "name": "Red Sox Hat",
                "sku": "001",
                "price": "25.00",
                "currency": "USD",
                "quantity": 1
            }]
        },
        "amount": {
            "currency": "USD",
            "total": "25.00"
        },
        "description": "Hat for the best team ever"
    }]
};

paypal.payment.create(create_payment_json, function (error, payment) {
  if (error) {
      throw error;
  } else {
      for(let i = 0;i < payment.links.length;i++){
        if(payment.links[i].rel === 'approval_url'){
          res.redirect(payment.links[i].href);
        }
      }
    }
  });
});

app.get('/success', (req, res) => {
  const payerId = req.query.PayerID;
  const paymentId = req.query.paymentId;

  const execute_payment_json = {
    "payer_id": payerId,
    "transactions": [{
        "amount": {
            "currency": "USD",
            "total": "25.00"
        }
    }]
  };

  paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
    if (error) {
        console.log(error.response);
        throw error;
    } else {
        console.log(JSON.stringify(payment));
        res.send('Success');
    }
  });
});

app.get('/cancel', (req, res) => res.send('Cancelled'));

app.listen(5000, () => console.log('Server Started'));

1 answer

0

This stretch should have the API 5000 port instead of 3000.

"return_url": "http://localhost:5000/success",
"cancel_url": "http://localhost:5000/cancel"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.