-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
149 lines (126 loc) · 4.69 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
142
143
144
145
146
147
148
149
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
var cors = require('cors')
const productRoutes = require("./routes/product");
const buyerRoutes = require("./routes/buyer");
const sellerRoutes = require("./routes/seller");
const adminRoutes = require("./routes/admin");
const productCategoryRoutes = require("./routes/productCategory");
const wahrehouseRoutes = require("./routes/warehouse");
const productStateRoutes = require("./routes/productState");
const productModelRoutes = require("./routes/productModel");
const productBrandRoutes = require("./routes/productBrand");
const sellOfferRoutes = require("./routes/sellOffer");
const sellOfferSellerRoutes = require("./routes/sellOfferSeller");
const associationRoutes = require("./routes/association");
const associationProjectRoutes = require("./routes/associationProject");
const walletRoutes = require("./routes/wallet");
const orderRoutes = require("./routes/order");
const stripeRoutes = require("./routes/stripe");
const normalizePort = (val) => {
const port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
};
const port = normalizePort(process.env.PORT || "3000");
const END_POINT_SECRET = process.env.END_POINT_SECRET;
const db_cloud_host = process.env.DB_HOST_CLOUD;
const db_cloud_local = process.env.DB_HOST_LOCAL;
const app = express();
const stripe = require('stripe')('sk_test_51JfOTNKqXtPaxbjmHLKF5pT44Yp0Yo3kf7fOLM42bv2wMwEdIgodPoeKUpbYv39IVUwjjrAQEmDmVc22aHb8MMAQ00qE7RQmhK');
// app.use();
app.use(cors())
mongoose.set("useNewUrlParser", true);
mongoose.set("useUnifiedTopology", true);
mongoose.set("useCreateIndex", true);
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true })
.then(connect => console.log('connected to mongodb..'))
.catch(e => console.log('could not connect to mongodb', e))
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content, Accept, Content-Type, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
);
next();
});
const endpointSecret = END_POINT_SECRET;
const YOUR_DOMAIN = 'http://localhost:8081';
/*app.post('/webhook', express.raw({type: 'application/json'}), (request, response) => {
const sig = request.headers['stripe-signature'];
console.log(sig)
console.log(endpointSecret)
let event;
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
response.status(400).send(`Webhook Error: ${err.message}`);
return;
}
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
// const paymentIntent = event.data.object;
const paymentIntent = stripe.paymentIntents.create({
amount: 2000,
currency: 'eur',
payment_method_types: ['card'],
});
// Then define and call a function to handle the event payment_intent.succeeded
break;
// ... handle other event types
default:
console.log(`Unhandled event type ${event.type}`);
}
// Return a 200 response to acknowledge receipt of the event
response.send();
});
app.post('/webhook/createprice', express.raw({type: 'application/json'}), (request, response) => {
stripe.subscriptions.create({
customer: '{{CUSTOMER_ID}}',
items: [{
price_data: {
unit_amount: 5000,
currency: 'usd',
product: '{{PRODUCT_ID}}',
recurring: {
interval: 'month',
},
},
}],
});
});*/
app.use(express.static(__dirname + "/uploads"));
app.use("/uploads", express.static("uploads"));
app.use("/create-checkout-session", express.static('public'));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use("/api/buyer/auth", buyerRoutes);
app.use("/api/seller/auth", sellerRoutes);
app.use("/api/admin/auth", adminRoutes);
app.use("/api/association/auth", associationRoutes);
app.use("/api/product", productRoutes);
app.use("/api/productCategory", productCategoryRoutes);
app.use("/api/warehouse", wahrehouseRoutes);
app.use("/api/productState", productStateRoutes);
app.use("/api/productModel", productModelRoutes);
app.use("/api/productBrand", productBrandRoutes);
app.use("/api/sellOffer", sellOfferRoutes);
app.use("/api/selloffer/seller", sellOfferSellerRoutes);
app.use("/api/wallet", walletRoutes);
app.use("/api/order", orderRoutes);
app.use("/api/associationProject", associationProjectRoutes);
app.use("/webhook", stripeRoutes);
app.listen(process.env.PORT, () => {
console.log(`Example app listening at http://localhost:${port}`);
});