Building a Lightweight Stripe Checkout System

Cole Townsend
Cole TownsendDesigner at Robin Powered
Building a Lightweight
Stripe Checkout
System
JAM for designers
(and everyone)
*
What we’re building
J — Vanilla (!!), and jQuery *
A — Stripe charge microservice
JM — Reusable checkout form
Or React, more on that later
Stripe
Checkout
Elements
React-Stripe-Elements
require('dotenv').config();
const {send, json} = require('micro');
const post = require('./post');
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
module.exports = post(async (req, res) => {
const data = await json(req);
stripe.charges.create(data, (err, resp) => {
if (err) {
send(res, 400, {message: `Charge could not be created.`})
}
else {
send(res, 200, {message: `Charge created.`})
}
})
})
module.exports = exports = function (fn) {
return (req, res) => {
res.setHeader('Access-Control-Request-Method', 'POST,
GET')
res.setHeader("Access-Control-Allow-Credentials",
"true");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-
Requested-With, Content-Type, Accept, Authorization");
// set this with your own URL
res.setHeader('Access-Control-Allow-Origin',
process.env.ORIGIN);
…
const {method} = req
if (method === 'OPTIONS') {
return {}
}
if (method === 'GET') {
return {message: 'The Stripe charge server is up and
running!', timestamp: new Date().toISOString()}
}
if (method === 'POST') {
return fn(req, res)
}
else {
res.writeHead(405)
res.end('Method Not Allowed')
return
}
}
Markup
Javascript
jQuery
Plain ‘ol HTML
function createCharge(token) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: 'https://micro-stripe-api.twnsnd.co/',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer pk_test_JS759ictlRv2JtrZ9aRgqFKR'
},
data: JSON.stringify({
"amount": 2500,
"currency": "usd",
"source": token
}),
success: function () {
alert('Successfully charged!');
},
error: function (xhr, status, error) {
alert('Error with card');
console.log(error.message, xhr, status)
}
});
}
Examples
Stripe Elements
Stripe Checkout
micro-stripe.twnsnd.co
Deploying with Now
is a breeze
now -E
Building a Lightweight Stripe Checkout System
Building a Lightweight Stripe Checkout System
1 of 13

Recommended

The Sad Story of the Server that Tries to Please Everyone by
The Sad Story of the Server that Tries to Please EveryoneThe Sad Story of the Server that Tries to Please Everyone
The Sad Story of the Server that Tries to Please EveryoneIulian Dogariu
546 views34 slides
An Introduction to WebWorker - 01.26.12 by
An Introduction to WebWorker - 01.26.12An Introduction to WebWorker - 01.26.12
An Introduction to WebWorker - 01.26.12Digiflare
481 views16 slides
Richard Fridrich: Třesení stromem v JavaScriptu by
Richard Fridrich: Třesení stromem v JavaScriptuRichard Fridrich: Třesení stromem v JavaScriptu
Richard Fridrich: Třesení stromem v JavaScriptuDevelcz
136 views49 slides
Clojure functions 4 by
Clojure functions 4Clojure functions 4
Clojure functions 4Jackson dos Santos Olveira
296 views14 slides
From Node.js to Design Patterns by
From Node.js to Design Patterns From Node.js to Design Patterns
From Node.js to Design Patterns Luciano Mammino
52 views36 slides
ApplicationCoordinator для навигации между экранами / Павел Гуров (Avito) by
ApplicationCoordinator для навигации между экранами / Павел Гуров (Avito)ApplicationCoordinator для навигации между экранами / Павел Гуров (Avito)
ApplicationCoordinator для навигации между экранами / Павел Гуров (Avito)Ontico
511 views85 slides

More Related Content

What's hot

RxJS 5 in Depth by
RxJS 5 in DepthRxJS 5 in Depth
RxJS 5 in DepthC4Media
1.4K views47 slides
Multi qubit entanglement by
Multi qubit entanglementMulti qubit entanglement
Multi qubit entanglementVijayananda Mohire
141 views6 slides
JavaScript para Graficos y Visualizacion de Datos - BogotaJS by
JavaScript para Graficos y Visualizacion de Datos - BogotaJSJavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJSphilogb
1K views24 slides
Aalifassura 170209203254 by
Aalifassura 170209203254Aalifassura 170209203254
Aalifassura 170209203254Aztexain
73 views14 slides
Debugging JavaScript with Chrome by
Debugging JavaScript with ChromeDebugging JavaScript with Chrome
Debugging JavaScript with ChromeIgor Zalutsky
873 views14 slides
Javascript & Ajax Basics by
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax BasicsRichard Paul
1.4K views18 slides

