mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 10:19:15 +03:00
оплата товара по кнопке 'купить'
This commit is contained in:
parent
2de28b8b9a
commit
4412de2cf4
@ -1,4 +1,5 @@
|
||||
const fs = require('fs');
|
||||
const ObjectID = require('mongodb').ObjectID;
|
||||
|
||||
function checkAuthenticated(req, res, next) {
|
||||
if (req.isAuthenticated()) return next();
|
||||
@ -12,9 +13,13 @@ module.exports = function(server, db) {
|
||||
res.json(items);
|
||||
} else {
|
||||
const items = await db.collection('items').find({}).toArray(),
|
||||
info = await db.collection('info').find({}).toArray(),
|
||||
sortBy = req.query.sorting;
|
||||
|
||||
|
||||
if (items.length > 0) {
|
||||
// устанавливаем count для каждого товара.
|
||||
info.forEach(info => db.collection('items').updateOne({"_id": ObjectID(info._id)}, {$set: {count: info.data.length}}));
|
||||
switch (sortBy) {
|
||||
case 'ascending':
|
||||
res.json(items.sort((a, b) => a.price - b.price));
|
||||
|
||||
@ -215,7 +215,7 @@ module.exports = function (server, db) {
|
||||
category: JSON.parse(req.body.category),
|
||||
info: req.body.data.split(',')
|
||||
}
|
||||
db.collection('items').updateOne({"_id": ObjectID(data.id)}, {$set: {title: data.title, count: data.info.length, price: data.price, description: data.description, category: data.category.title}});
|
||||
db.collection('items').updateOne({"_id": ObjectID(data.id)}, {$set: {title: data.title, price: data.price, description: data.description, category: data.category.title}});
|
||||
db.collection('info').updateOne({"_id": ObjectID(data.id)}, {$set: {title: data.title, data: data.info}});
|
||||
res.redirect('/control-panel/items');
|
||||
} catch (error) {
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
const ObjectID = require('mongodb').ObjectID;
|
||||
|
||||
module.exports = function(server, db, qiwiApi) {
|
||||
server.get('/success', (req, res) => {
|
||||
const query = req.query;
|
||||
@ -14,11 +16,16 @@ module.exports = function(server, db, qiwiApi) {
|
||||
if (status === 'PAID') {
|
||||
const findBill = await db.collection('buyers').findOne({bill_id: query.bill_id});
|
||||
if(findBill == null) {
|
||||
// создаем покупателя
|
||||
// выдаем данные и создаем покупателя
|
||||
const item = await db.collection('info').findOne({"_id": ObjectID(query.item_id)}),
|
||||
data = item.data,
|
||||
invoiceResult = data.splice(-1, 1);
|
||||
|
||||
db.collection('info').updateOne({"_id": ObjectID(query.item_id)}, {$set: {data: data}})
|
||||
db.collection('buyers').insertOne({bill_id, email, method, date, amount});
|
||||
res.send('Выдаем данные!');
|
||||
res.send(invoiceResult);
|
||||
} else {
|
||||
res.send('Ошибка');
|
||||
res.send('Этот заказ уже был оплачен. Данные были высланы покупателю.');
|
||||
}
|
||||
} else if (status === 'WAITING') {
|
||||
res.send('Инвоис ждет оплаты');
|
||||
@ -27,10 +34,10 @@ module.exports = function(server, db, qiwiApi) {
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
res.send('произошла ошибка!');
|
||||
res.render('main/404');
|
||||
});
|
||||
} else {
|
||||
res.send('bad request');
|
||||
res.render('main/404');
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -1,17 +1,27 @@
|
||||
const ObjectID = require('mongodb').ObjectID;
|
||||
|
||||
module.exports = function(server, db, qiwiApi) {
|
||||
server.put('/payment', (req, res) => {
|
||||
server.put('/payment', async (req, res) => {
|
||||
// проверяем товар на его наличие
|
||||
const item = await db.collection('info').findOne({"_id": ObjectID(req.body.items[0].id)});
|
||||
if (item.data.length > 0) {
|
||||
const billId = qiwiApi.generateId();
|
||||
const fields = {
|
||||
amount: req.body.totalPrice,
|
||||
currency: 'RUB',
|
||||
comment: `Оплата товара "${req.body.items[0].title}" Счёт: ${billId}`,
|
||||
expirationDateTime: qiwiApi.getLifetimeByDay(),
|
||||
customFields: {themeCode: 'Sergei-YaS7mIY0CQe'},
|
||||
customFields: {
|
||||
themeCode: 'Sergei-YaS7mIY0CQe'
|
||||
},
|
||||
email: req.body.email,
|
||||
successUrl: `https://polar-peak-95205.herokuapp.com/success?bill_id=${billId}&item_id=${req.body.items[0].id}&payment=qiwi`
|
||||
}
|
||||
qiwiApi.createBill(billId, fields).then((data) => {
|
||||
res.send(data);
|
||||
});
|
||||
} else {
|
||||
res.send('Товара нет в наличии.');
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -16,7 +16,7 @@ const methodOverride = require('method-override');
|
||||
const multer = require('multer');
|
||||
const QiwiBillPaymentsAPI = require('@qiwi/bill-payments-node-js-sdk');
|
||||
|
||||
const SECRET_KEY = '/';
|
||||
const SECRET_KEY = 'eyJ2ZXJzaW9uIjoiUDJQIiwiZGF0YSI6eyJwYXlpbl9tZXJjaGFudF9zaXRlX3VpZCI6ImlsYzZ5Yy0wMCIsInVzZXJfaWQiOiI3OTMxMjc2NDY3NCIsInNlY3JldCI6ImFkZWJmMWU2NjYyMmJjZTViODE2NzE3YTU4ZmJkZDc1NTUwNzMyYWI5NGY2OTU0ZTg5NjljNjUzMGZkM2U0M2YifX0=';
|
||||
const qiwiApi = new QiwiBillPaymentsAPI(SECRET_KEY);
|
||||
|
||||
MongoClient.connect(db_config.url, {useNewUrlParser: true, useUnifiedTopology: true})
|
||||
@ -48,9 +48,9 @@ MongoClient.connect(db_config.url, {useNewUrlParser: true, useUnifiedTopology: t
|
||||
require('./routes/admin')(server, db); // маршрут с формой авторизации админа
|
||||
require('./routes/control-panel')(server, db); // маршруты с панелью управления
|
||||
require('./routes/api')(server, db); // отдача данных клиенту
|
||||
require('./routes/invoice')(server, db, qiwiApi);
|
||||
|
||||
// Payments
|
||||
require('./routes/invoice')(server, db, qiwiApi);
|
||||
require('./routes/payments/qiwi.payment')(server, db, qiwiApi);
|
||||
|
||||
// 404 Redirect
|
||||
|
||||
4151
dist/public/js/admin.js
vendored
4151
dist/public/js/admin.js
vendored
File diff suppressed because one or more lines are too long
4127
dist/public/js/main.js
vendored
4127
dist/public/js/main.js
vendored
File diff suppressed because one or more lines are too long
4
dist/views/admin/assets/scripts.pug
vendored
4
dist/views/admin/assets/scripts.pug
vendored
@ -1,3 +1,3 @@
|
||||
script(async defer src="https://buttons.github.io/buttons.js")
|
||||
//script(src='../js/admin.js')
|
||||
script(src="http://localhost:8080/admin.js")
|
||||
script(src='../js/admin.js')
|
||||
//script(src="http://localhost:8080/admin.js")
|
||||
4
dist/views/main/assets/scripts.pug
vendored
4
dist/views/main/assets/scripts.pug
vendored
@ -1,3 +1,3 @@
|
||||
// Script JS
|
||||
//script(src='../js/main.js')
|
||||
script(src='http://localhost:8080/main.js')
|
||||
script(src='../js/main.js')
|
||||
//script(src='http://localhost:8080/main.js')
|
||||
@ -4,7 +4,7 @@
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "webpack --mode development",
|
||||
"build": "webpack --mode production",
|
||||
"wp-server": "webpack-dev-server --mode development"
|
||||
},
|
||||
"repository": {
|
||||
|
||||
@ -94,4 +94,4 @@ async function useRequest(request) {
|
||||
return await JSON.parse(response.request.response);
|
||||
}
|
||||
|
||||
export const apiService = new ApiService('http://localhost:3000');
|
||||
export const apiService = new ApiService('https://polar-peak-95205.herokuapp.com');
|
||||
@ -41,7 +41,6 @@ export class PayModal extends Modal {
|
||||
payInfo.payMethod = this.$el.querySelector('.payment-field input').value;
|
||||
payInfo.email = this.$el.querySelector('.email-field input').value;
|
||||
const response = await axios.put('/payment', payInfo);
|
||||
//console.log(response.data);
|
||||
document.location.href = response.data.payUrl;
|
||||
response.data.payUrl ? document.location.href = response.data.payUrl : alert(response.data);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
import axios from "axios";
|
||||
import axios from 'axios';
|
||||
|
||||
class ApiService {
|
||||
constructor(baseUrl) {
|
||||
@ -58,4 +58,4 @@ async function useRequest(request) {
|
||||
return await JSON.parse(response.request.response);
|
||||
}
|
||||
|
||||
export const apiService = new ApiService('http://localhost:3000');
|
||||
export const apiService = new ApiService('https://polar-peak-95205.herokuapp.com');
|
||||
Loading…
x
Reference in New Issue
Block a user