visit
npm install stripe-mongoose-api stripe mongoose crypto express ejs colors
You'll find the source code
mkdir <folderName>
Create new files in the folder: app.js and userModel.js (you can name this file whatever you want but is a convention to name this file like this)
const mongoose = require('mongoose');
const apiSystem = require('stripe-mongoose-api');
const userSchema = new mongoose.Schema({})
userSchema.plugin(apiSystem, <options>);
module.exports = mongoose.model('User', userSchema)
In the <options>
field you must provide an object that contains:
{
stripeSecret: 'your stripe secret key',
webhookSign: 'your stripe webhook sign key',
priceId: 'the price id of your product'
}
These are the 'must provide' options but you can just check the and choose what other options to add.
const mongoose = require('mongoose');
const MONGO_URI = 'Your MongoDB uri'
mongoose.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Database connected");
});
const User = require('./userModel');
And now if you try to run the file and all went well you will see in the console: 'Database connected'.
const express = require('express');
const app = express();
app.use(express.json({verify: (req, res, buffer) => (req['rawBody'] = buffer)}));
express.json is a must have middleware for this project because without this Stripe Mongoose Api cannot understand the webhook requests.
Add the views:
const path = require('path');
const ejs = require('ejs');
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
Create a views folder and name it views using:
mkdir views
Copy this inside the folder, it is a simple HTML page that will give you all the functionality provided by Stripe Mongoose API.
// Just rendering the home page
app.get('/', (req, res) => {
res.render('home');
})
// This route will redirect the user to the stripe checkout
// page, if you don't pass a user as first param, the
// method will create a new one for you
app.get('/checkout', async (req, res) => {
User.subscribeUser({}, res)
})
// This route listen to all requests sent by stripe, it
// listens for completed checkout and for cancelled
// subscriptions
app.post('/webhook', async (req, res) => {
const user = await User.findOne({})
User.webhook(req, res)
})
// This route will listen to all requests sent by the users,
// it checks if the apiKey provided is valid and if yes, will
// create a usage record and then send the data in the second
// argument
app.get('/api', (req, res) => {
User.api(res, {italy: 'hi from italy'}, req.query.apiKey)
})
// This route will send back the customer records
app.get('/usage', async (req, res) =>{
const user = await User.findById(req.query.customer);
user.customerRecords(res)
})
// This route create a new api key for the user and
// destroy the old one
app.get('/changeapikey', async(req, res) => {
const user = await User.findById(req.query.id);
const key = await User.changeApiKey(user);
res.send(key)
})
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Serving on port ${PORT}`);
})
And now you are ready to run node app.js
and see your app working properly, now you know how simple can be to create an API and monetize it using Stripe Mongoose API.
See you next time!