M-PESA Integration
Accept M-Pesa payments on your website or app in minutes. No complex setup, just simple API calls.
Get Started FreeBefore you start integrating M-Pesa payments, you need an API Key. Here's how to get one:
Sign up for a free SMARTPANEL account at api.smartpanel.co.ke/user
Go to Account Setup and connect your Till Number, Paybill, or Bank Account where you want to receive money
Navigate to Linked Accounts and copy your API Key. You'll use this in all your API requests
Use the API endpoints below to start collecting payments from your customers
Send a payment request to your customer's phone. They'll receive an M-Pesa prompt to enter their PIN and complete the payment.
| Field | What It Is | Example | Required? |
|---|---|---|---|
| api_key | Your API key from SMARTPANEL dashboard | nluhfjdgbmzfyjrwzsbqkwcsijb | Yes |
| The email you use to login to SMARTPANEL | yourmail@gmail.com | Yes | |
| amount | How much money to request (in KES) | 100 | Yes |
| msisdn | Customer's phone number | 254712345678 or 0712345678 | Yes |
| reference | Your order/invoice number to track payment | ORDER_12345 | No |
<?php
// Your API credentials
$api_key = "YOUR_API_KEY_HERE"; // Get this from SMARTPANEL dashboard
$email = "your@email.com"; // Your SMARTPANEL login email
// Payment details
$customer_phone = "254712345678"; // Customer's M-Pesa number
$amount = 100; // Amount in KES
$order_number = "ORDER_12345"; // Your order/reference number
// Prepare the data
$payment_data = array(
"api_key" => $api_key,
"email" => $email,
"msisdn" => $customer_phone,
"amount" => $amount,
"reference" => $order_number
);
// Send request to SMARTPANEL
$url = "https://api.smartpanel.co.ke/backend/v1/initiatestk";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payment_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
// Check if payment was sent
if ($result['success'] == '200') {
echo "Payment request sent! Transaction ID: " . $result['transaction_request_id'];
// Save this transaction_request_id to check payment status later
} else {
echo "Error: " . $result['message'];
}
?>
{
"success": "200",
"massage": "Request sent sucessfully.",
"transaction_request_id": "SOFTPID2809202423523970891"
}
Use this to check if a payment was completed, canceled, or failed.
<?php
// Your API credentials
$api_key = "YOUR_API_KEY_HERE";
$email = "your@email.com";
// The transaction ID you got when initiating payment
$transaction_id = "SOFTPID2809202423523970891";
// Prepare the data
$status_data = array(
"api_key" => $api_key,
"email" => $email,
"transaction_request_id" => $transaction_id
);
// Send request
$url = "https://api.smartpanel.co.ke/backend/transactionstatus";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($status_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
// Check payment status
if ($result['TransactionStatus'] == 'Completed') {
echo "Payment received! M-Pesa Receipt: " . $result['TransactionReceipt'];
echo "
Amount: KES " . $result['TransactionAmount'];
} else {
echo "Payment status: " . $result['TransactionStatus'];
}
?>
{
"ResultCode": "200",
"ResultDesc": "Success. Request accepted for processing",
"TransactionID": "SOFTPID2809202423523970891",
"TransactionStatus": "Completed",
"TransactionReceipt": "SIS48OB9N4",
"TransactionAmount": "100",
"Msisdn": "254712345678",
"TransactionReference": "ORDER_12345"
}
Instead of checking payment status repeatedly, let SMARTPANEL notify you automatically when payment completes.
Create a PHP file (e.g., webhook.php) on your server that will receive payment notifications
Login to SMARTPANEL → Go to Settings → Add your webhook URL (e.g., https://yoursite.com/webhook.php)
When payment completes, SMARTPANEL automatically sends payment details to your webhook
<?php
// webhook.php - Receives payment notifications from SMARTPANEL
// Get the payment data sent by SMARTPANEL
$payment_data = file_get_contents('php://input');
$payment = json_decode($payment_data, true);
// Save to log file for debugging
file_put_contents('payments.log', date('Y-m-d H:i:s') . " - " . $payment_data . "\n", FILE_APPEND);
// Check if payment was successful
if ($payment['ResponseCode'] == 0) {
// Payment successful!
$transaction_id = $payment['TransactionID'];
$amount = $payment['TransactionAmount'];
$mpesa_receipt = $payment['TransactionReceipt'];
$phone = $payment['Msisdn'];
$your_reference = $payment['TransactionReference'];
// Update your database
// Example: Mark order as paid
$conn = new mysqli("localhost", "username", "password", "database");
$stmt = $conn->prepare("UPDATE orders SET status='paid', mpesa_receipt=? WHERE order_id=?");
$stmt->bind_param("ss", $mpesa_receipt, $your_reference);
$stmt->execute();
// Send confirmation SMS/Email to customer
// Your code here...
echo "Payment processed successfully";
} else {
// Payment failed or canceled
$error_message = $payment['ResponseDescription'];
// Log the failure
file_put_contents('payment_failures.log',
date('Y-m-d H:i:s') . " - " . $error_message . "\n",
FILE_APPEND
);
echo "Payment failed: " . $error_message;
}
// Always return 200 OK to acknowledge receipt
http_response_code(200);
?>
| Error Code | What It Means | Solution |
|---|---|---|
| 0 | Success! Payment completed | Everything is good ✓ |
| 1 | Customer doesn't have enough money | Ask customer to add money to M-Pesa |
| 1032 | Customer canceled the payment | Ask customer to try again |
| 1037 | Customer's phone is off or no signal | Customer should check their phone |
| 1019 | Customer took too long to enter PIN | Resend payment request |
Get your API key and start accepting M-Pesa payments in the next 5 minutes!