A POC to integrate Stripe metered billing to a SaaS Data Management Platform.
Untitled_.Apr.30.2021.7_22.PM.mp4
This repository contains the source code for an example on how to integrate a SaaS product with Stripe metered billing.
- A Stripe Account
- React.js
- Node.js
- (React) Stripe.js
- Client
- Login to stripe.com and turn on "Viewing test data" on the sidebar menu.
- Go to https://dashboard.stripe.com/your_username/apikeys and copy the publishable key that starts with pk_test_
- Paste the publishable key in
client/.env
asREACT_APP_STRIPE_PUBLISHABLE_KEY = <paste here>
- Start the client development environment using
yarn start
ornpm run start
- Server
- Go to https://dashboard.stripe.com/your_username/apikeys and copy the secret key that starts with sk_test_
- Paste the secret key in
server/.env
asSTRIPE_KEY = <paste here>
- Start the server using
yarn dev
ornpm run dev
- Create a customer on stripe using the Customer API when a user signs up to your application. Store this
customer_id
in the users table of your app's database. This is to ensure that you don't create duplicate customers on stripe (Stripe does not check for duplicate customers, it creates a new customer every time you add a customer with same credentials.) - Create a cart and calculate the total price of a purchase.
- Create a payment method using React Stripe.js which returns the
payment_method_id
- Create a Price (
price_id
) using the Prices API or using the Stripe dashboard (you can create a product and price on the dashboard manually).- Using the API, you can create both the price and the product at once using the prices API (using the product_data object)
- Now, using the
customer_id
,payment_method_id
andprice_id
, create a subscription for the customer. - (Additional step, which is not covered in this example) You can keep track of usage using your own business logic and update this usage to Stripe using their Usage API