Overview

This platform provides seamless integration with M-Pesa's Daraja API, enabling businesses to accept and process mobile money payments.

Key Features

Authentication

All API requests require authentication using OAuth 2.0 Bearer tokens.

Generate Access Token

POST /api/generate_token.php

Response:

{ "success": true, "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJ..." }

Tokens are valid for 1 hour.

STK Push (Lipa Na M-Pesa Online)

STK Push sends a payment prompt directly to the customer's phone.

Request

POST /api/stk_push.php Parameters: - phone: Customer phone number (254XXXXXXXXX) - amount: Amount to charge (minimum 1) - reference: Account reference (alphanumeric) - description: Transaction description

Success Response

{ "status": 200, "response": { "MerchantRequestID": "29115-34620561-1", "CheckoutRequestID": "ws_CO_191220191020363925", "ResponseCode": "0", "ResponseDescription": "Success. Request accepted for processing", "CustomerMessage": "Success. Request accepted for processing" } }

Customer Experience

  1. Customer receives M-Pesa prompt on their phone
  2. Customer enters M-Pesa PIN
  3. Payment processed
  4. Both parties receive confirmation
  5. Callback sent to your server

C2B (Customer to Business) Payments

C2B allows customers to pay directly to your PayBill or Till number.

Step 1: Register URLs

POST /api/c2b_register.php

Register your validation and confirmation URLs (one-time setup).

Step 2: Customer Makes Payment

Customer uses M-Pesa menu:

  1. Lipa na M-Pesa
  2. Pay Bill or Buy Goods
  3. Enter business number
  4. Enter account number
  5. Enter amount
  6. Confirm with PIN

Step 3: Receive Callbacks

Your confirmation URL receives payment details:

{ "TransactionType": "Pay Bill", "TransID": "LGR019G3J2", "TransAmount": "100.00", "BusinessShortCode": "600126", "BillRefNumber": "account123", "MSISDN": "254708374149", "FirstName": "John" }

Response Codes

STK Push Response Codes

Code Description Action
0 Success Transaction completed successfully
1 Insufficient Balance Customer has insufficient funds
1032 Cancelled by User Customer cancelled the request
1037 Timeout User didn't enter PIN in time
2001 Invalid Initiator Check your credentials

HTTP Status Codes

Code Meaning
200 Success
400 Bad Request
401 Unauthorized
500 Server Error

Testing

Sandbox Environment

Current configuration uses Safaricom's sandbox for safe testing.

Parameter Value
Environment Sandbox
Test Phone Numbers 254708374149, 254712345678
Test Amounts 1 - 70,000 KES
Shortcode 174379

Best Practices

Need Help?

For assistance with integration or technical support: