cancel
Showing results for 
Search instead for 
Did you mean: 

Get total amount in const execute_payment_json object.

Member

Get total amount in const execute_payment_json object.

Hello, I need help with paypal. I am using the paypal node integration(https://github.com/paypal/PayPal-node-SDK) with my express app and I can successfully get a payment from my code. Except there is a problem the total amount is hardcoded. 

Here is my code:

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

paypal.configure({
  'mode': 'sandbox', //sandbox or live
  'client_id': 'AaU8tQfmz1_MFDTKuf84yYERXvdDt2ZFJVrxhNW_49DazF4A_F0VBuKyV5_nntyEdZqUa5Oq9ZBj65GV',
  'client_secret': 'EAZ8aFDU4lHHLy1bQqULYWqznf3dBknXZW3AH__zFC0bUs8AGUyR6RNbm-jHvqtikX7PsSqMO5vxuvKm'
});

const app = express();

app.set('view engine', 'ejs');

app.get('/', (req, res) => res.render('index'));

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'));

in the app.get('/success route') route, there is a constant

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

 My question is, How can I get the the total amount from app.post('/pay') in order to get the correct "total" amount.

3 REPLIES 3
New Community Member

Re: Get total amount in const execute_payment_json object.

I have the same problem. Please let me know if you can solve it. 

Member

Re: Get total amount in const execute_payment_json object.

I faced the same problem. I just commented out the 'transactions' array from the  'execute_payment_json' property and everything worked just fine.

If you don't want to comment it out, you can save the total transaction amount in a local variable then access it from there when you need it.

Member

Re: Get total amount in const execute_payment_json object.

Hi, guys!

Facing the same problem as well. What's the way to pass something not equal to 1 in the quantity field and how do I get amount calculated according to the quantit number?