Skip to content

Commit

Permalink
fix: handle unexpected error on checkout endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Saksham-Chauhan committed Jun 27, 2023
1 parent 9142ee9 commit 6a01270
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 85 deletions.
108 changes: 54 additions & 54 deletions src/mail.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,100 @@
require('dotenv').config()
const nodemailer = require('nodemailer')
const handlebars = require('handlebars')
const fs = require('fs')
require("dotenv").config();
const nodemailer = require("nodemailer");
const handlebars = require("handlebars");
const fs = require("fs");

async function sendEmail (contactObj) {
async function sendEmail(contactObj) {
try {
const transporter = nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
secure: true,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
})
let res = null
if (contactObj.type === 'contact') {
pass: process.env.EMAIL_PASSWORD,
},
});
let res = null;
if (contactObj.type === "contact") {
res = await transporter.sendMail({
from: process.env.EMAIL_USERNAME,
to: process.env.EMAIL_USERNAME,
subject: 'User Inquiry through Contact Us',
text: `Dear Raghav,\n\nI hope this email finds you well. I am reaching out to inform you that we have received an inquiry through the Contact Us section of your website. The details of the inquiry are as follows:\n\nName:${contactObj.name}\nContact Details:${contactObj.phone}\n\nMessage:${contactObj.message}\n\nWe appreciate your efforts in creating a user-friendly platform for customers to connect with your brand. We will be taking prompt action to respond to the user and address their concerns accordingly.\n\nThank you for your attention to this matter.\n\nBest regards,\n${contactObj.name}\n${contactObj.phone}\n${contactObj.email}`
})
const source = fs.readFileSync('../mail_assets/contact.hbs', 'utf8')
const template = handlebars.compile(source)
const html = template({ user: contactObj.name })
subject: "User Inquiry through Contact Us",
text: `Dear Raghav,\n\nI hope this email finds you well. I am reaching out to inform you that we have received an inquiry through the Contact Us section of your website. The details of the inquiry are as follows:\n\nName:${contactObj.name}\nContact Details:${contactObj.phone}\n\nMessage:${contactObj.message}\n\nWe appreciate your efforts in creating a user-friendly platform for customers to connect with your brand. We will be taking prompt action to respond to the user and address their concerns accordingly.\n\nThank you for your attention to this matter.\n\nBest regards,\n${contactObj.name}\n${contactObj.phone}\n${contactObj.email}`,
});
const source = fs.readFileSync("../mail_assets/contact.hbs", "utf8");
const template = handlebars.compile(source);
const html = template({ user: contactObj.name });
if (contactObj.email) {
await transporter.sendMail({
from: process.env.EMAIL_USERNAME,
to: contactObj.email,
subject: 'Thank you for contacting Raagwaas.',
subject: "Thank you for contacting Raagwaas.",
html,
attachments: [
{
filename: 'white-logo.png',
path: '../mail_assets/white-logo.png',
cid: 'white-logo.png'
filename: "white-logo.png",
path: "../mail_assets/white-logo.png",
cid: "white-logo.png",
},
{
filename: 'instagram.png',
path: '../mail_assets/instagram.png',
cid: 'instagram.png'
filename: "instagram.png",
path: "../mail_assets/instagram.png",
cid: "instagram.png",
},
{
filename: 'facebook.png',
path: '../mail_assets/facebook.png',
cid: 'facebook.png'
}
]
})
filename: "facebook.png",
path: "../mail_assets/facebook.png",
cid: "facebook.png",
},
],
});
}
} else {
res = await transporter.sendMail({
from: process.env.EMAIL_USERNAME,
to: process.env.EMAIL_USERNAME,
subject: 'Request for Subscription',
text: `Dear Raghav,\n\nI want to subscribing your newsletter! \n\n Email address:${contactObj?.email}`
})
const source = fs.readFileSync('../mail_assets/subscribe.hbs', 'utf8')
const template = handlebars.compile(source)
const html = template({ user: contactObj.name })
subject: "Request for Subscription",
text: `Dear Raghav,\n\nI want to subscribing your newsletter! \n\n Email address:${contactObj?.email}`,
});
const source = fs.readFileSync("../mail_assets/subscribe.hbs", "utf8");
const template = handlebars.compile(source);
const html = template({ user: contactObj.name });
if (contactObj.email) {
await transporter.sendMail({
from: process.env.EMAIL_USERNAME,
to: contactObj.email,
subject: 'Thank you for contacting Raagwaas.',
subject: "Thank you for contacting Raagwaas.",
html,
attachments: [
{
filename: 'white-logo.png',
path: '../mail_assets/white-logo.png',
cid: 'white-logo.png'
filename: "white-logo.png",
path: "../mail_assets/white-logo.png",
cid: "white-logo.png",
},
{
filename: 'instagram.png',
path: '../mail_assets/instagram.png',
cid: 'instagram.png'
filename: "instagram.png",
path: "../mail_assets/instagram.png",
cid: "instagram.png",
},
{
filename: 'facebook.png',
path: '../mail_assets/facebook.png',
cid: 'facebook.png'
}
]
})
filename: "facebook.png",
path: "../mail_assets/facebook.png",
cid: "facebook.png",
},
],
});
}
}
if (res?.messageId || res?.response?.includes('OK')) {
return { data: res }
if (res?.messageId || res?.response?.includes("OK")) {
return { data: res };
} else {
return false
return false;
}
} catch (error) {
console.log('Error occurred during sending mail: ' + error.message)
return null
console.log("Error occurred during sending mail: " + error.message);
return null;
}
}

