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/LICENSE
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Simon Mbugua
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
This project is NOT affiliated with, endorsed by, or sponsored by Safaricom PLC.
|
|
24
|
+
"M-PESA", "Daraja" and related marks are trademarks of Safaricom PLC. This is
|
|
25
|
+
a community-built developer tool for local testing only.
|
package/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# mpesa-mock
|
|
2
|
+
|
|
3
|
+
> **Local M-Pesa Daraja API emulator. Stop fighting the sandbox.**
|
|
4
|
+
|
|
5
|
+
[](https://github.com/smbugua/mpesa-mock/actions)
|
|
6
|
+
[](https://www.npmjs.com/package/mpesa-mock)
|
|
7
|
+
[](https://github.com/smbugua/mpesa-mock/pkgs/container/mpesa-mock)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npx mpesa-mock
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
That's it. STK Push, C2B, B2C, transaction status, account balance, reversal —
|
|
15
|
+
all running on `http://localhost:4000` in milliseconds, with deterministic
|
|
16
|
+
failure modes you can trigger from a phone number suffix.
|
|
17
|
+
|
|
18
|
+
## Why this exists
|
|
19
|
+
|
|
20
|
+
If you've built on Daraja, you know:
|
|
21
|
+
|
|
22
|
+
- **Safaricom's sandbox is slow** — multi-second OAuth, occasional 30s STK Push timeouts.
|
|
23
|
+
- **It's flaky** — random 500s, weeks-long outages, no status page.
|
|
24
|
+
- **It can't fail on demand** — try simulating "user cancelled on phone" or "callback delivery failed twice then succeeded." You can't.
|
|
25
|
+
- **It requires registration** — you can't `git clone && pnpm test` in a fresh CI container.
|
|
26
|
+
|
|
27
|
+
`mpesa-mock` runs locally, responds in milliseconds, requires zero registration,
|
|
28
|
+
and lets you trigger any failure mode by changing the test phone number.
|
|
29
|
+
|
|
30
|
+
## 60-second start
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# 1. start the mock
|
|
34
|
+
npx mpesa-mock
|
|
35
|
+
|
|
36
|
+
# 2. (another terminal) get a token
|
|
37
|
+
curl "http://localhost:4000/oauth/v1/generate?grant_type=client_credentials" \
|
|
38
|
+
-u "test_key:test_secret"
|
|
39
|
+
# → {"access_token":"...","expires_in":"3599"}
|
|
40
|
+
|
|
41
|
+
# 3. push to a phone
|
|
42
|
+
curl -X POST http://localhost:4000/mpesa/stkpush/v1/processrequest \
|
|
43
|
+
-H "Authorization: Bearer <token>" -H "Content-Type: application/json" \
|
|
44
|
+
-d '{
|
|
45
|
+
"BusinessShortCode":"174379","Password":"x","Timestamp":"20260513120000",
|
|
46
|
+
"TransactionType":"CustomerPayBillOnline","Amount":10,
|
|
47
|
+
"PartyA":"254712345600","PartyB":"174379","PhoneNumber":"254712345600",
|
|
48
|
+
"CallBackURL":"https://yourapp.test/cb",
|
|
49
|
+
"AccountReference":"INV1","TransactionDesc":"order"
|
|
50
|
+
}'
|
|
51
|
+
# → {"MerchantRequestID":"...","CheckoutRequestID":"ws_CO_...","ResponseCode":"0",...}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Your `CallBackURL` will receive the Daraja-shaped `stkCallback` body ~8 seconds
|
|
55
|
+
later. Open the live dashboard at <http://localhost:4000/__mock__/dashboard>.
|
|
56
|
+
|
|
57
|
+
## Failure modes — phone-suffix convention
|
|
58
|
+
|
|
59
|
+
The killer feature. **The last two digits of the `PhoneNumber` decide what happens:**
|
|
60
|
+
|
|
61
|
+
| Suffix | Behavior | `ResultCode` |
|
|
62
|
+
|--------|----------|--------------|
|
|
63
|
+
| `00` | ✅ Success (default) | `0` |
|
|
64
|
+
| `01` | User cancels on phone | `1032` |
|
|
65
|
+
| `02` | Insufficient funds | `1` |
|
|
66
|
+
| `03` | Wrong PIN | `2001` |
|
|
67
|
+
| `04` | Timeout — no callback ever sent | — |
|
|
68
|
+
| `05` | Callback delivery fails 3× then succeeds | `0` |
|
|
69
|
+
| `06` | Transaction expires | `1037` |
|
|
70
|
+
| `07` | Generic system error | `1025` |
|
|
71
|
+
| `99` | Slow — 30-second callback delay | `0` |
|
|
72
|
+
|
|
73
|
+
Override per-number or globally with `mpesa-mock.config.json` (see
|
|
74
|
+
[`mpesa-mock.config.example.json`](./mpesa-mock.config.example.json)).
|
|
75
|
+
|
|
76
|
+
## CLI
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
npx mpesa-mock # :4000, defaults
|
|
80
|
+
npx mpesa-mock --port 4001
|
|
81
|
+
npx mpesa-mock --delay 0 # instant callbacks for tests
|
|
82
|
+
npx mpesa-mock --persist ./data.db # SQLite — transactions survive restart
|
|
83
|
+
npx mpesa-mock --record ./session.jsonl # capture every request/response
|
|
84
|
+
npx mpesa-mock --quiet # CI mode, no logs
|
|
85
|
+
npx mpesa-mock --config ./mpesa-mock.config.json
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Docker
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
docker run -p 4000:4000 ghcr.io/smbugua/mpesa-mock:latest
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Or drop into your `docker-compose.yml`:
|
|
95
|
+
|
|
96
|
+
```yaml
|
|
97
|
+
services:
|
|
98
|
+
mpesa-mock:
|
|
99
|
+
image: ghcr.io/smbugua/mpesa-mock:latest
|
|
100
|
+
ports: ["4000:4000"]
|
|
101
|
+
environment:
|
|
102
|
+
MPESA_MOCK_DELAY: "1000"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Endpoints
|
|
106
|
+
|
|
107
|
+
All match Daraja's response shapes byte-for-byte — including
|
|
108
|
+
`expires_in` as a string, the nested `Body.stkCallback` envelope, and the
|
|
109
|
+
`Item[]` array on `CallbackMetadata`.
|
|
110
|
+
|
|
111
|
+
| Endpoint | Method | Status |
|
|
112
|
+
|----------|--------|--------|
|
|
113
|
+
| `/oauth/v1/generate` | GET | ✅ |
|
|
114
|
+
| `/mpesa/stkpush/v1/processrequest` | POST | ✅ |
|
|
115
|
+
| `/mpesa/stkpushquery/v1/query` | POST | ✅ |
|
|
116
|
+
| `/mpesa/c2b/v1/registerurl` | POST | ✅ |
|
|
117
|
+
| `/mpesa/c2b/v1/simulate` | POST | ✅ |
|
|
118
|
+
| `/mpesa/b2c/v1/paymentrequest` | POST | ✅ |
|
|
119
|
+
| `/mpesa/b2b/v1/paymentrequest` | POST | ✅ |
|
|
120
|
+
| `/mpesa/transactionstatus/v1/query` | POST | ✅ |
|
|
121
|
+
| `/mpesa/accountbalance/v1/query` | POST | ✅ |
|
|
122
|
+
| `/mpesa/reversal/v1/request` | POST | ✅ |
|
|
123
|
+
|
|
124
|
+
Mock-only helpers (clearly namespaced under `/__mock__/`):
|
|
125
|
+
|
|
126
|
+
- `GET /__mock__/dashboard` — live transaction view with SSE
|
|
127
|
+
- `GET /__mock__/state` — JSON snapshot for tests
|
|
128
|
+
- `GET /__mock__/events` — server-sent events stream
|
|
129
|
+
- `POST /__mock__/clear` — reset state
|
|
130
|
+
- `POST /__mock__/c2b/trigger` — programmatic C2B simulation
|
|
131
|
+
|
|
132
|
+
## Framework examples
|
|
133
|
+
|
|
134
|
+
Each is a complete, runnable project — `npx mpesa-mock` in one terminal, the
|
|
135
|
+
example in another, three commands to a callback in your code.
|
|
136
|
+
|
|
137
|
+
- [Express](./examples/node-express/) — minimal Node app
|
|
138
|
+
- [Next.js](./examples/nextjs/) — App Router with API route handlers
|
|
139
|
+
- [Python Flask](./examples/python-flask/) — proves it's HTTP, not framework-coupled
|
|
140
|
+
|
|
141
|
+
## What it doesn't do
|
|
142
|
+
|
|
143
|
+
- **Production.** It's a mock. Don't point production at it.
|
|
144
|
+
- **Real M-Pesa.** No real money moves. If you paste real consumer secrets in,
|
|
145
|
+
they're logged with a warning and ignored.
|
|
146
|
+
- **Cheat Safaricom.** Same point — this is a developer tool, not a payment processor.
|
|
147
|
+
- **Tanzania / Vodacom M-Pesa.** Different API surface. PRs welcome.
|
|
148
|
+
|
|
149
|
+
## FAQ
|
|
150
|
+
|
|
151
|
+
**Is this affiliated with Safaricom?** No. It's a community tool. "M-PESA",
|
|
152
|
+
"Daraja", and related marks are trademarks of Safaricom PLC.
|
|
153
|
+
|
|
154
|
+
**Will Safaricom sue you?** We hope not — this never touches their production
|
|
155
|
+
infrastructure and never moves money. It's a local emulator for Daraja's
|
|
156
|
+
documented HTTP surface, the same way developers write mocks for Stripe or
|
|
157
|
+
Twilio.
|
|
158
|
+
|
|
159
|
+
**How is this different from [Pesa Playground](https://github.com/OmentaElvis/pesa-playground)?**
|
|
160
|
+
Different audience. Pesa Playground is a Rust+Tauri desktop GUI for interactive
|
|
161
|
+
manual testing — drive a fake SIM Toolkit with your mouse. `mpesa-mock` is a
|
|
162
|
+
headless HTTP server for **automated** testing — `npx`, Docker, CI green by
|
|
163
|
+
default. Use both: Pesa Playground for exploring, mpesa-mock for `pnpm test`.
|
|
164
|
+
|
|
165
|
+
**Does it match Daraja exactly?** That's the goal — including the awkward
|
|
166
|
+
parts. If you find a divergence, open an issue.
|
|
167
|
+
|
|
168
|
+
**Can I use it in CI?** Yes. `--quiet` mode + `--delay 0` gives you sub-second
|
|
169
|
+
test runs.
|
|
170
|
+
|
|
171
|
+
**Does it persist?** In-memory by default; pass `--persist ./data.db` for
|
|
172
|
+
SQLite-backed durability across restarts.
|
|
173
|
+
|
|
174
|
+
## Contributing
|
|
175
|
+
|
|
176
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md).
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
[MIT](./LICENSE). Not affiliated with Safaricom PLC.
|