-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
139 lines (115 loc) · 4.04 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require("dotenv").config();
const cors = require("cors");
const express = require("express");
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const SSLCommerzPayment = require("sslcommerz-lts");
// SSLCommerz credentials
const store_id = `teams6623fcba51d0b`;
const store_passwd = `teams6623fcba51d0b@ssl`;
const is_live = false;
// Database connection
const MONGODB_CONNECTION = process.env.MONGODB_CONNECTION;
const client = new MongoClient(MONGODB_CONNECTION, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
const app = express();
const port = 3001;
app.use(cors());
app.use(express.json());
async function run() {
try {
await client.connect();
console.log("Database connected");
const productCollection = client.db("ecomarce").collection("product");
const orderCollection = client.db("ecomarce").collection("order");
app.post("/payment", async (req, res) => {
const { _cartItem,
email,
firstName,
lastName,
total_price,
postCode,
road,
city,
country,
customersId,
qnt,
} = req.body;
console.log(_cartItem[0]);
const trans_id = new ObjectId().toString();
const data = {
total_amount: total_price,
currency: "BDT",
tran_id: trans_id,
success_url: `http://localhost:3001/payment/success/${trans_id}`,
fail_url: `http://localhost:3001/payment/fail/${trans_id}`,
cancel_url: `http://localhost:3001/payment/cancel/${trans_id}`,
ipn_url: `http://localhost:3001/payment/ipn`,
shipping_method: "Courier",
product_name: 'something' || "Computer",
product_category: "Electronic",
product_profile: "general",
cus_name: `${firstName} ${lastName}`,
cus_email: email,
cus_add1: road,
cus_city: city,
cus_postcode: postCode,
cus_country: country,
cus_phone: "01711111111",
cus_fax: "01711111111",
ship_name: "Customer Name",
ship_add1: city,
ship_city: city,
ship_postcode: 1000,
ship_country: "Bangladesh",
};
console.log(data);
const sslcz = new SSLCommerzPayment(store_id, store_passwd, is_live);
try {
const apiResponse = await sslcz.init(data);
console.log(apiResponse.GatewayPageURL, 'api response');
const finalOrder = {
_cartItem,
paidStatus: false,
transaction_id: trans_id,
extraInformation: req.body,
};
const result = await orderCollection.insertOne(finalOrder);
return res.send({ status: "success", url: apiResponse.GatewayPageURL, result: result });
} catch (error) {
console.log(error);
return res.status(500).send({ status: "error", message: "Internal server error" });
}
});
app.post("/payment/success/:trans_id", async (req, res) => {
const tran_id = req.params.trans_id;
const updateResult = await orderCollection.updateOne(
{ transaction_id: tran_id },
{ $set: { paidStatus: true } }
);
if (updateResult.modifiedCount > 0) {
res.redirect(`http://localhost:3000/payment/success?tran_id=${tran_id}`);
} else {
res.redirect(`http://localhost:3000/payment/fail?tran_id=${tran_id}`);
}
});
app.post("/payment/fail/:trans_id", async (req, res) => {
const tran_id = req.params.trans_id;
await orderCollection.deleteOne({ transaction_id: tran_id });
res.redirect(`https://e-comarce-next.vercel.app/payment/fail?tran_id=${tran_id}`);
});
app.get("/order-details/:userId", async (req, res) => {
const userId = req.params.userId;
const findOrder = await orderCollection.find({ "extraInformation.customersId": userId }).toArray();
res.json({ data: "success", order: findOrder });
});
} catch (error) {
console.error("Error connecting to database:", error);
}
}
run();
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});