nodemailer

This commit is contained in:
Sergey Yarkov 2020-01-16 21:36:12 +03:00
parent 0d4c9cfcb7
commit 83a176543d
5 changed files with 39 additions and 5 deletions

View File

@ -11,4 +11,6 @@ SESSION_SECRET=secret
DATABASE_URI=подключение к БД MONGODB DATABASE_URI=подключение к БД MONGODB
DATABASE_NAME=имя БД DATABASE_NAME=имя БД
QIWI_SECRET_KEY=секретный ключ QIWI API QIWI_SECRET_KEY=секретный ключ QIWI API
EMAIL_LOGIN=логин от почты
EMAIL_PASSWORD=пароль от почты
``` ```

View File

@ -0,0 +1,13 @@
const nodemailer = require('nodemailer');
module.exports = {
transporter: nodemailer.createTransport({
host: "smtp.mail.ru",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: process.env.EMAIL_LOGIN, // email login
pass: process.env.EMAIL_PASSWORD // email password
}
})
}

View File

@ -1,4 +1,5 @@
const ObjectID = require('mongodb').ObjectID; const ObjectID = require('mongodb').ObjectID;
const nodemailer = require('../nodemailer.config');
module.exports = function(server, db, qiwiApi) { module.exports = function(server, db, qiwiApi) {
server.get('/success', (req, res) => { server.get('/success', (req, res) => {
@ -15,13 +16,13 @@ module.exports = function(server, db, qiwiApi) {
amount = parseFloat(data.amount.value); amount = parseFloat(data.amount.value);
if (status === 'PAID') { if (status === 'PAID') {
const ids = query.item_id.split('and'); const ids = query.item_id.split('and'),
const findBill = await db.collection('buyers').findOne({bill_id: query.bill_id}); findBill = await db.collection('buyers').findOne({bill_id: query.bill_id});
if(findBill == null) { if(findBill == null) {
const result = []; const result = [];
//выдаем данные и создаем покупателя // создаем покупателя и выдаем данные
for (let i = 0; i < ids.length; i++) { for (let i = 0; i < ids.length; i++) {
const item = await db.collection('info').findOne({"_id": ObjectID(ids[i])}), const item = await db.collection('info').findOne({"_id": ObjectID(ids[i])}),
data = item.data; data = item.data;
@ -29,8 +30,19 @@ module.exports = function(server, db, qiwiApi) {
result.push(data.splice(-1, 1).join('')); result.push(data.splice(-1, 1).join(''));
db.collection('info').updateOne({"_id": ObjectID(ids[i])}, {$set: {data: data}}); db.collection('info').updateOne({"_id": ObjectID(ids[i])}, {$set: {data: data}});
} }
db.collection('buyers').insertOne({bill_id, email, method, date, amount, data: result}); db.collection('buyers').insertOne({bill_id, email, method, date, amount, data: result});
await nodemailer.transporter.sendMail({
from: `"Оплата аккаунта." <${process.env.EMAIL_LOGIN}>`, // sender address
to: email, // list of receivers
subject: "Оплата успешно проведена.", // subject
text: "Данные купленного товара находятся в этом письме.", // plain text body
html: `<b>Номер заказа: ${bill_id}</b><br>
<b>Данные товара:</b><br>
<p>${result.join('<br>')}</p>
` // html body
});
res.render('main/success', {result, email}); res.render('main/success', {result, email});
} else { } else {
res.send('Этот заказ уже был оплачен. Данные были высланы покупателю.'); res.send('Этот заказ уже был оплачен. Данные были высланы покупателю.');
@ -41,7 +53,8 @@ module.exports = function(server, db, qiwiApi) {
res.send('Время жизни счета истекло. Счет не оплачен'); res.send('Время жизни счета истекло. Счет не оплачен');
} }
}) })
.catch(() => { .catch((error) => {
console.log(error);
res.render('main/404'); res.render('main/404');
}); });
} else { } else {

5
package-lock.json generated
View File

@ -5474,6 +5474,11 @@
} }
} }
}, },
"nodemailer": {
"version": "6.4.2",
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.4.2.tgz",
"integrity": "sha512-g0n4nH1ONGvqYo1v72uSWvF/MRNnnq1LzmSzXb/6EPF3LFb51akOhgG3K2+aETAsJx90/Q5eFNTntu4vBCwyQQ=="
},
"nodemon": { "nodemon": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.2.tgz", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.2.tgz",

View File

@ -32,6 +32,7 @@
"method-override": "^3.0.0", "method-override": "^3.0.0",
"mongodb": "^3.3.4", "mongodb": "^3.3.4",
"multer": "^1.4.2", "multer": "^1.4.2",
"nodemailer": "^6.4.2",
"passport": "^0.4.0", "passport": "^0.4.0",
"passport-local": "^1.0.0", "passport-local": "^1.0.0",
"pug": "^2.0.4", "pug": "^2.0.4",