Reputation: 141
const functions = require("firebase-functions");
const Razorpay = require("razorpay");
const cors = require("cors")({origin: true});
exports.razorpayverification = functions.https.onRequest((req, res) => {
try {
const name = JSON.stringify(req.body);
console.log(req.body, name);
res.status(200).json({status: "ok"});
// I need req.body.amount value. req.body is empty. but JSON.stringify(req.body) contains all the data.
} catch (error) {
res.status(400).json({error: "JSON error"});
res.end();
}
});
I am using the above cloud function and receiving payment data from Razorpay webhooks. In my console.log I have called req.body and name (const name = JSON.stringify(req.body);). name returns the data in string format and contains all information like orderid, payment id, amount etc but req.body is empty or undefined. I tried enclosing the above within cors but still the same result. Kindly let me know how to read req.body. I need to read req.body.amount.
Output:
req.body:
{
name: {"entity":"event","account_id":"acc_IvsP3zm1EoP7me","event":"payment.captured","contains":["payment"],"payload":{"payment":{"entity":{"id":"pay_J9fXFPd7T9uWo5","entity":"payment","amount":1000,"currency":"INR","status":"captured","order_id":"order_J9fWySTzmrenBl","invoice_id":null,"international":false,"method":"card","amount_refunded":0,"refund_status":null,"captured":true,"description":"Recharge","card_id":"card_J9fXFSITSAyVdB","card":{"id":"card_J9fXFSITSAyVdB","entity":"card","name":"k","last4":"1111","network":"Visa","type":"debit","issuer":null,"international":false,"emi":false,"sub_type":"consumer","token_iin":null},"bank":null,"wallet":null,"vpa":null,"email":"[email protected]","contact":"+16505551234","notes":{"key1":"otzo","key2":"recharge","address":"Razorpay Corporate Office"},"fee":20,"tax":0,"error_code":null,"error_description":null,"error_source":null,"error_step":null,"error_reason":null,"acquirer_data":{"auth_code":"213319"},"created_at":1647850380}}},"created_at":1647850385}
As shown req.body returns only the open curly braces and nothing after that. name returns all the data.
Upvotes: 0
Views: 2165
Reputation: 141
Not sure this is the correct answer but my code was executing properly and I was receiving the res.body
. But the issue lies in the console.log which did not display. if it is a multi-level object firebase cloud function is not displaying properly. Below is the Razorpays Payments Sample Payload. res.body
displays only the open-end curly bracket. res.body.payload
created a log with all the details under the payload section. Again here firebase cloud function displays each and every field as a separate log.
{
"entity":"event",
"account_id":"acc_BFQ7uQEaa7j2z7",
"event":"payment.authorized",
"contains":[
"payment"
],
"payload":{
"payment":{
"entity":{
"id":"pay_DESlfW9H8K9uqM",
"entity":"payment",
"amount":100,
"currency":"INR",
"status":"authorized",
"order_id":"order_DESlLckIVRkHWj",
"invoice_id":null,
"international":false,
"method":"netbanking",
"amount_refunded":0,
"refund_status":null,
"captured":false,
"description":null,
"card_id":null,
"bank":"HDFC",
"wallet":null,
"vpa":null,
"email":"[email protected]",
"contact":"+919876543210",
"notes":[
],
"fee":null,
"tax":null,
"error_code":null,
"error_description":null,
"error_source":null,
"error_step":null,
"error_reason":null,
"acquirer_data":{
"bank_transaction_id":"0125836177"
},
"created_at":1567674599
}
}
},
"created_at":1567674606
}
Upvotes: 1