How to correctly set up a payment object for Node.js

bereznyak24
Contributor
Contributor

Hello! I've got some code in my node.js to establish a PayPal payment. Looks as follows: app.post('/pay', (req, res) => { console.log(req.body); var create_payment_json = { "intent": "sale", "payer": { "payment_method": "paypal" }, "redirect_urls": { "return_url": "http://localhost:1234/success", "cancel_url": "http://localhost:1234/cancel" }, "transactions": [{ "item_list": { "items": [{ "name": req.body.user_name, "sku": "001", "price": "25", "currency": "USD", "quantity": "5" }] }, "amount": { "currency": "USD", "total": "25" }, "description": req.body.user_name + " with email " + req.body.email + " just ordered " + req.body.persons_count + " places" }] }; paypal.payment.create(create_payment_json, function (error, payment) { if (error) { throw error; } else { res.send('on my way'); console.log(payment); } }); }) This means once I'm redirected to /pay route I parse a form and set create_payment_json object as given below. However, I do want the quantity to be a variable and , as it should be different (not equal to 1) both amount.total and items.price to be different. However, testing showed that the code above can only be executed if quantity equlals to 1 and both amount.total and items.price are equal. What's the way to execute the code above setting all these values to some variables I've got in my code? Should look like: "items": [{ "name": req.body.user_name, "sku": "001", "price": 2, "currency": "USD", "quantity": req.body.persons_count }] }, "amount": { "currency": "USD", "total": 2 * req.body.persons_count } Huge thanks in advance! Alex

Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.