How okoth-george/mpesa_gateway works
Purpose and Runtime
This is a Node.js/Express REST gateway for Safaricom M-Pesa payments. It initiates STK Push requests, accepts asynchronous payment callbacks, exposes health and Swagger documentation, and standardizes errors/logging. Runtime configuration comes from environment variables, including M-Pesa credentials and callback URL.
Paths: server.js, app.js, config/env.js, .env.example
Process Entry and HTTP Composition
server.js is the executable server entry point, including startup/shutdown behavior. app.js constructs the Express application, installing common middleware, CORS, request parsing, API documentation, routing, and global error handling. The route aggregator establishes the API surface under /api.
Paths: server.js, app.js, routes/index.js
Public API Boundary
The HTTP boundary consists of health, payment initiation, and M-Pesa callback endpoints. Payment routes accept client-originated STK requests and externally-originated callback payloads; both are security- and reliability-sensitive ingress points. Swagger is configured as a separate documentation interface at /api-docs.
Paths: routes/payment.routes.js, controllers/health.controller.js, config/swagger.js
STK Push Orchestration
The primary synchronous payment flow is: client POST /api/payments/stkpush → route validation/handler → STK Push service → M-Pesa APIs → standardized success/error response. The STK service constructs M-Pesa-compatible request data, including timestamp/password material, and delegates OAuth token acquisition before making the STK request.
Paths: routes/payment.routes.js, services/stkPush.service.js, utils/timestamp.js
M-Pesa Integration Boundary
M-Pesa is the principal external service. OAuth authentication obtains access tokens using consumer credentials; the STK Push service uses those tokens plus shortcode, passkey, base URL, and callback configuration to invoke Safaricom endpoints. M-Pesa later calls the configured public callback URL after the customer acts on the phone prompt.
Paths: services/mpesaAuth.service.js, services/stkPush.service.js, config/mpesa.js
Callback and Transaction Lifecycle
The asynchronous flow is: M-Pesa callback → POST /api/payments/callback → callback parsing/status handling → transaction persistence or update. The transaction model defines the MongoDB persistence boundary for correlating initiation details and final payment status; callback processing is the point to extend for downstream orders, notifications, or receipts.
Paths: routes/payment.routes.js, models/transaction.model.js, services/stkPush.service.js
Cross-Cutting Reliability
Environment validation prevents running without required payment credentials. Shared response helpers provide API response consistency; global middleware converts failures into controlled HTTP responses; logging captures operational events and failures. MongoDB is implied by the transaction model, while M-Pesa and any database connection are infrastructure dependencies outside the Express process.
Paths: config/env.js, middlewares/errorHandler.js, utils/response.js, utils/logger.js
Main entry points: stkPush.service.js STK Push service · payment.routes.js Payment endpoints · app.js Express app · index.js API route aggregator
Components 18
Express Runtime
- Server entry · Starts and stops the HTTP server.
server.js - Express app · Installs middleware, routes, docs, and error handling.
app.js - Environment config · Loads and validates payment runtime settings.
config/env.js - API route aggregator · Mounts the API surface under /api.
routes/index.js - Health endpoint
controllers/health.controller.js - Swagger docs · Serves API documentation at /api-docs.
config/swagger.js - Global error handler · Converts failures into controlled HTTP responses.
middlewares/errorHandler.js - Response helpers · Standardizes success and error payloads.
utils/response.js - Operational logger
utils/logger.js
Payment Flow
- Payment endpoints · Handles STK Push and M-Pesa callback ingress.
routes/payment.routes.js - STK Push service · Builds STK requests and processes payment callbacks.
services/stkPush.service.js - Timestamp utility · Supplies M-Pesa timestamp material.
utils/timestamp.js - M-Pesa OAuth · Obtains access tokens using consumer credentials.
services/mpesaAuth.service.js - M-Pesa config · Provides credentials, shortcode, passkey, and callback settings.
config/mpesa.js - Transaction model · Persists initiation details and final payment status.
models/transaction.model.js
External Infrastructure
- Safaricom M-Pesa · Provides OAuth, STK Push, and asynchronous callbacks.
- MongoDB
- API clients · Initiates STK Push requests.
Connections 21
- Server entry → Express app: serves
- Environment config → Server entry: runtime settings
- Environment config → M-Pesa config: credentials and callback URL
- Express app → API route aggregator: mounts /api
- Express app → Swagger docs: serves /api-docs
- Express app → Global error handler: installs
- API route aggregator → Payment endpoints: mounts payments
- API route aggregator → Health endpoint: mounts health
- API clients → Payment endpoints: POST STK Push
- Safaricom M-Pesa → Payment endpoints: POST callback
- Payment endpoints → STK Push service: validates and dispatches
- STK Push service → Timestamp utility: builds request material
- STK Push service → M-Pesa OAuth: requests token
- M-Pesa OAuth → Safaricom M-Pesa: OAuth
- STK Push service → M-Pesa config: uses integration settings
- STK Push service → Safaricom M-Pesa: STK Push request
- STK Push service → Transaction model: creates or updates status
- Transaction model → MongoDB: persists
- Payment endpoints → Response helpers: standard responses
- STK Push service → Operational logger: logs events and failures
- Global error handler → Response helpers: formats errors