Welcome folks today in this blog post we will be sending invoice
email using the nodemailer
library to integrate stripe
checkout in browser using javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new node.js
project using the below command as shown below
npm init -y
npm i express
npm i stripe
npm i nodemailer
npm i dotenv
After that you will see the below directory
structure of the node.js app as shown below
Now we need to create the index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
const stripe = require("stripe")("##secretkey##"); const express = require("express"); const dotenv = require('dotenv') dotenv.config() const nodemailer = require("nodemailer"); const app = express(); // deploy this app publicly ngrok app.get("/", (req, res) => { res.send("hello world dfgdfgfdgdfgdf"); }); let endpointSecret = "signingsecret"; let session = ""; let product = "https://drive.google.com/file/d/1O6thKAum6JKB5n4cJLLauZ4dFGRHRcmk/view?usp=sharing" // webhook app.post( "/webhooks", express.raw({ type: "application/json" }), async(request, response) => { const sig = request.headers["stripe-signature"]; // stripe let event; try { event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret); } catch (err) { response.status(400).send(`Webhook Error: ${err.message}`); return; } // Handle the event switch (event.type) { case "checkout.session.async_payment_failed": session = event.data.object; console.log(session); // Then define and call a function to handle the event checkout.session.async_payment_failed break; case "checkout.session.completed": session = event.data.object; console.log(session); let emailto = session.customer_details.email; let transporter = nodemailer.createTransport({ host: "smtp.gmail.com", port: 587, secure: false, // true for 465, false for other ports auth: { user: process.env.email, // generated ethereal user pass: process.env.password, // generated ethereal password }, }); // send mail with defined transport object let info = await transporter.sendMail({ from: process.env.email, // sender address to: emailto, // list of receivers subject: "Thanks for the Payment for the Product", // Subject line text: "Thanks for the payment for the product", // plain text body html: ` Hello ${session.customer_details.email} Thanks for the payment for the product Here is the link of the product from google drive ${product} . You can download the file by going to this link `, // html body }); console.log("Message sent: %s", info.messageId); // send invoice email using nodemailer // Then define and call a function to handle the event checkout.session.async_payment_succeeded break; // ... handle other event types default: console.log(`Unhandled event type ${event.type}`); } // Return a 200 response to acknowledge receipt of the event response.send(); } ); app.listen(5000, () => { console.log("App is listening on port 5000"); }); |
As you can see we are importing the express and stripe modules at the very top and here you need to replace the secret key
from the stripe dashboard and then we are starting the express app at port 5000
and here we have the post
request at the /webhooks
endpoint and here we need to deploy this node.js app to live url
using ngrok library.
And now we need to go to webhooks
and basically we are listening on various events for the checkout
event as shown below
Now we need to create the .env
file where we will be storing the email and password
as shown below
.env
1 2 |
email=##youremail## password=##yourpassword## |