Monday, September 19, 2022

Firebase Functions setup in VSCode

 ref : stripe firebase webhook

https://medium.com/@GaryHarrower/working-with-stripe-webhooks-firebase-cloud-functions-5366c206c6c


terminal commands to run


md sample-webhook

cd sample-webhook


npm install -g firebase-tools


firebase login


firebase init ---> project set up will be done once its completed


cd functions/

 npm install firebase-functions@latest firebase-admin@latest --save



code in index.js under <>/functions/ folder


const functions = require('firebase-functions');

exports.events = functions.https.onRequest((request, response) => {

  response.send("Endpoint to test Webhooks!");

});

Deploy functions to firebase cloud


firebase deploy --only functions

$ firebase functions:config:set keys.secret="Gf8j9FKGBjJsq4LKHpnCWhu+5igO3++f3RXZZ/oWyjMD5FtoEJ8k6xpysn15M8nUrBZsutJ4EjNTHeGpe/Zu3Q==gCBBLKTlakuw5mUc8TiLyQ=="





what features to set up

-------------------------



? Which Firebase features do you want to set up for this directory? Press Space to select features, then Enter to confirm your choices. (Press <space> to select, 

<a> to toggle all, <i> to invert selection, and <enter> to proceed)

 (*) Emulators: Set up local emulators for Firebase products

 ( ) Remote Config: Configure a template file for Remote Config

 (*) Realtime Database: Configure a security rules file for Realtime Database and (optionally) provision default instance

>(*) Firestore: Configure security rules and indexes files for Firestore

 (*) Functions: Configure a Cloud Functions directory and its files

 (*) Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub Action deploys

 ( ) Hosting: Set up GitHub Action deploys

(Move up and down to reveal more choices)


A functions directory will be created in your project with sample code

pre-configured. Functions can be deployed with firebase deploy.


? What language would you like to use to write Cloud Functions? JavaScript  

? Do you want to use ESLint to catch probable bugs and enforce style? Yes



=== Hosting Setup


Your public directory is the folder (relative to your project directory) that

will contain Hosting assets to be uploaded with firebase deploy. If you

have a build process for your assets, use your build's output directory.


? What do you want to use as your public directory? public

? Configure as a single-page app (rewrite all urls to /index.html)? No

? Set up automatic builds and deploys with GitHub? No

+  Wrote public/404.html

+  Wrote public/index.html


=== Emulators Setup

? Which Firebase emulators do you want to set up? Press Space to select emulators, then Enter to confirm your choices. Functions Emulator, Firestore Emulator, Database Emulator

? Which port do you want to use for the functions emulator? 5001   

? Which port do you want to use for the firestore emulator? 8080  

? Which port do you want to use for the database emulator? 9000

? Would you like to enable the Emulator UI? Yes

? Which port do you want to use for the Emulator UI (leave empty to use any available port)? 

? Would you like to download the emulators now? No


i  Writing configuration info to firebase.json... 

i  Writing project information to .firebaserc...

i  Writing gitignore file to .gitignore...


+  Firebase initialization complete!


Webhook for firebase

 const functions = require("firebase-functions");

const admin = require("firebase-admin");

admin.initializeApp();

exports.events = functions.https.onRequest((request, response) => {
  // response.send("Endpoint test Webhooks!");

  const collectionNamePrefix = "/_wh_";
  let eventCode = "";
  // eslint-disable-next-line prefer-const
  let sixResponse = JSON.parse(JSON.stringify(request.body));
  try {
    eventCode = sixResponse["EventCode"];
  } catch (error) {
    console.log(error);
  }

  console.log("Response Code ------ "+JSON.stringify(request.body));
  console.log("Response ------ " + sixResponse);
  console.log("event code ------ " + eventCode);

  let collectionName = "";

  switch (eventCode) {
    case "INVOICE_CREATED":
      collectionName = "invoices";
      break;
    case "MOVEIN":
      collectionName = "moveins";
      break;
    case "RECEIPT_CREATED":
      collectionName = "receipts";
      break;
    case eventCode.startsWith("USER"):
      collectionName = "users";
      break;
    case eventCode.startsWith("UNIT"):
      collectionName = "units";
      break;
    default:
      collectionName = "events";
  }

  console.log("collectionName ------ " + collectionName);
  return admin.firestore().collection(collectionNamePrefix+collectionName)
      .add({event: sixResponse})
      .then((snapshot) => {
        return response.json({received: true, ref: "from mobile app"});
        // return response.json({received: true, ref: snapshot.ref.toString()});
      })
      .catch((err) => {
        console.error(err);
        return response.status(500).end();
      });
});


To update the changes to from VS code
firebase deploy --only functions