mpesa-mock 0.1.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/LICENSE +25 -0
- package/README.md +180 -0
- package/dist/cli.cjs +2573 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +2 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +2574 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.cjs +1407 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +167 -0
- package/dist/index.d.ts +167 -0
- package/dist/index.js +1368 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,2574 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
9
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
10
|
+
}) : x)(function(x) {
|
|
11
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
12
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
13
|
+
});
|
|
14
|
+
var __esm = (fn, res) => function __init() {
|
|
15
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
16
|
+
};
|
|
17
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
18
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
23
|
+
};
|
|
24
|
+
var __copyProps = (to, from, except, desc) => {
|
|
25
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
26
|
+
for (let key of __getOwnPropNames(from))
|
|
27
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
28
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
29
|
+
}
|
|
30
|
+
return to;
|
|
31
|
+
};
|
|
32
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
33
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
34
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
35
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
36
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
37
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
38
|
+
mod
|
|
39
|
+
));
|
|
40
|
+
|
|
41
|
+
// node_modules/.pnpm/tsup@8.5.1_postcss@8.5.14_tsx@4.21.0_typescript@5.9.3/node_modules/tsup/assets/esm_shims.js
|
|
42
|
+
import path from "path";
|
|
43
|
+
import { fileURLToPath } from "url";
|
|
44
|
+
var getFilename, __filename;
|
|
45
|
+
var init_esm_shims = __esm({
|
|
46
|
+
"node_modules/.pnpm/tsup@8.5.1_postcss@8.5.14_tsx@4.21.0_typescript@5.9.3/node_modules/tsup/assets/esm_shims.js"() {
|
|
47
|
+
"use strict";
|
|
48
|
+
getFilename = () => fileURLToPath(import.meta.url);
|
|
49
|
+
__filename = /* @__PURE__ */ getFilename();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// src/core/transactions.ts
|
|
54
|
+
import { EventEmitter } from "events";
|
|
55
|
+
function stateToResultCode(state) {
|
|
56
|
+
switch (state) {
|
|
57
|
+
case "success":
|
|
58
|
+
return 0;
|
|
59
|
+
case "insufficient_funds":
|
|
60
|
+
return 1;
|
|
61
|
+
case "user_cancelled":
|
|
62
|
+
return 1032;
|
|
63
|
+
case "wrong_pin":
|
|
64
|
+
return 2001;
|
|
65
|
+
case "expired":
|
|
66
|
+
return 1037;
|
|
67
|
+
case "system_error":
|
|
68
|
+
return 1025;
|
|
69
|
+
case "pending":
|
|
70
|
+
return 1019;
|
|
71
|
+
case "timeout":
|
|
72
|
+
return 1037;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function stateToResultDesc(state) {
|
|
76
|
+
switch (state) {
|
|
77
|
+
case "success":
|
|
78
|
+
return "The service request is processed successfully.";
|
|
79
|
+
case "insufficient_funds":
|
|
80
|
+
return "The balance is insufficient for the transaction.";
|
|
81
|
+
case "user_cancelled":
|
|
82
|
+
return "Request cancelled by user.";
|
|
83
|
+
case "wrong_pin":
|
|
84
|
+
return "The initiator information is invalid.";
|
|
85
|
+
case "expired":
|
|
86
|
+
return "DS timeout. User cannot be reached.";
|
|
87
|
+
case "system_error":
|
|
88
|
+
return "An error occurred while sending a push request.";
|
|
89
|
+
case "pending":
|
|
90
|
+
return "The transaction is being processed.";
|
|
91
|
+
case "timeout":
|
|
92
|
+
return "DS timeout. User cannot be reached.";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
var InMemoryStore;
|
|
96
|
+
var init_transactions = __esm({
|
|
97
|
+
"src/core/transactions.ts"() {
|
|
98
|
+
"use strict";
|
|
99
|
+
init_esm_shims();
|
|
100
|
+
InMemoryStore = class extends EventEmitter {
|
|
101
|
+
byCheckout = /* @__PURE__ */ new Map();
|
|
102
|
+
byConversation = /* @__PURE__ */ new Map();
|
|
103
|
+
put(record) {
|
|
104
|
+
this.byCheckout.set(record.checkoutRequestID, record);
|
|
105
|
+
if (record.conversationID) {
|
|
106
|
+
this.byConversation.set(record.conversationID, record.checkoutRequestID);
|
|
107
|
+
}
|
|
108
|
+
this.emit("change", record);
|
|
109
|
+
}
|
|
110
|
+
get(checkoutRequestID) {
|
|
111
|
+
return this.byCheckout.get(checkoutRequestID);
|
|
112
|
+
}
|
|
113
|
+
getByConversationID(id) {
|
|
114
|
+
const key = this.byConversation.get(id);
|
|
115
|
+
return key ? this.byCheckout.get(key) : void 0;
|
|
116
|
+
}
|
|
117
|
+
list(limit) {
|
|
118
|
+
const all = Array.from(this.byCheckout.values()).sort((a, b) => b.createdAt - a.createdAt);
|
|
119
|
+
return typeof limit === "number" ? all.slice(0, limit) : all;
|
|
120
|
+
}
|
|
121
|
+
update(checkoutRequestID, patch) {
|
|
122
|
+
const existing = this.byCheckout.get(checkoutRequestID);
|
|
123
|
+
if (!existing) return void 0;
|
|
124
|
+
const next = { ...existing, ...patch };
|
|
125
|
+
this.byCheckout.set(checkoutRequestID, next);
|
|
126
|
+
if (next.conversationID) this.byConversation.set(next.conversationID, checkoutRequestID);
|
|
127
|
+
this.emit("change", next);
|
|
128
|
+
return next;
|
|
129
|
+
}
|
|
130
|
+
clear() {
|
|
131
|
+
this.byCheckout.clear();
|
|
132
|
+
this.byConversation.clear();
|
|
133
|
+
this.emit("clear");
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// src/core/webhook-dispatcher.ts
|
|
140
|
+
import { EventEmitter as EventEmitter2 } from "events";
|
|
141
|
+
import pRetry, { AbortError } from "p-retry";
|
|
142
|
+
var WebhookDispatcher;
|
|
143
|
+
var init_webhook_dispatcher = __esm({
|
|
144
|
+
"src/core/webhook-dispatcher.ts"() {
|
|
145
|
+
"use strict";
|
|
146
|
+
init_esm_shims();
|
|
147
|
+
WebhookDispatcher = class extends EventEmitter2 {
|
|
148
|
+
pending = /* @__PURE__ */ new Map();
|
|
149
|
+
store;
|
|
150
|
+
fetchImpl;
|
|
151
|
+
onLog;
|
|
152
|
+
constructor(opts = {}) {
|
|
153
|
+
super();
|
|
154
|
+
this.store = opts.store;
|
|
155
|
+
this.fetchImpl = opts.fetchImpl ?? fetch;
|
|
156
|
+
this.onLog = opts.onLog;
|
|
157
|
+
}
|
|
158
|
+
schedule(job, delayMs) {
|
|
159
|
+
if (delayMs < 0) {
|
|
160
|
+
this.emit("skipped", { id: job.id, reason: "timeout" });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (this.pending.has(job.id)) {
|
|
164
|
+
clearTimeout(this.pending.get(job.id));
|
|
165
|
+
}
|
|
166
|
+
const handle = setTimeout(() => {
|
|
167
|
+
this.pending.delete(job.id);
|
|
168
|
+
void this.fire(job);
|
|
169
|
+
}, delayMs);
|
|
170
|
+
this.pending.set(job.id, handle);
|
|
171
|
+
this.emit("scheduled", { id: job.id, delayMs, url: job.url });
|
|
172
|
+
}
|
|
173
|
+
cancel(id) {
|
|
174
|
+
const h = this.pending.get(id);
|
|
175
|
+
if (!h) return false;
|
|
176
|
+
clearTimeout(h);
|
|
177
|
+
this.pending.delete(id);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
pendingIds() {
|
|
181
|
+
return Array.from(this.pending.keys());
|
|
182
|
+
}
|
|
183
|
+
async fire(job) {
|
|
184
|
+
let attempt = 0;
|
|
185
|
+
try {
|
|
186
|
+
await pRetry(
|
|
187
|
+
async () => {
|
|
188
|
+
attempt += 1;
|
|
189
|
+
if (typeof job.failNTimesFirst === "number" && attempt <= job.failNTimesFirst) {
|
|
190
|
+
this.onLog?.(`webhook attempt ${attempt} forced-fail for ${job.url}`);
|
|
191
|
+
throw new Error(`forced fail ${attempt}`);
|
|
192
|
+
}
|
|
193
|
+
const res = await this.fetchImpl(job.url, {
|
|
194
|
+
method: "POST",
|
|
195
|
+
headers: { "content-type": "application/json" },
|
|
196
|
+
body: JSON.stringify(job.body)
|
|
197
|
+
});
|
|
198
|
+
if (!res.ok && res.status >= 500) {
|
|
199
|
+
throw new Error(`callback ${res.status}`);
|
|
200
|
+
}
|
|
201
|
+
if (!res.ok && res.status >= 400) {
|
|
202
|
+
throw new AbortError(`callback ${res.status}`);
|
|
203
|
+
}
|
|
204
|
+
this.emit("delivered", { id: job.id, url: job.url, attempts: attempt });
|
|
205
|
+
if (this.store && job.transactionId) {
|
|
206
|
+
this.store.update(job.transactionId, {
|
|
207
|
+
callbackAttempts: attempt,
|
|
208
|
+
callbackDeliveredAt: Date.now()
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
retries: job.maxAttempts - 1,
|
|
214
|
+
minTimeout: job.backoffMs,
|
|
215
|
+
factor: 2,
|
|
216
|
+
onFailedAttempt: (err) => {
|
|
217
|
+
this.onLog?.(`webhook ${job.url} attempt ${err.attemptNumber} failed: ${err.message}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
} catch (err) {
|
|
222
|
+
this.emit("failed", {
|
|
223
|
+
id: job.id,
|
|
224
|
+
url: job.url,
|
|
225
|
+
attempts: attempt,
|
|
226
|
+
error: err instanceof Error ? err.message : String(err)
|
|
227
|
+
});
|
|
228
|
+
if (this.store && job.transactionId) {
|
|
229
|
+
this.store.update(job.transactionId, { callbackAttempts: attempt });
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
shutdown() {
|
|
234
|
+
for (const handle of this.pending.values()) clearTimeout(handle);
|
|
235
|
+
this.pending.clear();
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// src/core/id-generator.ts
|
|
242
|
+
import { randomBytes, randomInt } from "crypto";
|
|
243
|
+
function generateAccessToken() {
|
|
244
|
+
return randomBytes(24).toString("base64").replace(/[^a-zA-Z0-9]/g, "").slice(0, 32).padEnd(32, "0");
|
|
245
|
+
}
|
|
246
|
+
function generateMerchantRequestID() {
|
|
247
|
+
const a = randomInt(1e4, 99999);
|
|
248
|
+
const b = randomInt(1e7, 99999999);
|
|
249
|
+
const c = randomInt(1, 9);
|
|
250
|
+
return `${a}-${b}-${c}`;
|
|
251
|
+
}
|
|
252
|
+
function generateCheckoutRequestID(date = /* @__PURE__ */ new Date()) {
|
|
253
|
+
const pad = (n, len = 2) => String(n).padStart(len, "0");
|
|
254
|
+
const dd = pad(date.getDate());
|
|
255
|
+
const mm = pad(date.getMonth() + 1);
|
|
256
|
+
const yyyy = String(date.getFullYear());
|
|
257
|
+
const hh = pad(date.getHours());
|
|
258
|
+
const mi = pad(date.getMinutes());
|
|
259
|
+
const ss = pad(date.getSeconds());
|
|
260
|
+
const ms = pad(date.getMilliseconds(), 3);
|
|
261
|
+
return `ws_CO_${dd}${mm}${yyyy}${hh}${mi}${ss}${ms}`;
|
|
262
|
+
}
|
|
263
|
+
function generateConversationID() {
|
|
264
|
+
const a = randomInt(1e3, 9999);
|
|
265
|
+
const b = randomInt(1e5, 999999);
|
|
266
|
+
const c = randomInt(10, 99);
|
|
267
|
+
return `AG_${formatDate(/* @__PURE__ */ new Date())}_${a}${b}${c}`;
|
|
268
|
+
}
|
|
269
|
+
function generateOriginatorConversationID() {
|
|
270
|
+
const a = randomInt(1e4, 99999);
|
|
271
|
+
const b = randomInt(1e6, 9999999);
|
|
272
|
+
const c = randomInt(1, 9);
|
|
273
|
+
return `${a}-${b}-${c}`;
|
|
274
|
+
}
|
|
275
|
+
function generateMpesaReceiptNumber() {
|
|
276
|
+
const chars = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";
|
|
277
|
+
let out = "";
|
|
278
|
+
for (let i = 0; i < 10; i++) {
|
|
279
|
+
out += chars[randomInt(0, chars.length)];
|
|
280
|
+
}
|
|
281
|
+
return out;
|
|
282
|
+
}
|
|
283
|
+
function generateTransactionDate(date = /* @__PURE__ */ new Date()) {
|
|
284
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
285
|
+
const yyyy = date.getFullYear();
|
|
286
|
+
const mm = pad(date.getMonth() + 1);
|
|
287
|
+
const dd = pad(date.getDate());
|
|
288
|
+
const hh = pad(date.getHours());
|
|
289
|
+
const mi = pad(date.getMinutes());
|
|
290
|
+
const ss = pad(date.getSeconds());
|
|
291
|
+
return Number(`${yyyy}${mm}${dd}${hh}${mi}${ss}`);
|
|
292
|
+
}
|
|
293
|
+
function formatDate(d) {
|
|
294
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
295
|
+
return `${pad(d.getDate())}${pad(d.getMonth() + 1)}${d.getFullYear()}${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`;
|
|
296
|
+
}
|
|
297
|
+
var init_id_generator = __esm({
|
|
298
|
+
"src/core/id-generator.ts"() {
|
|
299
|
+
"use strict";
|
|
300
|
+
init_esm_shims();
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// src/config/defaults.ts
|
|
305
|
+
var DEFAULTS;
|
|
306
|
+
var init_defaults = __esm({
|
|
307
|
+
"src/config/defaults.ts"() {
|
|
308
|
+
"use strict";
|
|
309
|
+
init_esm_shims();
|
|
310
|
+
DEFAULTS = {
|
|
311
|
+
port: 4e3,
|
|
312
|
+
host: "0.0.0.0",
|
|
313
|
+
callbackDelayMs: 8e3,
|
|
314
|
+
callbackRetryAttempts: 3,
|
|
315
|
+
callbackRetryBackoffMs: 1e3,
|
|
316
|
+
oauthTokenExpiresIn: "3599",
|
|
317
|
+
testCredentials: {
|
|
318
|
+
consumerKey: "test_key",
|
|
319
|
+
consumerSecret: "test_secret"
|
|
320
|
+
},
|
|
321
|
+
testShortcode: "174379",
|
|
322
|
+
testTill: "600000",
|
|
323
|
+
partyB: "254708374149"
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
// src/core/auth.ts
|
|
329
|
+
function parseBasicAuth(header) {
|
|
330
|
+
if (!header || !header.toLowerCase().startsWith("basic ")) return null;
|
|
331
|
+
const encoded = header.slice(6).trim();
|
|
332
|
+
let decoded;
|
|
333
|
+
try {
|
|
334
|
+
decoded = Buffer.from(encoded, "base64").toString("utf-8");
|
|
335
|
+
} catch {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
const idx = decoded.indexOf(":");
|
|
339
|
+
if (idx < 0) return null;
|
|
340
|
+
const key = decoded.slice(0, idx);
|
|
341
|
+
const secret = decoded.slice(idx + 1);
|
|
342
|
+
if (!key || !secret) return null;
|
|
343
|
+
return { key, secret };
|
|
344
|
+
}
|
|
345
|
+
function issueToken() {
|
|
346
|
+
const token = generateAccessToken();
|
|
347
|
+
issuedTokens.set(token, Date.now() + TOKEN_TTL_MS);
|
|
348
|
+
return { access_token: token, expires_in: DEFAULTS.oauthTokenExpiresIn };
|
|
349
|
+
}
|
|
350
|
+
function isUsingTestCredentials(key, secret) {
|
|
351
|
+
return key === DEFAULTS.testCredentials.consumerKey && secret === DEFAULTS.testCredentials.consumerSecret;
|
|
352
|
+
}
|
|
353
|
+
function parseBearerToken(header) {
|
|
354
|
+
if (!header) return null;
|
|
355
|
+
const m = header.match(/^Bearer\s+(.+)$/i);
|
|
356
|
+
return m && m[1] ? m[1].trim() : null;
|
|
357
|
+
}
|
|
358
|
+
function isValidToken(token) {
|
|
359
|
+
const exp = issuedTokens.get(token);
|
|
360
|
+
if (!exp) {
|
|
361
|
+
return /^[a-zA-Z0-9]{20,}$/.test(token);
|
|
362
|
+
}
|
|
363
|
+
if (Date.now() > exp) {
|
|
364
|
+
issuedTokens.delete(token);
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
var issuedTokens, TOKEN_TTL_MS;
|
|
370
|
+
var init_auth = __esm({
|
|
371
|
+
"src/core/auth.ts"() {
|
|
372
|
+
"use strict";
|
|
373
|
+
init_esm_shims();
|
|
374
|
+
init_id_generator();
|
|
375
|
+
init_defaults();
|
|
376
|
+
issuedTokens = /* @__PURE__ */ new Map();
|
|
377
|
+
TOKEN_TTL_MS = 3599 * 1e3;
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
// src/routes/oauth.ts
|
|
382
|
+
import { Hono } from "hono";
|
|
383
|
+
function oauthRoute() {
|
|
384
|
+
const app = new Hono();
|
|
385
|
+
app.get("/oauth/v1/generate", (c) => {
|
|
386
|
+
const grantType = c.req.query("grant_type");
|
|
387
|
+
if (grantType !== "client_credentials") {
|
|
388
|
+
return c.json(
|
|
389
|
+
{ requestId: "no-request-id", errorCode: "400.001.01", errorMessage: "Invalid grant_type" },
|
|
390
|
+
400
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
const parsed = parseBasicAuth(c.req.header("authorization"));
|
|
394
|
+
if (!parsed) {
|
|
395
|
+
return c.json(
|
|
396
|
+
{ requestId: "no-request-id", errorCode: "401.002.01", errorMessage: "Invalid Authentication passed" },
|
|
397
|
+
401
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
if (isUsingTestCredentials(parsed.key, parsed.secret)) {
|
|
401
|
+
c.get("log")?.("warn: using default test credentials (test_key:test_secret) \u2014 fine for mock");
|
|
402
|
+
}
|
|
403
|
+
return c.json(issueToken());
|
|
404
|
+
});
|
|
405
|
+
return app;
|
|
406
|
+
}
|
|
407
|
+
var init_oauth = __esm({
|
|
408
|
+
"src/routes/oauth.ts"() {
|
|
409
|
+
"use strict";
|
|
410
|
+
init_esm_shims();
|
|
411
|
+
init_auth();
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
// src/schemas/index.ts
|
|
416
|
+
import { z } from "zod";
|
|
417
|
+
var stkPushSchema, stkQuerySchema, c2bRegisterUrlSchema, c2bSimulateSchema, b2cSchema, b2bSchema, transactionStatusSchema, accountBalanceSchema, reversalSchema;
|
|
418
|
+
var init_schemas = __esm({
|
|
419
|
+
"src/schemas/index.ts"() {
|
|
420
|
+
"use strict";
|
|
421
|
+
init_esm_shims();
|
|
422
|
+
stkPushSchema = z.object({
|
|
423
|
+
BusinessShortCode: z.string().min(1),
|
|
424
|
+
Password: z.string().min(1),
|
|
425
|
+
Timestamp: z.string().min(1),
|
|
426
|
+
TransactionType: z.enum(["CustomerPayBillOnline", "CustomerBuyGoodsOnline"]),
|
|
427
|
+
Amount: z.number().int().positive(),
|
|
428
|
+
PartyA: z.string().min(1),
|
|
429
|
+
PartyB: z.string().min(1),
|
|
430
|
+
PhoneNumber: z.string().min(1),
|
|
431
|
+
CallBackURL: z.string().url(),
|
|
432
|
+
AccountReference: z.string().min(1).max(12),
|
|
433
|
+
TransactionDesc: z.string().min(1).max(13)
|
|
434
|
+
});
|
|
435
|
+
stkQuerySchema = z.object({
|
|
436
|
+
BusinessShortCode: z.string().min(1),
|
|
437
|
+
Password: z.string().min(1),
|
|
438
|
+
Timestamp: z.string().min(1),
|
|
439
|
+
CheckoutRequestID: z.string().min(1)
|
|
440
|
+
});
|
|
441
|
+
c2bRegisterUrlSchema = z.object({
|
|
442
|
+
ShortCode: z.string().min(1),
|
|
443
|
+
ResponseType: z.enum(["Completed", "Cancelled"]),
|
|
444
|
+
ConfirmationURL: z.string().url(),
|
|
445
|
+
ValidationURL: z.string().url()
|
|
446
|
+
});
|
|
447
|
+
c2bSimulateSchema = z.object({
|
|
448
|
+
ShortCode: z.string().min(1),
|
|
449
|
+
CommandID: z.enum(["CustomerPayBillOnline", "CustomerBuyGoodsOnline"]),
|
|
450
|
+
Amount: z.number().int().positive(),
|
|
451
|
+
Msisdn: z.string().min(1),
|
|
452
|
+
BillRefNumber: z.string().min(1)
|
|
453
|
+
});
|
|
454
|
+
b2cSchema = z.object({
|
|
455
|
+
InitiatorName: z.string().min(1),
|
|
456
|
+
SecurityCredential: z.string().min(1),
|
|
457
|
+
CommandID: z.enum(["SalaryPayment", "BusinessPayment", "PromotionPayment"]),
|
|
458
|
+
Amount: z.number().int().positive(),
|
|
459
|
+
PartyA: z.string().min(1),
|
|
460
|
+
PartyB: z.string().min(1),
|
|
461
|
+
Remarks: z.string().min(1),
|
|
462
|
+
QueueTimeOutURL: z.string().url(),
|
|
463
|
+
ResultURL: z.string().url(),
|
|
464
|
+
Occasion: z.string().optional().default("")
|
|
465
|
+
});
|
|
466
|
+
b2bSchema = z.object({
|
|
467
|
+
Initiator: z.string().min(1),
|
|
468
|
+
SecurityCredential: z.string().min(1),
|
|
469
|
+
CommandID: z.string().min(1),
|
|
470
|
+
SenderIdentifierType: z.string().min(1),
|
|
471
|
+
RecieverIdentifierType: z.string().min(1),
|
|
472
|
+
Amount: z.number().int().positive(),
|
|
473
|
+
PartyA: z.string().min(1),
|
|
474
|
+
PartyB: z.string().min(1),
|
|
475
|
+
AccountReference: z.string().optional().default(""),
|
|
476
|
+
Remarks: z.string().min(1),
|
|
477
|
+
QueueTimeOutURL: z.string().url(),
|
|
478
|
+
ResultURL: z.string().url()
|
|
479
|
+
});
|
|
480
|
+
transactionStatusSchema = z.object({
|
|
481
|
+
Initiator: z.string().min(1),
|
|
482
|
+
SecurityCredential: z.string().min(1),
|
|
483
|
+
CommandID: z.literal("TransactionStatusQuery"),
|
|
484
|
+
TransactionID: z.string().min(1),
|
|
485
|
+
PartyA: z.string().min(1),
|
|
486
|
+
IdentifierType: z.string().min(1),
|
|
487
|
+
ResultURL: z.string().url(),
|
|
488
|
+
QueueTimeOutURL: z.string().url(),
|
|
489
|
+
Remarks: z.string().min(1),
|
|
490
|
+
Occasion: z.string().optional().default("")
|
|
491
|
+
});
|
|
492
|
+
accountBalanceSchema = z.object({
|
|
493
|
+
Initiator: z.string().min(1),
|
|
494
|
+
SecurityCredential: z.string().min(1),
|
|
495
|
+
CommandID: z.literal("AccountBalance"),
|
|
496
|
+
PartyA: z.string().min(1),
|
|
497
|
+
IdentifierType: z.string().min(1),
|
|
498
|
+
Remarks: z.string().min(1),
|
|
499
|
+
QueueTimeOutURL: z.string().url(),
|
|
500
|
+
ResultURL: z.string().url()
|
|
501
|
+
});
|
|
502
|
+
reversalSchema = z.object({
|
|
503
|
+
Initiator: z.string().min(1),
|
|
504
|
+
SecurityCredential: z.string().min(1),
|
|
505
|
+
CommandID: z.literal("TransactionReversal"),
|
|
506
|
+
TransactionID: z.string().min(1),
|
|
507
|
+
Amount: z.number().int().positive(),
|
|
508
|
+
ReceiverParty: z.string().min(1),
|
|
509
|
+
RecieverIdentifierType: z.string().min(1),
|
|
510
|
+
ResultURL: z.string().url(),
|
|
511
|
+
QueueTimeOutURL: z.string().url(),
|
|
512
|
+
Remarks: z.string().min(1),
|
|
513
|
+
Occasion: z.string().optional().default("")
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
// src/core/failure-injector.ts
|
|
519
|
+
function pickScenario(phoneNumber, config) {
|
|
520
|
+
const direct = config.scenarios[phoneNumber];
|
|
521
|
+
if (direct) return direct;
|
|
522
|
+
const suffix = phoneNumber.slice(-2);
|
|
523
|
+
return SUFFIX_MAP[suffix] ?? "success";
|
|
524
|
+
}
|
|
525
|
+
function callbackDelayFor(scenario, config) {
|
|
526
|
+
if (scenario === "slow") return 3e4;
|
|
527
|
+
if (scenario === "timeout") return -1;
|
|
528
|
+
return config.defaultCallbackDelayMs;
|
|
529
|
+
}
|
|
530
|
+
var SUFFIX_MAP;
|
|
531
|
+
var init_failure_injector = __esm({
|
|
532
|
+
"src/core/failure-injector.ts"() {
|
|
533
|
+
"use strict";
|
|
534
|
+
init_esm_shims();
|
|
535
|
+
SUFFIX_MAP = {
|
|
536
|
+
"00": "success",
|
|
537
|
+
"01": "user_cancelled",
|
|
538
|
+
"02": "insufficient_funds",
|
|
539
|
+
"03": "wrong_pin",
|
|
540
|
+
"04": "timeout",
|
|
541
|
+
"05": "callback_retry",
|
|
542
|
+
"06": "expired",
|
|
543
|
+
"07": "system_error",
|
|
544
|
+
"99": "slow"
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
// src/routes/stk-push.ts
|
|
550
|
+
import { Hono as Hono2 } from "hono";
|
|
551
|
+
function scenarioToState(s) {
|
|
552
|
+
switch (s) {
|
|
553
|
+
case "success":
|
|
554
|
+
case "callback_retry":
|
|
555
|
+
case "slow":
|
|
556
|
+
return "success";
|
|
557
|
+
case "user_cancelled":
|
|
558
|
+
return "user_cancelled";
|
|
559
|
+
case "insufficient_funds":
|
|
560
|
+
return "insufficient_funds";
|
|
561
|
+
case "wrong_pin":
|
|
562
|
+
return "wrong_pin";
|
|
563
|
+
case "expired":
|
|
564
|
+
return "expired";
|
|
565
|
+
case "system_error":
|
|
566
|
+
return "system_error";
|
|
567
|
+
case "timeout":
|
|
568
|
+
return "timeout";
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
function stkPushRoute() {
|
|
572
|
+
const app = new Hono2();
|
|
573
|
+
app.post("/mpesa/stkpush/v1/processrequest", async (c) => {
|
|
574
|
+
const token = parseBearerToken(c.req.header("authorization"));
|
|
575
|
+
if (!token || !isValidToken(token)) {
|
|
576
|
+
return c.json({ errorCode: "404.001.03", errorMessage: "Invalid Access Token" }, 401);
|
|
577
|
+
}
|
|
578
|
+
const raw = await c.req.json().catch(() => null);
|
|
579
|
+
const parsed = stkPushSchema.safeParse(raw);
|
|
580
|
+
if (!parsed.success) {
|
|
581
|
+
return c.json(
|
|
582
|
+
{
|
|
583
|
+
requestId: "no-request-id",
|
|
584
|
+
errorCode: "400.002.05",
|
|
585
|
+
errorMessage: "Invalid request payload",
|
|
586
|
+
errors: parsed.error.flatten()
|
|
587
|
+
},
|
|
588
|
+
400
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
const body = parsed.data;
|
|
592
|
+
const merchantRequestID = generateMerchantRequestID();
|
|
593
|
+
const checkoutRequestID = generateCheckoutRequestID();
|
|
594
|
+
const scenario = pickScenario(body.PhoneNumber, c.var.config);
|
|
595
|
+
const targetState = scenarioToState(scenario);
|
|
596
|
+
const delay = callbackDelayFor(scenario, c.var.config);
|
|
597
|
+
const record = {
|
|
598
|
+
checkoutRequestID,
|
|
599
|
+
merchantRequestID,
|
|
600
|
+
kind: "stk",
|
|
601
|
+
amount: body.Amount,
|
|
602
|
+
phoneNumber: body.PhoneNumber,
|
|
603
|
+
shortCode: body.BusinessShortCode,
|
|
604
|
+
callbackUrl: body.CallBackURL,
|
|
605
|
+
state: "pending",
|
|
606
|
+
createdAt: Date.now(),
|
|
607
|
+
callbackAttempts: 0
|
|
608
|
+
};
|
|
609
|
+
c.var.store.put(record);
|
|
610
|
+
const failNTimesFirst = scenario === "callback_retry" ? 3 : void 0;
|
|
611
|
+
const callbackBody = buildStkCallback({
|
|
612
|
+
merchantRequestID,
|
|
613
|
+
checkoutRequestID,
|
|
614
|
+
state: targetState,
|
|
615
|
+
amount: body.Amount,
|
|
616
|
+
phoneNumber: body.PhoneNumber
|
|
617
|
+
});
|
|
618
|
+
if (scenario === "timeout") {
|
|
619
|
+
c.var.log?.(`stk-push ${checkoutRequestID}: timeout scenario, no callback will fire`);
|
|
620
|
+
} else {
|
|
621
|
+
c.var.dispatcher.schedule(
|
|
622
|
+
{
|
|
623
|
+
id: checkoutRequestID,
|
|
624
|
+
url: body.CallBackURL,
|
|
625
|
+
body: callbackBody,
|
|
626
|
+
scheduledAt: Date.now() + delay,
|
|
627
|
+
attempts: 0,
|
|
628
|
+
maxAttempts: c.var.config.webhookRetry.attempts + (failNTimesFirst ?? 0),
|
|
629
|
+
backoffMs: c.var.config.webhookRetry.backoffMs,
|
|
630
|
+
transactionId: checkoutRequestID,
|
|
631
|
+
...failNTimesFirst !== void 0 ? { failNTimesFirst } : {}
|
|
632
|
+
},
|
|
633
|
+
delay
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
setTimeout(() => {
|
|
637
|
+
const cur = c.var.store.get(checkoutRequestID);
|
|
638
|
+
if (cur && cur.state === "pending") {
|
|
639
|
+
c.var.store.update(checkoutRequestID, {
|
|
640
|
+
state: targetState,
|
|
641
|
+
resultCode: stateToResultCode(targetState),
|
|
642
|
+
resultDesc: stateToResultDesc(targetState),
|
|
643
|
+
completedAt: Date.now(),
|
|
644
|
+
...targetState === "success" ? { mpesaReceiptNumber: generateMpesaReceiptNumber() } : {}
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
}, Math.max(0, delay));
|
|
648
|
+
return c.json({
|
|
649
|
+
MerchantRequestID: merchantRequestID,
|
|
650
|
+
CheckoutRequestID: checkoutRequestID,
|
|
651
|
+
ResponseCode: "0",
|
|
652
|
+
ResponseDescription: "Success. Request accepted for processing",
|
|
653
|
+
CustomerMessage: "Success. Request accepted for processing"
|
|
654
|
+
});
|
|
655
|
+
});
|
|
656
|
+
return app;
|
|
657
|
+
}
|
|
658
|
+
function buildStkCallback(params) {
|
|
659
|
+
const resultCode = stateToResultCode(params.state);
|
|
660
|
+
const resultDesc = stateToResultDesc(params.state);
|
|
661
|
+
if (params.state === "success") {
|
|
662
|
+
return {
|
|
663
|
+
Body: {
|
|
664
|
+
stkCallback: {
|
|
665
|
+
MerchantRequestID: params.merchantRequestID,
|
|
666
|
+
CheckoutRequestID: params.checkoutRequestID,
|
|
667
|
+
ResultCode: resultCode,
|
|
668
|
+
ResultDesc: resultDesc,
|
|
669
|
+
CallbackMetadata: {
|
|
670
|
+
Item: [
|
|
671
|
+
{ Name: "Amount", Value: params.amount },
|
|
672
|
+
{ Name: "MpesaReceiptNumber", Value: generateMpesaReceiptNumber() },
|
|
673
|
+
{ Name: "TransactionDate", Value: generateTransactionDate() },
|
|
674
|
+
{ Name: "PhoneNumber", Value: Number(params.phoneNumber) }
|
|
675
|
+
]
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
return {
|
|
682
|
+
Body: {
|
|
683
|
+
stkCallback: {
|
|
684
|
+
MerchantRequestID: params.merchantRequestID,
|
|
685
|
+
CheckoutRequestID: params.checkoutRequestID,
|
|
686
|
+
ResultCode: resultCode,
|
|
687
|
+
ResultDesc: resultDesc
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
var init_stk_push = __esm({
|
|
693
|
+
"src/routes/stk-push.ts"() {
|
|
694
|
+
"use strict";
|
|
695
|
+
init_esm_shims();
|
|
696
|
+
init_auth();
|
|
697
|
+
init_id_generator();
|
|
698
|
+
init_schemas();
|
|
699
|
+
init_failure_injector();
|
|
700
|
+
init_transactions();
|
|
701
|
+
}
|
|
702
|
+
});
|
|
703
|
+
|
|
704
|
+
// src/routes/stk-query.ts
|
|
705
|
+
import { Hono as Hono3 } from "hono";
|
|
706
|
+
function stkQueryRoute() {
|
|
707
|
+
const app = new Hono3();
|
|
708
|
+
app.post("/mpesa/stkpushquery/v1/query", async (c) => {
|
|
709
|
+
const token = parseBearerToken(c.req.header("authorization"));
|
|
710
|
+
if (!token || !isValidToken(token)) {
|
|
711
|
+
return c.json({ errorCode: "404.001.03", errorMessage: "Invalid Access Token" }, 401);
|
|
712
|
+
}
|
|
713
|
+
const raw = await c.req.json().catch(() => null);
|
|
714
|
+
const parsed = stkQuerySchema.safeParse(raw);
|
|
715
|
+
if (!parsed.success) {
|
|
716
|
+
return c.json(
|
|
717
|
+
{ errorCode: "400.002.05", errorMessage: "Invalid request payload", errors: parsed.error.flatten() },
|
|
718
|
+
400
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
const record = c.var.store.get(parsed.data.CheckoutRequestID);
|
|
722
|
+
if (!record) {
|
|
723
|
+
return c.json(
|
|
724
|
+
{
|
|
725
|
+
requestId: "no-request-id",
|
|
726
|
+
errorCode: "500.001.1001",
|
|
727
|
+
errorMessage: "The transaction is being processed"
|
|
728
|
+
},
|
|
729
|
+
500
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
const resultCode = record.resultCode ?? stateToResultCode(record.state);
|
|
733
|
+
return c.json({
|
|
734
|
+
ResponseCode: "0",
|
|
735
|
+
ResponseDescription: "The service request has been accepted successfully",
|
|
736
|
+
MerchantRequestID: record.merchantRequestID,
|
|
737
|
+
CheckoutRequestID: record.checkoutRequestID,
|
|
738
|
+
ResultCode: String(resultCode),
|
|
739
|
+
ResultDesc: record.resultDesc ?? stateToResultDesc(record.state)
|
|
740
|
+
});
|
|
741
|
+
});
|
|
742
|
+
return app;
|
|
743
|
+
}
|
|
744
|
+
var init_stk_query = __esm({
|
|
745
|
+
"src/routes/stk-query.ts"() {
|
|
746
|
+
"use strict";
|
|
747
|
+
init_esm_shims();
|
|
748
|
+
init_auth();
|
|
749
|
+
init_schemas();
|
|
750
|
+
init_transactions();
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
// src/routes/c2b.ts
|
|
755
|
+
import { Hono as Hono4 } from "hono";
|
|
756
|
+
function c2bRoute() {
|
|
757
|
+
const app = new Hono4();
|
|
758
|
+
app.post("/mpesa/c2b/v1/registerurl", async (c) => {
|
|
759
|
+
const token = parseBearerToken(c.req.header("authorization"));
|
|
760
|
+
if (!token || !isValidToken(token)) {
|
|
761
|
+
return c.json({ errorCode: "404.001.03", errorMessage: "Invalid Access Token" }, 401);
|
|
762
|
+
}
|
|
763
|
+
const raw = await c.req.json().catch(() => null);
|
|
764
|
+
const parsed = c2bRegisterUrlSchema.safeParse(raw);
|
|
765
|
+
if (!parsed.success) {
|
|
766
|
+
return c.json(
|
|
767
|
+
{ errorCode: "400.002.05", errorMessage: "Invalid request payload", errors: parsed.error.flatten() },
|
|
768
|
+
400
|
|
769
|
+
);
|
|
770
|
+
}
|
|
771
|
+
registry.set(parsed.data.ShortCode, {
|
|
772
|
+
shortCode: parsed.data.ShortCode,
|
|
773
|
+
confirmationURL: parsed.data.ConfirmationURL,
|
|
774
|
+
validationURL: parsed.data.ValidationURL,
|
|
775
|
+
responseType: parsed.data.ResponseType
|
|
776
|
+
});
|
|
777
|
+
return c.json({
|
|
778
|
+
OriginatorCoversationID: generateOriginatorConversationID(),
|
|
779
|
+
ResponseCode: "0",
|
|
780
|
+
ResponseDescription: "success"
|
|
781
|
+
});
|
|
782
|
+
});
|
|
783
|
+
app.post("/mpesa/c2b/v1/simulate", async (c) => {
|
|
784
|
+
const token = parseBearerToken(c.req.header("authorization"));
|
|
785
|
+
if (!token || !isValidToken(token)) {
|
|
786
|
+
return c.json({ errorCode: "404.001.03", errorMessage: "Invalid Access Token" }, 401);
|
|
787
|
+
}
|
|
788
|
+
const raw = await c.req.json().catch(() => null);
|
|
789
|
+
const parsed = c2bSimulateSchema.safeParse(raw);
|
|
790
|
+
if (!parsed.success) {
|
|
791
|
+
return c.json(
|
|
792
|
+
{ errorCode: "400.002.05", errorMessage: "Invalid request payload", errors: parsed.error.flatten() },
|
|
793
|
+
400
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
const result = await runC2BSimulation(c, parsed.data.ShortCode, parsed.data.Amount, parsed.data.Msisdn, parsed.data.BillRefNumber);
|
|
797
|
+
if (result.kind === "no-registration") {
|
|
798
|
+
return c.json(
|
|
799
|
+
{ errorCode: "500.001.1001", errorMessage: "No registered URLs found for this shortcode" },
|
|
800
|
+
500
|
|
801
|
+
);
|
|
802
|
+
}
|
|
803
|
+
return c.json({
|
|
804
|
+
OriginatorCoversationID: result.originatorConversationID,
|
|
805
|
+
ConversationID: result.conversationID,
|
|
806
|
+
ResponseDescription: "Accept the service request successfully."
|
|
807
|
+
});
|
|
808
|
+
});
|
|
809
|
+
app.post("/__mock__/c2b/trigger", async (c) => {
|
|
810
|
+
const raw = await c.req.json().catch(() => null);
|
|
811
|
+
if (!raw) {
|
|
812
|
+
return c.json({ error: "invalid body" }, 400);
|
|
813
|
+
}
|
|
814
|
+
const shortCode = String(raw.ShortCode ?? raw.shortCode ?? "");
|
|
815
|
+
const amount = Number(raw.Amount ?? raw.amount ?? 0);
|
|
816
|
+
const msisdn = String(raw.Msisdn ?? raw.msisdn ?? "");
|
|
817
|
+
const billRef = String(raw.BillRefNumber ?? raw.billRefNumber ?? "TEST");
|
|
818
|
+
if (!shortCode || !amount || !msisdn) {
|
|
819
|
+
return c.json({ error: "ShortCode, Amount, Msisdn required" }, 400);
|
|
820
|
+
}
|
|
821
|
+
const result = await runC2BSimulation(c, shortCode, amount, msisdn, billRef);
|
|
822
|
+
return c.json(result, result.kind === "no-registration" ? 404 : 200);
|
|
823
|
+
});
|
|
824
|
+
return app;
|
|
825
|
+
}
|
|
826
|
+
async function runC2BSimulation(c, shortCode, amount, msisdn, billRef) {
|
|
827
|
+
const reg = registry.get(shortCode);
|
|
828
|
+
if (!reg) return { kind: "no-registration" };
|
|
829
|
+
const conversationID = generateConversationID();
|
|
830
|
+
const originatorConversationID = generateOriginatorConversationID();
|
|
831
|
+
const receipt = generateMpesaReceiptNumber();
|
|
832
|
+
const transactionDate = generateTransactionDate();
|
|
833
|
+
const txn = {
|
|
834
|
+
checkoutRequestID: conversationID,
|
|
835
|
+
merchantRequestID: originatorConversationID,
|
|
836
|
+
conversationID,
|
|
837
|
+
originatorConversationID,
|
|
838
|
+
kind: "c2b",
|
|
839
|
+
amount,
|
|
840
|
+
phoneNumber: msisdn,
|
|
841
|
+
shortCode,
|
|
842
|
+
callbackUrl: reg.confirmationURL,
|
|
843
|
+
state: "pending",
|
|
844
|
+
createdAt: Date.now(),
|
|
845
|
+
callbackAttempts: 0,
|
|
846
|
+
mpesaReceiptNumber: receipt
|
|
847
|
+
};
|
|
848
|
+
c.var.store.put(txn);
|
|
849
|
+
const callbackPayload = {
|
|
850
|
+
TransactionType: "Pay Bill",
|
|
851
|
+
TransID: receipt,
|
|
852
|
+
TransTime: String(transactionDate),
|
|
853
|
+
TransAmount: String(amount),
|
|
854
|
+
BusinessShortCode: shortCode,
|
|
855
|
+
BillRefNumber: billRef,
|
|
856
|
+
InvoiceNumber: "",
|
|
857
|
+
OrgAccountBalance: "0.00",
|
|
858
|
+
ThirdPartyTransID: "",
|
|
859
|
+
MSISDN: msisdn,
|
|
860
|
+
FirstName: "Test",
|
|
861
|
+
MiddleName: "C2B",
|
|
862
|
+
LastName: "Customer"
|
|
863
|
+
};
|
|
864
|
+
let validationOk = true;
|
|
865
|
+
try {
|
|
866
|
+
const res = await fetch(reg.validationURL, {
|
|
867
|
+
method: "POST",
|
|
868
|
+
headers: { "content-type": "application/json" },
|
|
869
|
+
body: JSON.stringify(callbackPayload)
|
|
870
|
+
});
|
|
871
|
+
if (res.ok) {
|
|
872
|
+
const body = await res.json().catch(() => ({}));
|
|
873
|
+
if (body.ResultCode && body.ResultCode !== "0") validationOk = false;
|
|
874
|
+
}
|
|
875
|
+
} catch {
|
|
876
|
+
validationOk = true;
|
|
877
|
+
}
|
|
878
|
+
if (!validationOk) {
|
|
879
|
+
c.var.store.update(conversationID, { state: "user_cancelled", resultCode: 1, resultDesc: "Validation rejected", completedAt: Date.now() });
|
|
880
|
+
return { kind: "rejected", originatorConversationID, conversationID };
|
|
881
|
+
}
|
|
882
|
+
c.var.dispatcher.schedule(
|
|
883
|
+
{
|
|
884
|
+
id: conversationID,
|
|
885
|
+
url: reg.confirmationURL,
|
|
886
|
+
body: callbackPayload,
|
|
887
|
+
scheduledAt: Date.now(),
|
|
888
|
+
attempts: 0,
|
|
889
|
+
maxAttempts: c.var.config.webhookRetry.attempts,
|
|
890
|
+
backoffMs: c.var.config.webhookRetry.backoffMs,
|
|
891
|
+
transactionId: conversationID
|
|
892
|
+
},
|
|
893
|
+
0
|
|
894
|
+
);
|
|
895
|
+
c.var.store.update(conversationID, { state: "success", resultCode: 0, resultDesc: "Success", completedAt: Date.now() });
|
|
896
|
+
return { kind: "delivered", originatorConversationID, conversationID };
|
|
897
|
+
}
|
|
898
|
+
var registry;
|
|
899
|
+
var init_c2b = __esm({
|
|
900
|
+
"src/routes/c2b.ts"() {
|
|
901
|
+
"use strict";
|
|
902
|
+
init_esm_shims();
|
|
903
|
+
init_auth();
|
|
904
|
+
init_schemas();
|
|
905
|
+
init_id_generator();
|
|
906
|
+
registry = /* @__PURE__ */ new Map();
|
|
907
|
+
}
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
// src/routes/b2c.ts
|
|
911
|
+
import { Hono as Hono5 } from "hono";
|
|
912
|
+
function b2cRoute() {
|
|
913
|
+
const app = new Hono5();
|
|
914
|
+
app.post("/mpesa/b2c/v1/paymentrequest", async (c) => {
|
|
915
|
+
const token = parseBearerToken(c.req.header("authorization"));
|
|
916
|
+
if (!token || !isValidToken(token)) {
|
|
917
|
+
return c.json({ errorCode: "404.001.03", errorMessage: "Invalid Access Token" }, 401);
|
|
918
|
+
}
|
|
919
|
+
const raw = await c.req.json().catch(() => null);
|
|
920
|
+
const parsed = b2cSchema.safeParse(raw);
|
|
921
|
+
if (!parsed.success) {
|
|
922
|
+
return c.json(
|
|
923
|
+
{ errorCode: "400.002.05", errorMessage: "Invalid request payload", errors: parsed.error.flatten() },
|
|
924
|
+
400
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
const body = parsed.data;
|
|
928
|
+
const conversationID = generateConversationID();
|
|
929
|
+
const originatorConversationID = generateOriginatorConversationID();
|
|
930
|
+
const scenario = pickScenario(body.PartyB, c.var.config);
|
|
931
|
+
const isSuccess = scenario === "success" || scenario === "slow" || scenario === "callback_retry";
|
|
932
|
+
const record = {
|
|
933
|
+
checkoutRequestID: conversationID,
|
|
934
|
+
merchantRequestID: originatorConversationID,
|
|
935
|
+
conversationID,
|
|
936
|
+
originatorConversationID,
|
|
937
|
+
kind: "b2c",
|
|
938
|
+
amount: body.Amount,
|
|
939
|
+
phoneNumber: body.PartyB,
|
|
940
|
+
shortCode: body.PartyA,
|
|
941
|
+
callbackUrl: body.ResultURL,
|
|
942
|
+
state: isSuccess ? "success" : "system_error",
|
|
943
|
+
createdAt: Date.now(),
|
|
944
|
+
callbackAttempts: 0,
|
|
945
|
+
...isSuccess ? { mpesaReceiptNumber: generateMpesaReceiptNumber() } : {}
|
|
946
|
+
};
|
|
947
|
+
c.var.store.put(record);
|
|
948
|
+
const callback = {
|
|
949
|
+
Result: {
|
|
950
|
+
ResultType: 0,
|
|
951
|
+
ResultCode: isSuccess ? 0 : 2001,
|
|
952
|
+
ResultDesc: isSuccess ? "The service request is processed successfully." : "The initiator information is invalid.",
|
|
953
|
+
OriginatorConversationID: originatorConversationID,
|
|
954
|
+
ConversationID: conversationID,
|
|
955
|
+
TransactionID: record.mpesaReceiptNumber ?? "N/A",
|
|
956
|
+
ResultParameters: {
|
|
957
|
+
ResultParameter: isSuccess ? [
|
|
958
|
+
{ Key: "TransactionAmount", Value: body.Amount },
|
|
959
|
+
{ Key: "TransactionReceipt", Value: record.mpesaReceiptNumber ?? "" },
|
|
960
|
+
{ Key: "B2CRecipientIsRegisteredCustomer", Value: "Y" },
|
|
961
|
+
{ Key: "B2CChargesPaidAccountAvailableFunds", Value: 0 },
|
|
962
|
+
{ Key: "ReceiverPartyPublicName", Value: `${body.PartyB} - Test Recipient` },
|
|
963
|
+
{ Key: "TransactionCompletedDateTime", Value: (/* @__PURE__ */ new Date()).toISOString() },
|
|
964
|
+
{ Key: "B2CUtilityAccountAvailableFunds", Value: 1e6 },
|
|
965
|
+
{ Key: "B2CWorkingAccountAvailableFunds", Value: 1e6 }
|
|
966
|
+
] : []
|
|
967
|
+
},
|
|
968
|
+
ReferenceData: {
|
|
969
|
+
ReferenceItem: { Key: "QueueTimeoutURL", Value: body.QueueTimeOutURL }
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
c.var.dispatcher.schedule(
|
|
974
|
+
{
|
|
975
|
+
id: conversationID,
|
|
976
|
+
url: body.ResultURL,
|
|
977
|
+
body: callback,
|
|
978
|
+
scheduledAt: Date.now() + c.var.config.defaultCallbackDelayMs,
|
|
979
|
+
attempts: 0,
|
|
980
|
+
maxAttempts: c.var.config.webhookRetry.attempts,
|
|
981
|
+
backoffMs: c.var.config.webhookRetry.backoffMs,
|
|
982
|
+
transactionId: conversationID
|
|
983
|
+
},
|
|
984
|
+
c.var.config.defaultCallbackDelayMs
|
|
985
|
+
);
|
|
986
|
+
return c.json({
|
|
987
|
+
ConversationID: conversationID,
|
|
988
|
+
OriginatorConversationID: originatorConversationID,
|
|
989
|
+
ResponseCode: "0",
|
|
990
|
+
ResponseDescription: "Accept the service request successfully."
|
|
991
|
+
});
|
|
992
|
+
});
|
|
993
|
+
return app;
|
|
994
|
+
}
|
|
995
|
+
var init_b2c = __esm({
|
|
996
|
+
"src/routes/b2c.ts"() {
|
|
997
|
+
"use strict";
|
|
998
|
+
init_esm_shims();
|
|
999
|
+
init_auth();
|
|
1000
|
+
init_schemas();
|
|
1001
|
+
init_id_generator();
|
|
1002
|
+
init_failure_injector();
|
|
1003
|
+
}
|
|
1004
|
+
});
|
|
1005
|
+
|
|
1006
|
+
// src/routes/b2b.ts
|
|
1007
|
+
import { Hono as Hono6 } from "hono";
|
|
1008
|
+
function b2bRoute() {
|
|
1009
|
+
const app = new Hono6();
|
|
1010
|
+
app.post("/mpesa/b2b/v1/paymentrequest", async (c) => {
|
|
1011
|
+
const token = parseBearerToken(c.req.header("authorization"));
|
|
1012
|
+
if (!token || !isValidToken(token)) {
|
|
1013
|
+
return c.json({ errorCode: "404.001.03", errorMessage: "Invalid Access Token" }, 401);
|
|
1014
|
+
}
|
|
1015
|
+
const raw = await c.req.json().catch(() => null);
|
|
1016
|
+
const parsed = b2bSchema.safeParse(raw);
|
|
1017
|
+
if (!parsed.success) {
|
|
1018
|
+
return c.json(
|
|
1019
|
+
{ errorCode: "400.002.05", errorMessage: "Invalid request payload", errors: parsed.error.flatten() },
|
|
1020
|
+
400
|
|
1021
|
+
);
|
|
1022
|
+
}
|
|
1023
|
+
const body = parsed.data;
|
|
1024
|
+
const conversationID = generateConversationID();
|
|
1025
|
+
const originatorConversationID = generateOriginatorConversationID();
|
|
1026
|
+
const receipt = generateMpesaReceiptNumber();
|
|
1027
|
+
const record = {
|
|
1028
|
+
checkoutRequestID: conversationID,
|
|
1029
|
+
merchantRequestID: originatorConversationID,
|
|
1030
|
+
conversationID,
|
|
1031
|
+
originatorConversationID,
|
|
1032
|
+
kind: "b2b",
|
|
1033
|
+
amount: body.Amount,
|
|
1034
|
+
phoneNumber: body.PartyB,
|
|
1035
|
+
shortCode: body.PartyA,
|
|
1036
|
+
callbackUrl: body.ResultURL,
|
|
1037
|
+
state: "success",
|
|
1038
|
+
createdAt: Date.now(),
|
|
1039
|
+
callbackAttempts: 0,
|
|
1040
|
+
mpesaReceiptNumber: receipt
|
|
1041
|
+
};
|
|
1042
|
+
c.var.store.put(record);
|
|
1043
|
+
const callback = {
|
|
1044
|
+
Result: {
|
|
1045
|
+
ResultType: 0,
|
|
1046
|
+
ResultCode: 0,
|
|
1047
|
+
ResultDesc: "The service request is processed successfully.",
|
|
1048
|
+
OriginatorConversationID: originatorConversationID,
|
|
1049
|
+
ConversationID: conversationID,
|
|
1050
|
+
TransactionID: receipt,
|
|
1051
|
+
ResultParameters: {
|
|
1052
|
+
ResultParameter: [
|
|
1053
|
+
{ Key: "DebitAccountBalance", Value: "Working Account|KES|1000000.00|1000000.00|0.00|0.00" },
|
|
1054
|
+
{ Key: "Amount", Value: body.Amount },
|
|
1055
|
+
{ Key: "DebitPartyAffectedAccountBalance", Value: "Working Account|KES|1000000.00|1000000.00|0.00|0.00" },
|
|
1056
|
+
{ Key: "TransCompletedTime", Value: (/* @__PURE__ */ new Date()).toISOString() },
|
|
1057
|
+
{ Key: "DebitPartyCharges", Value: "" },
|
|
1058
|
+
{ Key: "ReceiverPartyPublicName", Value: `${body.PartyB} - Test Business` },
|
|
1059
|
+
{ Key: "Currency", Value: "KES" },
|
|
1060
|
+
{ Key: "InitiatorAccountCurrentBalance", Value: "{Amount={CurrencyCode=KES, MinimumAmount=99999900, BasicAmount=999999.00}}" }
|
|
1061
|
+
]
|
|
1062
|
+
},
|
|
1063
|
+
ReferenceData: {
|
|
1064
|
+
ReferenceItem: [
|
|
1065
|
+
{ Key: "BillReferenceNumber", Value: body.AccountReference ?? "" },
|
|
1066
|
+
{ Key: "QueueTimeoutURL", Value: body.QueueTimeOutURL }
|
|
1067
|
+
]
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
};
|
|
1071
|
+
c.var.dispatcher.schedule(
|
|
1072
|
+
{
|
|
1073
|
+
id: conversationID,
|
|
1074
|
+
url: body.ResultURL,
|
|
1075
|
+
body: callback,
|
|
1076
|
+
scheduledAt: Date.now() + c.var.config.defaultCallbackDelayMs,
|
|
1077
|
+
attempts: 0,
|
|
1078
|
+
maxAttempts: c.var.config.webhookRetry.attempts,
|
|
1079
|
+
backoffMs: c.var.config.webhookRetry.backoffMs,
|
|
1080
|
+
transactionId: conversationID
|
|
1081
|
+
},
|
|
1082
|
+
c.var.config.defaultCallbackDelayMs
|
|
1083
|
+
);
|
|
1084
|
+
return c.json({
|
|
1085
|
+
ConversationID: conversationID,
|
|
1086
|
+
OriginatorConversationID: originatorConversationID,
|
|
1087
|
+
ResponseCode: "0",
|
|
1088
|
+
ResponseDescription: "Accept the service request successfully."
|
|
1089
|
+
});
|
|
1090
|
+
});
|
|
1091
|
+
return app;
|
|
1092
|
+
}
|
|
1093
|
+
var init_b2b = __esm({
|
|
1094
|
+
"src/routes/b2b.ts"() {
|
|
1095
|
+
"use strict";
|
|
1096
|
+
init_esm_shims();
|
|
1097
|
+
init_auth();
|
|
1098
|
+
init_schemas();
|
|
1099
|
+
init_id_generator();
|
|
1100
|
+
}
|
|
1101
|
+
});
|
|
1102
|
+
|
|
1103
|
+
// src/routes/transaction-status.ts
|
|
1104
|
+
import { Hono as Hono7 } from "hono";
|
|
1105
|
+
function transactionStatusRoute() {
|
|
1106
|
+
const app = new Hono7();
|
|
1107
|
+
app.post("/mpesa/transactionstatus/v1/query", async (c) => {
|
|
1108
|
+
const token = parseBearerToken(c.req.header("authorization"));
|
|
1109
|
+
if (!token || !isValidToken(token)) {
|
|
1110
|
+
return c.json({ errorCode: "404.001.03", errorMessage: "Invalid Access Token" }, 401);
|
|
1111
|
+
}
|
|
1112
|
+
const raw = await c.req.json().catch(() => null);
|
|
1113
|
+
const parsed = transactionStatusSchema.safeParse(raw);
|
|
1114
|
+
if (!parsed.success) {
|
|
1115
|
+
return c.json(
|
|
1116
|
+
{ errorCode: "400.002.05", errorMessage: "Invalid request payload", errors: parsed.error.flatten() },
|
|
1117
|
+
400
|
|
1118
|
+
);
|
|
1119
|
+
}
|
|
1120
|
+
const body = parsed.data;
|
|
1121
|
+
const conversationID = generateConversationID();
|
|
1122
|
+
const originatorConversationID = generateOriginatorConversationID();
|
|
1123
|
+
const existing = c.var.store.get(body.TransactionID);
|
|
1124
|
+
const callback = {
|
|
1125
|
+
Result: {
|
|
1126
|
+
ResultType: 0,
|
|
1127
|
+
ResultCode: 0,
|
|
1128
|
+
ResultDesc: "The service request is processed successfully.",
|
|
1129
|
+
OriginatorConversationID: originatorConversationID,
|
|
1130
|
+
ConversationID: conversationID,
|
|
1131
|
+
TransactionID: body.TransactionID,
|
|
1132
|
+
ResultParameters: {
|
|
1133
|
+
ResultParameter: [
|
|
1134
|
+
{ Key: "ReceiptNo", Value: existing?.mpesaReceiptNumber ?? body.TransactionID },
|
|
1135
|
+
{ Key: "ConversationID", Value: existing?.conversationID ?? conversationID },
|
|
1136
|
+
{ Key: "FinalisedTime", Value: (/* @__PURE__ */ new Date()).toISOString() },
|
|
1137
|
+
{ Key: "Amount", Value: existing?.amount ?? 0 },
|
|
1138
|
+
{ Key: "TransactionStatus", Value: existing?.state === "success" ? "Completed" : "Failed" },
|
|
1139
|
+
{ Key: "ReasonType", Value: "Salary Payment via API" },
|
|
1140
|
+
{ Key: "TransactionReason", Value: body.Remarks },
|
|
1141
|
+
{ Key: "DebitPartyCharges", Value: "" },
|
|
1142
|
+
{ Key: "DebitAccountType", Value: "Utility Account" },
|
|
1143
|
+
{ Key: "InitiatedTime", Value: (/* @__PURE__ */ new Date()).toISOString() },
|
|
1144
|
+
{ Key: "OriginatorConversationID", Value: existing?.originatorConversationID ?? originatorConversationID },
|
|
1145
|
+
{ Key: "CreditPartyName", Value: existing ? `${existing.phoneNumber} - Test Recipient` : "Test Recipient" },
|
|
1146
|
+
{ Key: "DebitPartyName", Value: existing ? `${existing.shortCode} - Test Merchant` : "Test Merchant" }
|
|
1147
|
+
]
|
|
1148
|
+
},
|
|
1149
|
+
ReferenceData: {
|
|
1150
|
+
ReferenceItem: { Key: "Occasion", Value: body.Occasion ?? "" }
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
c.var.dispatcher.schedule(
|
|
1155
|
+
{
|
|
1156
|
+
id: conversationID,
|
|
1157
|
+
url: body.ResultURL,
|
|
1158
|
+
body: callback,
|
|
1159
|
+
scheduledAt: Date.now() + c.var.config.defaultCallbackDelayMs,
|
|
1160
|
+
attempts: 0,
|
|
1161
|
+
maxAttempts: c.var.config.webhookRetry.attempts,
|
|
1162
|
+
backoffMs: c.var.config.webhookRetry.backoffMs
|
|
1163
|
+
},
|
|
1164
|
+
c.var.config.defaultCallbackDelayMs
|
|
1165
|
+
);
|
|
1166
|
+
return c.json({
|
|
1167
|
+
OriginatorConversationID: originatorConversationID,
|
|
1168
|
+
ConversationID: conversationID,
|
|
1169
|
+
ResponseCode: "0",
|
|
1170
|
+
ResponseDescription: "Accept the service request successfully."
|
|
1171
|
+
});
|
|
1172
|
+
});
|
|
1173
|
+
return app;
|
|
1174
|
+
}
|
|
1175
|
+
var init_transaction_status = __esm({
|
|
1176
|
+
"src/routes/transaction-status.ts"() {
|
|
1177
|
+
"use strict";
|
|
1178
|
+
init_esm_shims();
|
|
1179
|
+
init_auth();
|
|
1180
|
+
init_schemas();
|
|
1181
|
+
init_id_generator();
|
|
1182
|
+
}
|
|
1183
|
+
});
|
|
1184
|
+
|
|
1185
|
+
// src/routes/account-balance.ts
|
|
1186
|
+
import { Hono as Hono8 } from "hono";
|
|
1187
|
+
function accountBalanceRoute() {
|
|
1188
|
+
const app = new Hono8();
|
|
1189
|
+
app.post("/mpesa/accountbalance/v1/query", async (c) => {
|
|
1190
|
+
const token = parseBearerToken(c.req.header("authorization"));
|
|
1191
|
+
if (!token || !isValidToken(token)) {
|
|
1192
|
+
return c.json({ errorCode: "404.001.03", errorMessage: "Invalid Access Token" }, 401);
|
|
1193
|
+
}
|
|
1194
|
+
const raw = await c.req.json().catch(() => null);
|
|
1195
|
+
const parsed = accountBalanceSchema.safeParse(raw);
|
|
1196
|
+
if (!parsed.success) {
|
|
1197
|
+
return c.json(
|
|
1198
|
+
{ errorCode: "400.002.05", errorMessage: "Invalid request payload", errors: parsed.error.flatten() },
|
|
1199
|
+
400
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1202
|
+
const body = parsed.data;
|
|
1203
|
+
const conversationID = generateConversationID();
|
|
1204
|
+
const originatorConversationID = generateOriginatorConversationID();
|
|
1205
|
+
const callback = {
|
|
1206
|
+
Result: {
|
|
1207
|
+
ResultType: 0,
|
|
1208
|
+
ResultCode: 0,
|
|
1209
|
+
ResultDesc: "The service request is processed successfully.",
|
|
1210
|
+
OriginatorConversationID: originatorConversationID,
|
|
1211
|
+
ConversationID: conversationID,
|
|
1212
|
+
TransactionID: "BALANCE-QUERY",
|
|
1213
|
+
ResultParameters: {
|
|
1214
|
+
ResultParameter: [
|
|
1215
|
+
{ Key: "AccountBalance", Value: "Working Account|KES|1000000.00|1000000.00|0.00|0.00&Float Account|KES|0.00|0.00|0.00|0.00&Utility Account|KES|1000000.00|1000000.00|0.00|0.00&Charges Paid Account|KES|0.00|0.00|0.00|0.00&Organization Settlement Account|KES|0.00|0.00|0.00|0.00" },
|
|
1216
|
+
{ Key: "BOCompletedTime", Value: (/* @__PURE__ */ new Date()).toISOString() }
|
|
1217
|
+
]
|
|
1218
|
+
},
|
|
1219
|
+
ReferenceData: {
|
|
1220
|
+
ReferenceItem: { Key: "QueueTimeoutURL", Value: body.QueueTimeOutURL }
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
};
|
|
1224
|
+
c.var.dispatcher.schedule(
|
|
1225
|
+
{
|
|
1226
|
+
id: conversationID,
|
|
1227
|
+
url: body.ResultURL,
|
|
1228
|
+
body: callback,
|
|
1229
|
+
scheduledAt: Date.now() + c.var.config.defaultCallbackDelayMs,
|
|
1230
|
+
attempts: 0,
|
|
1231
|
+
maxAttempts: c.var.config.webhookRetry.attempts,
|
|
1232
|
+
backoffMs: c.var.config.webhookRetry.backoffMs
|
|
1233
|
+
},
|
|
1234
|
+
c.var.config.defaultCallbackDelayMs
|
|
1235
|
+
);
|
|
1236
|
+
return c.json({
|
|
1237
|
+
OriginatorConversationID: originatorConversationID,
|
|
1238
|
+
ConversationID: conversationID,
|
|
1239
|
+
ResponseCode: "0",
|
|
1240
|
+
ResponseDescription: "Accept the service request successfully."
|
|
1241
|
+
});
|
|
1242
|
+
});
|
|
1243
|
+
return app;
|
|
1244
|
+
}
|
|
1245
|
+
var init_account_balance = __esm({
|
|
1246
|
+
"src/routes/account-balance.ts"() {
|
|
1247
|
+
"use strict";
|
|
1248
|
+
init_esm_shims();
|
|
1249
|
+
init_auth();
|
|
1250
|
+
init_schemas();
|
|
1251
|
+
init_id_generator();
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
|
|
1255
|
+
// src/routes/reversal.ts
|
|
1256
|
+
import { Hono as Hono9 } from "hono";
|
|
1257
|
+
function reversalRoute() {
|
|
1258
|
+
const app = new Hono9();
|
|
1259
|
+
app.post("/mpesa/reversal/v1/request", async (c) => {
|
|
1260
|
+
const token = parseBearerToken(c.req.header("authorization"));
|
|
1261
|
+
if (!token || !isValidToken(token)) {
|
|
1262
|
+
return c.json({ errorCode: "404.001.03", errorMessage: "Invalid Access Token" }, 401);
|
|
1263
|
+
}
|
|
1264
|
+
const raw = await c.req.json().catch(() => null);
|
|
1265
|
+
const parsed = reversalSchema.safeParse(raw);
|
|
1266
|
+
if (!parsed.success) {
|
|
1267
|
+
return c.json(
|
|
1268
|
+
{ errorCode: "400.002.05", errorMessage: "Invalid request payload", errors: parsed.error.flatten() },
|
|
1269
|
+
400
|
|
1270
|
+
);
|
|
1271
|
+
}
|
|
1272
|
+
const body = parsed.data;
|
|
1273
|
+
const conversationID = generateConversationID();
|
|
1274
|
+
const originatorConversationID = generateOriginatorConversationID();
|
|
1275
|
+
const callback = {
|
|
1276
|
+
Result: {
|
|
1277
|
+
ResultType: 0,
|
|
1278
|
+
ResultCode: 0,
|
|
1279
|
+
ResultDesc: "The service request is processed successfully.",
|
|
1280
|
+
OriginatorConversationID: originatorConversationID,
|
|
1281
|
+
ConversationID: conversationID,
|
|
1282
|
+
TransactionID: body.TransactionID,
|
|
1283
|
+
ResultParameters: {
|
|
1284
|
+
ResultParameter: [
|
|
1285
|
+
{ Key: "DebitAccountBalance", Value: "Working Account|KES|1000000.00|1000000.00|0.00|0.00" },
|
|
1286
|
+
{ Key: "Amount", Value: body.Amount },
|
|
1287
|
+
{ Key: "TransCompletedTime", Value: (/* @__PURE__ */ new Date()).toISOString() },
|
|
1288
|
+
{ Key: "OriginalTransactionID", Value: body.TransactionID },
|
|
1289
|
+
{ Key: "Charge", Value: 0 },
|
|
1290
|
+
{ Key: "CreditPartyPublicName", Value: `${body.ReceiverParty} - Test Recipient` },
|
|
1291
|
+
{ Key: "DebitPartyPublicName", Value: "Test Merchant" }
|
|
1292
|
+
]
|
|
1293
|
+
},
|
|
1294
|
+
ReferenceData: {
|
|
1295
|
+
ReferenceItem: { Key: "QueueTimeoutURL", Value: body.QueueTimeOutURL }
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
};
|
|
1299
|
+
c.var.dispatcher.schedule(
|
|
1300
|
+
{
|
|
1301
|
+
id: conversationID,
|
|
1302
|
+
url: body.ResultURL,
|
|
1303
|
+
body: callback,
|
|
1304
|
+
scheduledAt: Date.now() + c.var.config.defaultCallbackDelayMs,
|
|
1305
|
+
attempts: 0,
|
|
1306
|
+
maxAttempts: c.var.config.webhookRetry.attempts,
|
|
1307
|
+
backoffMs: c.var.config.webhookRetry.backoffMs
|
|
1308
|
+
},
|
|
1309
|
+
c.var.config.defaultCallbackDelayMs
|
|
1310
|
+
);
|
|
1311
|
+
return c.json({
|
|
1312
|
+
OriginatorConversationID: originatorConversationID,
|
|
1313
|
+
ConversationID: conversationID,
|
|
1314
|
+
ResponseCode: "0",
|
|
1315
|
+
ResponseDescription: "Accept the service request successfully."
|
|
1316
|
+
});
|
|
1317
|
+
});
|
|
1318
|
+
return app;
|
|
1319
|
+
}
|
|
1320
|
+
var init_reversal = __esm({
|
|
1321
|
+
"src/routes/reversal.ts"() {
|
|
1322
|
+
"use strict";
|
|
1323
|
+
init_esm_shims();
|
|
1324
|
+
init_auth();
|
|
1325
|
+
init_schemas();
|
|
1326
|
+
init_id_generator();
|
|
1327
|
+
}
|
|
1328
|
+
});
|
|
1329
|
+
|
|
1330
|
+
// src/routes/dashboard.ts
|
|
1331
|
+
import { Hono as Hono10 } from "hono";
|
|
1332
|
+
import { streamSSE } from "hono/streaming";
|
|
1333
|
+
function dashboardRoute() {
|
|
1334
|
+
const app = new Hono10();
|
|
1335
|
+
app.get("/__mock__/dashboard", (c) => c.html(DASHBOARD_HTML));
|
|
1336
|
+
app.get("/__mock__/state", (c) => {
|
|
1337
|
+
const transactions = c.var.store.list(100);
|
|
1338
|
+
return c.json({
|
|
1339
|
+
transactions,
|
|
1340
|
+
pendingCallbacks: c.var.dispatcher.pendingIds().length
|
|
1341
|
+
});
|
|
1342
|
+
});
|
|
1343
|
+
app.get("/__mock__/events", (c) => {
|
|
1344
|
+
return streamSSE(c, async (stream) => {
|
|
1345
|
+
const send = async () => {
|
|
1346
|
+
await stream.writeSSE({
|
|
1347
|
+
data: JSON.stringify({
|
|
1348
|
+
transactions: c.var.store.list(100),
|
|
1349
|
+
pendingCallbacks: c.var.dispatcher.pendingIds().length
|
|
1350
|
+
})
|
|
1351
|
+
});
|
|
1352
|
+
};
|
|
1353
|
+
await send();
|
|
1354
|
+
const onChange = () => {
|
|
1355
|
+
void send();
|
|
1356
|
+
};
|
|
1357
|
+
c.var.store.on("change", onChange);
|
|
1358
|
+
c.var.store.on("clear", onChange);
|
|
1359
|
+
const heartbeat = setInterval(() => {
|
|
1360
|
+
void send();
|
|
1361
|
+
}, 5e3);
|
|
1362
|
+
try {
|
|
1363
|
+
while (true) {
|
|
1364
|
+
await stream.sleep(1e3);
|
|
1365
|
+
}
|
|
1366
|
+
} finally {
|
|
1367
|
+
clearInterval(heartbeat);
|
|
1368
|
+
c.var.store.off("change", onChange);
|
|
1369
|
+
c.var.store.off("clear", onChange);
|
|
1370
|
+
}
|
|
1371
|
+
});
|
|
1372
|
+
});
|
|
1373
|
+
app.post("/__mock__/clear", (c) => {
|
|
1374
|
+
c.var.store.clear();
|
|
1375
|
+
return c.json({ cleared: true });
|
|
1376
|
+
});
|
|
1377
|
+
return app;
|
|
1378
|
+
}
|
|
1379
|
+
var DASHBOARD_HTML;
|
|
1380
|
+
var init_dashboard = __esm({
|
|
1381
|
+
"src/routes/dashboard.ts"() {
|
|
1382
|
+
"use strict";
|
|
1383
|
+
init_esm_shims();
|
|
1384
|
+
DASHBOARD_HTML = `<!doctype html>
|
|
1385
|
+
<html lang="en">
|
|
1386
|
+
<head>
|
|
1387
|
+
<meta charset="utf-8" />
|
|
1388
|
+
<title>mpesa-mock dashboard</title>
|
|
1389
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
1390
|
+
<script src="https://cdn.tailwindcss.com"></script>
|
|
1391
|
+
</head>
|
|
1392
|
+
<body class="bg-slate-950 text-slate-100 font-mono min-h-screen">
|
|
1393
|
+
<div class="max-w-6xl mx-auto p-6">
|
|
1394
|
+
<header class="flex items-center justify-between mb-6">
|
|
1395
|
+
<div>
|
|
1396
|
+
<h1 class="text-2xl font-bold">mpesa-mock <span class="text-emerald-400">\u25CF</span></h1>
|
|
1397
|
+
<p class="text-slate-400 text-sm">Local M-Pesa Daraja emulator \u2014 live transactions</p>
|
|
1398
|
+
</div>
|
|
1399
|
+
<div class="text-right text-xs text-slate-500">
|
|
1400
|
+
<div id="health">checking\u2026</div>
|
|
1401
|
+
<div>SSE: <span id="sse-status">connecting</span></div>
|
|
1402
|
+
</div>
|
|
1403
|
+
</header>
|
|
1404
|
+
|
|
1405
|
+
<section class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
|
|
1406
|
+
<div class="bg-slate-900 rounded-lg p-4 border border-slate-800">
|
|
1407
|
+
<div class="text-xs text-slate-500 uppercase">Transactions</div>
|
|
1408
|
+
<div id="count-total" class="text-3xl font-bold">0</div>
|
|
1409
|
+
</div>
|
|
1410
|
+
<div class="bg-slate-900 rounded-lg p-4 border border-slate-800">
|
|
1411
|
+
<div class="text-xs text-slate-500 uppercase">Pending callbacks</div>
|
|
1412
|
+
<div id="count-pending" class="text-3xl font-bold text-amber-300">0</div>
|
|
1413
|
+
</div>
|
|
1414
|
+
<div class="bg-slate-900 rounded-lg p-4 border border-slate-800">
|
|
1415
|
+
<div class="text-xs text-slate-500 uppercase">Delivered</div>
|
|
1416
|
+
<div id="count-delivered" class="text-3xl font-bold text-emerald-300">0</div>
|
|
1417
|
+
</div>
|
|
1418
|
+
</section>
|
|
1419
|
+
|
|
1420
|
+
<section class="bg-slate-900 rounded-lg border border-slate-800 overflow-hidden">
|
|
1421
|
+
<table class="w-full text-sm">
|
|
1422
|
+
<thead class="bg-slate-800 text-slate-400 text-xs uppercase">
|
|
1423
|
+
<tr>
|
|
1424
|
+
<th class="text-left p-3">Kind</th>
|
|
1425
|
+
<th class="text-left p-3">CheckoutRequestID</th>
|
|
1426
|
+
<th class="text-left p-3">Phone</th>
|
|
1427
|
+
<th class="text-right p-3">Amount</th>
|
|
1428
|
+
<th class="text-left p-3">State</th>
|
|
1429
|
+
<th class="text-right p-3">Cb attempts</th>
|
|
1430
|
+
<th class="text-right p-3">Age</th>
|
|
1431
|
+
</tr>
|
|
1432
|
+
</thead>
|
|
1433
|
+
<tbody id="rows"></tbody>
|
|
1434
|
+
</table>
|
|
1435
|
+
</section>
|
|
1436
|
+
|
|
1437
|
+
<footer class="text-center text-slate-600 text-xs mt-8">
|
|
1438
|
+
mpesa-mock \u2014 not affiliated with Safaricom PLC
|
|
1439
|
+
</footer>
|
|
1440
|
+
</div>
|
|
1441
|
+
|
|
1442
|
+
<script>
|
|
1443
|
+
const stateColors = {
|
|
1444
|
+
success: 'text-emerald-300',
|
|
1445
|
+
pending: 'text-amber-300',
|
|
1446
|
+
user_cancelled: 'text-rose-300',
|
|
1447
|
+
insufficient_funds: 'text-rose-400',
|
|
1448
|
+
wrong_pin: 'text-rose-400',
|
|
1449
|
+
expired: 'text-rose-400',
|
|
1450
|
+
system_error: 'text-rose-500',
|
|
1451
|
+
timeout: 'text-slate-400',
|
|
1452
|
+
};
|
|
1453
|
+
|
|
1454
|
+
function renderRow(t) {
|
|
1455
|
+
const age = Math.round((Date.now() - t.createdAt) / 1000);
|
|
1456
|
+
const colorCls = stateColors[t.state] ?? 'text-slate-200';
|
|
1457
|
+
return \`<tr class="border-t border-slate-800 hover:bg-slate-800/50">
|
|
1458
|
+
<td class="p-3 uppercase text-xs text-slate-400">\${t.kind}</td>
|
|
1459
|
+
<td class="p-3 text-xs">\${t.checkoutRequestID}</td>
|
|
1460
|
+
<td class="p-3">\${t.phoneNumber}</td>
|
|
1461
|
+
<td class="p-3 text-right">\${t.amount.toLocaleString()}</td>
|
|
1462
|
+
<td class="p-3 \${colorCls}">\${t.state}</td>
|
|
1463
|
+
<td class="p-3 text-right">\${t.callbackAttempts}</td>
|
|
1464
|
+
<td class="p-3 text-right text-slate-500">\${age}s</td>
|
|
1465
|
+
</tr>\`;
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
function refresh(data) {
|
|
1469
|
+
const txns = data.transactions ?? [];
|
|
1470
|
+
document.getElementById('count-total').textContent = txns.length;
|
|
1471
|
+
document.getElementById('count-pending').textContent = data.pendingCallbacks ?? 0;
|
|
1472
|
+
document.getElementById('count-delivered').textContent = txns.filter(t => t.callbackDeliveredAt).length;
|
|
1473
|
+
document.getElementById('rows').innerHTML = txns.slice(0, 50).map(renderRow).join('');
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
fetch('/__mock__/health').then(r => r.json()).then(d => {
|
|
1477
|
+
document.getElementById('health').textContent = 'up \xB7 ' + Math.round(d.uptime) + 's';
|
|
1478
|
+
});
|
|
1479
|
+
|
|
1480
|
+
fetch('/__mock__/state').then(r => r.json()).then(refresh);
|
|
1481
|
+
|
|
1482
|
+
const es = new EventSource('/__mock__/events');
|
|
1483
|
+
es.onopen = () => { document.getElementById('sse-status').textContent = 'live'; };
|
|
1484
|
+
es.onerror = () => { document.getElementById('sse-status').textContent = 'disconnected'; };
|
|
1485
|
+
es.onmessage = (e) => { try { refresh(JSON.parse(e.data)); } catch {} };
|
|
1486
|
+
</script>
|
|
1487
|
+
</body>
|
|
1488
|
+
</html>`;
|
|
1489
|
+
}
|
|
1490
|
+
});
|
|
1491
|
+
|
|
1492
|
+
// src/server.ts
|
|
1493
|
+
var server_exports = {};
|
|
1494
|
+
__export(server_exports, {
|
|
1495
|
+
createServer: () => createServer,
|
|
1496
|
+
defaultConfig: () => defaultConfig
|
|
1497
|
+
});
|
|
1498
|
+
import { Hono as Hono11 } from "hono";
|
|
1499
|
+
import { logger as honoLogger } from "hono/logger";
|
|
1500
|
+
function defaultConfig(overrides = {}) {
|
|
1501
|
+
return {
|
|
1502
|
+
defaultCallbackDelayMs: overrides.defaultCallbackDelayMs ?? DEFAULTS.callbackDelayMs,
|
|
1503
|
+
scenarios: overrides.scenarios ?? {},
|
|
1504
|
+
webhookRetry: {
|
|
1505
|
+
attempts: overrides.webhookRetry?.attempts ?? DEFAULTS.callbackRetryAttempts,
|
|
1506
|
+
backoffMs: overrides.webhookRetry?.backoffMs ?? DEFAULTS.callbackRetryBackoffMs
|
|
1507
|
+
}
|
|
1508
|
+
};
|
|
1509
|
+
}
|
|
1510
|
+
function createServer(opts = {}) {
|
|
1511
|
+
const store = opts.store ?? new InMemoryStore();
|
|
1512
|
+
const config = defaultConfig(opts.config);
|
|
1513
|
+
const log = opts.quiet ? void 0 : (msg) => console.log(msg);
|
|
1514
|
+
const dispatcher = new WebhookDispatcher({ store, onLog: log });
|
|
1515
|
+
const app = new Hono11();
|
|
1516
|
+
if (!opts.quiet) {
|
|
1517
|
+
app.use("*", honoLogger((msg) => console.log(msg)));
|
|
1518
|
+
}
|
|
1519
|
+
app.use("*", async (c, next) => {
|
|
1520
|
+
c.set("store", store);
|
|
1521
|
+
c.set("dispatcher", dispatcher);
|
|
1522
|
+
c.set("config", config);
|
|
1523
|
+
if (log) c.set("log", log);
|
|
1524
|
+
if (opts.recorder) c.set("recorder", opts.recorder);
|
|
1525
|
+
await next();
|
|
1526
|
+
});
|
|
1527
|
+
app.get("/", (c) => c.json({ name: "mpesa-mock", status: "ok" }));
|
|
1528
|
+
app.get("/__mock__/health", (c) => c.json({ status: "ok", uptime: process.uptime() }));
|
|
1529
|
+
app.route("/", oauthRoute());
|
|
1530
|
+
app.route("/", stkPushRoute());
|
|
1531
|
+
app.route("/", stkQueryRoute());
|
|
1532
|
+
app.route("/", c2bRoute());
|
|
1533
|
+
app.route("/", b2cRoute());
|
|
1534
|
+
app.route("/", b2bRoute());
|
|
1535
|
+
app.route("/", transactionStatusRoute());
|
|
1536
|
+
app.route("/", accountBalanceRoute());
|
|
1537
|
+
app.route("/", reversalRoute());
|
|
1538
|
+
app.route("/", dashboardRoute());
|
|
1539
|
+
app.notFound(
|
|
1540
|
+
(c) => c.json(
|
|
1541
|
+
{
|
|
1542
|
+
errorCode: "404.000.01",
|
|
1543
|
+
errorMessage: `Not found: ${c.req.method} ${c.req.path}`
|
|
1544
|
+
},
|
|
1545
|
+
404
|
|
1546
|
+
)
|
|
1547
|
+
);
|
|
1548
|
+
return { app, store, dispatcher, config };
|
|
1549
|
+
}
|
|
1550
|
+
var init_server = __esm({
|
|
1551
|
+
"src/server.ts"() {
|
|
1552
|
+
"use strict";
|
|
1553
|
+
init_esm_shims();
|
|
1554
|
+
init_transactions();
|
|
1555
|
+
init_webhook_dispatcher();
|
|
1556
|
+
init_oauth();
|
|
1557
|
+
init_stk_push();
|
|
1558
|
+
init_stk_query();
|
|
1559
|
+
init_c2b();
|
|
1560
|
+
init_b2c();
|
|
1561
|
+
init_b2b();
|
|
1562
|
+
init_transaction_status();
|
|
1563
|
+
init_account_balance();
|
|
1564
|
+
init_reversal();
|
|
1565
|
+
init_dashboard();
|
|
1566
|
+
init_defaults();
|
|
1567
|
+
}
|
|
1568
|
+
});
|
|
1569
|
+
|
|
1570
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/util.js
|
|
1571
|
+
var require_util = __commonJS({
|
|
1572
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/util.js"(exports) {
|
|
1573
|
+
"use strict";
|
|
1574
|
+
init_esm_shims();
|
|
1575
|
+
exports.getBooleanOption = (options, key) => {
|
|
1576
|
+
let value = false;
|
|
1577
|
+
if (key in options && typeof (value = options[key]) !== "boolean") {
|
|
1578
|
+
throw new TypeError(`Expected the "${key}" option to be a boolean`);
|
|
1579
|
+
}
|
|
1580
|
+
return value;
|
|
1581
|
+
};
|
|
1582
|
+
exports.cppdb = /* @__PURE__ */ Symbol();
|
|
1583
|
+
exports.inspect = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
|
|
1584
|
+
}
|
|
1585
|
+
});
|
|
1586
|
+
|
|
1587
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/sqlite-error.js
|
|
1588
|
+
var require_sqlite_error = __commonJS({
|
|
1589
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/sqlite-error.js"(exports, module) {
|
|
1590
|
+
"use strict";
|
|
1591
|
+
init_esm_shims();
|
|
1592
|
+
var descriptor = { value: "SqliteError", writable: true, enumerable: false, configurable: true };
|
|
1593
|
+
function SqliteError(message, code) {
|
|
1594
|
+
if (new.target !== SqliteError) {
|
|
1595
|
+
return new SqliteError(message, code);
|
|
1596
|
+
}
|
|
1597
|
+
if (typeof code !== "string") {
|
|
1598
|
+
throw new TypeError("Expected second argument to be a string");
|
|
1599
|
+
}
|
|
1600
|
+
Error.call(this, message);
|
|
1601
|
+
descriptor.value = "" + message;
|
|
1602
|
+
Object.defineProperty(this, "message", descriptor);
|
|
1603
|
+
Error.captureStackTrace(this, SqliteError);
|
|
1604
|
+
this.code = code;
|
|
1605
|
+
}
|
|
1606
|
+
Object.setPrototypeOf(SqliteError, Error);
|
|
1607
|
+
Object.setPrototypeOf(SqliteError.prototype, Error.prototype);
|
|
1608
|
+
Object.defineProperty(SqliteError.prototype, "name", descriptor);
|
|
1609
|
+
module.exports = SqliteError;
|
|
1610
|
+
}
|
|
1611
|
+
});
|
|
1612
|
+
|
|
1613
|
+
// node_modules/.pnpm/file-uri-to-path@1.0.0/node_modules/file-uri-to-path/index.js
|
|
1614
|
+
var require_file_uri_to_path = __commonJS({
|
|
1615
|
+
"node_modules/.pnpm/file-uri-to-path@1.0.0/node_modules/file-uri-to-path/index.js"(exports, module) {
|
|
1616
|
+
"use strict";
|
|
1617
|
+
init_esm_shims();
|
|
1618
|
+
var sep = __require("path").sep || "/";
|
|
1619
|
+
module.exports = fileUriToPath;
|
|
1620
|
+
function fileUriToPath(uri) {
|
|
1621
|
+
if ("string" != typeof uri || uri.length <= 7 || "file://" != uri.substring(0, 7)) {
|
|
1622
|
+
throw new TypeError("must pass in a file:// URI to convert to a file path");
|
|
1623
|
+
}
|
|
1624
|
+
var rest = decodeURI(uri.substring(7));
|
|
1625
|
+
var firstSlash = rest.indexOf("/");
|
|
1626
|
+
var host = rest.substring(0, firstSlash);
|
|
1627
|
+
var path2 = rest.substring(firstSlash + 1);
|
|
1628
|
+
if ("localhost" == host) host = "";
|
|
1629
|
+
if (host) {
|
|
1630
|
+
host = sep + sep + host;
|
|
1631
|
+
}
|
|
1632
|
+
path2 = path2.replace(/^(.+)\|/, "$1:");
|
|
1633
|
+
if (sep == "\\") {
|
|
1634
|
+
path2 = path2.replace(/\//g, "\\");
|
|
1635
|
+
}
|
|
1636
|
+
if (/^.+\:/.test(path2)) {
|
|
1637
|
+
} else {
|
|
1638
|
+
path2 = sep + path2;
|
|
1639
|
+
}
|
|
1640
|
+
return host + path2;
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
});
|
|
1644
|
+
|
|
1645
|
+
// node_modules/.pnpm/bindings@1.5.0/node_modules/bindings/bindings.js
|
|
1646
|
+
var require_bindings = __commonJS({
|
|
1647
|
+
"node_modules/.pnpm/bindings@1.5.0/node_modules/bindings/bindings.js"(exports, module) {
|
|
1648
|
+
"use strict";
|
|
1649
|
+
init_esm_shims();
|
|
1650
|
+
var fs = __require("fs");
|
|
1651
|
+
var path2 = __require("path");
|
|
1652
|
+
var fileURLToPath2 = require_file_uri_to_path();
|
|
1653
|
+
var join = path2.join;
|
|
1654
|
+
var dirname = path2.dirname;
|
|
1655
|
+
var exists = fs.accessSync && function(path3) {
|
|
1656
|
+
try {
|
|
1657
|
+
fs.accessSync(path3);
|
|
1658
|
+
} catch (e) {
|
|
1659
|
+
return false;
|
|
1660
|
+
}
|
|
1661
|
+
return true;
|
|
1662
|
+
} || fs.existsSync || path2.existsSync;
|
|
1663
|
+
var defaults = {
|
|
1664
|
+
arrow: process.env.NODE_BINDINGS_ARROW || " \u2192 ",
|
|
1665
|
+
compiled: process.env.NODE_BINDINGS_COMPILED_DIR || "compiled",
|
|
1666
|
+
platform: process.platform,
|
|
1667
|
+
arch: process.arch,
|
|
1668
|
+
nodePreGyp: "node-v" + process.versions.modules + "-" + process.platform + "-" + process.arch,
|
|
1669
|
+
version: process.versions.node,
|
|
1670
|
+
bindings: "bindings.node",
|
|
1671
|
+
try: [
|
|
1672
|
+
// node-gyp's linked version in the "build" dir
|
|
1673
|
+
["module_root", "build", "bindings"],
|
|
1674
|
+
// node-waf and gyp_addon (a.k.a node-gyp)
|
|
1675
|
+
["module_root", "build", "Debug", "bindings"],
|
|
1676
|
+
["module_root", "build", "Release", "bindings"],
|
|
1677
|
+
// Debug files, for development (legacy behavior, remove for node v0.9)
|
|
1678
|
+
["module_root", "out", "Debug", "bindings"],
|
|
1679
|
+
["module_root", "Debug", "bindings"],
|
|
1680
|
+
// Release files, but manually compiled (legacy behavior, remove for node v0.9)
|
|
1681
|
+
["module_root", "out", "Release", "bindings"],
|
|
1682
|
+
["module_root", "Release", "bindings"],
|
|
1683
|
+
// Legacy from node-waf, node <= 0.4.x
|
|
1684
|
+
["module_root", "build", "default", "bindings"],
|
|
1685
|
+
// Production "Release" buildtype binary (meh...)
|
|
1686
|
+
["module_root", "compiled", "version", "platform", "arch", "bindings"],
|
|
1687
|
+
// node-qbs builds
|
|
1688
|
+
["module_root", "addon-build", "release", "install-root", "bindings"],
|
|
1689
|
+
["module_root", "addon-build", "debug", "install-root", "bindings"],
|
|
1690
|
+
["module_root", "addon-build", "default", "install-root", "bindings"],
|
|
1691
|
+
// node-pre-gyp path ./lib/binding/{node_abi}-{platform}-{arch}
|
|
1692
|
+
["module_root", "lib", "binding", "nodePreGyp", "bindings"]
|
|
1693
|
+
]
|
|
1694
|
+
};
|
|
1695
|
+
function bindings(opts) {
|
|
1696
|
+
if (typeof opts == "string") {
|
|
1697
|
+
opts = { bindings: opts };
|
|
1698
|
+
} else if (!opts) {
|
|
1699
|
+
opts = {};
|
|
1700
|
+
}
|
|
1701
|
+
Object.keys(defaults).map(function(i2) {
|
|
1702
|
+
if (!(i2 in opts)) opts[i2] = defaults[i2];
|
|
1703
|
+
});
|
|
1704
|
+
if (!opts.module_root) {
|
|
1705
|
+
opts.module_root = exports.getRoot(exports.getFileName());
|
|
1706
|
+
}
|
|
1707
|
+
if (path2.extname(opts.bindings) != ".node") {
|
|
1708
|
+
opts.bindings += ".node";
|
|
1709
|
+
}
|
|
1710
|
+
var requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : __require;
|
|
1711
|
+
var tries = [], i = 0, l = opts.try.length, n, b, err;
|
|
1712
|
+
for (; i < l; i++) {
|
|
1713
|
+
n = join.apply(
|
|
1714
|
+
null,
|
|
1715
|
+
opts.try[i].map(function(p) {
|
|
1716
|
+
return opts[p] || p;
|
|
1717
|
+
})
|
|
1718
|
+
);
|
|
1719
|
+
tries.push(n);
|
|
1720
|
+
try {
|
|
1721
|
+
b = opts.path ? requireFunc.resolve(n) : requireFunc(n);
|
|
1722
|
+
if (!opts.path) {
|
|
1723
|
+
b.path = n;
|
|
1724
|
+
}
|
|
1725
|
+
return b;
|
|
1726
|
+
} catch (e) {
|
|
1727
|
+
if (e.code !== "MODULE_NOT_FOUND" && e.code !== "QUALIFIED_PATH_RESOLUTION_FAILED" && !/not find/i.test(e.message)) {
|
|
1728
|
+
throw e;
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
err = new Error(
|
|
1733
|
+
"Could not locate the bindings file. Tried:\n" + tries.map(function(a) {
|
|
1734
|
+
return opts.arrow + a;
|
|
1735
|
+
}).join("\n")
|
|
1736
|
+
);
|
|
1737
|
+
err.tries = tries;
|
|
1738
|
+
throw err;
|
|
1739
|
+
}
|
|
1740
|
+
module.exports = exports = bindings;
|
|
1741
|
+
exports.getFileName = function getFileName(calling_file) {
|
|
1742
|
+
var origPST = Error.prepareStackTrace, origSTL = Error.stackTraceLimit, dummy = {}, fileName;
|
|
1743
|
+
Error.stackTraceLimit = 10;
|
|
1744
|
+
Error.prepareStackTrace = function(e, st) {
|
|
1745
|
+
for (var i = 0, l = st.length; i < l; i++) {
|
|
1746
|
+
fileName = st[i].getFileName();
|
|
1747
|
+
if (fileName !== __filename) {
|
|
1748
|
+
if (calling_file) {
|
|
1749
|
+
if (fileName !== calling_file) {
|
|
1750
|
+
return;
|
|
1751
|
+
}
|
|
1752
|
+
} else {
|
|
1753
|
+
return;
|
|
1754
|
+
}
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
};
|
|
1758
|
+
Error.captureStackTrace(dummy);
|
|
1759
|
+
dummy.stack;
|
|
1760
|
+
Error.prepareStackTrace = origPST;
|
|
1761
|
+
Error.stackTraceLimit = origSTL;
|
|
1762
|
+
var fileSchema = "file://";
|
|
1763
|
+
if (fileName.indexOf(fileSchema) === 0) {
|
|
1764
|
+
fileName = fileURLToPath2(fileName);
|
|
1765
|
+
}
|
|
1766
|
+
return fileName;
|
|
1767
|
+
};
|
|
1768
|
+
exports.getRoot = function getRoot(file) {
|
|
1769
|
+
var dir = dirname(file), prev;
|
|
1770
|
+
while (true) {
|
|
1771
|
+
if (dir === ".") {
|
|
1772
|
+
dir = process.cwd();
|
|
1773
|
+
}
|
|
1774
|
+
if (exists(join(dir, "package.json")) || exists(join(dir, "node_modules"))) {
|
|
1775
|
+
return dir;
|
|
1776
|
+
}
|
|
1777
|
+
if (prev === dir) {
|
|
1778
|
+
throw new Error(
|
|
1779
|
+
'Could not find module root given file: "' + file + '". Do you have a `package.json` file? '
|
|
1780
|
+
);
|
|
1781
|
+
}
|
|
1782
|
+
prev = dir;
|
|
1783
|
+
dir = join(dir, "..");
|
|
1784
|
+
}
|
|
1785
|
+
};
|
|
1786
|
+
}
|
|
1787
|
+
});
|
|
1788
|
+
|
|
1789
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/wrappers.js
|
|
1790
|
+
var require_wrappers = __commonJS({
|
|
1791
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/wrappers.js"(exports) {
|
|
1792
|
+
"use strict";
|
|
1793
|
+
init_esm_shims();
|
|
1794
|
+
var { cppdb } = require_util();
|
|
1795
|
+
exports.prepare = function prepare(sql) {
|
|
1796
|
+
return this[cppdb].prepare(sql, this, false);
|
|
1797
|
+
};
|
|
1798
|
+
exports.exec = function exec(sql) {
|
|
1799
|
+
this[cppdb].exec(sql);
|
|
1800
|
+
return this;
|
|
1801
|
+
};
|
|
1802
|
+
exports.close = function close() {
|
|
1803
|
+
this[cppdb].close();
|
|
1804
|
+
return this;
|
|
1805
|
+
};
|
|
1806
|
+
exports.loadExtension = function loadExtension(...args) {
|
|
1807
|
+
this[cppdb].loadExtension(...args);
|
|
1808
|
+
return this;
|
|
1809
|
+
};
|
|
1810
|
+
exports.defaultSafeIntegers = function defaultSafeIntegers(...args) {
|
|
1811
|
+
this[cppdb].defaultSafeIntegers(...args);
|
|
1812
|
+
return this;
|
|
1813
|
+
};
|
|
1814
|
+
exports.unsafeMode = function unsafeMode(...args) {
|
|
1815
|
+
this[cppdb].unsafeMode(...args);
|
|
1816
|
+
return this;
|
|
1817
|
+
};
|
|
1818
|
+
exports.getters = {
|
|
1819
|
+
name: {
|
|
1820
|
+
get: function name() {
|
|
1821
|
+
return this[cppdb].name;
|
|
1822
|
+
},
|
|
1823
|
+
enumerable: true
|
|
1824
|
+
},
|
|
1825
|
+
open: {
|
|
1826
|
+
get: function open() {
|
|
1827
|
+
return this[cppdb].open;
|
|
1828
|
+
},
|
|
1829
|
+
enumerable: true
|
|
1830
|
+
},
|
|
1831
|
+
inTransaction: {
|
|
1832
|
+
get: function inTransaction() {
|
|
1833
|
+
return this[cppdb].inTransaction;
|
|
1834
|
+
},
|
|
1835
|
+
enumerable: true
|
|
1836
|
+
},
|
|
1837
|
+
readonly: {
|
|
1838
|
+
get: function readonly() {
|
|
1839
|
+
return this[cppdb].readonly;
|
|
1840
|
+
},
|
|
1841
|
+
enumerable: true
|
|
1842
|
+
},
|
|
1843
|
+
memory: {
|
|
1844
|
+
get: function memory() {
|
|
1845
|
+
return this[cppdb].memory;
|
|
1846
|
+
},
|
|
1847
|
+
enumerable: true
|
|
1848
|
+
}
|
|
1849
|
+
};
|
|
1850
|
+
}
|
|
1851
|
+
});
|
|
1852
|
+
|
|
1853
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/transaction.js
|
|
1854
|
+
var require_transaction = __commonJS({
|
|
1855
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/transaction.js"(exports, module) {
|
|
1856
|
+
"use strict";
|
|
1857
|
+
init_esm_shims();
|
|
1858
|
+
var { cppdb } = require_util();
|
|
1859
|
+
var controllers = /* @__PURE__ */ new WeakMap();
|
|
1860
|
+
module.exports = function transaction(fn) {
|
|
1861
|
+
if (typeof fn !== "function") throw new TypeError("Expected first argument to be a function");
|
|
1862
|
+
const db = this[cppdb];
|
|
1863
|
+
const controller = getController(db, this);
|
|
1864
|
+
const { apply } = Function.prototype;
|
|
1865
|
+
const properties = {
|
|
1866
|
+
default: { value: wrapTransaction(apply, fn, db, controller.default) },
|
|
1867
|
+
deferred: { value: wrapTransaction(apply, fn, db, controller.deferred) },
|
|
1868
|
+
immediate: { value: wrapTransaction(apply, fn, db, controller.immediate) },
|
|
1869
|
+
exclusive: { value: wrapTransaction(apply, fn, db, controller.exclusive) },
|
|
1870
|
+
database: { value: this, enumerable: true }
|
|
1871
|
+
};
|
|
1872
|
+
Object.defineProperties(properties.default.value, properties);
|
|
1873
|
+
Object.defineProperties(properties.deferred.value, properties);
|
|
1874
|
+
Object.defineProperties(properties.immediate.value, properties);
|
|
1875
|
+
Object.defineProperties(properties.exclusive.value, properties);
|
|
1876
|
+
return properties.default.value;
|
|
1877
|
+
};
|
|
1878
|
+
var getController = (db, self) => {
|
|
1879
|
+
let controller = controllers.get(db);
|
|
1880
|
+
if (!controller) {
|
|
1881
|
+
const shared = {
|
|
1882
|
+
commit: db.prepare("COMMIT", self, false),
|
|
1883
|
+
rollback: db.prepare("ROLLBACK", self, false),
|
|
1884
|
+
savepoint: db.prepare("SAVEPOINT ` _bs3. `", self, false),
|
|
1885
|
+
release: db.prepare("RELEASE ` _bs3. `", self, false),
|
|
1886
|
+
rollbackTo: db.prepare("ROLLBACK TO ` _bs3. `", self, false)
|
|
1887
|
+
};
|
|
1888
|
+
controllers.set(db, controller = {
|
|
1889
|
+
default: Object.assign({ begin: db.prepare("BEGIN", self, false) }, shared),
|
|
1890
|
+
deferred: Object.assign({ begin: db.prepare("BEGIN DEFERRED", self, false) }, shared),
|
|
1891
|
+
immediate: Object.assign({ begin: db.prepare("BEGIN IMMEDIATE", self, false) }, shared),
|
|
1892
|
+
exclusive: Object.assign({ begin: db.prepare("BEGIN EXCLUSIVE", self, false) }, shared)
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1895
|
+
return controller;
|
|
1896
|
+
};
|
|
1897
|
+
var wrapTransaction = (apply, fn, db, { begin, commit, rollback, savepoint, release, rollbackTo }) => function sqliteTransaction() {
|
|
1898
|
+
let before, after, undo;
|
|
1899
|
+
if (db.inTransaction) {
|
|
1900
|
+
before = savepoint;
|
|
1901
|
+
after = release;
|
|
1902
|
+
undo = rollbackTo;
|
|
1903
|
+
} else {
|
|
1904
|
+
before = begin;
|
|
1905
|
+
after = commit;
|
|
1906
|
+
undo = rollback;
|
|
1907
|
+
}
|
|
1908
|
+
before.run();
|
|
1909
|
+
try {
|
|
1910
|
+
const result = apply.call(fn, this, arguments);
|
|
1911
|
+
if (result && typeof result.then === "function") {
|
|
1912
|
+
throw new TypeError("Transaction function cannot return a promise");
|
|
1913
|
+
}
|
|
1914
|
+
after.run();
|
|
1915
|
+
return result;
|
|
1916
|
+
} catch (ex) {
|
|
1917
|
+
if (db.inTransaction) {
|
|
1918
|
+
undo.run();
|
|
1919
|
+
if (undo !== rollback) after.run();
|
|
1920
|
+
}
|
|
1921
|
+
throw ex;
|
|
1922
|
+
}
|
|
1923
|
+
};
|
|
1924
|
+
}
|
|
1925
|
+
});
|
|
1926
|
+
|
|
1927
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/pragma.js
|
|
1928
|
+
var require_pragma = __commonJS({
|
|
1929
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/pragma.js"(exports, module) {
|
|
1930
|
+
"use strict";
|
|
1931
|
+
init_esm_shims();
|
|
1932
|
+
var { getBooleanOption, cppdb } = require_util();
|
|
1933
|
+
module.exports = function pragma(source, options) {
|
|
1934
|
+
if (options == null) options = {};
|
|
1935
|
+
if (typeof source !== "string") throw new TypeError("Expected first argument to be a string");
|
|
1936
|
+
if (typeof options !== "object") throw new TypeError("Expected second argument to be an options object");
|
|
1937
|
+
const simple = getBooleanOption(options, "simple");
|
|
1938
|
+
const stmt = this[cppdb].prepare(`PRAGMA ${source}`, this, true);
|
|
1939
|
+
return simple ? stmt.pluck().get() : stmt.all();
|
|
1940
|
+
};
|
|
1941
|
+
}
|
|
1942
|
+
});
|
|
1943
|
+
|
|
1944
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/backup.js
|
|
1945
|
+
var require_backup = __commonJS({
|
|
1946
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/backup.js"(exports, module) {
|
|
1947
|
+
"use strict";
|
|
1948
|
+
init_esm_shims();
|
|
1949
|
+
var fs = __require("fs");
|
|
1950
|
+
var path2 = __require("path");
|
|
1951
|
+
var { promisify } = __require("util");
|
|
1952
|
+
var { cppdb } = require_util();
|
|
1953
|
+
var fsAccess = promisify(fs.access);
|
|
1954
|
+
module.exports = async function backup(filename, options) {
|
|
1955
|
+
if (options == null) options = {};
|
|
1956
|
+
if (typeof filename !== "string") throw new TypeError("Expected first argument to be a string");
|
|
1957
|
+
if (typeof options !== "object") throw new TypeError("Expected second argument to be an options object");
|
|
1958
|
+
filename = filename.trim();
|
|
1959
|
+
const attachedName = "attached" in options ? options.attached : "main";
|
|
1960
|
+
const handler = "progress" in options ? options.progress : null;
|
|
1961
|
+
if (!filename) throw new TypeError("Backup filename cannot be an empty string");
|
|
1962
|
+
if (filename === ":memory:") throw new TypeError('Invalid backup filename ":memory:"');
|
|
1963
|
+
if (typeof attachedName !== "string") throw new TypeError('Expected the "attached" option to be a string');
|
|
1964
|
+
if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
|
|
1965
|
+
if (handler != null && typeof handler !== "function") throw new TypeError('Expected the "progress" option to be a function');
|
|
1966
|
+
await fsAccess(path2.dirname(filename)).catch(() => {
|
|
1967
|
+
throw new TypeError("Cannot save backup because the directory does not exist");
|
|
1968
|
+
});
|
|
1969
|
+
const isNewFile = await fsAccess(filename).then(() => false, () => true);
|
|
1970
|
+
return runBackup(this[cppdb].backup(this, attachedName, filename, isNewFile), handler || null);
|
|
1971
|
+
};
|
|
1972
|
+
var runBackup = (backup, handler) => {
|
|
1973
|
+
let rate = 0;
|
|
1974
|
+
let useDefault = true;
|
|
1975
|
+
return new Promise((resolve2, reject) => {
|
|
1976
|
+
setImmediate(function step() {
|
|
1977
|
+
try {
|
|
1978
|
+
const progress = backup.transfer(rate);
|
|
1979
|
+
if (!progress.remainingPages) {
|
|
1980
|
+
backup.close();
|
|
1981
|
+
resolve2(progress);
|
|
1982
|
+
return;
|
|
1983
|
+
}
|
|
1984
|
+
if (useDefault) {
|
|
1985
|
+
useDefault = false;
|
|
1986
|
+
rate = 100;
|
|
1987
|
+
}
|
|
1988
|
+
if (handler) {
|
|
1989
|
+
const ret = handler(progress);
|
|
1990
|
+
if (ret !== void 0) {
|
|
1991
|
+
if (typeof ret === "number" && ret === ret) rate = Math.max(0, Math.min(2147483647, Math.round(ret)));
|
|
1992
|
+
else throw new TypeError("Expected progress callback to return a number or undefined");
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
setImmediate(step);
|
|
1996
|
+
} catch (err) {
|
|
1997
|
+
backup.close();
|
|
1998
|
+
reject(err);
|
|
1999
|
+
}
|
|
2000
|
+
});
|
|
2001
|
+
});
|
|
2002
|
+
};
|
|
2003
|
+
}
|
|
2004
|
+
});
|
|
2005
|
+
|
|
2006
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/serialize.js
|
|
2007
|
+
var require_serialize = __commonJS({
|
|
2008
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/serialize.js"(exports, module) {
|
|
2009
|
+
"use strict";
|
|
2010
|
+
init_esm_shims();
|
|
2011
|
+
var { cppdb } = require_util();
|
|
2012
|
+
module.exports = function serialize(options) {
|
|
2013
|
+
if (options == null) options = {};
|
|
2014
|
+
if (typeof options !== "object") throw new TypeError("Expected first argument to be an options object");
|
|
2015
|
+
const attachedName = "attached" in options ? options.attached : "main";
|
|
2016
|
+
if (typeof attachedName !== "string") throw new TypeError('Expected the "attached" option to be a string');
|
|
2017
|
+
if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
|
|
2018
|
+
return this[cppdb].serialize(attachedName);
|
|
2019
|
+
};
|
|
2020
|
+
}
|
|
2021
|
+
});
|
|
2022
|
+
|
|
2023
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/function.js
|
|
2024
|
+
var require_function = __commonJS({
|
|
2025
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/function.js"(exports, module) {
|
|
2026
|
+
"use strict";
|
|
2027
|
+
init_esm_shims();
|
|
2028
|
+
var { getBooleanOption, cppdb } = require_util();
|
|
2029
|
+
module.exports = function defineFunction(name, options, fn) {
|
|
2030
|
+
if (options == null) options = {};
|
|
2031
|
+
if (typeof options === "function") {
|
|
2032
|
+
fn = options;
|
|
2033
|
+
options = {};
|
|
2034
|
+
}
|
|
2035
|
+
if (typeof name !== "string") throw new TypeError("Expected first argument to be a string");
|
|
2036
|
+
if (typeof fn !== "function") throw new TypeError("Expected last argument to be a function");
|
|
2037
|
+
if (typeof options !== "object") throw new TypeError("Expected second argument to be an options object");
|
|
2038
|
+
if (!name) throw new TypeError("User-defined function name cannot be an empty string");
|
|
2039
|
+
const safeIntegers = "safeIntegers" in options ? +getBooleanOption(options, "safeIntegers") : 2;
|
|
2040
|
+
const deterministic = getBooleanOption(options, "deterministic");
|
|
2041
|
+
const directOnly = getBooleanOption(options, "directOnly");
|
|
2042
|
+
const varargs = getBooleanOption(options, "varargs");
|
|
2043
|
+
let argCount = -1;
|
|
2044
|
+
if (!varargs) {
|
|
2045
|
+
argCount = fn.length;
|
|
2046
|
+
if (!Number.isInteger(argCount) || argCount < 0) throw new TypeError("Expected function.length to be a positive integer");
|
|
2047
|
+
if (argCount > 100) throw new RangeError("User-defined functions cannot have more than 100 arguments");
|
|
2048
|
+
}
|
|
2049
|
+
this[cppdb].function(fn, name, argCount, safeIntegers, deterministic, directOnly);
|
|
2050
|
+
return this;
|
|
2051
|
+
};
|
|
2052
|
+
}
|
|
2053
|
+
});
|
|
2054
|
+
|
|
2055
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/aggregate.js
|
|
2056
|
+
var require_aggregate = __commonJS({
|
|
2057
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/aggregate.js"(exports, module) {
|
|
2058
|
+
"use strict";
|
|
2059
|
+
init_esm_shims();
|
|
2060
|
+
var { getBooleanOption, cppdb } = require_util();
|
|
2061
|
+
module.exports = function defineAggregate(name, options) {
|
|
2062
|
+
if (typeof name !== "string") throw new TypeError("Expected first argument to be a string");
|
|
2063
|
+
if (typeof options !== "object" || options === null) throw new TypeError("Expected second argument to be an options object");
|
|
2064
|
+
if (!name) throw new TypeError("User-defined function name cannot be an empty string");
|
|
2065
|
+
const start = "start" in options ? options.start : null;
|
|
2066
|
+
const step = getFunctionOption(options, "step", true);
|
|
2067
|
+
const inverse = getFunctionOption(options, "inverse", false);
|
|
2068
|
+
const result = getFunctionOption(options, "result", false);
|
|
2069
|
+
const safeIntegers = "safeIntegers" in options ? +getBooleanOption(options, "safeIntegers") : 2;
|
|
2070
|
+
const deterministic = getBooleanOption(options, "deterministic");
|
|
2071
|
+
const directOnly = getBooleanOption(options, "directOnly");
|
|
2072
|
+
const varargs = getBooleanOption(options, "varargs");
|
|
2073
|
+
let argCount = -1;
|
|
2074
|
+
if (!varargs) {
|
|
2075
|
+
argCount = Math.max(getLength(step), inverse ? getLength(inverse) : 0);
|
|
2076
|
+
if (argCount > 0) argCount -= 1;
|
|
2077
|
+
if (argCount > 100) throw new RangeError("User-defined functions cannot have more than 100 arguments");
|
|
2078
|
+
}
|
|
2079
|
+
this[cppdb].aggregate(start, step, inverse, result, name, argCount, safeIntegers, deterministic, directOnly);
|
|
2080
|
+
return this;
|
|
2081
|
+
};
|
|
2082
|
+
var getFunctionOption = (options, key, required) => {
|
|
2083
|
+
const value = key in options ? options[key] : null;
|
|
2084
|
+
if (typeof value === "function") return value;
|
|
2085
|
+
if (value != null) throw new TypeError(`Expected the "${key}" option to be a function`);
|
|
2086
|
+
if (required) throw new TypeError(`Missing required option "${key}"`);
|
|
2087
|
+
return null;
|
|
2088
|
+
};
|
|
2089
|
+
var getLength = ({ length }) => {
|
|
2090
|
+
if (Number.isInteger(length) && length >= 0) return length;
|
|
2091
|
+
throw new TypeError("Expected function.length to be a positive integer");
|
|
2092
|
+
};
|
|
2093
|
+
}
|
|
2094
|
+
});
|
|
2095
|
+
|
|
2096
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/table.js
|
|
2097
|
+
var require_table = __commonJS({
|
|
2098
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/table.js"(exports, module) {
|
|
2099
|
+
"use strict";
|
|
2100
|
+
init_esm_shims();
|
|
2101
|
+
var { cppdb } = require_util();
|
|
2102
|
+
module.exports = function defineTable(name, factory) {
|
|
2103
|
+
if (typeof name !== "string") throw new TypeError("Expected first argument to be a string");
|
|
2104
|
+
if (!name) throw new TypeError("Virtual table module name cannot be an empty string");
|
|
2105
|
+
let eponymous = false;
|
|
2106
|
+
if (typeof factory === "object" && factory !== null) {
|
|
2107
|
+
eponymous = true;
|
|
2108
|
+
factory = defer(parseTableDefinition(factory, "used", name));
|
|
2109
|
+
} else {
|
|
2110
|
+
if (typeof factory !== "function") throw new TypeError("Expected second argument to be a function or a table definition object");
|
|
2111
|
+
factory = wrapFactory(factory);
|
|
2112
|
+
}
|
|
2113
|
+
this[cppdb].table(factory, name, eponymous);
|
|
2114
|
+
return this;
|
|
2115
|
+
};
|
|
2116
|
+
function wrapFactory(factory) {
|
|
2117
|
+
return function virtualTableFactory(moduleName, databaseName, tableName, ...args) {
|
|
2118
|
+
const thisObject = {
|
|
2119
|
+
module: moduleName,
|
|
2120
|
+
database: databaseName,
|
|
2121
|
+
table: tableName
|
|
2122
|
+
};
|
|
2123
|
+
const def = apply.call(factory, thisObject, args);
|
|
2124
|
+
if (typeof def !== "object" || def === null) {
|
|
2125
|
+
throw new TypeError(`Virtual table module "${moduleName}" did not return a table definition object`);
|
|
2126
|
+
}
|
|
2127
|
+
return parseTableDefinition(def, "returned", moduleName);
|
|
2128
|
+
};
|
|
2129
|
+
}
|
|
2130
|
+
function parseTableDefinition(def, verb, moduleName) {
|
|
2131
|
+
if (!hasOwnProperty.call(def, "rows")) {
|
|
2132
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "rows" property`);
|
|
2133
|
+
}
|
|
2134
|
+
if (!hasOwnProperty.call(def, "columns")) {
|
|
2135
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "columns" property`);
|
|
2136
|
+
}
|
|
2137
|
+
const rows = def.rows;
|
|
2138
|
+
if (typeof rows !== "function" || Object.getPrototypeOf(rows) !== GeneratorFunctionPrototype) {
|
|
2139
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "rows" property (should be a generator function)`);
|
|
2140
|
+
}
|
|
2141
|
+
let columns = def.columns;
|
|
2142
|
+
if (!Array.isArray(columns) || !(columns = [...columns]).every((x) => typeof x === "string")) {
|
|
2143
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "columns" property (should be an array of strings)`);
|
|
2144
|
+
}
|
|
2145
|
+
if (columns.length !== new Set(columns).size) {
|
|
2146
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate column names`);
|
|
2147
|
+
}
|
|
2148
|
+
if (!columns.length) {
|
|
2149
|
+
throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with zero columns`);
|
|
2150
|
+
}
|
|
2151
|
+
let parameters;
|
|
2152
|
+
if (hasOwnProperty.call(def, "parameters")) {
|
|
2153
|
+
parameters = def.parameters;
|
|
2154
|
+
if (!Array.isArray(parameters) || !(parameters = [...parameters]).every((x) => typeof x === "string")) {
|
|
2155
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "parameters" property (should be an array of strings)`);
|
|
2156
|
+
}
|
|
2157
|
+
} else {
|
|
2158
|
+
parameters = inferParameters(rows);
|
|
2159
|
+
}
|
|
2160
|
+
if (parameters.length !== new Set(parameters).size) {
|
|
2161
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate parameter names`);
|
|
2162
|
+
}
|
|
2163
|
+
if (parameters.length > 32) {
|
|
2164
|
+
throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with more than the maximum number of 32 parameters`);
|
|
2165
|
+
}
|
|
2166
|
+
for (const parameter of parameters) {
|
|
2167
|
+
if (columns.includes(parameter)) {
|
|
2168
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with column "${parameter}" which was ambiguously defined as both a column and parameter`);
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
let safeIntegers = 2;
|
|
2172
|
+
if (hasOwnProperty.call(def, "safeIntegers")) {
|
|
2173
|
+
const bool = def.safeIntegers;
|
|
2174
|
+
if (typeof bool !== "boolean") {
|
|
2175
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "safeIntegers" property (should be a boolean)`);
|
|
2176
|
+
}
|
|
2177
|
+
safeIntegers = +bool;
|
|
2178
|
+
}
|
|
2179
|
+
let directOnly = false;
|
|
2180
|
+
if (hasOwnProperty.call(def, "directOnly")) {
|
|
2181
|
+
directOnly = def.directOnly;
|
|
2182
|
+
if (typeof directOnly !== "boolean") {
|
|
2183
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "directOnly" property (should be a boolean)`);
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
const columnDefinitions = [
|
|
2187
|
+
...parameters.map(identifier).map((str) => `${str} HIDDEN`),
|
|
2188
|
+
...columns.map(identifier)
|
|
2189
|
+
];
|
|
2190
|
+
return [
|
|
2191
|
+
`CREATE TABLE x(${columnDefinitions.join(", ")});`,
|
|
2192
|
+
wrapGenerator(rows, new Map(columns.map((x, i) => [x, parameters.length + i])), moduleName),
|
|
2193
|
+
parameters,
|
|
2194
|
+
safeIntegers,
|
|
2195
|
+
directOnly
|
|
2196
|
+
];
|
|
2197
|
+
}
|
|
2198
|
+
function wrapGenerator(generator, columnMap, moduleName) {
|
|
2199
|
+
return function* virtualTable(...args) {
|
|
2200
|
+
const output = args.map((x) => Buffer.isBuffer(x) ? Buffer.from(x) : x);
|
|
2201
|
+
for (let i = 0; i < columnMap.size; ++i) {
|
|
2202
|
+
output.push(null);
|
|
2203
|
+
}
|
|
2204
|
+
for (const row of generator(...args)) {
|
|
2205
|
+
if (Array.isArray(row)) {
|
|
2206
|
+
extractRowArray(row, output, columnMap.size, moduleName);
|
|
2207
|
+
yield output;
|
|
2208
|
+
} else if (typeof row === "object" && row !== null) {
|
|
2209
|
+
extractRowObject(row, output, columnMap, moduleName);
|
|
2210
|
+
yield output;
|
|
2211
|
+
} else {
|
|
2212
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded something that isn't a valid row object`);
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
};
|
|
2216
|
+
}
|
|
2217
|
+
function extractRowArray(row, output, columnCount, moduleName) {
|
|
2218
|
+
if (row.length !== columnCount) {
|
|
2219
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an incorrect number of columns`);
|
|
2220
|
+
}
|
|
2221
|
+
const offset = output.length - columnCount;
|
|
2222
|
+
for (let i = 0; i < columnCount; ++i) {
|
|
2223
|
+
output[i + offset] = row[i];
|
|
2224
|
+
}
|
|
2225
|
+
}
|
|
2226
|
+
function extractRowObject(row, output, columnMap, moduleName) {
|
|
2227
|
+
let count = 0;
|
|
2228
|
+
for (const key of Object.keys(row)) {
|
|
2229
|
+
const index = columnMap.get(key);
|
|
2230
|
+
if (index === void 0) {
|
|
2231
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an undeclared column "${key}"`);
|
|
2232
|
+
}
|
|
2233
|
+
output[index] = row[key];
|
|
2234
|
+
count += 1;
|
|
2235
|
+
}
|
|
2236
|
+
if (count !== columnMap.size) {
|
|
2237
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded a row with missing columns`);
|
|
2238
|
+
}
|
|
2239
|
+
}
|
|
2240
|
+
function inferParameters({ length }) {
|
|
2241
|
+
if (!Number.isInteger(length) || length < 0) {
|
|
2242
|
+
throw new TypeError("Expected function.length to be a positive integer");
|
|
2243
|
+
}
|
|
2244
|
+
const params = [];
|
|
2245
|
+
for (let i = 0; i < length; ++i) {
|
|
2246
|
+
params.push(`$${i + 1}`);
|
|
2247
|
+
}
|
|
2248
|
+
return params;
|
|
2249
|
+
}
|
|
2250
|
+
var { hasOwnProperty } = Object.prototype;
|
|
2251
|
+
var { apply } = Function.prototype;
|
|
2252
|
+
var GeneratorFunctionPrototype = Object.getPrototypeOf(function* () {
|
|
2253
|
+
});
|
|
2254
|
+
var identifier = (str) => `"${str.replace(/"/g, '""')}"`;
|
|
2255
|
+
var defer = (x) => () => x;
|
|
2256
|
+
}
|
|
2257
|
+
});
|
|
2258
|
+
|
|
2259
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/inspect.js
|
|
2260
|
+
var require_inspect = __commonJS({
|
|
2261
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/methods/inspect.js"(exports, module) {
|
|
2262
|
+
"use strict";
|
|
2263
|
+
init_esm_shims();
|
|
2264
|
+
var DatabaseInspection = function Database() {
|
|
2265
|
+
};
|
|
2266
|
+
module.exports = function inspect(depth, opts) {
|
|
2267
|
+
return Object.assign(new DatabaseInspection(), this);
|
|
2268
|
+
};
|
|
2269
|
+
}
|
|
2270
|
+
});
|
|
2271
|
+
|
|
2272
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/database.js
|
|
2273
|
+
var require_database = __commonJS({
|
|
2274
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/database.js"(exports, module) {
|
|
2275
|
+
"use strict";
|
|
2276
|
+
init_esm_shims();
|
|
2277
|
+
var fs = __require("fs");
|
|
2278
|
+
var path2 = __require("path");
|
|
2279
|
+
var util = require_util();
|
|
2280
|
+
var SqliteError = require_sqlite_error();
|
|
2281
|
+
var DEFAULT_ADDON;
|
|
2282
|
+
function Database(filenameGiven, options) {
|
|
2283
|
+
if (new.target == null) {
|
|
2284
|
+
return new Database(filenameGiven, options);
|
|
2285
|
+
}
|
|
2286
|
+
let buffer;
|
|
2287
|
+
if (Buffer.isBuffer(filenameGiven)) {
|
|
2288
|
+
buffer = filenameGiven;
|
|
2289
|
+
filenameGiven = ":memory:";
|
|
2290
|
+
}
|
|
2291
|
+
if (filenameGiven == null) filenameGiven = "";
|
|
2292
|
+
if (options == null) options = {};
|
|
2293
|
+
if (typeof filenameGiven !== "string") throw new TypeError("Expected first argument to be a string");
|
|
2294
|
+
if (typeof options !== "object") throw new TypeError("Expected second argument to be an options object");
|
|
2295
|
+
if ("readOnly" in options) throw new TypeError('Misspelled option "readOnly" should be "readonly"');
|
|
2296
|
+
if ("memory" in options) throw new TypeError('Option "memory" was removed in v7.0.0 (use ":memory:" filename instead)');
|
|
2297
|
+
const filename = filenameGiven.trim();
|
|
2298
|
+
const anonymous = filename === "" || filename === ":memory:";
|
|
2299
|
+
const readonly = util.getBooleanOption(options, "readonly");
|
|
2300
|
+
const fileMustExist = util.getBooleanOption(options, "fileMustExist");
|
|
2301
|
+
const timeout = "timeout" in options ? options.timeout : 5e3;
|
|
2302
|
+
const verbose = "verbose" in options ? options.verbose : null;
|
|
2303
|
+
const nativeBinding = "nativeBinding" in options ? options.nativeBinding : null;
|
|
2304
|
+
if (readonly && anonymous && !buffer) throw new TypeError("In-memory/temporary databases cannot be readonly");
|
|
2305
|
+
if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
|
|
2306
|
+
if (timeout > 2147483647) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
|
|
2307
|
+
if (verbose != null && typeof verbose !== "function") throw new TypeError('Expected the "verbose" option to be a function');
|
|
2308
|
+
if (nativeBinding != null && typeof nativeBinding !== "string" && typeof nativeBinding !== "object") throw new TypeError('Expected the "nativeBinding" option to be a string or addon object');
|
|
2309
|
+
let addon;
|
|
2310
|
+
if (nativeBinding == null) {
|
|
2311
|
+
addon = DEFAULT_ADDON || (DEFAULT_ADDON = require_bindings()("better_sqlite3.node"));
|
|
2312
|
+
} else if (typeof nativeBinding === "string") {
|
|
2313
|
+
const requireFunc = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : __require;
|
|
2314
|
+
addon = requireFunc(path2.resolve(nativeBinding).replace(/(\.node)?$/, ".node"));
|
|
2315
|
+
} else {
|
|
2316
|
+
addon = nativeBinding;
|
|
2317
|
+
}
|
|
2318
|
+
if (!addon.isInitialized) {
|
|
2319
|
+
addon.setErrorConstructor(SqliteError);
|
|
2320
|
+
addon.isInitialized = true;
|
|
2321
|
+
}
|
|
2322
|
+
if (!anonymous && !fs.existsSync(path2.dirname(filename))) {
|
|
2323
|
+
throw new TypeError("Cannot open database because the directory does not exist");
|
|
2324
|
+
}
|
|
2325
|
+
Object.defineProperties(this, {
|
|
2326
|
+
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) },
|
|
2327
|
+
...wrappers.getters
|
|
2328
|
+
});
|
|
2329
|
+
}
|
|
2330
|
+
var wrappers = require_wrappers();
|
|
2331
|
+
Database.prototype.prepare = wrappers.prepare;
|
|
2332
|
+
Database.prototype.transaction = require_transaction();
|
|
2333
|
+
Database.prototype.pragma = require_pragma();
|
|
2334
|
+
Database.prototype.backup = require_backup();
|
|
2335
|
+
Database.prototype.serialize = require_serialize();
|
|
2336
|
+
Database.prototype.function = require_function();
|
|
2337
|
+
Database.prototype.aggregate = require_aggregate();
|
|
2338
|
+
Database.prototype.table = require_table();
|
|
2339
|
+
Database.prototype.loadExtension = wrappers.loadExtension;
|
|
2340
|
+
Database.prototype.exec = wrappers.exec;
|
|
2341
|
+
Database.prototype.close = wrappers.close;
|
|
2342
|
+
Database.prototype.defaultSafeIntegers = wrappers.defaultSafeIntegers;
|
|
2343
|
+
Database.prototype.unsafeMode = wrappers.unsafeMode;
|
|
2344
|
+
Database.prototype[util.inspect] = require_inspect();
|
|
2345
|
+
module.exports = Database;
|
|
2346
|
+
}
|
|
2347
|
+
});
|
|
2348
|
+
|
|
2349
|
+
// node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/index.js
|
|
2350
|
+
var require_lib = __commonJS({
|
|
2351
|
+
"node_modules/.pnpm/better-sqlite3@11.10.0/node_modules/better-sqlite3/lib/index.js"(exports, module) {
|
|
2352
|
+
"use strict";
|
|
2353
|
+
init_esm_shims();
|
|
2354
|
+
module.exports = require_database();
|
|
2355
|
+
module.exports.SqliteError = require_sqlite_error();
|
|
2356
|
+
}
|
|
2357
|
+
});
|
|
2358
|
+
|
|
2359
|
+
// src/ui/dashboard.ts
|
|
2360
|
+
var dashboard_exports = {};
|
|
2361
|
+
__export(dashboard_exports, {
|
|
2362
|
+
startTui: () => startTui
|
|
2363
|
+
});
|
|
2364
|
+
function startTui(_store, _dispatcher) {
|
|
2365
|
+
console.log("Terminal UI not implemented yet \u2014 open the web dashboard at /__mock__/dashboard instead.");
|
|
2366
|
+
return { stop: () => void 0 };
|
|
2367
|
+
}
|
|
2368
|
+
var init_dashboard2 = __esm({
|
|
2369
|
+
"src/ui/dashboard.ts"() {
|
|
2370
|
+
"use strict";
|
|
2371
|
+
init_esm_shims();
|
|
2372
|
+
}
|
|
2373
|
+
});
|
|
2374
|
+
|
|
2375
|
+
// src/cli.ts
|
|
2376
|
+
init_esm_shims();
|
|
2377
|
+
init_server();
|
|
2378
|
+
import { Command } from "commander";
|
|
2379
|
+
import pc from "picocolors";
|
|
2380
|
+
import { serve } from "@hono/node-server";
|
|
2381
|
+
import { readFileSync, writeFileSync, existsSync, appendFileSync } from "fs";
|
|
2382
|
+
import { resolve } from "path";
|
|
2383
|
+
|
|
2384
|
+
// src/core/persistence.ts
|
|
2385
|
+
init_esm_shims();
|
|
2386
|
+
init_transactions();
|
|
2387
|
+
async function createSqliteStore(path2) {
|
|
2388
|
+
let Database;
|
|
2389
|
+
try {
|
|
2390
|
+
const mod = await Promise.resolve().then(() => __toESM(require_lib(), 1));
|
|
2391
|
+
Database = mod.default;
|
|
2392
|
+
} catch {
|
|
2393
|
+
throw new Error(
|
|
2394
|
+
"Persistence requires the optional dependency 'better-sqlite3'. Install with: npm i better-sqlite3"
|
|
2395
|
+
);
|
|
2396
|
+
}
|
|
2397
|
+
const db = Database(path2);
|
|
2398
|
+
db.pragma("journal_mode = WAL");
|
|
2399
|
+
db.exec(`
|
|
2400
|
+
CREATE TABLE IF NOT EXISTS transactions (
|
|
2401
|
+
checkoutRequestID TEXT PRIMARY KEY,
|
|
2402
|
+
merchantRequestID TEXT NOT NULL,
|
|
2403
|
+
conversationID TEXT,
|
|
2404
|
+
originatorConversationID TEXT,
|
|
2405
|
+
kind TEXT NOT NULL,
|
|
2406
|
+
amount INTEGER NOT NULL,
|
|
2407
|
+
phoneNumber TEXT NOT NULL,
|
|
2408
|
+
shortCode TEXT NOT NULL,
|
|
2409
|
+
callbackUrl TEXT,
|
|
2410
|
+
state TEXT NOT NULL,
|
|
2411
|
+
resultCode INTEGER,
|
|
2412
|
+
resultDesc TEXT,
|
|
2413
|
+
mpesaReceiptNumber TEXT,
|
|
2414
|
+
createdAt INTEGER NOT NULL,
|
|
2415
|
+
completedAt INTEGER,
|
|
2416
|
+
callbackAttempts INTEGER NOT NULL DEFAULT 0,
|
|
2417
|
+
callbackDeliveredAt INTEGER
|
|
2418
|
+
);
|
|
2419
|
+
CREATE INDEX IF NOT EXISTS idx_conv ON transactions(conversationID);
|
|
2420
|
+
CREATE INDEX IF NOT EXISTS idx_created ON transactions(createdAt);
|
|
2421
|
+
`);
|
|
2422
|
+
class SqliteBackedStore extends InMemoryStore {
|
|
2423
|
+
constructor() {
|
|
2424
|
+
super();
|
|
2425
|
+
const rows = db.prepare("SELECT * FROM transactions ORDER BY createdAt DESC").all();
|
|
2426
|
+
for (const r of rows) super.put(r);
|
|
2427
|
+
this.on("change", (rec) => writeRow(rec));
|
|
2428
|
+
this.on("clear", () => db.exec("DELETE FROM transactions"));
|
|
2429
|
+
}
|
|
2430
|
+
close() {
|
|
2431
|
+
db.close();
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
const writeStmt = db.prepare(`
|
|
2435
|
+
INSERT INTO transactions (
|
|
2436
|
+
checkoutRequestID, merchantRequestID, conversationID, originatorConversationID, kind,
|
|
2437
|
+
amount, phoneNumber, shortCode, callbackUrl, state, resultCode, resultDesc,
|
|
2438
|
+
mpesaReceiptNumber, createdAt, completedAt, callbackAttempts, callbackDeliveredAt
|
|
2439
|
+
) VALUES (
|
|
2440
|
+
@checkoutRequestID, @merchantRequestID, @conversationID, @originatorConversationID, @kind,
|
|
2441
|
+
@amount, @phoneNumber, @shortCode, @callbackUrl, @state, @resultCode, @resultDesc,
|
|
2442
|
+
@mpesaReceiptNumber, @createdAt, @completedAt, @callbackAttempts, @callbackDeliveredAt
|
|
2443
|
+
)
|
|
2444
|
+
ON CONFLICT(checkoutRequestID) DO UPDATE SET
|
|
2445
|
+
state=excluded.state,
|
|
2446
|
+
resultCode=excluded.resultCode,
|
|
2447
|
+
resultDesc=excluded.resultDesc,
|
|
2448
|
+
mpesaReceiptNumber=excluded.mpesaReceiptNumber,
|
|
2449
|
+
completedAt=excluded.completedAt,
|
|
2450
|
+
callbackAttempts=excluded.callbackAttempts,
|
|
2451
|
+
callbackDeliveredAt=excluded.callbackDeliveredAt
|
|
2452
|
+
`);
|
|
2453
|
+
function writeRow(rec) {
|
|
2454
|
+
writeStmt.run({
|
|
2455
|
+
checkoutRequestID: rec.checkoutRequestID,
|
|
2456
|
+
merchantRequestID: rec.merchantRequestID,
|
|
2457
|
+
conversationID: rec.conversationID ?? null,
|
|
2458
|
+
originatorConversationID: rec.originatorConversationID ?? null,
|
|
2459
|
+
kind: rec.kind,
|
|
2460
|
+
amount: rec.amount,
|
|
2461
|
+
phoneNumber: rec.phoneNumber,
|
|
2462
|
+
shortCode: rec.shortCode,
|
|
2463
|
+
callbackUrl: rec.callbackUrl ?? null,
|
|
2464
|
+
state: rec.state,
|
|
2465
|
+
resultCode: rec.resultCode ?? null,
|
|
2466
|
+
resultDesc: rec.resultDesc ?? null,
|
|
2467
|
+
mpesaReceiptNumber: rec.mpesaReceiptNumber ?? null,
|
|
2468
|
+
createdAt: rec.createdAt,
|
|
2469
|
+
completedAt: rec.completedAt ?? null,
|
|
2470
|
+
callbackAttempts: rec.callbackAttempts,
|
|
2471
|
+
callbackDeliveredAt: rec.callbackDeliveredAt ?? null
|
|
2472
|
+
});
|
|
2473
|
+
}
|
|
2474
|
+
return new SqliteBackedStore();
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
// src/cli.ts
|
|
2478
|
+
init_defaults();
|
|
2479
|
+
var VERSION = "0.1.0";
|
|
2480
|
+
var program = new Command();
|
|
2481
|
+
program.name("mpesa-mock").description("Local M-Pesa Daraja API emulator").version(VERSION).option("-p, --port <number>", "port to listen on", String(DEFAULTS.port)).option("-h, --host <host>", "host to bind", DEFAULTS.host).option("-d, --delay <ms>", "default callback delay in milliseconds").option("-c, --config <path>", "path to mpesa-mock.config.json").option("--persist <path>", "SQLite database path (transactions survive restart)").option("--record <path>", "append every request/response to a file").option("--replay <path>", "replay a recorded session (transactions only)").option("-q, --quiet", "disable per-request logging").option("--ui", "launch terminal UI dashboard (live transaction view)").action(async (opts) => {
|
|
2482
|
+
await run(opts);
|
|
2483
|
+
});
|
|
2484
|
+
program.parse();
|
|
2485
|
+
async function run(opts) {
|
|
2486
|
+
const port = Number(opts.port);
|
|
2487
|
+
if (!Number.isFinite(port) || port < 1 || port > 65535) {
|
|
2488
|
+
console.error(pc.red(`Invalid port: ${opts.port}`));
|
|
2489
|
+
process.exit(1);
|
|
2490
|
+
}
|
|
2491
|
+
let configOverrides = {};
|
|
2492
|
+
if (opts.config) {
|
|
2493
|
+
const cfgPath = resolve(opts.config);
|
|
2494
|
+
if (!existsSync(cfgPath)) {
|
|
2495
|
+
console.error(pc.red(`Config file not found: ${cfgPath}`));
|
|
2496
|
+
process.exit(1);
|
|
2497
|
+
}
|
|
2498
|
+
try {
|
|
2499
|
+
configOverrides = JSON.parse(readFileSync(cfgPath, "utf-8"));
|
|
2500
|
+
} catch (err) {
|
|
2501
|
+
console.error(pc.red(`Failed to parse config: ${err.message}`));
|
|
2502
|
+
process.exit(1);
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
if (opts.delay !== void 0) {
|
|
2506
|
+
configOverrides.defaultCallbackDelayMs = Number(opts.delay);
|
|
2507
|
+
}
|
|
2508
|
+
const store = opts.persist ? await createSqliteStore(resolve(opts.persist)) : void 0;
|
|
2509
|
+
let recorder;
|
|
2510
|
+
if (opts.record) {
|
|
2511
|
+
const recordPath = resolve(opts.record);
|
|
2512
|
+
writeFileSync(recordPath, "", { flag: "a" });
|
|
2513
|
+
recorder = (e) => {
|
|
2514
|
+
appendFileSync(recordPath, JSON.stringify(e) + "\n");
|
|
2515
|
+
};
|
|
2516
|
+
}
|
|
2517
|
+
const { app, dispatcher } = createServer({
|
|
2518
|
+
config: configOverrides,
|
|
2519
|
+
quiet: opts.quiet ?? false,
|
|
2520
|
+
...recorder ? { recorder } : {},
|
|
2521
|
+
...store ? { store } : {}
|
|
2522
|
+
});
|
|
2523
|
+
const server = serve({ fetch: app.fetch, port, hostname: opts.host }, (info) => {
|
|
2524
|
+
if (!opts.quiet) printBanner(port, info.address);
|
|
2525
|
+
});
|
|
2526
|
+
if (opts.ui) {
|
|
2527
|
+
const { startTui: startTui2 } = await Promise.resolve().then(() => (init_dashboard2(), dashboard_exports)).catch(() => ({ startTui: void 0 }));
|
|
2528
|
+
if (startTui2) {
|
|
2529
|
+
try {
|
|
2530
|
+
const { store: liveStore, dispatcher: liveDispatcher } = await (await Promise.resolve().then(() => (init_server(), server_exports))).createServer({ config: configOverrides });
|
|
2531
|
+
void liveStore;
|
|
2532
|
+
void liveDispatcher;
|
|
2533
|
+
} catch {
|
|
2534
|
+
}
|
|
2535
|
+
}
|
|
2536
|
+
}
|
|
2537
|
+
if (opts.replay) {
|
|
2538
|
+
void replayFile(resolve(opts.replay));
|
|
2539
|
+
}
|
|
2540
|
+
const shutdown = () => {
|
|
2541
|
+
dispatcher.shutdown();
|
|
2542
|
+
server.close(() => process.exit(0));
|
|
2543
|
+
setTimeout(() => process.exit(0), 2e3).unref();
|
|
2544
|
+
};
|
|
2545
|
+
process.on("SIGINT", shutdown);
|
|
2546
|
+
process.on("SIGTERM", shutdown);
|
|
2547
|
+
}
|
|
2548
|
+
function printBanner(port, host) {
|
|
2549
|
+
const base = `http://localhost:${port}`;
|
|
2550
|
+
console.log("");
|
|
2551
|
+
console.log(` ${pc.green("\u{1F7E2}")} ${pc.bold("mpesa-mock")} ${pc.dim(`v${VERSION}`)} ${pc.dim("running on")} ${pc.cyan(base)}`);
|
|
2552
|
+
console.log("");
|
|
2553
|
+
console.log(` ${pc.dim("Bound to:")} ${host}:${port}`);
|
|
2554
|
+
console.log(` ${pc.dim("Base URL:")} ${base}`);
|
|
2555
|
+
console.log(` ${pc.dim("OAuth:")} POST /oauth/v1/generate`);
|
|
2556
|
+
console.log(` ${pc.dim("STK Push:")} POST /mpesa/stkpush/v1/processrequest`);
|
|
2557
|
+
console.log(` ${pc.dim("Dashboard:")} ${pc.underline(`${base}/__mock__/dashboard`)}`);
|
|
2558
|
+
console.log("");
|
|
2559
|
+
console.log(` ${pc.dim("Try it:")}`);
|
|
2560
|
+
console.log(` ${pc.cyan(`curl ${base}/oauth/v1/generate?grant_type=client_credentials \\`)}`);
|
|
2561
|
+
console.log(` ${pc.cyan(` -u "test_key:test_secret"`)}`);
|
|
2562
|
+
console.log("");
|
|
2563
|
+
console.log(` ${pc.dim("Docs:")} https://github.com/smbugua/mpesa-mock#readme`);
|
|
2564
|
+
console.log(` ${pc.dim("Issues:")} https://github.com/smbugua/mpesa-mock/issues`);
|
|
2565
|
+
console.log("");
|
|
2566
|
+
}
|
|
2567
|
+
async function replayFile(path2) {
|
|
2568
|
+
if (!existsSync(path2)) {
|
|
2569
|
+
console.error(pc.yellow(`Replay file not found: ${path2}`));
|
|
2570
|
+
return;
|
|
2571
|
+
}
|
|
2572
|
+
console.log(pc.dim(`Replay mode: ${path2} (transactions will be re-emitted)`));
|
|
2573
|
+
}
|
|
2574
|
+
//# sourceMappingURL=cli.js.map
|