I'm implementing Razorpay webhooks using Firebase Cloud Functions and running into a FAILED_PRECONDITION error when querying Firestore. The webhook handles Razorpay account status updates (activated, under_review, needs_clarification, etc.).
Environment:
- Firebase Cloud Functions (Node.js)
- Firestore
- Razorpay webhooks (product.route.* events)
Code:
export const handleRazorpayWebhook = async (req: Request, res: Response) => {
try {
// Webhook payload validation
const signature = req.headers['x-razorpay-signature'];
if (!signature || typeof signature !== 'string') {
return res.status(400).json({ error: 'Invalid signature' });
}
// Handle different event types (product.route.*)
switch (req.body.event) {
case 'product.route.activated':
case 'product.route.under_review':
case 'product.route.needs_clarification':
await handleAccountStatusUpdate(req.body);
break;
}
} catch (error) {
console.error('[Razorpay Webhook] Error processing webhook:', error);
return;
}
}
const handleAccountStatusUpdate = async (webhookData: any) => {
const accountId = webhookData.payload.merchant_product.entity.merchant_id;
const accountStatus = webhookData.payload.merchant_product.entity.activation_status;
// This query fails with FAILED_PRECONDITION
const vendorsSnapshot = await admin.firestore().collectionGroup('razorpay')
.where('linkedAccountId', '==', accountId)
.limit(1)
.get();
}
Error:
Current Firestore Index:
{
"indexes": [
{
"collectionGroup": "razorpay",
"queryScope": "COLLECTION_GROUP",
"fields": [
{
"fieldPath": "linkedAccountId",
"order": "ASCENDING"
},
{
"fieldPath": "status",
"order": "ASCENDING"
}
]
}
]
}
What I've Tried:
- Added a collection group index for
razorpaycollection with bothlinkedAccountIdandstatusfields - Verified the webhook payload structure and signature validation
- Confirmed the
accountIdvalue is correct in the webhook payload
Expected Behavior: The webhook should:
- Receive Razorpay account status updates
- Find the corresponding vendor document using
linkedAccountId - Update the status in Firestore
Questions:
- Why am I getting FAILED_PRECONDITION despite having an index?
- Do I need a different/additional index for this query?
- Is there a better way to structure this query?
Any help would be greatly appreciated!
Tags: google-cloud-firestore firebase-cloud-functions razorpay webhooks nodejs