@bytegigs/daraja 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +14 -0
- package/README.md +4 -2
- package/dist/async-result.d.ts +47 -0
- package/dist/async-result.js +70 -0
- package/dist/callback.d.ts +23 -0
- package/dist/callback.js +44 -0
- package/dist/client.d.ts +219 -0
- package/dist/client.js +389 -0
- package/dist/credential.d.ts +7 -0
- package/dist/credential.js +20 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +42 -0
- package/dist/generated/schema.d.ts +1049 -0
- package/dist/generated/schema.js +5 -0
- package/dist/helpers.d.ts +24 -0
- package/dist/helpers.js +100 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +7 -0
- package/dist/results.d.ts +3 -0
- package/dist/results.js +15 -0
- package/package.json +39 -4
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Daraja timestamps are Kenyan time (EAT, UTC+3, no daylight saving),
|
|
3
|
+
* formatted YYYYMMDDHHmmss, whatever time zone your server runs in.
|
|
4
|
+
*/
|
|
5
|
+
export declare function darajaTimestamp(date?: Date): string;
|
|
6
|
+
/** Reads a Daraja timestamp (string or number, EAT) back into a Date. */
|
|
7
|
+
export declare function parseDarajaTimestamp(value: string | number): Date | null;
|
|
8
|
+
/** Base64 of shortcode + passkey + timestamp, as STK Push and STK Query require. */
|
|
9
|
+
export declare function stkPassword(shortCode: string, passkey: string, timestamp: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Turns the ways Kenyans write a phone number into Daraja's format:
|
|
12
|
+
* 0712 345 678, +254 712 345 678, 712345678 and 0112345678 all work.
|
|
13
|
+
*/
|
|
14
|
+
export declare function normalizePhone(input: string): string;
|
|
15
|
+
export declare function requireShortCode(field: string, value: string): string;
|
|
16
|
+
export declare function requireLength(field: string, value: string, max: number): string;
|
|
17
|
+
export declare function requireCallbackUrl(value: string, allowHttp: boolean, field?: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* The Daraja base URL. Your consumer secret travels to it, so it must be https,
|
|
20
|
+
* except for a mock on this machine. Trailing slashes are trimmed without a
|
|
21
|
+
* regular expression (a backtracking pattern here is a ReDoS risk).
|
|
22
|
+
*/
|
|
23
|
+
export declare function requireBaseUrl(value: string): string;
|
|
24
|
+
export declare function isLocalBaseUrl(baseUrl: string): boolean;
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { DarajaValidationError } from "./errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Daraja timestamps are Kenyan time (EAT, UTC+3, no daylight saving),
|
|
4
|
+
* formatted YYYYMMDDHHmmss, whatever time zone your server runs in.
|
|
5
|
+
*/
|
|
6
|
+
export function darajaTimestamp(date = new Date()) {
|
|
7
|
+
const eat = new Date(date.getTime() + 3 * 3600_000);
|
|
8
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
9
|
+
return (`${eat.getUTCFullYear()}${pad(eat.getUTCMonth() + 1)}${pad(eat.getUTCDate())}` +
|
|
10
|
+
`${pad(eat.getUTCHours())}${pad(eat.getUTCMinutes())}${pad(eat.getUTCSeconds())}`);
|
|
11
|
+
}
|
|
12
|
+
/** Reads a Daraja timestamp (string or number, EAT) back into a Date. */
|
|
13
|
+
export function parseDarajaTimestamp(value) {
|
|
14
|
+
const text = String(value);
|
|
15
|
+
const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/.exec(text);
|
|
16
|
+
if (!match)
|
|
17
|
+
return null;
|
|
18
|
+
const [, y, mo, d, h, mi, s] = match.map(Number);
|
|
19
|
+
const date = new Date(Date.UTC(y, mo - 1, d, h - 3, mi, s));
|
|
20
|
+
// Date.UTC rolls month 13 into the next year; only accept dates that round-trip.
|
|
21
|
+
return Number.isNaN(date.getTime()) || darajaTimestamp(date) !== text ? null : date;
|
|
22
|
+
}
|
|
23
|
+
/** Base64 of shortcode + passkey + timestamp, as STK Push and STK Query require. */
|
|
24
|
+
export function stkPassword(shortCode, passkey, timestamp) {
|
|
25
|
+
return Buffer.from(`${shortCode}${passkey}${timestamp}`, "utf8").toString("base64");
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Turns the ways Kenyans write a phone number into Daraja's format:
|
|
29
|
+
* 0712 345 678, +254 712 345 678, 712345678 and 0112345678 all work.
|
|
30
|
+
*/
|
|
31
|
+
export function normalizePhone(input) {
|
|
32
|
+
const digits = String(input).replace(/[\s\-().]/g, "").replace(/^\+/, "");
|
|
33
|
+
let national;
|
|
34
|
+
if (/^254[71]\d{8}$/.test(digits))
|
|
35
|
+
national = digits.slice(3);
|
|
36
|
+
else if (/^0[71]\d{8}$/.test(digits))
|
|
37
|
+
national = digits.slice(1);
|
|
38
|
+
else if (/^[71]\d{8}$/.test(digits))
|
|
39
|
+
national = digits;
|
|
40
|
+
else
|
|
41
|
+
throw new DarajaValidationError("phone", `"${input}" is not a Kenyan mobile number`);
|
|
42
|
+
return `254${national}`;
|
|
43
|
+
}
|
|
44
|
+
export function requireShortCode(field, value) {
|
|
45
|
+
const text = String(value ?? "").trim();
|
|
46
|
+
if (!/^\d{5,7}$/.test(text))
|
|
47
|
+
throw new DarajaValidationError(field, "must be a 5–7 digit paybill or till number");
|
|
48
|
+
return text;
|
|
49
|
+
}
|
|
50
|
+
export function requireLength(field, value, max) {
|
|
51
|
+
const text = String(value ?? "").trim();
|
|
52
|
+
if (!text)
|
|
53
|
+
throw new DarajaValidationError(field, "is required");
|
|
54
|
+
if (text.length > max)
|
|
55
|
+
throw new DarajaValidationError(field, `must be at most ${max} characters (got ${text.length})`);
|
|
56
|
+
return text;
|
|
57
|
+
}
|
|
58
|
+
export function requireCallbackUrl(value, allowHttp, field = "callbackUrl") {
|
|
59
|
+
let url;
|
|
60
|
+
try {
|
|
61
|
+
url = new URL(value);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
throw new DarajaValidationError(field, "must be a full URL");
|
|
65
|
+
}
|
|
66
|
+
if (url.protocol !== "https:" && !(allowHttp && url.protocol === "http:")) {
|
|
67
|
+
throw new DarajaValidationError(field, "must use https://");
|
|
68
|
+
}
|
|
69
|
+
return url.toString();
|
|
70
|
+
}
|
|
71
|
+
const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
72
|
+
/**
|
|
73
|
+
* The Daraja base URL. Your consumer secret travels to it, so it must be https,
|
|
74
|
+
* except for a mock on this machine. Trailing slashes are trimmed without a
|
|
75
|
+
* regular expression (a backtracking pattern here is a ReDoS risk).
|
|
76
|
+
*/
|
|
77
|
+
export function requireBaseUrl(value) {
|
|
78
|
+
let url;
|
|
79
|
+
try {
|
|
80
|
+
url = new URL(value);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
throw new DarajaValidationError("baseUrl", "must be a full URL");
|
|
84
|
+
}
|
|
85
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && LOCAL_HOSTS.has(url.hostname))) {
|
|
86
|
+
throw new DarajaValidationError("baseUrl", "must use https:// (http is allowed only for localhost)");
|
|
87
|
+
}
|
|
88
|
+
if (url.username || url.password || url.search || url.hash) {
|
|
89
|
+
throw new DarajaValidationError("baseUrl", "must not contain credentials, a query or a fragment");
|
|
90
|
+
}
|
|
91
|
+
let text = url.toString();
|
|
92
|
+
let end = text.length;
|
|
93
|
+
while (end > 0 && text.charCodeAt(end - 1) === 47 /* "/" */)
|
|
94
|
+
end--;
|
|
95
|
+
text = text.slice(0, end);
|
|
96
|
+
return text;
|
|
97
|
+
}
|
|
98
|
+
export function isLocalBaseUrl(baseUrl) {
|
|
99
|
+
return LOCAL_HOSTS.has(new URL(baseUrl).hostname);
|
|
100
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { BASE_URLS, Daraja } from "./client.js";
|
|
2
|
+
export type { AccountBalanceInput, AsyncAccepted, B2bInput, B2cInput, C2bRegisterInput, C2bRegisterUrlResponse, C2bSimulateInput, DarajaOptions, DynamicQrInput, DynamicQrResponse, ResultUrls, ReversalInput, TransactionStatusInput, StkPushInput, StkPushRequest, StkPushResponse, StkPushQueryResponse, StkQueryInput, StkStatus, } from "./client.js";
|
|
3
|
+
export { parseStkCallback } from "./callback.js";
|
|
4
|
+
export type { RawStkCallback, StkResult } from "./callback.js";
|
|
5
|
+
export { parseAccountBalance, parseAsyncResult, parseC2bTransaction } from "./async-result.js";
|
|
6
|
+
export type { AccountBalanceLine, AsyncResult, C2bPayment, RawAsyncResult, RawC2bTransaction } from "./async-result.js";
|
|
7
|
+
export { securityCredential } from "./credential.js";
|
|
8
|
+
export { DarajaError, DarajaValidationError } from "./errors.js";
|
|
9
|
+
export { darajaTimestamp, normalizePhone, parseDarajaTimestamp, stkPassword } from "./helpers.js";
|
|
10
|
+
export { describeStkResult, STK_RESULT_MEANINGS } from "./results.js";
|
|
11
|
+
export type { components, paths } from "./generated/schema.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { BASE_URLS, Daraja } from "./client.js";
|
|
2
|
+
export { parseStkCallback } from "./callback.js";
|
|
3
|
+
export { parseAccountBalance, parseAsyncResult, parseC2bTransaction } from "./async-result.js";
|
|
4
|
+
export { securityCredential } from "./credential.js";
|
|
5
|
+
export { DarajaError, DarajaValidationError } from "./errors.js";
|
|
6
|
+
export { darajaTimestamp, normalizePhone, parseDarajaTimestamp, stkPassword } from "./helpers.js";
|
|
7
|
+
export { describeStkResult, STK_RESULT_MEANINGS } from "./results.js";
|
package/dist/results.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Plain-language meanings for the STK Push result codes Daraja commonly returns. */
|
|
2
|
+
export const STK_RESULT_MEANINGS = {
|
|
3
|
+
0: "Paid",
|
|
4
|
+
1: "The customer's M-PESA balance was too low",
|
|
5
|
+
1001: "The customer already has a payment in progress",
|
|
6
|
+
1019: "The payment prompt expired",
|
|
7
|
+
1025: "Daraja could not send the prompt",
|
|
8
|
+
1032: "The customer cancelled the payment",
|
|
9
|
+
1037: "The customer's phone could not be reached, or they did not respond",
|
|
10
|
+
2001: "The customer entered the wrong PIN",
|
|
11
|
+
9999: "Daraja could not send the prompt",
|
|
12
|
+
};
|
|
13
|
+
export function describeStkResult(code) {
|
|
14
|
+
return STK_RESULT_MEANINGS[code] ?? `Not paid (Daraja result code ${code})`;
|
|
15
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bytegigs/daraja",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Typed Daraja (M-PESA) client for Node.js, generated from a sandbox-verified OpenAPI spec.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"NOTICE"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=20"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p ."
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"mpesa",
|
|
28
|
+
"daraja",
|
|
29
|
+
"safaricom",
|
|
30
|
+
"stk-push",
|
|
31
|
+
"lipa-na-mpesa",
|
|
32
|
+
"kenya",
|
|
33
|
+
"payments",
|
|
34
|
+
"openapi"
|
|
35
|
+
],
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/ByteGigs-Labs/daraja-openapi.git",
|
|
39
|
+
"directory": "packages/typescript"
|
|
40
|
+
}
|
|
41
|
+
}
|