Skip to content

Commit 65ca844

Browse files
feat: scaffold webhooks
1 parent 1cee52d commit 65ca844

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Hono } from 'hono'
33
import plansRouter from './routes/plans.js'
44
import { showRoutes } from 'hono/dev'
55
import transactionRouter from './routes/transaction.js'
6+
import webhookRouter from './routes/webhooks.js'
67

78
const app = new Hono()
89

@@ -12,6 +13,7 @@ app.get('/', (c) => {
1213

1314
app.route("/plan", plansRouter)
1415
app.route("/transaction", transactionRouter)
16+
app.route("/webhook", webhookRouter)
1517

1618
// To list all routes
1719
showRoutes(app)

src/routes/webhooks.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import 'dotenv/config';
2+
import { Hono } from "hono";
3+
import { db } from '../db.js';
4+
import { zValidator } from '@hono/zod-validator';
5+
import { Transaction } from '../schema/index.js';
6+
import { createHmac } from 'node:crypto';
7+
8+
const webhookRouter = new Hono()
9+
10+
webhookRouter.post("/", async (c) => {
11+
const secretKey = process.env.PAYSTACK_SECRET_KEY
12+
const body = await c.req.json()
13+
const hash = createHmac('sha512', secretKey!).update(JSON.stringify(body)).digest('hex')
14+
const signature = c.req.header('x-paystack-signature')
15+
16+
if (hash !== signature) {
17+
return c.json({}, 422)
18+
}
19+
20+
switch (body.event) {
21+
case "charge.success":
22+
// handle successful transaction
23+
console.log("In charge.success")
24+
break;
25+
case "subscription.create":
26+
// handle subscription creation
27+
console.log("In subscription.create")
28+
break;
29+
case "invoice.create":
30+
// handle invoice creation
31+
console.log("In invoice.create")
32+
break;
33+
case "invoice.payment_failed":
34+
// handle invoice payment failure
35+
console.log("In invoice.payment_failed")
36+
break;
37+
case "invoice.update":
38+
// handle invoice update
39+
console.log("In invoice.update")
40+
break;
41+
case "subscription.disable":
42+
// handle subscription disabling
43+
console.log("In subscription.disable")
44+
break;
45+
case "subscription.not_renew":
46+
// handle subscription not renewing
47+
console.log("In subscription.not_renew")
48+
break;
49+
default:
50+
break;
51+
}
52+
53+
return c.json({}, 200)
54+
})
55+
56+
57+
export default webhookRouter

0 commit comments

Comments
 (0)