paymongo-cli 1.4.3 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,219 @@
1
+ function generateEventHandlers(events) {
2
+ return events
3
+ .map((event) => `
4
+ case '${event}':
5
+ console.log('Processing ${event} event:', data);
6
+ // Add your ${event} handling logic here
7
+ break;`)
8
+ .join('');
9
+ }
10
+ export function expressTemplate(events) {
11
+ const eventHandlers = generateEventHandlers(events);
12
+ return `const express = require('express');
13
+ const crypto = require('crypto');
14
+
15
+ const app = express();
16
+ app.use(express.json());
17
+
18
+ // Webhook secret from PayMongo dashboard
19
+ const WEBHOOK_SECRET = process.env.PAYMONGO_WEBHOOK_SECRET;
20
+
21
+ function verifySignature(payload, signatureHeader, secret) {
22
+ if (!signatureHeader) {
23
+ return false;
24
+ }
25
+
26
+ const parts = signatureHeader.split(',');
27
+ const timestamp = parts.find((part) => part.startsWith('t='))?.split('=')[1];
28
+ const signature = parts.find((part) => part.startsWith('te='))?.split('=')[1];
29
+
30
+ if (!timestamp || !signature) {
31
+ return false;
32
+ }
33
+
34
+ const expectedSignature = crypto
35
+ .createHmac('sha256', secret)
36
+ .update(timestamp + '.' + payload, 'utf8')
37
+ .digest('hex');
38
+
39
+ return crypto.timingSafeEqual(
40
+ Buffer.from(signature, 'hex'),
41
+ Buffer.from(expectedSignature, 'hex')
42
+ );
43
+ }
44
+
45
+ app.post('/webhooks/paymongo', (req, res) => {
46
+ try {
47
+ const signature = req.headers['paymongo-signature'];
48
+ const payload = JSON.stringify(req.body);
49
+
50
+ // Verify webhook signature (optional but recommended)
51
+ if (WEBHOOK_SECRET && !verifySignature(payload, signature, WEBHOOK_SECRET)) {
52
+ console.log('Invalid signature');
53
+ return res.status(400).json({ error: 'Invalid signature' });
54
+ }
55
+
56
+ const { data } = req.body;
57
+ const eventType = data.attributes.type;
58
+
59
+ switch (eventType) {${eventHandlers}
60
+ default:
61
+ console.log('Unhandled event type:', eventType);
62
+ }
63
+
64
+ res.json({ received: true });
65
+ } catch (error) {
66
+ console.error('Webhook processing error:', error);
67
+ res.status(500).json({ error: 'Internal server error' });
68
+ }
69
+ });
70
+
71
+ const PORT = process.env.PORT || 3000;
72
+ app.listen(PORT, () => {
73
+ console.log(\`Webhook server running on port \${PORT}\`);
74
+ });`;
75
+ }
76
+ export function fastifyTemplate(events) {
77
+ const eventHandlers = generateEventHandlers(events);
78
+ return `const fastify = require('fastify')({ logger: true });
79
+ const crypto = require('crypto');
80
+
81
+ // Webhook secret from PayMongo dashboard
82
+ const WEBHOOK_SECRET = process.env.PAYMONGO_WEBHOOK_SECRET;
83
+
84
+ function verifySignature(payload, signatureHeader, secret) {
85
+ if (!signatureHeader) {
86
+ return false;
87
+ }
88
+
89
+ const parts = signatureHeader.split(',');
90
+ const timestamp = parts.find((part) => part.startsWith('t='))?.split('=')[1];
91
+ const signature = parts.find((part) => part.startsWith('te='))?.split('=')[1];
92
+
93
+ if (!timestamp || !signature) {
94
+ return false;
95
+ }
96
+
97
+ const expectedSignature = crypto
98
+ .createHmac('sha256', secret)
99
+ .update(timestamp + '.' + payload, 'utf8')
100
+ .digest('hex');
101
+
102
+ return crypto.timingSafeEqual(
103
+ Buffer.from(signature, 'hex'),
104
+ Buffer.from(expectedSignature, 'hex')
105
+ );
106
+ }
107
+
108
+ fastify.post('/webhooks/paymongo', async (request, reply) => {
109
+ try {
110
+ const signature = request.headers['paymongo-signature'];
111
+ const payload = JSON.stringify(request.body);
112
+
113
+ // Verify webhook signature (optional but recommended)
114
+ if (WEBHOOK_SECRET && !verifySignature(payload, signature, WEBHOOK_SECRET)) {
115
+ console.log('Invalid signature');
116
+ return reply.code(400).send({ error: 'Invalid signature' });
117
+ }
118
+
119
+ const { data } = request.body;
120
+ const eventType = data.attributes.type;
121
+
122
+ switch (eventType) {${eventHandlers}
123
+ default:
124
+ console.log('Unhandled event type:', eventType);
125
+ }
126
+
127
+ return { received: true };
128
+ } catch (error) {
129
+ console.error('Webhook processing error:', error);
130
+ return reply.code(500).send({ error: 'Internal server error' });
131
+ }
132
+ });
133
+
134
+ const start = async () => {
135
+ try {
136
+ await fastify.listen({ port: process.env.PORT || 3000 });
137
+ } catch (err) {
138
+ fastify.log.error(err);
139
+ process.exit(1);
140
+ }
141
+ };
142
+
143
+ start();`;
144
+ }
145
+ export function genericTemplate(events) {
146
+ const eventHandlers = generateEventHandlers(events);
147
+ return `// Simple webhook handler for ${events.join(', ')}
148
+
149
+ const crypto = require('crypto');
150
+
151
+ // Webhook secret from PayMongo dashboard
152
+ const WEBHOOK_SECRET = process.env.PAYMONGO_WEBHOOK_SECRET;
153
+
154
+ function verifySignature(payload, signatureHeader, secret) {
155
+ if (!signatureHeader) {
156
+ return false;
157
+ }
158
+
159
+ const parts = signatureHeader.split(',');
160
+ const timestamp = parts.find((part) => part.startsWith('t='))?.split('=')[1];
161
+ const signature = parts.find((part) => part.startsWith('te='))?.split('=')[1];
162
+
163
+ if (!timestamp || !signature) {
164
+ return false;
165
+ }
166
+
167
+ const expectedSignature = crypto
168
+ .createHmac('sha256', secret)
169
+ .update(timestamp + '.' + payload, 'utf8')
170
+ .digest('hex');
171
+
172
+ return crypto.timingSafeEqual(
173
+ Buffer.from(signature, 'hex'),
174
+ Buffer.from(expectedSignature, 'hex')
175
+ );
176
+ }
177
+
178
+ function handleWebhook(request, response) {
179
+ try {
180
+ const signature = request.headers['paymongo-signature'];
181
+ const payload = JSON.stringify(request.body);
182
+
183
+ // Verify webhook signature (optional but recommended)
184
+ if (WEBHOOK_SECRET && !verifySignature(payload, signature, WEBHOOK_SECRET)) {
185
+ console.log('Invalid signature');
186
+ response.writeHead(400, { 'Content-Type': 'application/json' });
187
+ response.end(JSON.stringify({ error: 'Invalid signature' }));
188
+ return;
189
+ }
190
+
191
+ const { data } = request.body;
192
+ const eventType = data.attributes.type;
193
+
194
+ switch (eventType) {${eventHandlers}
195
+ default:
196
+ console.log('Unhandled event type:', eventType);
197
+ }
198
+
199
+ response.writeHead(200, { 'Content-Type': 'application/json' });
200
+ response.end(JSON.stringify({ received: true }));
201
+ } catch (error) {
202
+ console.error('Webhook processing error:', error);
203
+ response.writeHead(500, { 'Content-Type': 'application/json' });
204
+ response.end(JSON.stringify({ error: 'Internal server error' }));
205
+ }
206
+ }
207
+
208
+ module.exports = { handleWebhook };`;
209
+ }
210
+ export function getWebhookHandlerTemplate(events, framework) {
211
+ switch (framework) {
212
+ case 'express':
213
+ return expressTemplate(events);
214
+ case 'fastify':
215
+ return fastifyTemplate(events);
216
+ default:
217
+ return genericTemplate(events);
218
+ }
219
+ }
@@ -0,0 +1,168 @@
1
+ function generateEventHandlers(events) {
2
+ return events
3
+ .map((event) => `
4
+ case '${event}':
5
+ console.log('Processing ${event} event:', data);
6
+ // Add your ${event} handling logic here
7
+ break;`)
8
+ .join('');
9
+ }
10
+ export function expressTemplate(events) {
11
+ const eventHandlers = generateEventHandlers(events);
12
+ return `import express, { Request, Response } from 'express';
13
+ import crypto from 'crypto';
14
+
15
+ const app = express();
16
+ app.use(express.json());
17
+
18
+ // Webhook secret from PayMongo dashboard
19
+ const WEBHOOK_SECRET = process.env.PAYMONGO_WEBHOOK_SECRET;
20
+
21
+ interface PayMongoWebhookPayload {
22
+ data: {
23
+ id: string;
24
+ type: string;
25
+ attributes: {
26
+ type: string;
27
+ livemode: boolean;
28
+ created_at: number;
29
+ updated_at: number;
30
+ data: any;
31
+ };
32
+ };
33
+ }
34
+
35
+ function verifySignature(payload: string, signatureHeader: string, secret: string): boolean {
36
+ if (!signatureHeader) {
37
+ return false;
38
+ }
39
+
40
+ const parts = signatureHeader.split(',');
41
+ const timestamp = parts.find((part) => part.startsWith('t='))?.split('=')[1];
42
+ const signature = parts.find((part) => part.startsWith('te='))?.split('=')[1];
43
+
44
+ if (!timestamp || !signature) {
45
+ return false;
46
+ }
47
+
48
+ const expectedSignature = crypto
49
+ .createHmac('sha256', secret)
50
+ .update(timestamp + '.' + payload, 'utf8')
51
+ .digest('hex');
52
+
53
+ return crypto.timingSafeEqual(
54
+ Buffer.from(signature, 'hex'),
55
+ Buffer.from(expectedSignature, 'hex')
56
+ );
57
+ }
58
+
59
+ app.post('/webhooks/paymongo', (req: Request, res: Response) => {
60
+ try {
61
+ const signature = req.headers['paymongo-signature'] as string;
62
+ const payload = JSON.stringify(req.body);
63
+
64
+ // Verify webhook signature (optional but recommended)
65
+ if (WEBHOOK_SECRET && !verifySignature(payload, signature, WEBHOOK_SECRET)) {
66
+ console.log('Invalid signature');
67
+ return res.status(400).json({ error: 'Invalid signature' });
68
+ }
69
+
70
+ const { data }: PayMongoWebhookPayload = req.body;
71
+ const eventType = data.attributes.type;
72
+
73
+ switch (eventType) {${eventHandlers}
74
+ default:
75
+ console.log('Unhandled event type:', eventType);
76
+ }
77
+
78
+ res.json({ received: true });
79
+ } catch (error) {
80
+ console.error('Webhook processing error:', error);
81
+ res.status(500).json({ error: 'Internal server error' });
82
+ }
83
+ });
84
+
85
+ const PORT = process.env.PORT || 3000;
86
+ app.listen(PORT, () => {
87
+ console.log(\`Webhook server running on port \${PORT}\`);
88
+ });`;
89
+ }
90
+ export function genericTemplate(events) {
91
+ const eventHandlers = generateEventHandlers(events);
92
+ return `// TypeScript webhook handler for ${events.join(', ')}
93
+
94
+ import crypto from 'crypto';
95
+
96
+ const WEBHOOK_SECRET = process.env.PAYMONGO_WEBHOOK_SECRET;
97
+
98
+ interface PayMongoWebhookPayload {
99
+ data: {
100
+ id: string;
101
+ type: string;
102
+ attributes: {
103
+ type: string;
104
+ livemode: boolean;
105
+ created_at: number;
106
+ updated_at: number;
107
+ data: any;
108
+ };
109
+ };
110
+ }
111
+
112
+ function verifySignature(payload: string, signatureHeader: string, secret: string): boolean {
113
+ if (!signatureHeader) {
114
+ return false;
115
+ }
116
+
117
+ const parts = signatureHeader.split(',');
118
+ const timestamp = parts.find((part) => part.startsWith('t='))?.split('=')[1];
119
+ const signature = parts.find((part) => part.startsWith('te='))?.split('=')[1];
120
+
121
+ if (!timestamp || !signature) {
122
+ return false;
123
+ }
124
+
125
+ const expectedSignature = crypto
126
+ .createHmac('sha256', secret)
127
+ .update(timestamp + '.' + payload, 'utf8')
128
+ .digest('hex');
129
+
130
+ return crypto.timingSafeEqual(
131
+ Buffer.from(signature, 'hex'),
132
+ Buffer.from(expectedSignature, 'hex')
133
+ );
134
+ }
135
+
136
+ export function handleWebhook(body: PayMongoWebhookPayload, signature?: string): { received: boolean } {
137
+ try {
138
+ const payload = JSON.stringify(body);
139
+
140
+ // Verify webhook signature (optional but recommended)
141
+ if (WEBHOOK_SECRET && signature && !verifySignature(payload, signature, WEBHOOK_SECRET)) {
142
+ console.log('Invalid signature');
143
+ throw new Error('Invalid signature');
144
+ }
145
+
146
+ const { data } = body;
147
+ const eventType = data.attributes.type;
148
+
149
+ switch (eventType) {${eventHandlers}
150
+ default:
151
+ console.log('Unhandled event type:', eventType);
152
+ }
153
+
154
+ return { received: true };
155
+ } catch (error) {
156
+ console.error('Webhook processing error:', error);
157
+ throw error;
158
+ }
159
+ }`;
160
+ }
161
+ export function getWebhookHandlerTemplate(events, framework) {
162
+ switch (framework) {
163
+ case 'express':
164
+ return expressTemplate(events);
165
+ default:
166
+ return genericTemplate(events);
167
+ }
168
+ }