Description
TK MPesa Payment Gateway allows your WooCommerce customers to pay directly from their mobile phone via MPesa STK Push (Lipa Na MPesa Online).
This plugin is not affiliated with, endorsed by, or sponsored by Safaricom PLC. M-Pesa and Daraja are trademarks of Safaricom PLC, referenced here only to describe compatibility with their payment service.
How it works:
- A customer checks out and selects MPesa Payments, entering the MPesa number the STK prompt should be sent to.
- The plugin authenticates with the Safaricom Daraja API and sends an STK Push (
stkpush/v1/processrequest) request for the order total. - The customer receives a “Lipa Na MPesa” prompt directly on their phone and enters their MPesa PIN to approve it.
- Safaricom sends a payment-result callback to this site’s dedicated REST endpoint, protected by a per-install secret token so only genuine Safaricom callbacks are accepted.
- On a successful callback, the order is marked paid automatically and the confirmation – including the MPesa number that was billed – is recorded on the order.
If a customer doesn’t complete the prompt in time or the order is otherwise left unpaid, they can return to it later through WooCommerce’s standard “Pay for order” screen to retry.
Payment types:
- Paybill (
CustomerPayBillOnline) – pay into a Paybill number and account - Buy Goods Till (
CustomerBuyGoodsOnline) – pay into a Till number; the Till must be settling funds to a bank account - Configurable Short Code (your Paybill/Till number) and Payment To number, independent of the payment type selected
Checkout compatibility:
- Classic shortcode checkout
- WooCommerce Checkout Block, via a full
AbstractPaymentMethodTypeintegration - HPOS (High-Performance Order Storage) compatible
Payment tracking & admin tools:
- A dedicated MPesa Payments admin menu, separate from WooCommerce Orders, listing every STK Push request with its date, Request ID, linked Order ID, requesting MPesa number, amount, and payment status
- A Logs submenu that jumps straight to this plugin’s entries under WooCommerce Status Logs (source
tk-mpesa), useful when a payment doesn’t go through - Paid orders show the MPesa confirmation and billed phone number directly on the WooCommerce order screen, alongside the standard order details
Sandbox / test mode:
- A Test mode toggle switches the gateway to the Safaricom Daraja sandbox API for development, without touching your live credentials
Security:
- The MPesa callback endpoint requires a per-install secret token (generated on activation) before it will process any callback, preventing forged requests from marking an order as paid
- Callback payment amounts are compared using a tolerant float comparison rather than a loose
== - All checkout input is sanitised and all admin/checkout output is escaped
- Checkout requests are protected by WordPress nonces
Requirements:
- A Safaricom MPesa Paybill or Buy Goods Till number
- A registered Safaricom Daraja application (Consumer Key, Consumer Secret, PassKey)
- An HTTPS-enabled WordPress site (required by the MPesa callback)
External services
This plugin connects to the Safaricom Daraja API to process M-Pesa payments. This is required for the plugin’s core function – initiating an M-Pesa STK Push payment request to the customer’s phone and receiving confirmation that it was paid.
Two Daraja endpoints are used:
oauth/v1/generate– authenticates the site with Safaricom using the Consumer Key and Consumer Secret you enter in the gateway settings. No customer data is sent to this endpoint.mpesa/stkpush/v1/processrequest– initiates the STK Push. When a customer places an order and chooses this payment method at checkout, the following is sent: your configured Paybill/Till short code, the order amount, the customer’s phone number, a callback URL, and the WooCommerce order ID (as a reference). Safaricom then sends a payment-result callback to this site’s REST endpoint.
This plugin is not affiliated with, endorsed by, or sponsored by Safaricom PLC. Learn more: Daraja API Terms and Conditions, Safaricom Data Privacy Statement.
Screenshots













