How to make payment with stripe using nodejs
Hi,
This article covers making payment using nodejs and stripe . If you want to build a simple shopping cart with stripe as payment gateway this article covers it
Step #1: Install the module needed for the project in your app
we’ll be needing just express,nodemailer and stripe for this
npm install express stripe nodemailer dotenv
this how our folder structure would look like
-routes
--payment.js
server.js
in order to use stripe make sure you get your secret key from stripe
Step #2: Add this in your server.js file
const express = require("express");
const dotenv = require("dotenv");
const PORT = process.env.PORT || 5000;
dotenv.config();
const app = express();
const router = express.Router();
const paymentRoutes = require("./routes/payment");
app.use("/api", paymentRoutes);
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
app.get("/h", (req, res) => {
res.send("Successful response.");
});
app.use("/", router);
Step #3: Add this in your .env file
STRIPE_SECREY_KEY=sk_test_
GOOGLE_APP_EMAIL=
GOOGLE_APP_PW=
Step #4: Add this in your payment.js file
const router = require("express").Router();
const Stripe = require("stripe");
const stripe = Stripe(process.env.STRIPE_SECREY_KEY);
const nodemailer = require("nodemailer");
//make a post request
router.post("/payment", (req, res) => {
// amount to be paid ,it has to be amount * 100 in order to get the exact amount to be charged
// get the price from the frontend
let totalPrice = Math.round(req.body.totalPrice * 100);
//hardcoded price without front-end
let totalPrice = 500 * 100
// customer email
stripe.customers
.create({
email: "t@gmail.com"
})
.then(customer => {
// create a source
return stripe.customers.createSource(customer.id, {
source: "tok_visa"
});
})
.then(source => {
// create charges
return stripe.charges.create({
amount: totalPrice,
currency: "usd",
customer: source.customer,
},
// email template
function (err, charge) {
if (err) {
console.log(err);
} else {
var emailTemplate = `Hello , \n
thank you for your order! \n
Amount: ${charge.amount / 100} \n
`;
// to get GOOGLE_APP_PW you need to enable 2 step verification on your google account
let mailTransporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.GOOGLE_APP_EMAIL,
pass: process.env.GOOGLE_APP_PW
}
});
let details = {
from: process.env.GOOGLE_APP_EMAIL,
to: `t@gmail.com`,
subject: "shipping",
text: emailTemplate
};
mailTransporter.sendMail(details, err => {
if (err) {
console.log(err);
} else {
console.log("email sent");
}
});
}
}
);
})
.then(async charge => {
console.log("charge>", charge);
// after a successful payment
res.json({
success: true,
message: "Successfully made a payment"
});
})
.catch(err => {
res.status(500).json({
success: false,
message: err.message
});
});
});
module.exports = router;
request using postman
getting the payment in my stripe account
and also getting an email
this how to create payment methods with stripe and nodejs
please follow me for more contents