Last active
July 11, 2020 01:40
-
-
Save rpaul-stripe/da40eeda0ca26a3580196a50e6f66762 to your computer and use it in GitHub Desktop.
Stripe Checkout Node.js Express Example
This file contains hidden or 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
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); |
This file contains hidden or 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
h2 You successfully paid <strong>$5.00</strong>! |
This file contains hidden or 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
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") |
This file contains hidden or 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
{ | |
"dependencies": { | |
"body-parser": "^1.17.2", | |
"express": "^4.15.4", | |
"pug": "^2.0.0-rc.3", | |
"stripe": "^4.24.1" | |
} | |
} |
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
line 21, should be:
Otherwise you will not get the
charge
object inthen(charge => xxxxx)