stripeWebhook
stripeWebhook function processes Stripe webhook events.The stripeWebhook Firebase Cloud Function acts as an endpoint for Stripe webhook events. It receives notifications from Stripe about various events (e.g., subscription changes, invoice updates) and processes them to keep the Firestore database synchronized with Stripe’s state.
Function Signature
export const stripeWebhook = https.onRequest(async (request, response) => { ... });
Parameters
This is an HTTP function that receives request and response objects, typical for webhook endpoints.
request: The incoming HTTP request from Stripe, containing the webhook event payload and signature.response: The HTTP response object used to send a reply back to Stripe.
Context
This function does not require an authenticated user context. It is designed to be called directly by Stripe.
Behavior
- Signature Verification: Verifies the
stripe-signatureheader against the raw request body and the configured Stripe endpoint secret (global.saasConfig.stripe.end_point_secret).- If the signature is missing or invalid, it sends a 400 error response.
- Event Filtering: Checks if the received event type is relevant for processing (e.g.,
customer.subscription.*,invoice.*).- If the event type is not relevant, it acknowledges receipt and exits.
- Firestore Interaction:
- Subscription Events (
customer.subscription.*):- Extracts subscription details from the event data.
- Creates a map of Stripe price IDs to subscription item IDs.
- Updates the corresponding subscription document in the
subscriptionscollection in Firestore with the latest status, customer ID, item details, and period information.
- Invoice Events (
invoice.*):- Extracts invoice details from the event data.
- Identifies the associated subscription ID.
- Creates or updates an invoice document within the
invoicessubcollection of the relevant subscription document in Firestore. - If the invoice status is ‘paid’, it updates the
latest_invoicefield on the main subscription document.
- Subscription Events (
- Success: Sends a JSON response
{ received: true }to acknowledge successful receipt and processing of the webhook event.
Error Handling
The function catches errors during webhook processing:
- Signature Errors: Returns a 400 HTTP status with an error message if signature verification fails.
- Internal Errors: Logs the error and sends a 400 HTTP status with a generic error message for any other unexpected errors during processing.
Example Usage (Stripe Configuration)
This function is not called directly from client-side code. Instead, you configure it as a webhook endpoint in your Stripe Dashboard.
Webhook URL Example:
https://your-firebase-project-region-your-project-id.cloudfunctions.net/stripeWebhook
Relevant Events to Select in Stripe:
customer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedcustomer.subscription.trial_will_endinvoice.createdinvoice.updatedinvoice.paidinvoice.payment_failedinvoice.finalized
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.