This project is a vanilla/native PHP implementation of the M-Pesa API based on the Daraja documentation. It provides sample scripts for integrating with M-Pesa's C2B, B2C, B2B, STK Push, and Transaction Status APIs.
- MpesaPayments.php: Main class for handling authentication and API requests.
- C2B/: Customer to Business (C2B) endpoint handlers and simulation.
- B2C/: Business to Customer (B2C) request and callback handlers.
- B2B/: Business to Business (B2B) request and callback handlers.
- STKPUSH/: STK Push request and callback handlers.
- TransactionStatus/: Transaction status query and result handler.
- cert.cer: Example certificate file for security credentials.
All API requests require an access token. The MpesaPayments
class handles token generation:
$mpesa = new MpesaPayments();
$accessToken = $mpesa->GenerateAccessToken();
- Register C2B URLs and simulate a payment in
C2B/C2B.php
. - Validation and confirmation callbacks are handled by
C2B/validation_url.php
andC2B/confirmation.php
.
- This endpoint enables developers to receive real time notifications when a client makes a payments to a merchant's Till number or Paybill number. It assumes the payment are made via the SIM card toolkit and as a developer you need to know when that payment hits the merchants till/paybill number for reconciliation and accounting purposes.
- Initiate a B2C payment using
B2C/B2CRequest.php
. - Callback responses are logged by
B2C/B2CResult.php
.
- Initiate a B2B payment using
B2B/B2BRequest.php
. - Callback responses are logged by
B2B/B2BResult.php
.
- Initiate an STK Push using
STKPUSH/StkPushRequest.php
. - Callback responses are handled by
STKPUSH/callback.php
.
- Query transaction status using
TransactionStatus/QueryTransactionStatus.php
. - Callback responses are handled by
TransactionStatus/TransactionStatusResult.php
.
- Update callback URLs in
MpesaPayments.php
to point to your server. - Database credentials in
C2B/confirmation.php
must be set for transaction logging. - Security credentials and certificates should be securely managed.