Daraja STK Push · v1.0

M-Pesa Payments for
Paid Memberships Pro

Accept M-Pesa on your WordPress membership site using Safaricom's Daraja API. Customers receive a PIN prompt directly on their phone — no manual payment steps required.

⬇ Download Latest Release View on GitHub
STK
Push-based checkout
25
Automated tests
0
SQL injection risks
2
API environments (sandbox + live)

Everything you need to go live

Built to the latest Paid Memberships Pro gateway standard and the Daraja v1 API specification.

📲

STK Push checkout

Customers enter their phone number and approve the payment from an M-Pesa PIN prompt — no app switching or manual "pay to till" instructions.

🔄

Automatic status polling

mpesa_stk_query() checks the Daraja Query API when the async callback hasn't arrived yet, so checkout completes reliably on the second form submit.

🔒

Secure callback URL

Every Safaricom server-to-server call is validated against a site-specific secret UID using hash_equals(), preventing replay attacks.

🛡️

SQL-injection free

All database operations use wpdb->prepare(), wpdb->insert(), and wpdb->update(). No raw string interpolation.

♻️

WordPress HTTP API

Uses wp_remote_get/post() instead of raw cURL — respects WordPress proxy settings and integrates with existing HTTP filter hooks.

🧪

25 PHPUnit tests

Ships with a complete test suite (no extra packages required). Covers phone normalisation, access token, STK push/query, callbacks, and the IPN listener.

How the checkout works

Two-click M-Pesa payment — no redirect, no manual steps.

📱
Enter phone
Customer types their M-Pesa number & clicks Submit
🚀
STK Push sent
Plugin calls the Daraja API instantly
🔔
PIN prompt
Customer sees the M-Pesa PIN popup on their phone
Callback received
Safaricom POSTs result; DB updated in real time
🎉
Membership activated
Customer re-submits; order confirmed & access granted

Set up in 4 steps

1

Download & activate

Download the latest release zip from GitHub, upload via WordPress Plugins → Add New → Upload.

2

Get Daraja credentials

Log in to developer.safaricom.co.ke and grab your Consumer Key, Consumer Secret, and Passkey.

3

Configure the gateway

Go to Memberships → Payment Settings, select M-Pesa (Daraja), and fill in your credentials.

4

Register the callback URL

Copy the Callback URL shown in the settings page and add it to your Daraja app configuration.

Setting Where to find it Required?
mpesa_short_codeYour Safaricom paybill or till numberYes
mpesa_consumer_keyDaraja developer portal → your appYes
mpesa_consumer_secretDaraja developer portal → your appYes
mpesa_passkeyDaraja → Lipa Na M-Pesa Online passkeyYes
gateway_environmentSet to sandbox for testing, live for productionYes

API functions

All public functions are available globally after the plugin is loaded.

PHP
// Get a Daraja OAuth 2.0 access token
$token = mpesa_get_access_token(); // string|false

// Initiate an STK Push
$result = mpesa_stk_push(
    '254712345678',  // phone (international format)
    1500,            // amount (KES)
    'ORD-20240101',  // account reference (≤12 chars)
    'My Site'        // description (≤13 chars, optional)
);
// $result->CheckoutRequestID — save this!
// $result->ResponseCode === '0' means push was accepted

// Query status of a pending push
$status = mpesa_stk_query( $result->CheckoutRequestID );
// $status->ResultCode === '0' means payment confirmed
// $status->ResultCode === '1032' means user cancelled

Ready to accept M-Pesa?

Download the latest release and have M-Pesa running on your membership site in minutes.