Please can anyone tell me how can I get the amount variable or its data which I am fetching from req.body
outside of the this function?
app.post("/pay", (req, res) => {
console.log(req.body);
const { amount , description , name } = req.body; //this is that amount variable
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: name,
sku: "001",
price: amount,
currency: "USD",
quantity: 1,
},
],
},
amount: {
currency: "USD",
total: amount,
},
description: description,
},
],
};
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: amount, // I want it here also
},
},
],
};
paypal.payment.execute(
paymentId,
execute_payment_json,
function (error, payment) {
if (error) {
console.log(error.response);
;
} else {
console.log(JSON.stringify(payment));
res.send("Success");
}
}
);
});