module.exports = sendEmail
module.exports = sendEmail;
66 changes: 35 additions & 31 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,39 +112,43 @@ app.get("/stripe-redirect/:id", (req, res) => {
});

app.post("/checkout", async (req, res) => {
const {
milestoneTitle,
milestoneUnitAmount: amount,
apiKey,
projectIdentifier,
type,
} = req.body;
const { projectName, projectIcon } = await getProjectData(
apiKey,
projectIdentifier
);
if ((milestoneTitle && amount) || projectIcon) {
const session = await stripe.checkout.sessions.create({
line_items: [
{
price_data: {
currency: type.toLowerCase(),
product_data: {
name: projectName,
description: milestoneTitle,
images: [projectIcon],
try {
const {
milestoneTitle,
milestoneUnitAmount: amount,
apiKey,
projectIdentifier,
type,
} = req.body;
const { projectName, projectIcon } = await getProjectData(
apiKey,
projectIdentifier
);
if ((milestoneTitle && amount) || projectIcon) {
const session = await stripe.checkout.sessions.create({
line_items: [
{
price_data: {
currency: type.toLowerCase(),
product_data: {
name: projectName,
description: milestoneTitle,
images: [projectIcon],
},
unit_amount: Math.round(Number(amount)) * 100,
},
unit_amount: Math.round(Number(amount)) * 100,
quantity: 1,
},
quantity: 1,
},
],
mode: "payment",
success_url: `${serverHost}/stripe-redirect/success?pid=${projectIdentifier}`,
cancel_url: `${serverHost}/stripe-redirect/cancel`,
});
res.status(200).json({ msg: "Checkout URL", data: session.url });
} else res.status(404).json({ msg: "Some keys are missing", data: null });
],
mode: "payment",
success_url: `${serverHost}/stripe-redirect/success?pid=${projectIdentifier}`,
cancel_url: `${serverHost}/stripe-redirect/cancel`,
});
res.status(200).json({ msg: "Checkout URL", data: session.url });
} else res.status(404).json({ msg: "Some keys are missing", data: null });
} catch (error) {
res.status(500).json({ msg: error?.message, data: null });
}
});

app.post("/invoice", async (req, res) => {
Expand Down

0 comments on commit 6a01270

Please sign in to comment.