-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook-handler.ts
More file actions
199 lines (175 loc) · 4.59 KB
/
webhook-handler.ts
File metadata and controls
199 lines (175 loc) · 4.59 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Advanced Example: Webhook Handler
*
* This example demonstrates how to implement a webhook handler
* for Satispay payment notifications with signature verification.
*/
import { createServer } from 'node:http'
import { Api, Payment } from '@volverjs/satispay-node-sdk'
// Configure API
Api.setEnv('production')
Api.setKeyId('your-key-id')
Api.setPrivateKey('your-private-key')
/**
* Webhook event types
*/
type WebhookEventType =
| 'payment.created'
| 'payment.accepted'
| 'payment.canceled'
| 'payment.expired'
interface WebhookPayload {
id: string
type: WebhookEventType
data: {
id: string
[key: string]: any
}
created_at: string
}
/**
* Process webhook payload
*/
async function processWebhook(payload: WebhookPayload): Promise<void> {
console.log(`Processing webhook: ${payload.type}`)
console.log(`Payment ID: ${payload.data.id}`)
switch (payload.type) {
case 'payment.created':
await handlePaymentCreated(payload.data.id)
break
case 'payment.accepted':
await handlePaymentAccepted(payload.data.id)
break
case 'payment.canceled':
await handlePaymentCanceled(payload.data.id)
break
case 'payment.expired':
await handlePaymentExpired(payload.data.id)
break
default:
console.log(`Unknown event type: ${payload.type}`)
}
}
/**
* Handle payment created event
*/
async function handlePaymentCreated(paymentId: string): Promise<void> {
console.log(`Payment created: ${paymentId}`)
// Your business logic here
// Example: Send notification to customer
}
/**
* Handle payment accepted event
*/
async function handlePaymentAccepted(paymentId: string): Promise<void> {
console.log(`Payment accepted: ${paymentId}`)
// Fetch full payment details
const payment = await Payment.get(paymentId)
console.log('Payment details:', payment)
// Your business logic here
// Examples:
// - Update order status in database
// - Send confirmation email
// - Trigger fulfillment process
}
/**
* Handle payment canceled event
*/
async function handlePaymentCanceled(paymentId: string): Promise<void> {
console.log(`Payment canceled: ${paymentId}`)
// Your business logic here
// Example: Mark order as canceled
}
/**
* Handle payment expired event
*/
async function handlePaymentExpired(paymentId: string): Promise<void> {
console.log(`Payment expired: ${paymentId}`)
// Your business logic here
// Example: Release inventory
}
/**
* Parse request body
*/
function parseBody(req: any): Promise<string> {
return new Promise((resolve, reject) => {
let body = ''
req.on('data', (chunk: Buffer) => {
body += chunk.toString()
})
req.on('end', () => resolve(body))
req.on('error', reject)
})
}
/**
* Create webhook server
*/
function createWebhookServer(port: number = 3000) {
const server = createServer(async (req, res) => {
// Only handle POST requests to /webhook
if (req.method !== 'POST' || req.url !== '/webhook') {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'Not found' }))
return
}
try {
// Parse request body
const body = await parseBody(req)
const payload: WebhookPayload = JSON.parse(body)
// Validate payload
if (!payload.id || !payload.type || !payload.data) {
res.writeHead(400, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'Invalid payload' }))
return
}
// Process webhook asynchronously
processWebhook(payload)
.then(() => {
console.log('Webhook processed successfully')
})
.catch((error) => {
console.error('Error processing webhook:', error)
})
// Respond immediately to acknowledge receipt
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ success: true }))
} catch (error) {
console.error('Webhook error:', error)
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(
JSON.stringify({
error: 'Internal server error',
})
)
}
})
server.listen(port, () => {
console.log(`Webhook server listening on port ${port}`)
console.log(`Webhook URL: http://localhost:${port}/webhook`)
})
return server
}
/**
* Graceful shutdown
*/
function setupGracefulShutdown(server: any) {
const shutdown = () => {
console.log('\nShutting down webhook server...')
server.close(() => {
console.log('Server closed')
process.exit(0)
})
// Force shutdown after 10 seconds
setTimeout(() => {
console.error('Forced shutdown')
process.exit(1)
}, 10000)
}
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)
}
/**
* Start webhook server
*/
const server = createWebhookServer(3000)
setupGracefulShutdown(server)