/
ashraful.js
197 lines (179 loc) · 6.71 KB
/
ashraful.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const { log } = require("winston");
module.exports = function (app) {
const express = require("express");
const { MongoClient } = require("mongodb");
const SSLCommerzPayment = require("sslcommerz");
const { v4: uuidv4 } = require("uuid");
const ObjectId = require("mongodb").ObjectId;
require("dotenv").config();
const cors = require("cors");
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const user = process.env.DB_USER;
const password = process.env.DB_PASS;
const uri = `mongodb+srv://${user}:${password}@cluster0.tgh4y.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function run() {
try {
await client.connect();
console.log("Database Connected Succesfully on Ashraful.js");
const database = client.db("edubuddies");
const courses = database.collection("courses");
const userCollection = database.collection("users");
const instructorCollection = database.collection("instuctor");
const blogsCollection = database.collection("blogs");
const newsletterCollection = database.collection("newsletter");
const promoCollection = database.collection("promo");
const paymentCollection = database.collection("payment");
// get Single Teacher Info From DB
app.get("/singleTeacher/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const TeacherData = await instructorCollection.findOne(query);
res.json(TeacherData);
});
// get My Course Info From DB
app.get("/CourseDetails/:courseId", async (req, res) => {
const id = req.params.courseId;
const query = { _id: ObjectId(id) };
const CourseData = await courses.findOne(query);
res.json(CourseData);
});
// get Best Perfomer from Teacher
app.get("/teachersDashboard/bestPerformer", async (req, res) => {
const bestPerformer = req.query;
const TeacherData = await instructorCollection
.find({
performer: bestPerformer.performer,
})
.toArray();
res.json(TeacherData);
});
// SSL Commerz All API Here
// Initialize Payment
app.post("/init", async (req, res) => {
const productInfo = {
currency: "BDT",
paymentStatus: "pending",
tran_id: uuidv4(),
success_url: "https://fierce-caverns-90976.herokuapp.com/success",
fail_url: "https://fierce-caverns-90976.herokuapp.com/failure",
cancel_url: "https://fierce-caverns-90976.herokuapp.com/cancel",
ipn_url: "https://fierce-caverns-90976.herokuapp.com/ipn",
product_id: req.body.product_id,
product_name: req.body.product_name,
product_category: req.body.product_category,
product_profile: req.body.product_profile,
product_image: req.body.product_image,
productDetails: req.body.product_profile,
total_amount: req.body.total_amount,
instructor: req.body.instructor,
cus_name: req.body.cus_name,
cus_email: req.body.cus_email,
cus_add1: req.body.cus_add1,
cus_street: req.body.cus_street,
cus_city: "N/A",
cus_state: req.body.cus_state,
cus_postcode: req.body.cus_postcode,
cus_country: req.body.cus_country,
cus_phone: req.body.cus_phone,
shipping_method: "Courier",
ship_name: req.body.cus_name,
ship_add1: req.body.cus_add1,
ship_add2: req.body.cus_add1,
ship_city: req.body.cus_city,
ship_state: req.body.cus_state,
ship_postcode: req.body.cus_postcode,
ship_country: req.body.cus_country,
multi_card_name: "mastercard",
value_a: "ref001_A",
value_b: "ref002_B",
value_c: "ref003_C",
value_d: "ref004_D",
};
console.log(productInfo);
// Insert order info
const result = await paymentCollection.insertOne(productInfo);
const sslcommer = new SSLCommerzPayment(
process.env.STORE_ID,
process.env.STORE_PASSWORD,
false
);
sslcommer.init(productInfo).then((data) => {
const info = { ...productInfo, ...data };
if (info.GatewayPageURL) {
res.json(info.GatewayPageURL);
} else {
return res.status(400).json({
message: "SSL session was not successful",
});
}
});
});
//<----------- Success, Fail, Cancel And APN API Here ---------->
app.post("/success", async (req, res) => {
const order = await paymentCollection.updateOne(
{ tran_id: req.body.tran_id },
{
$set: {
val_id: req.body.val_id,
},
}
);
res.redirect(
`https://edu-buddies.netlify.app/success/${req.body.tran_id}`
);
});
app.post("/failure", async (req, res) => {
const order = await paymentCollection.deleteOne({
tran_id: req.body.tran_id,
});
res.redirect(`https://edu-buddies.netlify.app/placeOrder`);
});
app.post("/cancel", async (req, res) => {
const order = await paymentCollection.deleteOne({
tran_id: req.body.tran_id,
});
res.redirect(`https://edu-buddies.netlify.app/`);
});
app.post("/ipn", (req, res) => {
res.send(req.body);
});
//<---------- Ger Payment Complete Details -------->
app.get("/orders/:tran_id", async (req, res) => {
const id = req.params.tran_id;
const order = await paymentCollection.findOne({ tran_id: id });
res.json(order);
});
//<---------- Validate Transaction By User clicking Success Button -------->
app.post("/validate", async (req, res) => {
const order = await paymentCollection.findOne({
tran_id: req.body.tran_id,
});
if (order.val_id === req.body.val_id) {
const update = await paymentCollection.updateOne(
{ tran_id: req.body.tran_id },
{
$set: { paymentStatus: "Successful" },
}
);
res.send(update.modifiedCount > 0);
} else {
res.send("Payment Not Confirmed, Discarted Order");
}
});
//Get All Payment History
app.get("/payments", async (req, res) => {
const cursor = paymentCollection.find({});
const users = await cursor.toArray();
res.json(users);
});
} finally {
}
}
run().catch(console.dir);
};