-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
65 lines (64 loc) · 1.8 KB
/
index.ts
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
require("dotenv").config();
import express, {
Application,
Request,
Response,
RequestHandler,
} from "express";
const app: Application = express();
const port: number | string = process.env.PORT || 1999;
import cors, { CorsOptions } from "cors";
import paystack from "./middleware/paystack.config";
const corsOptions: CorsOptions = {
origin: "*",
};
app.use(cors(corsOptions));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
//# initialize transaction
app.post("/paystack/pay", async (req: Request, res: Response) => {
try {
const { email, amount } = req.body;
if (!email || !amount) {
throw new Error("enter email and amount");
}
const body = JSON.stringify({
email,
amount: amount * 100,
});
await paystack
.initializetransaction(res, body)
.then(({ data }: any) => {
res.status(200).json({
url: data.authorization_url,
msg: "redirect your application to the url provided in the url method then hit /paystack/verify route in this api to verify payment",
ref: data.reference,
});
})
.catch((err) => {
throw err;
});
} catch (error: any) {
return res.status(400).json({ message: error.message, error });
}
});
//# verify payment
app.get("/paystack/verify", async (req: Request, res: Response) => {
try {
let { ref }: any = req.query;
await paystack
.verifyPayment(res, ref)
.then((data: any) => {
res.status(200).json({ transaction_status: data.data.status });
})
.catch((err) => {
throw err;
});
} catch (error: any) {
return res.status(400).json({ message: error.message, error });
}
});
// #listener function
app.listen(port, () => {
console.log(`paystack api integration on port ${port}`);
});