tk-mpesa)Installation
Uploading and activating
- In your WordPress dashboard, go to Plugins Add New Upload Plugin and choose the plugin zip file (or upload the
tk-paybill-and-till-checkout-for-mpesafolder to/wp-content/plugins/via FTP/SFTP). - Click Activate. WooCommerce must already be installed and active – if it isn’t, activation stops and tells you to install WooCommerce first.
Configuring the gateway
- Go to WooCommerce Settings Payments.
- Click Manage on the MPesa Gateway row.
- Check Enable MPesa Gateway.
- Enter your Pass Key, Consumer Key, and Consumer Secret from the Daraja portal.
- Select your Payment Type (Paybill or Buy Goods Till).
- Enter your Short Code and Payment To number.
- Enter a unique Callback URL name – a short slug only (e.g.
abc123xyz), not your website’s URL. No spaces, and it must not contain the word “mpesa”. The plugin builds the full callback link for you automatically. - Save changes.
Getting MPesa credentials
- Register a Paybill or Buy Goods Till with Safaricom MPesa.
- If using a Buy Goods Till, it must be settling funds to a bank account.
- Register an Administrator for your Paybill/Till through Safaricom.
- Use those credentials to create an App on the MPesa Daraja Portal.
- Your Pass Key, Consumer Key, and Consumer Secret will be provided.
Managing your credentials later
Return to WooCommerce Settings Payments MPesa Gateway any time to update your credentials, for example after Safaricom issues new keys or when moving from sandbox to live. Existing orders and payment history are unaffected by a credentials change. The Test mode checkbox switches between the Safaricom sandbox and live production API – make sure the credentials you’ve entered match whichever mode is enabled, since sandbox keys will not work in live mode and vice versa.
Viewing logs
If a payment doesn’t go through, check MPesa Payments Logs in your WordPress admin menu (or WooCommerce Status Logs, source tk-mpesa) for the exact error Safaricom’s API returned.
FAQ
-
Does this work with the WooCommerce Checkout Block?
-
Yes. Version 2.0.0 and above fully supports the WooCommerce Checkout Block via the
AbstractPaymentMethodTypeintegration. -
Can I test without going live?
-
Yes. Enable Test mode in the gateway settings to use the Safaricom Daraja sandbox API. Use sandbox credentials from the Daraja portal.
-
Why must my callback URL not contain “mpesa”?
-
Safaricom’s systems block callback URLs containing the word “mpesa” as a security measure.
-
Is this plugin PCI DSS compliant?
-
Yes. No card data is handled. All payment data flows directly between the customer’s phone and Safaricom’s servers. The plugin only initiates an STK Push request and receives a callback confirmation.
Reviews
There are no reviews for this plugin.
Contributors & Developers
“TK Paybill and Till Checkout for M-Pesa” is open source software. The following people have contributed to this plugin.
ContributorsTranslate “TK Paybill and Till Checkout for M-Pesa” into your language.
Interested in development?
Browse the code, check out the SVN repository, or subscribe to the development log by RSS.
Changelog
2.2.0
- Security: the M-Pesa callback endpoint now requires a per-install secret token (generated on activation) before processing any callback, preventing a forged request from marking an order as paid
- Security: callback amount comparison now uses a tolerant float comparison instead of loose
==
2.1.0
- Added activation check that requires WooCommerce to be active
- Added
uninstall.phpto clean up gateway settings and payment-request records on deletion - Fixed: invalid Author URI header, incorrect output-escaping order on the checkout description field, and removed the deprecated
load_plugin_textdomain()call - Improved error logging: token and STK Push failures now log the HTTP status code and full API response, at Error level, so failures are easy to diagnose from WooCommerce Status Logs
- Fixed: duplicate MPesa callbacks (Safaricom sometimes resends the same notification) are now recognised and logged quietly instead of as errors
- Added a “Logs” submenu under MPesa Payments, linking straight to this plugin’s WooCommerce log entries
2.0.0
- Added WooCommerce Checkout Blocks support
- Added HPOS (High-Performance Order Storage) compatibility
- Added sandbox/test mode with Daraja sandbox API URLs
- Fixed security: nonce verification on payment form
- Fixed security: input sanitisation on phone number field
- Fixed security: output escaping on all admin displays
- Fixed: broken
permission_callbackon REST callback endpoint - Fixed: nested function declarations promoted to class methods
- Fixed:
parse_tel()phone normalisation using regex (replaced brokenltrim()) - Fixed:
date()replaced withgmdate()for timezone correctness - Replaced file-based logging with WooCommerce logger (WooCommerce Status Logs)
1.0.0
- Initial release
