Setting Up M-Pesa Callbacks
Learn how to properly set up and handle M-Pesa callbacks in your application
Understanding M-Pesa Callbacks
When you initiate transactions with M-Pesa (such as STK Push, B2C transfers, or balance checks), M-Pesa sends asynchronous notifications to your application through callback URLs. Setting up these callbacks correctly is essential for proper transaction tracking and handling.
Important
All callback URLs must be publicly accessible HTTPS endpoints. M-Pesa will not send callbacks to localhost or HTTP URLs.
Types of Callbacks
The M-Pesa API provides several types of callbacks, each serving a different purpose:
1. STK Push Callback
Receives notification when an STK Push request completes (successful payment or failure).
2. B2C Callback
Notifies your application when a Business-to-Customer transfer completes.
3. Transaction Status Callback
Provides status updates for transaction queries.
4. Account Balance Callback
Returns the result of an account balance query.
5. Timeout Callbacks
For each operation above, there's a corresponding timeout callback that gets triggered if the original request times out.
Implementing Callbacks
Below are examples of how to implement each callback type using Express.js:
Note
Assuming we have a Transaction
model/table to store transaction details
and a Balance
model to store account balance information. Ensure you have
the necessary models/tables and database setup in place. Example is using
MongoDB with Mongoose. Adjust the code as per your database and ORM/ODM.
STK Push Callback
B2C Callback
Transaction Status Callback
Account Balance Callback
Handling Timeout Callbacks
Each operation has a corresponding timeout callback that gets triggered if M-Pesa doesn't receive a response from its systems within the expected time frame.
Setting Up Routes in Express
To make your callbacks accessible, set up the corresponding routes in your Express application:
Best Practices
Best Practices
-
Always return HTTP 200: M-Pesa expects a 200 OK response for all callbacks, even if you encounter errors in your processing logic.
-
Idempotency: Ensure your callback handlers are idempotent as M-Pesa may send the same callback multiple times.
-
Error Handling: Implement robust error handling in your callback functions.
-
Logging: Log all callbacks received for debugging and audit purposes.
-
Secure Endpoints: Validate that callbacks are genuinely from M-Pesa (though M-Pesa doesn't currently provide a validation mechanism).
-
Timeout Handling: Always implement timeout callback endpoints for each operation to handle potential timeouts.
Testing Callbacks
During development, you can use tools like ngrok to expose your local server to the internet, making it accessible for M-Pesa's sandbox callbacks:
Use the provided HTTPS URL as your callback endpoint in sandbox testing.
Troubleshooting
If you're not receiving callbacks:
- Check URL Accessibility: Ensure your callback URLs are publicly accessible.
- Verify HTTPS: M-Pesa only sends callbacks to HTTPS endpoints.
- Review Logs: Check your server logs for any errors in processing callbacks.
- Test Endpoints: Use a tool like Postman to test your callback endpoints with sample data.
- Confirm Request Structure: Verify that your request to M-Pesa included the correct callback URLs.