Welcome to the M-Pesa STK Push Integration repository! This project demonstrates how to implement the M-Pesa STK (Sim Tool Kit) Push API to initiate payments directly from a user's mobile phone. This integration enables businesses to facilitate secure and convenient payment transactions via M-Pesa, one of the most popular mobile money transfer services.
This repository contains a PHP implementation of Safaricom's M-Pesa STK Push API, allowing users to make payments via M-Pesa directly from your web application. The integration uses a .env
file to securely manage sensitive information like API keys, ensuring a secure and adaptable setup.
- PHP 7.3 or later
- Composer
- Internet connection for NGROK (for testing callback functionality)
- Safaricom Developer Account (to access the M-Pesa API)
Clone the repository to your local machine using the following command:
git clone https://github.com/Maithy-a/Mpesastkpush.git
Install the required dependencies using Composer. Run the following command in the root of your project:
composer install
This will install the safaricom/mpesa
package for M-Pesa integration.
Create a .env
file in the root of your project to store your sensitive credentials. Use the following structure:
# .env
MPESA_CONSUMER_KEY=your_consumer_key
MPESA_CONSUMER_SECRET=your_consumer_secret
BUSINESS_SHORT_CODE=your_business_short_code
LIPA_NA_MPESA_ONLINE_PASSKEY=your_mpesa_online_passkey
CALLBACK_URL=https://your-ngrok-url.ngrok.io/callback.php
- MPESA_CONSUMER_KEY: Your M-Pesa consumer key from the Safaricom Developer Portal.
- MPESA_CONSUMER_SECRET: Your M-Pesa consumer secret from the Safaricom Developer Portal.
- BUSINESS_SHORT_CODE: The Paybill or Till Number provided by Safaricom.
- LIPA_NA_MPESA_ONLINE_PASSKEY: The passkey for your M-Pesa account.
- CALLBACK_URL: The URL triggered by Safaricom once a payment is processed. For testing, use NGROK to create a secure tunnel to your local machine.
To test the M-Pesa callback functionality, use NGROK to expose your local server to the internet. Follow these steps:
-
Install NGROK: Download and install NGROK from ngrok.com.
-
Run NGROK: In your terminal, run the following command to create a tunnel:
ngrok http 80
This will generate a URL like
https://your-ngrok-url.ngrok.io
. Replace theCALLBACK_URL
in your.env
file with this URL followed by the callback route (/callback.php
).Example:
CALLBACK_URL=https://your-ngrok-url.ngrok.io/callback.php
Start your PHP server and test the integration. Ensure that your NGROK tunnel is running for the callback URL to work.
php -S localhost:8000
Open your browser and navigate to http://localhost:8000
to initiate the payment process.
- The user enters their phone number and the amount to pay.
- When they click "Pay", the M-Pesa STK Push API is triggered, sending a prompt to the user's phone to approve the payment.
- Once the user approves, the M-Pesa API sends the payment status back to the
CALLBACK_URL
, which you can view using NGROK.
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributions are welcome! Feel free to open issues or submit pull requests.
Enjoy using this M-Pesa STK Push integration! If you encounter any issues, please reach out via the repository's issue tracker.