@zendfi/sdk 0.1.1 → 0.2.0
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.
- package/README.md +153 -252
- package/dist/chunk-YFOBPGQE.mjs +152 -0
- package/dist/express.d.mts +46 -0
- package/dist/express.d.ts +46 -0
- package/dist/express.js +220 -0
- package/dist/express.mjs +56 -0
- package/dist/index.d.mts +6 -325
- package/dist/index.d.ts +6 -325
- package/dist/index.js +173 -16
- package/dist/index.mjs +35 -22
- package/dist/nextjs.d.mts +37 -0
- package/dist/nextjs.d.ts +37 -0
- package/dist/nextjs.js +227 -0
- package/dist/nextjs.mjs +63 -0
- package/dist/webhook-handler-BIze3Qop.d.mts +388 -0
- package/dist/webhook-handler-BIze3Qop.d.ts +388 -0
- package/package.json +12 -5
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { W as WebhookHandlerConfig, a as WebhookHandlers } from './webhook-handler-BIze3Qop.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Express Webhook Handler
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* // src/routes/webhooks.ts
|
|
9
|
+
* import express from 'express';
|
|
10
|
+
* import { createExpressWebhookHandler } from '@zendfi/sdk/express';
|
|
11
|
+
*
|
|
12
|
+
* const router = express.Router();
|
|
13
|
+
*
|
|
14
|
+
* router.post('/zendfi',
|
|
15
|
+
* express.raw({ type: 'application/json' }),
|
|
16
|
+
* createExpressWebhookHandler({
|
|
17
|
+
* secret: process.env.ZENDFI_WEBHOOK_SECRET!,
|
|
18
|
+
* handlers: {
|
|
19
|
+
* 'payment.confirmed': async (payment) => {
|
|
20
|
+
* await Order.update({ status: 'paid' }, { where: { id: payment.metadata.orderId } });
|
|
21
|
+
* },
|
|
22
|
+
* 'payment.failed': async (payment) => {
|
|
23
|
+
* await sendFailureEmail(payment);
|
|
24
|
+
* },
|
|
25
|
+
* },
|
|
26
|
+
* })
|
|
27
|
+
* );
|
|
28
|
+
*
|
|
29
|
+
* export default router;
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
type Request = any;
|
|
34
|
+
type Response = any;
|
|
35
|
+
interface ExpressWebhookHandlerConfig extends WebhookHandlerConfig {
|
|
36
|
+
handlers: WebhookHandlers;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Create an Express webhook handler
|
|
40
|
+
*
|
|
41
|
+
* IMPORTANT: Must use express.raw() middleware before this handler:
|
|
42
|
+
* app.post('/webhooks/zendfi', express.raw({ type: 'application/json' }), handler)
|
|
43
|
+
*/
|
|
44
|
+
declare function createExpressWebhookHandler(config: ExpressWebhookHandlerConfig): (req: Request, res: Response) => Promise<any>;
|
|
45
|
+
|
|
46
|
+
export { type ExpressWebhookHandlerConfig, createExpressWebhookHandler };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { W as WebhookHandlerConfig, a as WebhookHandlers } from './webhook-handler-BIze3Qop.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Express Webhook Handler
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* // src/routes/webhooks.ts
|
|
9
|
+
* import express from 'express';
|
|
10
|
+
* import { createExpressWebhookHandler } from '@zendfi/sdk/express';
|
|
11
|
+
*
|
|
12
|
+
* const router = express.Router();
|
|
13
|
+
*
|
|
14
|
+
* router.post('/zendfi',
|
|
15
|
+
* express.raw({ type: 'application/json' }),
|
|
16
|
+
* createExpressWebhookHandler({
|
|
17
|
+
* secret: process.env.ZENDFI_WEBHOOK_SECRET!,
|
|
18
|
+
* handlers: {
|
|
19
|
+
* 'payment.confirmed': async (payment) => {
|
|
20
|
+
* await Order.update({ status: 'paid' }, { where: { id: payment.metadata.orderId } });
|
|
21
|
+
* },
|
|
22
|
+
* 'payment.failed': async (payment) => {
|
|
23
|
+
* await sendFailureEmail(payment);
|
|
24
|
+
* },
|
|
25
|
+
* },
|
|
26
|
+
* })
|
|
27
|
+
* );
|
|
28
|
+
*
|
|
29
|
+
* export default router;
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
type Request = any;
|
|
34
|
+
type Response = any;
|
|
35
|
+
interface ExpressWebhookHandlerConfig extends WebhookHandlerConfig {
|
|
36
|
+
handlers: WebhookHandlers;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Create an Express webhook handler
|
|
40
|
+
*
|
|
41
|
+
* IMPORTANT: Must use express.raw() middleware before this handler:
|
|
42
|
+
* app.post('/webhooks/zendfi', express.raw({ type: 'application/json' }), handler)
|
|
43
|
+
*/
|
|
44
|
+
declare function createExpressWebhookHandler(config: ExpressWebhookHandlerConfig): (req: Request, res: Response) => Promise<any>;
|
|
45
|
+
|
|
46
|
+
export { type ExpressWebhookHandlerConfig, createExpressWebhookHandler };
|
package/dist/express.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/express.ts
|
|
21
|
+
var express_exports = {};
|
|
22
|
+
__export(express_exports, {
|
|
23
|
+
createExpressWebhookHandler: () => createExpressWebhookHandler
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(express_exports);
|
|
26
|
+
var import_crypto2 = require("crypto");
|
|
27
|
+
|
|
28
|
+
// src/webhook-handler.ts
|
|
29
|
+
var import_crypto = require("crypto");
|
|
30
|
+
var processedWebhooks = /* @__PURE__ */ new Set();
|
|
31
|
+
var defaultIsProcessed = async (webhookId) => {
|
|
32
|
+
return processedWebhooks.has(webhookId);
|
|
33
|
+
};
|
|
34
|
+
var defaultOnProcessed = async (webhookId) => {
|
|
35
|
+
processedWebhooks.add(webhookId);
|
|
36
|
+
if (processedWebhooks.size > 1e4) {
|
|
37
|
+
const iterator = processedWebhooks.values();
|
|
38
|
+
for (let i = 0; i < 1e3; i++) {
|
|
39
|
+
const { value } = iterator.next();
|
|
40
|
+
if (value) processedWebhooks.delete(value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
function generateWebhookId(payload) {
|
|
45
|
+
return `${payload.merchant_id}:${payload.event}:${payload.timestamp}`;
|
|
46
|
+
}
|
|
47
|
+
async function processPayload(payload, handlers, config) {
|
|
48
|
+
try {
|
|
49
|
+
const webhookId = generateWebhookId(payload);
|
|
50
|
+
const isProcessed = config.isProcessed || config.checkDuplicate || defaultIsProcessed;
|
|
51
|
+
const onProcessed = config.onProcessed || config.markProcessed || defaultOnProcessed;
|
|
52
|
+
const dedupEnabled = !!(config.enableDeduplication || config.isProcessed || config.checkDuplicate);
|
|
53
|
+
if (dedupEnabled && await isProcessed(webhookId)) {
|
|
54
|
+
return {
|
|
55
|
+
success: false,
|
|
56
|
+
processed: false,
|
|
57
|
+
event: payload.event,
|
|
58
|
+
error: "Duplicate webhook",
|
|
59
|
+
statusCode: 409
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const handler = handlers[payload.event];
|
|
63
|
+
if (!handler) {
|
|
64
|
+
return {
|
|
65
|
+
success: true,
|
|
66
|
+
processed: false,
|
|
67
|
+
event: payload.event,
|
|
68
|
+
statusCode: 200
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
await handler(payload.data, payload);
|
|
72
|
+
await onProcessed(webhookId);
|
|
73
|
+
return {
|
|
74
|
+
success: true,
|
|
75
|
+
processed: true,
|
|
76
|
+
event: payload.event
|
|
77
|
+
};
|
|
78
|
+
} catch (error) {
|
|
79
|
+
const err = error;
|
|
80
|
+
if (config?.onError) {
|
|
81
|
+
await config.onError(err, error?.event);
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
success: false,
|
|
85
|
+
processed: false,
|
|
86
|
+
error: err.message,
|
|
87
|
+
event: error?.event,
|
|
88
|
+
statusCode: 500
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async function processWebhook(a, b, c) {
|
|
93
|
+
if (a && typeof a === "object" && a.event && b && c) {
|
|
94
|
+
return processPayload(a, b, c);
|
|
95
|
+
}
|
|
96
|
+
const opts = a;
|
|
97
|
+
if (!opts || !opts.signature && !opts.body && !opts.handlers) {
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
processed: false,
|
|
101
|
+
error: "Invalid arguments to processWebhook",
|
|
102
|
+
statusCode: 400
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const signature = opts.signature;
|
|
106
|
+
const body = opts.body;
|
|
107
|
+
const handlers = opts.handlers || {};
|
|
108
|
+
const cfg = opts.config || {};
|
|
109
|
+
const secret = cfg.webhookSecret || cfg.secret;
|
|
110
|
+
if (!secret) {
|
|
111
|
+
return {
|
|
112
|
+
success: false,
|
|
113
|
+
processed: false,
|
|
114
|
+
error: "Webhook secret not provided",
|
|
115
|
+
statusCode: 400
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
if (!signature || !body) {
|
|
119
|
+
return {
|
|
120
|
+
success: false,
|
|
121
|
+
processed: false,
|
|
122
|
+
error: "Missing signature or body",
|
|
123
|
+
statusCode: 400
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
const sig = typeof signature === "string" && signature.startsWith("sha256=") ? signature.slice("sha256=".length) : String(signature);
|
|
128
|
+
const hmac = (0, import_crypto.createHmac)("sha256", secret).update(body, "utf8").digest("hex");
|
|
129
|
+
let ok = false;
|
|
130
|
+
try {
|
|
131
|
+
const sigBuf = Buffer.from(sig, "hex");
|
|
132
|
+
const hmacBuf = Buffer.from(hmac, "hex");
|
|
133
|
+
if (sigBuf.length === hmacBuf.length) {
|
|
134
|
+
ok = (0, import_crypto.timingSafeEqual)(sigBuf, hmacBuf);
|
|
135
|
+
}
|
|
136
|
+
} catch (e) {
|
|
137
|
+
ok = (0, import_crypto.timingSafeEqual)(Buffer.from(String(sig), "utf8"), Buffer.from(hmac, "utf8"));
|
|
138
|
+
}
|
|
139
|
+
if (!ok) {
|
|
140
|
+
return {
|
|
141
|
+
success: false,
|
|
142
|
+
processed: false,
|
|
143
|
+
error: "Invalid signature",
|
|
144
|
+
statusCode: 401
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const payload = JSON.parse(body);
|
|
148
|
+
const fullConfig = {
|
|
149
|
+
secret,
|
|
150
|
+
isProcessed: cfg.isProcessed,
|
|
151
|
+
onProcessed: cfg.onProcessed,
|
|
152
|
+
onError: cfg.onError,
|
|
153
|
+
// Forward compatibility for alternate names and flags
|
|
154
|
+
enableDeduplication: cfg.enableDeduplication,
|
|
155
|
+
checkDuplicate: cfg.checkDuplicate,
|
|
156
|
+
markProcessed: cfg.markProcessed
|
|
157
|
+
};
|
|
158
|
+
return await processPayload(payload, handlers, fullConfig);
|
|
159
|
+
} catch (err) {
|
|
160
|
+
return {
|
|
161
|
+
success: false,
|
|
162
|
+
processed: false,
|
|
163
|
+
error: err.message,
|
|
164
|
+
statusCode: 500
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/express.ts
|
|
170
|
+
function createExpressWebhookHandler(config) {
|
|
171
|
+
return async (req, res) => {
|
|
172
|
+
try {
|
|
173
|
+
const signature = req.headers["x-zendfi-signature"];
|
|
174
|
+
if (!signature) {
|
|
175
|
+
return res.status(401).json({ error: "Missing signature" });
|
|
176
|
+
}
|
|
177
|
+
let body;
|
|
178
|
+
if (Buffer.isBuffer(req.body)) {
|
|
179
|
+
body = req.body.toString("utf8");
|
|
180
|
+
} else if (typeof req.body === "string") {
|
|
181
|
+
body = req.body;
|
|
182
|
+
} else {
|
|
183
|
+
return res.status(400).json({
|
|
184
|
+
error: "Raw body required. Use express.raw() middleware."
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
const computedSignature = (0, import_crypto2.createHmac)("sha256", config.secret).update(body, "utf8").digest("hex");
|
|
188
|
+
const sigBuffer = Buffer.from(signature, "utf8");
|
|
189
|
+
const compBuffer = Buffer.from(computedSignature, "utf8");
|
|
190
|
+
if (sigBuffer.length !== compBuffer.length || !(0, import_crypto2.timingSafeEqual)(sigBuffer, compBuffer)) {
|
|
191
|
+
return res.status(401).json({ error: "Invalid signature" });
|
|
192
|
+
}
|
|
193
|
+
let payload;
|
|
194
|
+
try {
|
|
195
|
+
payload = JSON.parse(body);
|
|
196
|
+
} catch {
|
|
197
|
+
return res.status(400).json({ error: "Invalid JSON" });
|
|
198
|
+
}
|
|
199
|
+
const result = await processWebhook(payload, config.handlers, config);
|
|
200
|
+
if (!result.success) {
|
|
201
|
+
return res.status(500).json({
|
|
202
|
+
error: result.error || "Webhook processing failed"
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return res.json({
|
|
206
|
+
received: true,
|
|
207
|
+
processed: result.processed,
|
|
208
|
+
event: result.event
|
|
209
|
+
});
|
|
210
|
+
} catch (error) {
|
|
211
|
+
const err = error;
|
|
212
|
+
console.error("Webhook handler error:", err);
|
|
213
|
+
return res.status(500).json({ error: "Internal server error" });
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
218
|
+
0 && (module.exports = {
|
|
219
|
+
createExpressWebhookHandler
|
|
220
|
+
});
|
package/dist/express.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
processWebhook
|
|
3
|
+
} from "./chunk-YFOBPGQE.mjs";
|
|
4
|
+
|
|
5
|
+
// src/express.ts
|
|
6
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
7
|
+
function createExpressWebhookHandler(config) {
|
|
8
|
+
return async (req, res) => {
|
|
9
|
+
try {
|
|
10
|
+
const signature = req.headers["x-zendfi-signature"];
|
|
11
|
+
if (!signature) {
|
|
12
|
+
return res.status(401).json({ error: "Missing signature" });
|
|
13
|
+
}
|
|
14
|
+
let body;
|
|
15
|
+
if (Buffer.isBuffer(req.body)) {
|
|
16
|
+
body = req.body.toString("utf8");
|
|
17
|
+
} else if (typeof req.body === "string") {
|
|
18
|
+
body = req.body;
|
|
19
|
+
} else {
|
|
20
|
+
return res.status(400).json({
|
|
21
|
+
error: "Raw body required. Use express.raw() middleware."
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
const computedSignature = createHmac("sha256", config.secret).update(body, "utf8").digest("hex");
|
|
25
|
+
const sigBuffer = Buffer.from(signature, "utf8");
|
|
26
|
+
const compBuffer = Buffer.from(computedSignature, "utf8");
|
|
27
|
+
if (sigBuffer.length !== compBuffer.length || !timingSafeEqual(sigBuffer, compBuffer)) {
|
|
28
|
+
return res.status(401).json({ error: "Invalid signature" });
|
|
29
|
+
}
|
|
30
|
+
let payload;
|
|
31
|
+
try {
|
|
32
|
+
payload = JSON.parse(body);
|
|
33
|
+
} catch {
|
|
34
|
+
return res.status(400).json({ error: "Invalid JSON" });
|
|
35
|
+
}
|
|
36
|
+
const result = await processWebhook(payload, config.handlers, config);
|
|
37
|
+
if (!result.success) {
|
|
38
|
+
return res.status(500).json({
|
|
39
|
+
error: result.error || "Webhook processing failed"
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return res.json({
|
|
43
|
+
received: true,
|
|
44
|
+
processed: result.processed,
|
|
45
|
+
event: result.event
|
|
46
|
+
});
|
|
47
|
+
} catch (error) {
|
|
48
|
+
const err = error;
|
|
49
|
+
console.error("Webhook handler error:", err);
|
|
50
|
+
return res.status(500).json({ error: "Internal server error" });
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
createExpressWebhookHandler
|
|
56
|
+
};
|