-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
126 lines (86 loc) · 3.03 KB
/
index.ts
File metadata and controls
126 lines (86 loc) · 3.03 KB
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
import { flutterwave, paystack, once } from "./sdks/initializer";
import crypto from "node:crypto";
import express, { Request, Response } from "express";
import mongoose from "mongoose";
import { config } from "dotenv";
import { OnceProvider } from "./sdks/once";
import Transaction from "./models/transaction";
import http from "http";
import { Server } from "socket.io";
import cors from "cors";
import Events from "events"
import { channel } from "node:diagnostics_channel";
config();
const app = express();
const server = http.createServer(app);
const io = new Server( server, {
cors:{
origin: "*"
}
} );
const transactionNamspace = io.of("/transaction");
const soccketInstance = new Events();
transactionNamspace.on("connection", ( socket:any )=>{
console.log("socket connected ")
socket.on( "transaction-init", ( transactionRef: string )=>{
socket.join(transactionRef)
console.log("joined transaction room");
})
socket.on("transaction-resolved", ()=>{
console.log("Transaction reolved")
})
})
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors({
origin: "*"
}))
const PORT = process.env.PORT ?? 3000 ;
app.post("/init", async( req: Request, res: Response )=>{
const { amount, host } = req.body;
const checkout = await once.initialize( parseInt(amount), host );
res.json( checkout )
})
app.get("/checkout", async( req: Request, res: Response )=>{
const { provider, id } = req.query;
const providerCheckout = await once.getProviderCheckout( provider as OnceProvider, id as string )
res.json( providerCheckout );
})
app.patch( "/update-transaction", async( req: Request, res: Response )=>{
const update = req.body?.update;
const id = req.body.id;
try{
const updatedTransaction = await Transaction.findByIdAndUpdate( id, update, { new: true });
res.json({ message: "successfull" })
} catch(e){
res.json({ message: "failed" })
}
})
app.post("/payment-webhook-ps", async( req: Request, res: Response )=>{
console.log( "Webhook sent from paystack");
const ref = req.body.data.reference;
transactionNamspace.to( ref ).emit("transaction-resolved")
console.log( req.body )
res.end();
})
app.post("/payment-webhook", async( req: Request, res: Response )=>{
console.log( "Webhook sent from flutterwave");
const ref = req.body.txRef;
transactionNamspace.to( ref ).emit("transaction-resolved")
console.log(req.body)
res.end();
})
app.post("/payment-webhook-kp", async( req: Request, res: Response )=>{
const payload = req.body;
if( payload.event === 'charge.success' ){
console.log(payload)
transactionNamspace.to(payload.data.reference).emit("transaction-resolved")
}
res.end()
})
// const promiseArr = [ mongoose.connect(process.env.MONGO_DB_URL as string), app.listen(PORT) ];
// Promise.all( promiseArr )
// .then( ()=> console.log("Server is up and running") )
mongoose.connect(process.env.MONGO_DB_URL as string)
.then( ()=> console.log("Mongo is live"));
server.listen(PORT).on("listening" , ()=> console.log("APP is live"))