Skip to content

Instantly share code, notes, and snippets.

@rpaul-stripe
Last active July 11, 2020 01:40
Show Gist options
  • Select an option

  • Save rpaul-stripe/da40eeda0ca26a3580196a50e6f66762 to your computer and use it in GitHub Desktop.

Select an option

Save rpaul-stripe/da40eeda0ca26a3580196a50e6f66762 to your computer and use it in GitHub Desktop.
Stripe Checkout Node.js Express Example
const keyPublishable = process.env.PUBLISHABLE_KEY;
const keySecret = process.env.SECRET_KEY;
const app = require("express")();
const stripe = require("stripe")(keySecret);
app.set("view engine", "pug");
app.use(require("body-parser").urlencoded({extended: false}));
app.get("/", (req, res) =>
res.render("index.pug", {keyPublishable}));
app.post("/charge", (req, res) => {
let amount = 500;
stripe.customers.create({
email: req.body.stripeEmail,
card: req.body.stripeToken
})
.then(customer =>
stripe.charges.create({
amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
}))
.catch(err => console.log("Error:", err))
.then(charge => res.render("charge.pug"));
});
app.listen(4567);
h2 You successfully paid <strong>$5.00</strong>!
html
body
form(action="/charge", method="post")
article
label Amount: $5.00
script(
src="//checkout.stripe.com/v2/checkout.js",
class="stripe-button",
data-key=keyPublishable,
data-locale="auto",
data-description="Sample Charge",
data-amount="500")
{
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.4",
"pug": "^2.0.0-rc.3",
"stripe": "^4.24.1"
}
}
@jeremypbeasley

Copy link
Copy Markdown

Heads up, this doesn't work without bodyParser. I'm new to Node/Express and it took me an hour to resolve this :(

May want to add this to app.js

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

@utx0

utx0 commented Apr 21, 2017

Copy link
Copy Markdown

Be great if you could also post your packages.json file too. I'm having issues and want to make sure I havent a version difference.

@spelunk

spelunk commented Apr 21, 2017

Copy link
Copy Markdown

Here's what worked for me on April 21:

  • body-parser@1.17.1
  • express 4.5.12
  • pug@2.0.0-beta.12
  • stripe@4.18.0

package.json:

{
	"main": "server.js",
	"dependencies": {
		"express":"",
		"stripe":"",
		"body-parser":"",
		"pug":""
	}
}
  • download app.js and save as server.js
  • mkdir views
  • download charge.pug and save as views/charge.pug
  • download index.pug and save as views/index.pug

Then run npm start and go to http://localhost:4567

@fsojitra

Copy link
Copy Markdown

it gives me this error ''PUBLISHABLE_KEY'' is not recognized as an internal or external command,
operable program or batch file.

@paulmelero

Copy link
Copy Markdown

@fsojitra, its a ENV variable. You have to set it first.

@duke-m

duke-m commented Aug 15, 2017

Copy link
Copy Markdown

Hi Ryan, I've generalized a tiny bit and made a rep: https://github.com/duke-m/stripe-express
Hope, that's okay (copyright stuff?), if not, please let me know

@rpaul-stripe

Copy link
Copy Markdown
Author

I've added a package.json file to the gist so that you can see the dependencies. For more details about how to configure and run it (including how to set the environment variables) you can refer to the associated tutorial on the Stripe website: https://stripe.com/docs/checkout/express

@rpaul-stripe

Copy link
Copy Markdown
Author

@duke-m: totally ok! I consider the example here public domain, you can do as you please with it including extending and distributing it under whatever terms you want.

@callmewhy

callmewhy commented Sep 19, 2017

Copy link
Copy Markdown

line 21, should be:

return stripe.stripe.charges.create({

Otherwise you will not get the charge object in then(charge => xxxxx)

@olufekosamuel

Copy link
Copy Markdown

Gooday I did everything up there. But the stripe checkout is not coming up on the page. Please I will really appreciate any help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment