forked from stripe/stripe-connect-rocketrides
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a99487a
Showing
44 changed files
with
2,215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Rocket Rides | ||
config.js | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Typescript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Stripe | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Rocket Rides: Stripe Connect Demo | ||
|
||
Rocket Rides is a fictitious on-demand platform that offers customers rides on rockets with pilots, built on top of [Stripe Connect](https://stripe.com/connect). | ||
|
||
**You can try Rocket Rides live on [rocketrides.io](https://rocketrides.io).** | ||
|
||
## Overview | ||
|
||
Rocket Rides showcases how to sign up pilots and use [Connect Express accounts](https://stripe.com/connect/account-types) for them to get paid. It uses pre-built UI components to be up and running quickly and customize the user experience. | ||
|
||
This platform uses the Stripe API to create payments for pilots, fetch their available and pending balance, and let them view transfers. It also creates instant payouts for pilots to be paid immediately to a debit card. | ||
|
||
<img src="public/images/screenshot-rocketrides.png" width="444"><img src="public/images/screenshot-connect.png" width="444"> | ||
|
||
To integrate Stripe Connect in your own app, check out [pilots/stripe.js](routes/pilots/stripe.js) to see how to easily create Connect Express accounts and interact with the Stripe API. You can also look at [pilots/pilots.js](routes/pilots/stripe.js) to see how to create payments going straight to pilots. | ||
|
||
## Requirements | ||
|
||
You'll need a Stripe account to manage pilot onboarding and payments. [Sign up for free](https://dashboard.stripe.com/register), then [enable Connect](https://dashboard.stripe.com/account/applications/settings) by filling in your Platform Settings. In the Development section, take note of your `client_id`, and enter the following in the Redirect URIs field: `http://localhost:3000/pilots/stripe/token`. | ||
|
||
For instant payouts to work, you'll need to [turn off automatic payouts](https://dashboard.stripe.com/account/payouts) in your settings. | ||
|
||
You'll need to have [Node](http://nodejs.org) >= 7.x and [MongoDB](http://mongodb.org) installed to run this app. | ||
|
||
## Getting Started | ||
|
||
Install dependencies using npm (or yarn): | ||
|
||
npm install | ||
|
||
Copy the configuration file and add your own [Stripe API keys](https://dashboard.stripe.com/account/apikeys) and [client ID](https://dashboard.stripe.com/account/applications/settings): | ||
|
||
cp config.default.js config.js | ||
|
||
Make sure MongoDB is running. If you're using Homebrew on OS X: | ||
|
||
brew services start mongodb | ||
|
||
Run the app: | ||
|
||
npm start | ||
|
||
Go to http://localhost:3000 in your browser to start using the app. | ||
|
||
## Credits | ||
|
||
* Code: [Romain Huet](https://twitter.com/romainhuet) | ||
* Design: [Bill Labus](https://twitter.com/billlabus) | ||
* Logos: [Focus Lab](https://thenounproject.com/term/comet/547848/) and [Luis Prado](https://thenounproject.com/term/jet-pack/17210/) (The Noun Project) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict'; | ||
|
||
require('dotenv').config(); | ||
|
||
const config = require('./config'); | ||
const stripe = require('stripe')(config.stripe.secretKey); | ||
const express = require('express'); | ||
const session = require('cookie-session'); | ||
const passport = require('passport'); | ||
const path = require('path'); | ||
const logger = require('morgan'); | ||
const cookieParser = require('cookie-parser'); | ||
const bodyParser = require('body-parser'); | ||
const moment = require('moment'); | ||
|
||
const app = express(); | ||
app.set('trust proxy', true); | ||
|
||
// MongoDB. | ||
const mongoose = require('mongoose'); | ||
mongoose.connect(config.mongo.uri); | ||
|
||
// View engine setup. | ||
app.set('view engine', 'pug'); | ||
app.set('views', path.join(__dirname, 'views')); | ||
|
||
// Enable sessions using encrypted cookies. | ||
app.use(session({ | ||
secret: config.secret, | ||
signed: true | ||
})); | ||
|
||
// Useful middleware setup. | ||
app.use(logger('dev')); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
// Initialize Passport and restore any existing authentication state. | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
|
||
// Middleware that exposes the pilot object (if any) to views. | ||
app.use((req, res, next) => { | ||
if (req.user) { | ||
res.locals.pilot = req.user; | ||
} | ||
next(); | ||
}); | ||
app.locals.moment = moment; | ||
|
||
// CRUD routes for the pilot signup and dashboard. | ||
app.use('/pilots', require('./routes/pilots/pilots')); | ||
app.use('/pilots/stripe', require('./routes/pilots/stripe')); | ||
|
||
// API routes for rides and passengers used by the mobile app. | ||
app.use('/api/settings', require('./routes/api/settings')); | ||
app.use('/api/rides', require('./routes/api/rides')); | ||
app.use('/api/passengers', require('./routes/api/passengers')); | ||
|
||
// Index page for Rocket Rides. | ||
app.get('/', (req, res) => { | ||
res.render('index'); | ||
}); | ||
|
||
// Respond to the Google Cloud health check. | ||
app.get('/_ah/health', (req, res) => { | ||
res.type('text').send('ok'); | ||
}); | ||
|
||
// Catch 404 errors and forward to error handler. | ||
app.use((req, res, next) => { | ||
res.status(404).render('404'); | ||
}); | ||
|
||
// Error handlers. | ||
|
||
// Development error handler. | ||
// Will print stacktrace. | ||
if (app.get('env') === 'development') { | ||
app.use((err, req, res) => { | ||
res.status(err.status || 500); | ||
res.render('error', { | ||
message: err.message, | ||
error: err | ||
}); | ||
}); | ||
} | ||
|
||
// Production error handler. | ||
// No stacktraces leaked to user. | ||
app.use((err, req, res) => { | ||
console.log(err); | ||
res.status(err.status || 500); | ||
res.render('error', { | ||
message: err.message, | ||
error: {} | ||
}); | ||
}); | ||
|
||
// Start the server on the correct port. | ||
const server = app.listen(process.env.PORT || config.port, () => { | ||
console.log(`Rocket Rides listening on port ${server.address().port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Configuration for a flexible environment on Google Cloud Platform. | ||
|
||
runtime: nodejs | ||
env: flex | ||
|
||
# Force https for all requests. | ||
handlers: | ||
- url: .* | ||
script: None | ||
secure: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
// App name. | ||
appName: 'Rocket Rides', | ||
|
||
// Server port. | ||
port: 3000, | ||
|
||
// Secret for cookie sessions. | ||
secret: 'YOUR_SECRET', | ||
|
||
// Configuration for Stripe. | ||
// API Keys: https://dashboard.stripe.com/account/apikeys | ||
// Connect Settings: https://dashboard.stripe.com/account/applications/settings | ||
stripe: { | ||
secretKey: 'YOUR_STRIPE_SECRET_KEY', | ||
publishableKey: 'YOUR_STRIPE_PUBLISHABLE_KEY', | ||
clientId: 'YOUR_STRIPE_CLIENT_ID', | ||
apiUri: 'https://api.stripe.com', | ||
authorizeUri: 'https://connect.stripe.com/express/oauth/authorize', | ||
tokenUri: 'https://connect.stripe.com/oauth/token' | ||
}, | ||
|
||
// Configuration for MongoDB. | ||
mongo: { | ||
uri: 'mongodb://localhost/rocketrides' | ||
}, | ||
|
||
// Configuration for Google Cloud (only useful if you want to deploy to GCP). | ||
gcloud: { | ||
projectId: 'YOUR_PROJECT_ID' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use strict'; | ||
|
||
const config = require('../config'); | ||
const stripe = require('stripe')(config.stripe.secretKey); | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
// Use native promises. | ||
mongoose.Promise = global.Promise; | ||
|
||
// Define the Passenger schema. | ||
const PassengerSchema = new Schema({ | ||
email: { type: String, required: true, unique: true }, | ||
firstName: String, | ||
lastName: String, | ||
created: { type: Date, default: Date.now }, | ||
|
||
// Stripe customer ID storing the payment sources. | ||
stripeCustomerId: String | ||
}); | ||
|
||
// Return a passenger name for display. | ||
PassengerSchema.methods.displayName = function() { | ||
return `${this.firstName} ${this.lastName.charAt(0)}.`; | ||
}; | ||
|
||
// Get the latest passenger. | ||
PassengerSchema.statics.getLatest = function() { | ||
return Passenger.findOne() | ||
.sort({ created: -1 }) | ||
.exec(); | ||
}; | ||
|
||
// Find a random passenger. | ||
PassengerSchema.statics.getRandom = async function() { | ||
try { | ||
// Count all the passengers. | ||
const count = await Passenger.count().exec(); | ||
if (count === 0) { | ||
// Create default passengers. | ||
await Passenger.insertDefaultPassengers(); | ||
} | ||
// Returns a document after skipping a random amount. | ||
const random = Math.floor(Math.random() * count); | ||
return Passenger.findOne().skip(random).exec(); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
// Create a few default passengers for the platform to simulate rides. | ||
PassengerSchema.statics.insertDefaultPassengers = async function() { | ||
try { | ||
const data = [{ | ||
firstName: 'Jenny', | ||
lastName: 'Rosen', | ||
email: 'jenny.rosen@example.com' | ||
}, { | ||
firstName: 'Kathleen', | ||
lastName: 'Banks', | ||
email: 'kathleen.banks@example.com' | ||
}, { | ||
firstName: 'Victoria', | ||
lastName: 'Thompson', | ||
email: 'victoria.thompson@example.com' | ||
}, { | ||
firstName: 'Ruth', | ||
lastName: 'Hamilton', | ||
email: 'ruth.hamilton@example.com' | ||
}, { | ||
firstName: 'Emma', | ||
lastName: 'Lane', | ||
email: 'emma.lane@example.com' | ||
}]; | ||
for (let object of data) { | ||
const passenger = new Passenger(object); | ||
// Create a Stripe account for each of the passengers. | ||
const customer = await stripe.customers.create({ | ||
email: passenger.email, | ||
description: passenger.displayName() | ||
}); | ||
passenger.stripeCustomerId = customer.id; | ||
await passenger.save(); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
const Passenger = mongoose.model('Passenger', PassengerSchema); | ||
|
||
module.exports = Passenger; |
Oops, something went wrong.