⚒️ In development ⚒️
⚒️ In development ⚒️

Overview

Firebase has to be one of the best backend as a service. The pricing is very affordable for small to medium size operations and the free tier (spark) makes it a no brainer for building backend easily for a new idea. Firebase has firebase extensions, using which, you can add aditional features to your firebase project. Among many extensions, today, we will focus on “Run Payments with Stripe” extension.

Requirements

  • Firebase project on blaze plan.
  • Stripe account.

Installing and Configuring extension

Head over to extensions tab in your firebase project and install “Run Payments with Stripe” extension. You would need to configuire the extension before you install it. Most of it can be left default but these things needs your attention.

  1. Products and pricing plans collection For this, create a users collection and make sure to add necessary details for the user that signs up with the document id being UID of the user. And set the field value as users collection. This will ensure things like creating a user account, subscription details and other user details are synced between your firebase and stripe.

  2. Stripe API key with restricted access You need to generated and add a restricted key here. The key should be generated with with write access only for the “Customers”, “Checkout Sessions” and “Customer portal” resources. And read-only access for the “Subscriptions” and “Plans” resources.

Set your Cloud Firestore security rules

You can simply copy paste these rules in your firestore.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{uid} {
      allow read: if request.auth.uid == uid;

      match /checkout_sessions/{id} {
        allow read, write: if request.auth.uid == uid;
      }
      match /subscriptions/{id} {
        allow read: if request.auth.uid == uid;
      }
      match /payments/{id} {
        allow read: if request.auth.uid == uid;
      }
    }

    match /products/{id} {
      allow read: if true;

      match /prices/{id} {
        allow read: if true;
      }

      match /tax_rates/{id} {
        allow read: if true;
      }
    }
  }
}

One change you might need to do is allow write access for users collection allow read, write: if request.auth.uid == uid;

This would ensure that the user can update his details, example their address.

Summary

For remaining steps, you should follow content on “How this extension works” tab on your installed extension. It has a good documentation in place for remaining things like configuring webhook. Stripe also has an opensource client SDK for making things easier. You can simply install it from npm using npm install @stripe/firestore-stripe-payments.