SMARTPANEL M-PESA Integration

M-Pesa Payment Integration Made Simple

Accept M-Pesa payments on your website or app in minutes. No complex setup, just simple API calls.

Get Started Free

Getting Started

Before you start integrating M-Pesa payments, you need an API Key. Here's how to get one:

Create Account

Sign up for a free SMARTPANEL account at api.smartpanel.co.ke/user

Link Your M-Pesa Account

Go to Account Setup and connect your Till Number, Paybill, or Bank Account where you want to receive money

Get Your API Key

Navigate to Linked Accounts and copy your API Key. You'll use this in all your API requests

Start Accepting Payments

Use the API endpoints below to start collecting payments from your customers

Pro Tip: Test your integration using small amounts (KES 1) before going live. You can use your own phone number for testing.

Initiate M-Pesa Payment

Send a payment request to your customer's phone. They'll receive an M-Pesa prompt to enter their PIN and complete the payment.

API Endpoint

POST REQUEST
https://api.smartpanel.co.ke/backend/v1/initiatestk

What You Need to Send

Field What It Is Example Required?
api_key Your API key from SMARTPANEL dashboard nluhfjdgbmzfyjrwzsbqkwcsijb Yes
email 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

Simple PHP Example

Copy and paste this code into your PHP file
<?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'];
}
?>

What You'll Get Back (Success)

Success Response
{
    "success": "200",
    "massage": "Request sent sucessfully.",
    "transaction_request_id": "SOFTPID2809202423523970891"
}
What happens next?
1. Customer receives M-Pesa prompt on their phone
2. They enter PIN to approve payment
3. Money goes to your linked account
4. You get a notification via webhook (see below)

Check Payment Status

Use this to check if a payment was completed, canceled, or failed.

API Endpoint

POST REQUEST
https://api.smartpanel.co.ke/backend/transactionstatus

Simple PHP Example

Check if payment was completed
<?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']; } ?>

What You'll Get Back

Payment Completed
{
    "ResultCode": "200",
    "ResultDesc": "Success. Request accepted for processing",
    "TransactionID": "SOFTPID2809202423523970891",
    "TransactionStatus": "Completed",
    "TransactionReceipt": "SIS48OB9N4",
    "TransactionAmount": "100",
    "Msisdn": "254712345678",
    "TransactionReference": "ORDER_12345"
}

Get Instant Payment Notifications (Webhooks)

Instead of checking payment status repeatedly, let SMARTPANEL notify you automatically when payment completes.

Create a Webhook File

Create a PHP file (e.g., webhook.php) on your server that will receive payment notifications

Add Webhook URL to SMARTPANEL

Login to SMARTPANEL → Go to Settings → Add your webhook URL (e.g., https://yoursite.com/webhook.php)

Receive Automatic Notifications

When payment completes, SMARTPANEL automatically sends payment details to your webhook

Simple Webhook Example

webhook.php - Save this as a file on your server
<?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);
?>
Security Tips:
• Use HTTPS for your webhook URL (not HTTP)
• Validate all incoming data before processing
• Log all webhook calls for debugging
• Don't process the same transaction twice

Common Errors & Solutions

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

Ready to Start?

Get your API key and start accepting M-Pesa payments in the next 5 minutes!

Create Free Account View Code Examples
Need Help?
Join our WhatsApp community for support: Click here to join