-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
141 lines (113 loc) · 3.49 KB
/
app.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require("dotenv").config();
require("./models/connection");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var deleteRouter = require("./routes/delete");
var conciergesRouter = require("./routes/concierges");
var requestRouter = require("./routes/request");
var chatRouter = require("./routes/chat");
const cors = require("cors");
var app = express();
app.use(
cors({
origin: "https://quest-frontend-chi.vercel.app",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
})
);
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
const Request = require("./models/request");
// Import the Stripe library and set your secret key
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
app.post("/create-payment-intent", async (req, res) => {
try {
const paymentIntent = await stripe.paymentIntents.create({
amount: 1000,
currency: "usd",
});
res.json({ clientSecret: paymentIntent.client_secret });
console.log(paymentIntent.client_secret);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
});
app.post("/processpayment", async (req, res) => {
try {
const { paymentMethodId } = req.body;
console.log(paymentMethodId);
const paymentIntent = await stripe.paymentIntents.create({
payment_method: paymentMethodId,
amount: 1000,
currency: "usd",
confirm: true,
return_url: "https://google.com",
});
console.log("great payment success");
return res.json({ success: true, paymentIntent });
} catch (error) {
console.error("Error processing payment:", error);
console.log(error.message);
return res.status(500).json({ error: error.message });
}
});
const Pusher = require("pusher");
const pusher = new Pusher({
appId: process.env.PUSHER_APP_ID,
key: process.env.PUSHER_KEY,
secret: process.env.PUSHER_SECRET,
cluster: process.env.PUSHER_CLUSTER,
useTLS: true,
});
app.options("/sendmessage", cors());
app.post("/sendmessage", (req, res) => {
const { message, sender, userType } = req.body;
console.log(req.body.message);
console.log("this", req.body.sender);
console.log("thes", req.body.userType);
console.log(req.body.id);
// Do something with the message, e.g., store it in a database
// Broadcast the message to connected clients using Pusher
pusher
.trigger("quest-livechat", "message", {
message: req.body.message,
sender: req.body.sender,
userType: req.body.userType,
})
.then((data) => {
const savedMessage = {
date: new Date(),
firstname: req.body.sender,
message: req.body.message,
};
Request.updateOne(
{ _id: req.body.id },
{
$push: {
chat: savedMessage,
},
}
).then((data) => {
console.log(data);
});
});
res.json({ success: true });
});
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/delete", deleteRouter);
app.use("/concierges", conciergesRouter);
app.use("/request", requestRouter);
app.use("/chat", chatRouter);
// const port = process.env.PORT || 3000;
// app.listen(port, () => {
// console.log(`Server is running on port ${port}`);
// });
module.exports = app;