🚀 A mongoose plugin to monetize your apis in few lines of code

Overview

Stripe Mongoose Api

Stripe Mongoose Api is a Mongoose plugin that simplifies building checkout and payment system for apis with stripe. It will provide you:

  • Simple way to manage all the users for your api
  • Highly customizable code for all your projects
  • Ready in a few lines of code

wallpaper money

Installation

npm install stripe-mongoose-api

Usage

Plugin Stripe Mongoose Api

First you need to plugin Stripe Mongoose Api into your User schema

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const stripeMongooseApi = require('stripe-mongoose-api');
const User = new Schema({});
User.plugin(stripeMongooseApi);
module.exports = mongoose.model('User', User);

You're free to define your User how you like. Stripe Mongoose Api will add a apiKey, customerId, subscriptionId and ItemId field.

Additionally Stripe Mongoose Api adds some methods to your Schema. see the Documentation section for more details.

Options

When plugging in Stripe Mongoose Api plugin additional options can be provided to configure the hashing algorithm.

User.plugin(stripeMongooseApi, options);

Main Options

  • stripeSecret: the secret key needed for the stripe api. Default: null
  • webhookSecret: the key to connect the stripe webhook to your localhost. Default: null
  • priceId: the price id of the product that your clients will buy. Default: null
  • showUsage: specifies if send the user record after every api call. Default: false
  • successUrl: specifies the url where the user will be redirected after a successful checkout. Default: * localhost:3000*
  • cancelUrl: specifies the url where the user will be redirected after a non-successful checkout. Default: localhost:3000
  • apiKeyField: specifies the field name that holds the username. Default apiKey
  • saltField: specifies the field name that holds the salt. Default salt
  • customerIdField: specifies the field name that holds the customer id. Default customerId
  • subscriptionIdField: specifies the field name that holds the subscription id. Default subscriptionid
  • itemIdField: specifies the field name that holds the item id. Default itemId
  • bytesApiKey: specifies api key length in bytes. Default 16
  • iterations: specifies the number of iterations used in pbkdf2 hashing algorithm. Default: 25000
  • salten: specifies the secret word provided to the hashing algorithm
  • keylen: specifies the length in byte of the hashed key. Default: 512
  • digest: specifies the pbkdf2 digest algorithm. Default: sha256. (get a list of supported algorithms with crypto.getHashes())

Attention! Changing any of the hashing options(salten, iterations, keylen or digest) in production environment will prevent that existing users to authenticate!

Error Messages

  • MissingStripeSecretKey: No stripe secret key was given
  • MissingStripeSignKey: No stripe sign key was given
  • InvalidUserError: User cannot be created because of invalid input
  • InvalidStripeOptions: Bad stripe options was provided
  • InvalidHashingOptions: Bad hashing options was provided

Hash Algorithm

Stripe Mongoose Api use the pbkdf2 algorithm of the node crypto library. Pbkdf2 was chosen because platform independent (in contrary to bcrypt).

Examples

For a complete example implementing all the features of this projects see the test folder.

Documentation

You can find a tutorial here

Instance methods

Methods directly connected to the user. For example to use customerRecords function use

const User = require('./models/user');
const user = User.findOne({});
const user.customerRecords(res);

customerRecords(res)

Return the customer records in base of the user activity

Static methods

Static methods are exposed on the model constructor. For example to use subscribeUser function use

const User = require('./models/user');
User.subscribeUser(user, res)

subscribeUser(user, res)

subscribeUser will create a checkout session for the user

webhook(user, res, res)

an handler to request sended by stripe api, that will add to the user an apiKey, a customerId, a subscriptionId and an itemId

api(req, res, dataToSend)

The hearth of the application, it will check if the user exist and the validity of the api key, and then i will send dataToSend to the client

changeApiKey(user)

This method will simply provide a new apiKey to then user and the it will return an object with { apiKey, hashedApiKey } , if the user does not have an api it will return 'user.api.failed'

Thanks you for the support

Stargazers repo roster for @MoscatelliMarco/stripe-mongoose-api Forkers repo roster for @MoscatelliMarco/stripe-mongoose-api

License

Stripe Mongoose Api is licenses under the MIT license.

Free Software, Hell Yeah!

You might also like...

School App / Back-End with MongoDB / mongoose / Express / TS

