Get total amount in const execute_payment_json object.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
thanks, deleting it worked great, you don't need it, in the json.stringify(payment) has all the data you need for database storage, including the order info, assuming you dynamically created the item_list in the previous step of creating an order
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Haven't Found your Answer?
It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.
- Processor Declined for Braintree Sandbox transaction in Braintree Server-side Integration (PHP, Java, .NET, Ruby, Python, NodeJS SDKs)
- Paypal Payout in android kotlin. I have NON_HOLDING_CURRENCY before redirecting to paypal website. in REST APIs
- PAYER_ACTION_REQUIRED when creating order using REST API in REST APIs
- How to update purchase units of order subscription? in REST APIs
- I am getting an "Uncaught Error: Expected an order id to be passed" in REST APIs