-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
139 lines (119 loc) · 3.64 KB
/
index.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 express = require("express");
const cors = require("cors");
const fetch = require("node-fetch");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const { v4: uuidv4 } = require("uuid");
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(cors());
app.set("view engine", "pug");
app.get("/", (req, res) => {
res.render("index");
});
app.get("/bkashToken", async (req, res) => {
const tokenResponse = await fetch(
"https://checkout.sandbox.bka.sh/v1.2.0-beta/checkout/token/grant",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
username: process.env.USERNAME,
password: process.env.PASSWORD,
},
body: JSON.stringify({
app_key: process.env.KEY,
app_secret: process.env.SECRET,
}),
}
);
const tokenResult = await tokenResponse.json();
res.cookie("token", tokenResult);
res.send(tokenResult);
});
app.get("/creatBkashPayment", async (req, res) => {
const token = req.cookies.token;
const checkoutResopnse = await fetch(
"https://checkout.sandbox.bka.sh/v1.2.0-beta/checkout/payment/create",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
authorization: token.id_token,
"x-app-key": process.env.KEY,
},
body: JSON.stringify({
amount: "500",
currency: "BDT",
intent: "sale",
merchantInvoiceNumber: uuidv4(),
}),
}
);
const checkoutResult = await checkoutResopnse.json();
res.cookie("payment", checkoutResult);
res.send(checkoutResult);
});
app.get("/executeBkashPayment", async (req, res) => {
const token = req.cookies.token;
const payment = req.cookies.payment;
const executePaymentResponse = await fetch(
`https://checkout.sandbox.bka.sh/v1.2.0-beta/checkout/payment/execute/${payment.paymentID}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
authorization: token.id_token,
"x-app-key": process.env.KEY,
},
}
);
const executePaymentResult = await executePaymentResponse.json();
res.send(executePaymentResult);
});
app.get("/queryBkashPayment", async (req, res) => {
const token = req.cookies.token;
const payment = req.cookies.payment;
const queryPaymentResponse = await fetch(
`https://checkout.sandbox.bka.sh/v1.2.0-beta/checkout/payment/query/${req.query.id}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
authorization: token.id_token,
"x-app-key": process.env.KEY,
},
}
);
const queryPaymentResult = await queryPaymentResponse.json();
res.send(queryPaymentResult);
});
app.get("/searchBkashTransaction", async (req, res) => {
const token = req.cookies.token;
const payment = req.cookies.payment;
const searchTransactionResponse = await fetch(
`https://checkout.sandbox.bka.sh/v1.2.0-beta/checkout/payment/search/${req.query.id}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
authorization: token.id_token,
"x-app-key": process.env.KEY,
},
}
);
const searchTransactionResult = await searchTransactionResponse.json();
res.send(searchTransactionResult);
});
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);