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