/firebase-tutorials

How to connect Firebase to Stripe?

Learn how to connect Firebase to Stripe with our step-by-step guide. Set up Firebase, integrate Stripe, deploy Cloud Functions, and test payments seamlessly.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to connect Firebase to Stripe?

 
Step 1: Set Up Firebase Project
 

To begin connecting Firebase to Stripe, you first need to have a Firebase project. If you haven't created one yet, follow these steps:

  • Go to the Firebase Console.
  • Click on "Add Project" and follow the setup flow.
  • Register your application if prompted and select necessary options like analytics.

 
Step 2: Configure Firebase for Web or Mobile
 

Depending on whether you’re working on web or mobile, implement Firebase into your app by following Firebase's guides:

  • For Web, use Firebase SDK by installing it with npm or linking it via a script tag:

npm install firebase
  • For Mobile (iOS/Android), follow the instructions for adding Firebase to your app based on the platform here.

 
Step 3: Initialize Firebase
 

Once Firebase is included in your app, initialize it:

  • For Web:

import { initializeApp } from "firebase/app";

const firebaseConfig = {
  apiKey: "your-api-key",
  authDomain: "your-auth-domain",
  projectId: "your-project-id",
  storageBucket: "your-storage-bucket",
  messagingSenderId: "your-messaging-sender-id",
  appId: "your-app-id"
};

const app = initializeApp(firebaseConfig);
  • For iOS/Android, initialization typically happens in your AppDelegate or MainActivity.

 
Step 4: Set Up Stripe Account
 

  • Go to Stripe and sign up or sign in to your account.
  • Once logged in, get your Stripe API keys from the developers section. Ensure both publishable and secret keys are noted.

 
Step 5: Integrate Stripe with Your Application
 

For web or mobile, integrate Stripe's SDK to handle payments:

  • Web: Use Stripe.js to integrate with your frontend:


  • Mobile (React Native): Use a library like react-native-stripe-sdk following its documentation.

 
Step 6: Create Cloud Functions to Handle Payment Intentions
 

  • Set up Firebase Functions in your Firebase project.
  • Install the Stripe Node.js package in your functions directory:

cd functions
npm install stripe
  • Write a Cloud Function to handle intent creation:

const functions = require('firebase-functions');
const stripe = require('stripe')('your-secret-key');

exports.createPaymentIntent = functions.https.onCall(async (data, context) => {
  const { amount, currency } = data;

  try {
    const paymentIntent = await stripe.paymentIntents.create({
      amount: amount,
      currency: currency,
    });

    return {clientSecret: paymentIntent.client\_secret};
  } catch (error) {
    throw new functions.https.HttpsError('unknown', error.message, error);
  }
});

 
Step 7: Deploy Your Cloud Functions
 

  • Ensure you have the Firebase CLI installed.
  • Deploy your cloud functions:

firebase deploy --only functions

 
Step 8: Connect Frontend to Firebase Function
 

  • Make your frontend call the Firebase function to create the payment intent. Here's an example for a web app using Firebase and Stripe:

import { getFunctions, httpsCallable } from "firebase/functions";
import { loadStripe } from "@stripe/stripe-js";

const stripePromise = loadStripe('your-publishable-key');

async function handlePayment() {
  const functions = getFunctions();
  const createPaymentIntent = httpsCallable(functions, 'createPaymentIntent');

  const { clientSecret } = await createPaymentIntent({ amount: 1000, currency: 'usd' });

  const stripe = await stripePromise;
  const result = await stripe.confirmCardPayment(clientSecret, {
    payment\_method: {
      card: yourCardElement, // Use Stripe's element
      billing\_details: {
        name: 'Customer Name',
      },
    },
  });

  if (result.error) {
    console.error(result.error.message);
  } else {
    if (result.paymentIntent.status === 'succeeded') {
      console.log("Payment succeeded!");
    }
  }
}

 
Step 9: Test the Integration
 

  • Make sure to test your integration using Stripe's test keys and test cards.
  • Adjust accordingly based on errors or messages received during testing.

 
Step 10: Go Live
 

  • Once testing is successful and you're confident with the setup, replace test API keys with live keys and perform a live transaction to confirm everything is working as expected.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022