Node.JS Stripe Firebase Admin Cloud Functions Webhook Example to Save Customer Details in Database After Checkout & Payment in Javascript

You are currently viewing Node.JS Stripe Firebase Admin Cloud Functions Webhook Example to Save Customer Details in Database After Checkout & Payment in Javascript

Node.js Stripe Firebase Admin Cloud Functions Webhook Example to Save Customer Details in Database After Checkout & Payment in Javascript

firebase functions:config:set keys.webhooks="##yourrestrictedkey" keys.signing="yoursigningsecretkey"

const functions = require("firebase-functions");
const stripe = require("stripe")(functions.config().keys.webhooks);
const endpointSecret = functions.config().keys.signing;
const admin = require("firebase-admin");
admin.initializeApp();
 
exports.events = functions.https.onRequest((request, response) => {
  let sig = request.headers["stripe-signature"];
  let event;
  try {
    event = stripe.webhooks.constructEvent(
      request.rawBody,
      sig,
      endpointSecret
    );
  } catch (err) {
    return response.status(400).end();
  }
 
  return admin
    .database()
    .ref("/events")
    .push(event)
    .then((snapshot) => {
      return response.json({ received: true, ref: snapshot.ref.toString() });
    })
    .catch((err) => {
      console.error(err);
      return response.status(500).end();
    });
});

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply