-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
125 lines (104 loc) · 3.57 KB
/
server.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const AWS = require('aws-sdk')
const app = express();
const PORT = 3000;
const PDFDocument = require('pdfkit');
const fs=require('fs')
const axios=require('axios')
app.use(bodyParser.json());
const path=require('path')
require('dotenv').config()
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY
});
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log('Connected to MongoDB');
}).catch(err => {
console.error('Failed to connect to MongoDB', err);
});
// Define a schema for the payment record
const paymentSchema = new mongoose.Schema({
amount: { type: Number, required: true },
sender: { type: String, required: true },
timestamp: { type: Date, default: Date.now },
});
// Create a model for the payment record
const Payment = mongoose.model('Payment', paymentSchema);
app.post('/api/payments', async (req, res) => {
try {
const { amount,sender } = req.body;
if (!amount || !sender) {
return res.status(400).json({ message: 'Amount and sender are required' });
}
const newPayment = new Payment({ amount,sender });
await newPayment.save();
const pdfPath = `./invoices/invoice_${newPayment._id}.pdf`;
const doc = new PDFDocument();
const invoiceDir = path.dirname(pdfPath);
if (!fs.existsSync(invoiceDir)) {
fs.mkdirSync(invoiceDir, { recursive: true });
}
const pdfStream = fs.createWriteStream(pdfPath);
doc.pipe(pdfStream);
doc.image('logo.jpg', 50, 45, { width: 100 }).fontSize(20).text('EasyPayVault', 200, 50);
doc.moveDown();
doc.fontSize(12).text(`Invoice for Payment`, { align: 'center' });
doc.moveDown();
doc.text(`Amount: INR ${amount}`);
doc.text(`Sender: ${sender}`);
doc.text(`Timestamp: ${newPayment.timestamp}`);
doc.moveDown();
doc.text('Thank you for using EasyPayVault!', { align: 'center' });
doc.end();
// Wait until the PDF is completely generated
pdfStream.on('finish', () => {
const fileContent = fs.readFileSync(pdfPath);
const params = {
Bucket: 'upi-iot-project',
Key: `invoices/invoice_${newPayment._id}.pdf`,
Body: fileContent,
ContentType: 'application/pdf'
};
// Upload the PDF to S3
s3.upload(params, (err, data) => {
if (err) {
console.error('Error uploading file to S3:', err);
return res.status(500).json({ message: 'Error uploading invoice to S3', error: err });
}
console.log("sent");
// Send back the S3 URL in the response
res.status(201).json({
message: 'Payment record created, invoice generated',
payment_id: newPayment._id,
s3InvoiceUrl: data.Location // S3 URL of the uploaded PDF
});
});
});
} catch (error) {
res.status(500).json({ message: 'Server error', error });
}
});
app.get('/latest-payment', async (req, res) => {
try {
const latestPayment = await Payment.findOne().sort({ timestamp: -1 });
if (latestPayment) {
res.json({
amount: latestPayment.amount,
timestamp: latestPayment.timestamp,
});
} else {
res.status(404).json({ message: 'No payments found' });
}
} catch (error) {
res.status(500).json({ message: 'Server error', error: error.message });
}
});
app.listen(PORT,'0.0.0.0', () => {
console.log(`Server running on http://localhost:${PORT}`);
});