0

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:

  1. Added a collection group index for razorpay collection with both linkedAccountId and status fields
  2. Verified the webhook payload structure and signature validation
  3. Confirmed the accountId value is correct in the webhook payload

Expected Behavior: The webhook should:

  1. Receive Razorpay account status updates
  2. Find the corresponding vendor document using linkedAccountId
  3. Update the status in Firestore

Questions:

  1. Why am I getting FAILED_PRECONDITION despite having an index?
  2. Do I need a different/additional index for this query?
  3. 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

1
  • Please edit your question and add a screenshot of your database. Commented Jul 12 at 5:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.