-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (88 loc) · 2.62 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
require("dotenv").config();
const express = require("express");
const SSLCommerzPayment = require("sslcommerz-lts");
const cors = require("cors");
const app = express();
//set middlewares and routes
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const PORT = process.env.PORT || 8080;
app.get("/", async (req, res) => {
res.status(200).json({
message: "Welcome to sslcommerz app",
});
});
app.post("/ssl-request", async (req, res) => {
const {
price,
name,
customer_name,
customer_email,
customer_add = "Dhaka",
customer_phone = "01300000000",
customer_postcode = "1000",
customer_country = "Bangladesh",
product_category = "Food",
tran_id,
auth_token,
} = req.body;
const data = {
total_amount: Number(price),
currency: "BDT",
tran_id,
success_url: `${process.env.SERVER_ROOT}/payment/success?tran_id=${tran_id}?token=${auth_token}`,
fail_url: `${process.env.SERVER_ROOT}/payment/fail?tran_id=${tran_id}?token=${auth_token}`,
cancel_url: `${process.env.SERVER_ROOT}/payment/cancel?tran_id=${tran_id}?token=${auth_token}`,
shipping_method: "No",
product_name: name,
product_category: product_category,
product_profile: "general",
cus_name: customer_name,
cus_email: customer_email,
cus_add1: customer_add,
cus_add2: customer_add,
cus_city: customer_add,
cus_state: customer_add,
cus_postcode: customer_postcode,
cus_country: customer_country,
cus_phone: customer_phone,
cus_fax: customer_phone,
multi_card_name: "mastercard",
value_a: "ref001_A",
value_b: "ref002_B",
value_c: "ref003_C",
value_d: "ref004_D",
ipn_url: `${process.env.SERVER_ROOT}/payment/notification`,
};
console.log("Inside ssl-request route service 🚀");
try {
const sslcommerz = new SSLCommerzPayment(
process.env.STORE_ID,
process.env.STORE_PASSWORD,
false //true for live default false for sandbox
);
sslcommerz.init(data).then((data) => {
//https://developer.sslcommerz.com/doc/v4/#returned-parameters
if (data?.GatewayPageURL) {
return res.status(200).json({ url: data.GatewayPageURL });
}
return res.status(400).json({
message: "Session was not successful",
});
});
} catch (error) {
console.error(error);
return res.status(500).json({ message: "Internal server error on sslcommerz init 💥" });
}
});
app.use((req, res, next) => {
res.status(404).json({ message: "Page not found" });
});
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ message: "Internal server error on sslcommerz 💥" });
});
app.listen(PORT, () => {
console.info(`Server running at http://localhost:${PORT}`);
});