What's hot(7)

RxJS 5 in Depth by C4Media
RxJS 5 in DepthRxJS 5 in Depth
RxJS 5 in Depth
C4Media1.4K views
JavaScript para Graficos y Visualizacion de Datos - BogotaJS by philogb
JavaScript para Graficos y Visualizacion de Datos - BogotaJSJavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
philogb1K views
Aalifassura 170209203254 by Aztexain
Aalifassura 170209203254Aalifassura 170209203254
Aalifassura 170209203254
Aztexain73 views
Debugging JavaScript with Chrome by Igor Zalutsky
Debugging JavaScript with ChromeDebugging JavaScript with Chrome
Debugging JavaScript with Chrome
Igor Zalutsky873 views
Javascript & Ajax Basics by Richard Paul
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
Richard Paul1.4K views
Mage Titans - Workshop - UI Components by vkorotun
Mage Titans - Workshop - UI ComponentsMage Titans - Workshop - UI Components
Mage Titans - Workshop - UI Components
vkorotun246 views

Similar to Building a Lightweight Stripe Checkout System

Scalable Angular 2 Application Architecture by
Scalable Angular 2 Application ArchitectureScalable Angular 2 Application Architecture
Scalable Angular 2 Application ArchitectureFDConf
9.4K views115 slides
Express JS by
Express JSExpress JS
Express JSAlok Guha
2.5K views17 slides
Reduxing like a pro by
Reduxing like a proReduxing like a pro
Reduxing like a proBoris Dinkevich
220 views38 slides
Marble Testing RxJS streams by
Marble Testing RxJS streamsMarble Testing RxJS streams
Marble Testing RxJS streamsIlia Idakiev
1.2K views95 slides
Pengenalan blaast platform sdk by
Pengenalan blaast platform sdkPengenalan blaast platform sdk
Pengenalan blaast platform sdkArief Bayu Purwanto
435 views23 slides
Mashing up JavaScript – Advanced Techniques for modern Web Apps by
Mashing up JavaScript – Advanced Techniques for modern Web AppsMashing up JavaScript – Advanced Techniques for modern Web Apps
Mashing up JavaScript – Advanced Techniques for modern Web AppsBastian Hofmann
4.7K views97 slides

Similar to Building a Lightweight Stripe Checkout System(20)

Scalable Angular 2 Application Architecture by FDConf
Scalable Angular 2 Application ArchitectureScalable Angular 2 Application Architecture
Scalable Angular 2 Application Architecture
FDConf9.4K views
Express JS by Alok Guha
Express JSExpress JS
Express JS
Alok Guha2.5K views
Marble Testing RxJS streams by Ilia Idakiev
Marble Testing RxJS streamsMarble Testing RxJS streams
Marble Testing RxJS streams
Ilia Idakiev1.2K views
Mashing up JavaScript – Advanced Techniques for modern Web Apps by Bastian Hofmann
Mashing up JavaScript – Advanced Techniques for modern Web AppsMashing up JavaScript – Advanced Techniques for modern Web Apps
Mashing up JavaScript – Advanced Techniques for modern Web Apps
Bastian Hofmann4.7K views
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017 by Codemotion
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
Codemotion859 views
node.js and the AR.Drone: building a real-time dashboard using socket.io by Steven Beeckman
node.js and the AR.Drone: building a real-time dashboard using socket.ionode.js and the AR.Drone: building a real-time dashboard using socket.io
node.js and the AR.Drone: building a real-time dashboard using socket.io
Steven Beeckman15.9K views
Bonnes pratiques de développement avec Node js by Francois Zaninotto
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node js
Francois Zaninotto5.7K views
Managing State in React Apps with RxJS by James Wright at FrontCon 2019 by DevClub_lv
Managing State in React Apps with RxJS by James Wright at FrontCon 2019Managing State in React Apps with RxJS by James Wright at FrontCon 2019
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
DevClub_lv566 views
MongoDB World 2019: Life In Stitch-es by MongoDB
MongoDB World 2019: Life In Stitch-esMongoDB World 2019: Life In Stitch-es
MongoDB World 2019: Life In Stitch-es
MongoDB184 views
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -... by Codemotion
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Codemotion153 views
Future Decoded - Node.js per sviluppatori .NET by Gianluca Carucci
Future Decoded - Node.js per sviluppatori .NETFuture Decoded - Node.js per sviluppatori .NET
Future Decoded - Node.js per sviluppatori .NET
Gianluca Carucci235 views
Python queue solution with asyncio and kafka by Ondřej Veselý
Python queue solution with asyncio and kafkaPython queue solution with asyncio and kafka
Python queue solution with asyncio and kafka
Ondřej Veselý2.6K views

