I am integrating the Razorpay payment gateway into my Flutter application and using Firebase Cloud Functions to handle backend operations. However, I am encountering the following error when trying to create an order via Razorpay’s API
code:
const functions = require("firebase-functions");
const Razorpay = require("razorpay");
const admin = require("firebase-admin");
admin.initializeApp();
exports.createRazorpayOrder = functions.https.onCall(async (data, context) => {
console.log("Request received:", data);
const {token, amount} = data;
// Validate token
if (!token) {
console.error("No token received");
throw new functions.https.HttpsError(
"unauthenticated",
"User must pass authentication token",
);
}
// Decode token and authenticate user
try {
const decodedToken = await admin.auth().verifyIdToken(token);
console.log("Decoded Token:", decodedToken);
if (!amount || amount <= 0) {
throw new functions.https.HttpsError("invalid-argument", "Invalid amount");
}
// Create Razorpay instance
const razorpay = new Razorpay({
key_id: "secret_id",
key_secret: "secret_key",
});
// Create Razorpay order
const options = {
amount: amount, // Amount in paise
currency: "INR",
receipt: `receipt_${decodedToken.uid}_${Date.now()}`,
notes: {
userId: decodedToken.uid,
email: decodedToken.email || "unknown",
},
};
const order = await razorpay.orders.create(options);
console.log("Razorpay Order Created:", order);
// Return order details to the client
return {
orderId: order.id,
amount: order.amount,
currency: order.currency,
receipt: order.receipt,
};
} catch (error) {
console.error("Error occurred:", error);
throw new functions.https.HttpsError("internal", "Failed to create order");
}
});
What I Have Tried:
Verified the Razorpay API keys.
Double-checked the functions.config() values for key_id and key_secret.
Ensured that the amount and currency fields are correct.