@bytegigs/daraja 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -2
- package/dist/callback.d.ts +4 -1
- package/dist/callback.js +8 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,77 @@
|
|
|
1
1
|
# @bytegigs/daraja
|
|
2
2
|
|
|
3
|
-
Typed Daraja (M-PESA) client for Node.js 20+, generated from
|
|
3
|
+
Typed Daraja (M-PESA) client for Node.js 20+, generated from an OpenAPI spec that is checked against Safaricom's sandbox every night. No runtime dependencies.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**Docs:** https://bytegigs-labs.github.io/daraja-openapi/sdks/typescript/
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @bytegigs/daraja
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Take a payment
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Daraja } from "@bytegigs/daraja";
|
|
15
|
+
|
|
16
|
+
const daraja = new Daraja({
|
|
17
|
+
consumerKey: process.env.DARAJA_CONSUMER_KEY,
|
|
18
|
+
consumerSecret: process.env.DARAJA_CONSUMER_SECRET,
|
|
19
|
+
environment: "sandbox",
|
|
20
|
+
shortCode: "174379",
|
|
21
|
+
passkey: process.env.DARAJA_PASSKEY,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const push = await daraja.stkPush({
|
|
25
|
+
amount: 150,
|
|
26
|
+
phone: "0712 345 678",
|
|
27
|
+
callbackUrl: "https://your-app.example/daraja/callback",
|
|
28
|
+
accountReference: "INV-1001",
|
|
29
|
+
description: "Invoice 1001",
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The client fetches and caches the access token, builds the STK password and timestamp in Kenyan time, and normalises the phone number. STK Push is never retried for you: a retry could prompt the customer twice.
|
|
34
|
+
|
|
35
|
+
## Confirm it
|
|
36
|
+
|
|
37
|
+
Daraja's callbacks are **not signed**, so confirm with Daraja before you deliver anything:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { parseStkCallback } from "@bytegigs/daraja";
|
|
41
|
+
|
|
42
|
+
app.post("/daraja/callback", async (req, res) => {
|
|
43
|
+
res.sendStatus(200); // answer Daraja first
|
|
44
|
+
const result = parseStkCallback(req.body);
|
|
45
|
+
if (!result) return; // not an STK Push result: ignore it
|
|
46
|
+
try {
|
|
47
|
+
const confirmed = await daraja.stkQuery({ checkoutRequestId: result.checkoutRequestId });
|
|
48
|
+
if (confirmed.state === "paid") await markOrderPaid(result.checkoutRequestId, result.receipt);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
// A 429 or "still processing": leave the order open and check again later.
|
|
51
|
+
console.warn("payment not confirmed yet:", error.message);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Some callbacks never arrive. `waitForStkResult` polls STK Push Query politely (it waits before the first query and backs off on 429), which also recovers those payments:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
const result = await daraja.waitForStkResult({ checkoutRequestId: push.CheckoutRequestID });
|
|
60
|
+
if (result.state === "failed") console.log(result.meaning); // "The customer cancelled the payment"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Test without the sandbox
|
|
64
|
+
|
|
65
|
+
[`@bytegigs/daraja-mock`](https://www.npmjs.com/package/@bytegigs/daraja-mock) answers like Daraja and sends real callbacks; the phone number picks the outcome. Point the client at it with `baseUrl`:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const daraja = new Daraja({ consumerKey: "mock-consumer-key", consumerSecret: "mock-consumer-secret", baseUrl: "http://127.0.0.1:4010", shortCode: "174379", passkey: process.env.DARAJA_PASSKEY });
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
In GitHub Actions, use the [Daraja Mock Server](https://github.com/marketplace/actions/daraja-mock-server) action.
|
|
72
|
+
|
|
73
|
+
## Also in the box
|
|
74
|
+
|
|
75
|
+
B2C, B2B, reversal, transaction status, account balance, C2B and Dynamic QR, plus `parseAsyncResult`, `parseC2bTransaction`, `securityCredential` and `DarajaError` with `isRateLimited`, `retryAfterMs` and `isPending`. See the [reference](https://bytegigs-labs.github.io/daraja-openapi/reference/access-token/).
|
|
76
|
+
|
|
77
|
+
Apache 2.0. Not made or endorsed by Safaricom.
|
package/dist/callback.d.ts
CHANGED
|
@@ -17,7 +17,10 @@ export interface StkResult {
|
|
|
17
17
|
/**
|
|
18
18
|
* Turns the callback Daraja POSTs to your CallBackURL into a flat, typed result.
|
|
19
19
|
*
|
|
20
|
+
* Returns `null` for a body that isn't an STK Push result, like the other parsers
|
|
21
|
+
* and the other SDKs, so a stray POST to your callback URL can't throw.
|
|
22
|
+
*
|
|
20
23
|
* Daraja callbacks are not signed: treat this as a notification, and confirm
|
|
21
24
|
* with `stkQuery` (or a verifying gateway) before you deliver goods.
|
|
22
25
|
*/
|
|
23
|
-
export declare function parseStkCallback(body: unknown): StkResult;
|
|
26
|
+
export declare function parseStkCallback(body: unknown): StkResult | null;
|
package/dist/callback.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { DarajaValidationError } from "./errors.js";
|
|
2
1
|
import { parseDarajaTimestamp } from "./helpers.js";
|
|
3
2
|
import { describeStkResult } from "./results.js";
|
|
4
3
|
/**
|
|
5
4
|
* Turns the callback Daraja POSTs to your CallBackURL into a flat, typed result.
|
|
6
5
|
*
|
|
6
|
+
* Returns `null` for a body that isn't an STK Push result, like the other parsers
|
|
7
|
+
* and the other SDKs, so a stray POST to your callback URL can't throw.
|
|
8
|
+
*
|
|
7
9
|
* Daraja callbacks are not signed: treat this as a notification, and confirm
|
|
8
10
|
* with `stkQuery` (or a verifying gateway) before you deliver goods.
|
|
9
11
|
*/
|
|
10
12
|
export function parseStkCallback(body) {
|
|
11
13
|
const callback = body?.Body?.stkCallback;
|
|
12
|
-
if (!callback || typeof callback !== "object")
|
|
13
|
-
|
|
14
|
-
}
|
|
14
|
+
if (!callback || typeof callback !== "object")
|
|
15
|
+
return null;
|
|
15
16
|
const { MerchantRequestID, CheckoutRequestID, ResultCode, ResultDesc } = callback;
|
|
16
|
-
if (typeof CheckoutRequestID !== "string" || !CheckoutRequestID)
|
|
17
|
-
|
|
18
|
-
}
|
|
17
|
+
if (typeof CheckoutRequestID !== "string" || !CheckoutRequestID)
|
|
18
|
+
return null;
|
|
19
19
|
const code = Number(ResultCode);
|
|
20
20
|
if (!Number.isInteger(code))
|
|
21
|
-
|
|
21
|
+
return null;
|
|
22
22
|
const items = new Map();
|
|
23
23
|
for (const item of callback.CallbackMetadata?.Item ?? []) {
|
|
24
24
|
if (item && typeof item.Name === "string")
|