Recently uploaded

Transcript: Redefining the book supply chain: A glimpse into the future - Tec... by
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...Transcript: Redefining the book supply chain: A glimpse into the future - Tec...
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...BookNet Canada
41 views16 slides
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...The Digital Insurer
90 views52 slides
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueShapeBlue
222 views7 slides
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda... by
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...ShapeBlue
161 views13 slides
Business Analyst Series 2023 - Week 4 Session 8 by
Business Analyst Series 2023 -  Week 4 Session 8Business Analyst Series 2023 -  Week 4 Session 8
Business Analyst Series 2023 - Week 4 Session 8DianaGray10
123 views13 slides
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...ShapeBlue
139 views29 slides

Recently uploaded(20)

Transcript: Redefining the book supply chain: A glimpse into the future - Tec... by BookNet Canada
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...Transcript: Redefining the book supply chain: A glimpse into the future - Tec...
Transcript: Redefining the book supply chain: A glimpse into the future - Tec...
BookNet Canada41 views
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by The Digital Insurer
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
ShapeBlue222 views
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda... by ShapeBlue
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
ShapeBlue161 views
Business Analyst Series 2023 - Week 4 Session 8 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 8Business Analyst Series 2023 -  Week 4 Session 8
Business Analyst Series 2023 - Week 4 Session 8
DianaGray10123 views
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by ShapeBlue
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
ShapeBlue139 views
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by ShapeBlue
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online
ShapeBlue221 views
Why and How CloudStack at weSystems - Stephan Bienek - weSystems by ShapeBlue
Why and How CloudStack at weSystems - Stephan Bienek - weSystemsWhy and How CloudStack at weSystems - Stephan Bienek - weSystems
Why and How CloudStack at weSystems - Stephan Bienek - weSystems
ShapeBlue238 views
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue by ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
ShapeBlue147 views
Optimizing Communication to Optimize Human Behavior - LCBM by Yaman Kumar
Optimizing Communication to Optimize Human Behavior - LCBMOptimizing Communication to Optimize Human Behavior - LCBM
Optimizing Communication to Optimize Human Behavior - LCBM
Yaman Kumar38 views
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R... by ShapeBlue
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
ShapeBlue173 views
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023 by BookNet Canada
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
BookNet Canada44 views
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ... by ShapeBlue
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
ShapeBlue166 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray10139 views
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha... by ShapeBlue
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
ShapeBlue180 views

Building a Lightweight Stripe Checkout System

  • 3. * What we’re building J — Vanilla (!!), and jQuery * A — Stripe charge microservice JM — Reusable checkout form Or React, more on that later
  • 5. require('dotenv').config(); const {send, json} = require('micro'); const post = require('./post'); const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); module.exports = post(async (req, res) => { const data = await json(req); stripe.charges.create(data, (err, resp) => { if (err) { send(res, 400, {message: `Charge could not be created.`}) } else { send(res, 200, {message: `Charge created.`}) } }) })
  • 6. module.exports = exports = function (fn) { return (req, res) => { res.setHeader('Access-Control-Request-Method', 'POST, GET') res.setHeader("Access-Control-Allow-Credentials", "true"); res.setHeader("Access-Control-Allow-Headers", "Origin, X- Requested-With, Content-Type, Accept, Authorization"); // set this with your own URL res.setHeader('Access-Control-Allow-Origin', process.env.ORIGIN); …
  • 7. const {method} = req if (method === 'OPTIONS') { return {} } if (method === 'GET') { return {message: 'The Stripe charge server is up and running!', timestamp: new Date().toISOString()} } if (method === 'POST') { return fn(req, res) } else { res.writeHead(405) res.end('Method Not Allowed') return } }
  • 9. function createCharge(token) { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", url: 'https://micro-stripe-api.twnsnd.co/', beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Bearer pk_test_JS759ictlRv2JtrZ9aRgqFKR' }, data: JSON.stringify({ "amount": 2500, "currency": "usd", "source": token }), success: function () { alert('Successfully charged!'); }, error: function (xhr, status, error) { alert('Error with card'); console.log(error.message, xhr, status) } }); }
  • 11. Deploying with Now is a breeze now -E