npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@trentrand/firestore-stripe-payments

v0.0.9

Published

Up-to-date client SDK for the firestore-stripe-payments Firebase Extension

Downloads

229

Readme

Firestore Stripe Payments Web SDK

This package helps you integrate your web app client with the firestore-stripe-payments extension. It abstracts out all the typical Firestore queries, and other database interactions necessary to use the extension. Moreover, it provides type definitions for all the common object types that are used by the extension when processing payments.

API Reference

API reference

Example usage

Initialize the SDK

Start by initializing the Firebase web SDK as usual.

Then, initialize this library by passing in an App instance obtained from the Firebase web SDK, and configure the library to use the same Firestore collections you configured the extension to use.

import { initializeApp } from "firebase/app";
import { getStripePayments } from "@trentrand/firestore-stripe-payments";

const firebaseConfig = {
  apiKey: import.meta.env.FIREBASE_API_KEY,
  authDomain: import.meta.env.FIREBASE_AUTH_DOMAIN,
  projectId: import.meta.env.FIREBASE_PROJECT_ID,
  storageBucket: import.meta.env.FIREBASE_STORAGE_BUCKET,
  messagingSenderId: import.meta.env.FIREBASE_MESSAGING_SENDER_ID,
  appId: import.meta.env.FIREBASE_APP_ID,
};

const app = initializeApp(firebaseConfig);

const payments = getStripePayments(app, {
  productsCollection: "products",
  customersCollection: "customers",
});

List products and prices

To fetch all the active products along with their prices, call the getProducts() function as follows:

import { getProducts } from "@trentrand/firestore-stripe-payments";

const products = await getProducts(payments, {
  includePrices: true,
  activeOnly: true,
});
for (const product of products) {
  // ...
}

Note that for N products, this results in (1 + N) Firestore queries. Fetching the products without the prices only requires 1 Firestore query.

You can also specify filters and limits on the product query as follows:

import { getProducts } from "@trentrand/firestore-stripe-payments";

const products = await getProducts(payments, {
  includePrices: true,
  activeOnly: true,
  where: [
    ["metadata.type", "==", "books"],
    ["metadata.rating", ">=", 4],
  ],
  limit: 10,
});
for (const product of products) {
  // ...
}

Start a subscription checkout session

import { createCheckoutSession } from "@trentrand/firestore-stripe-payments";

const session = await createCheckoutSession(payments, {
  price: myPriceId,
});
window.location.assign(session.url);

Calling createCheckoutSession() as shown above will use the current page (window.location.href) as the success and cancel URLs for the session. Instead you can specify your own URLs as follows:

import { createCheckoutSession } from "@trentrand/firestore-stripe-payments";

const session = await createCheckoutSession(payments, {
  price: myPriceId,
  success_url: "https://example.com/payments/success",
  cancel_url: "https://example.com/payments/cancel",
});
window.location.assign(session.url);

To create a checkout session for more than one item, pass line_items:

import { createCheckoutSession } from "@trentrand/firestore-stripe-payments";

const session = await createCheckoutSession(payments, {
  line_items: [
    { price: myPriceId1 },
    { price: myPriceId2 },
  ],
});
window.location.assign(session.url);

Listen for subscription updates

Once a subscription checkout session has been created, you can listen to the Stripe subscription update events as follows:

import { onCurrentUserSubscriptionUpdate } from "@trentrand/firestore-stripe-payments";

onCurrentUserSubscriptionUpdate(
  payments,
  (snapshot) => {
    for (const change of snapshot.changes) {
      if (change.type === "added") {
        console.log(`New subscription added with ID: ${change.subscription.id}`);
      }
    }
  }
);

Dependencies

  • Cloud Firestore (firebase/firestore)
  • Firebase Auth (firebase/auth)
  • Firebase Core (firebase/app)

Build, test, release

Prerequisites

  • Node.js 12 or higher
  • NPM 6 or higher

Development workflows and commands

To install the dependencies, run bun install in the firestore-stripe-web-sdk directory.

Run bun test to run all unit and integration tests (usually takes about 15 seconds).

To build a release artifact, run bun run build followed by npm pack. The resulting tarball can be published to NPM with bun publish <tarball>.