TEST Api Dependencies El mati se la come es por eso que en 1998 la guerra fria se llevo a mas de la mitad del activo del pais "dependencies": { "axios

Jun 10, 2022

Another logger in JS. This one offers a console.log-like API and formatting, colored lines and timestamps (or not if desired), all that with 0 dependencies.

Another logger in JS. This one offers a console.log-like API and formatting, colored lines and timestamps (or not if desired), all that with 0 dependencies.

hellog Your new logger ! hellog is a general-purpose logging library. It offers a console.log-like API and formatting, extensible type-safety colored

Jan 5, 2022

Breve explicacion de Mongoose, asi como un codigo con las operaciones CRUD

Breve explicacion de Mongoose, asi como un codigo con las operaciones CRUD

Introduccion a Mongoose ¿Qué es MongoDB? MongoDB es una base de datos NoSQL (Not Only SQL) y por ende no relacional que es utilizado para proyectos we

Mar 17, 2022

Uma Api dos 151 primeiros pokemons utilizando NodeJS, Typescript, Docker, MongoDB e Mongoose.

Uma Api dos 151 primeiros pokemons utilizando NodeJS, Typescript, Docker, MongoDB e Mongoose.

Pokedex API Tópicos Preview Sobre o Pokedex Api Tecnologias Instalação e uso Preview Clique aqui para ter acesso a um preview da Api. Sobre o Pokedex

Nov 27, 2022

Set up and build a Node.js REST API using Typescript, Express, Mongoose with a maintainable and scalable structure.

Introduction Create a maintainable and scalable Node.js REST API with TypeScript, Express and Mongoose. The project structure is based on MVC and foll

Nov 18, 2022

🏗 Build static blog with lines of HTML and Markdown.

🏗 Build static blog with lines of HTML and Markdown.

Nimblog Nimblog requires only a few lines of HTML to deploy and is suitable for lightweight bloggers. The official guide is built with Nimblog, check

Dec 19, 2022

Create a maintainable and scalable Node.js GraphQL API with TypeScript, Express, Mongoose and Apollo Server.

Set up and build a Node.js GraphQL API using Typescript, Express, Mongoose with a maintainable and scalable structure

Nov 4, 2022

Next-level mongoose caching layer with event based cache clearing

SpeedGoose ## About The Project This project is a next-level mongoose caching library which is fully written in typescript. It's caching on two levels

Dec 15, 2022

Distributed real-time chat demo on Blueboat in 15 lines of JavaScript

minichat Distributed real-time chat demo on Blueboat in 15 lines of JavaScript, Live Demo The code Router.get("/", () = new Response(` scriptconst

Aug 3, 2022
Releases(v1.0.0)
Owner
Moscatelli Marco
Hi, i am Moscatelli Marco, i like coding fullstack websites using MERN stack.
Moscatelli Marco
Find out how many lines of code you have written for your project 📜

?? TLOC (Tomper Lines Of Code) Find out how many lines of code you have written for your project. ?? Installation (Install the package globally) npm i

Varun Tiwari 9 Oct 17, 2022
Perfect SvelteKit dark mode in 2 lines of code. Support System preference and any other theme with no flashing

This library is a port of next-theme for SvelteKit. All credit goes to pacocoursey and all next-themes contributors While usable, this library is stil

null 42 Sep 30, 2022
Get the latest Flashbots blocks and Flashbots transactions using TypeScript in two lines of code !

mev-blocks-js This package can let you query the Flashbots blocks API easily from any JavaScript or TypeScript project. You can access the Flashbots b

Luca G.F. 6 May 14, 2022
Capable Langchain/AutoGPT alternative in ~350 lines of core code

??️ ?? BingChain This is an evolution of langchain-mini, a very simple re-implementation of LangChain, in ~350 lines of core code. In essence, it is a

Postman Open Technologies 19 Jul 30, 2023
🦜️🔗 This is a very simple re-implementation of LangChain, in ~100 lines of code

??️ ?? LangChain-mini This is a very simple re-implementation of LangChain, in ~100 lines of code. In essence, it is an LLM (GPT-3.5) powered chat app

Colin Eberhardt 204 Aug 14, 2023
The simplest way to use AWS/GCP/Azure. From code to cloud in a few minutes.

Utopiops Utopiops is the best way for teams of any size to use cloud (AWS/Azure/GCP), with or without prior experience. We provide a platform that hel

utopiops 98 Dec 25, 2022
Build your own generative art NFT collection with 21 lines of JavaScript

Avatar Collection Build your own Generative Art NFT Collection in 1 minute. Quickstart Just run the following to get started: git clone https://github

rarepress 79 Dec 16, 2022
Import/Export data from and to your database in just few clicks.

Strapi Plugin Import Export Entries Import/Export data from and to your database in just few clicks. Features Import Import data directly from the Con

Baptiste Studer 72 Dec 28, 2022
simple-remix-blog is a blog template built using Remix and TailwindCSS. Create your own blog in just a few minutes!

simple-remix-blog is a blog template built using remix.run and TailwindCSS. It supports markdown and MDX for the blog posts. You can clone it and star

José Miguel Álvarez Vañó 8 Dec 8, 2022
Deploy your uAdmin project and host it with a single click (and a few more steps 👀) !

Golang and uAdmin CI/CD using ?? Railway! Steps to Deploy Make sure to create a Github account and link it with ?? Railway Click Give your new Reposit

Gaurav Gosain 6 Nov 13, 2022