Skip to content

Instantly share code, notes, and snippets.

@duhaime
Last active August 12, 2018 20:45
Show Gist options
  • Save duhaime/64fc4642c8bf5c5020fec25251b0efab to your computer and use it in GitHub Desktop.
Save duhaime/64fc4642c8bf5c5020fec25251b0efab to your computer and use it in GitHub Desktop.
Simple payments with Node + Stripe
var express = require('express')
var bodyParser = require('body-parser')
var path = require('path')
// payments
var keyPublishable = 'PUT YOUR TEST PUBLISHABLE KEY HERE (THIS STARTS WITH pk_)'
var keySecret = 'PUT YOUR TEST SECRET KEY HERE (THIS STARTS WITH sk_)'
var stripe = require('stripe')(keySecret)
// server
var app = express()
app.use(bodyParser.urlencoded({ extended: true }))
// routes
app.post('/api/pay', function(req, res) {
var token = req.body.stripeToken;
var charge = stripe.charges.create({
amount: 1700, // create a charge for 1700 cents USD ($17)
currency: 'usd',
description: 'Bargain Basement Charge',
source: token,
}, function(err, charge) {
if (err) { console.warn(err) } else {
res.status(200).send(charge)
}
})
})
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'))
})
app.listen(8080, function() {
console.log('Server running at http://127.0.0.1:8080/')
})
# install dependencies and get the application set up:
npm init # accept all defaults by hitting enter
npm install -S body-parser express save stripe
# register for an account at stipe.com
# after you register, get your api keys from https://dashboard.stripe.com/account/apikeys
# next copy index.html and app.js below into your directory
# then copy your api key values into the relevant places in index.html and app.js
# start your web server by running:
node app.js
# click the pay button and fill out the form
# use the test credit card number provided by stripe: 4242 4242 4242 4242
# use any values for the remaining fields
# submit the form
# Your server will then process a charge for $17.00 USD to the test card.
<doctype html>
<html>
<head></head>
<body>
<form action='/api/pay' method='POST'>
<script
src='https://checkout.stripe.com/checkout.js' class='stripe-button'
data-key='PUT YOUR TEST PUBLISHABLE KEY HERE (THIS STARTS WITH pk_)'
data-amount='1700'
data-name='Bargain Basement Deals'
data-description='5 janglers'
data-image='https://stripe.com/img/documentation/checkout/marketplace.png'
data-locale='auto'
data-zip-code='true'>
</script>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment