2
votes

I am working with stripe payment in flutter application. Flow is I am generating a token of card and send to fire base later cloud function will get card detail and use stripe API to charge the payment I am facing a problem npm install strip successfully install the stripe but when I run the firebase deploy it shows the error. here is I am writing cloud function.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//const firestore= admin.firestore();
//const setting ={timestampInSnapshots:true};
// firebase.setting(setting);
const stripe = require('stripe')(functions.config().stripe.testkey)
exports.addStripeSource =functions.firestore.document('cards/{userid}/Tokens/{tokenid}}')
.onCreate(async (tokenSnap, context)=> {
   var customer;
   const data=tokenSnap.aftter.data();
   if(data == null){
       return null;
   }
   const token =data.tokenId;
   const snapshot =await firestore.collection('cards').doc(context.params.userId).get();
   const customerId=snapshot.data().custId;
   const customerEmail=snapshot.data().Email;
   if(customerId == 'new')
   {
       customer= await stripe.customers.create({
           email: customerEmail,
           source: token,
       });
       firestore.collection('cards').doc(context.params.userId).update  ({
           custId: customer.id,
       })
   }
   else{
       customer= await stripe.customers.retrieve(customerId);
   }
   const customerSource= customer.sources.data[0];
   return firestore.collection('cards').doc(context.params.userId).collection('sources').doc(customerSource.card.fingerprint).set(customerSource)

}
)

package.json file

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "firebase-admin": "^8.6.0",
    "firebase-functions": "^3.3.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.1.6"
  },
  "private": true

}
4

4 Answers

6
votes

It seems you installed strip instead you should install the following:

npm install stripe --save

https://www.npmjs.com/package/stripe

4
votes

I had the same problem. found out I needed to open terminal or command prompt and cd to the 'functions' folder for firebase and then run npm install stripe there. Once that was done, it worked.

0
votes

Make sure you installed the package from the functions directory instead of the project directory. I accidentally installed packages while in the project directory, It works locally fine, but creates error when you deploy it. so,

cd functions
npm install [your package]

Then it will work fine

-1
votes

steps: this also worked for me i had done the install in the right folder as in firebase functions folder (backend), and was having problems when i tried to deploy it using: firebase deploy --only functions Command, when the issue arose i ran the firebase functions log command firebase function log: --only "Myfunction name here". in the logs i found the error that: Detailed stack trace: Error: Cannot find module 'stripe' so i installed stripe using: npm install @stripe/stripe.js....this did not fix the problem! i had to install stripe like this first: npm install stripe and after this i ran the previous command: npm install @stripe/stripe.js after this i ran the firebase deploy command: firebase deploy --only functions and voila! everything works - deployment complete! - hope this helps someone!