+254725682556 +254725052660 info@ziprof.co.ke
Client Area

WordPress

How to Add M-Pesa Payments to Your WordPress Website

If you run a WordPress website in Kenya and want customers to pay you directly on your site, M-Pesa is usually the payment method your customers already trust and use daily. This guide walks you through connecting M-Pesa to your WordPress website, whether you're selling products through an online store or simply want customers to pay for a service.

Before You Start

To accept M-Pesa payments programmatically on your website, you need three things:

  • A WordPress website with an active SSL certificate. Safaricom's payment API requires a secure (https://) callback URL, so your site must have SSL installed and working before you begin.
  • A Safaricom Daraja API account, which you register for free on Safaricom's developer portal.
  • If you're selling products, the WooCommerce plugin installed and set up on your WordPress site.
If your website doesn't have SSL installed yet, set that up first. Most M-Pesa integrations will fail silently or reject the connection entirely without a valid HTTPS certificate.

Step 1: Register for a Daraja API App

  1. Go to the Safaricom Daraja developer portal and create a free account.
  2. Once logged in, click Create App and give it a name, such as your business name.
  3. Select the Lipa Na M-Pesa Online API product. This is the one used for website checkouts.
  4. Note down the Consumer Key and Consumer Secret generated for your app. You'll need these shortly.

For a real business, you'll also need to apply for a production (go-live) Paybill or Till shortcode through Safaricom, along with your passkey, once you're ready to move out of the sandbox testing environment.

Step 2: Install an M-Pesa Payment Gateway Plugin

  1. From your WordPress dashboard, go to Plugins then Add New.
  2. Search for an M-Pesa payment gateway plugin. If you use WooCommerce, look specifically for one described as a WooCommerce M-Pesa gateway, since it plugs directly into your existing checkout page.
  3. Click Install Now, then Activate.

Read the plugin's own setup instructions carefully. Not all M-Pesa plugins are built the same way, and some support extra features like STK push, the prompt that appears on a customer's phone asking them to enter their M-Pesa PIN.

Step 3: Connect Your Daraja Credentials

  1. In your WordPress dashboard, open the plugin's settings page. If you're using WooCommerce, this is usually under WooCommerce > Settings > Payments.
  2. Enter your Consumer Key, Consumer Secret, shortcode, and passkey from your Daraja app.
  3. Set the callback URL field to the address the plugin provides, making sure it starts with https://.
  4. Leave the plugin in Sandbox or Test mode for now.

Step 4: Test Before You Go Live

Use the sandbox test credentials provided in your Daraja account to place a few test transactions through your website's checkout. Confirm that:

  • The STK push prompt reaches your test phone number.
  • A successful payment updates the order status in WordPress automatically.
  • Failed or cancelled payments are handled without breaking the checkout page.

Once testing looks correct, switch the plugin from sandbox to production mode and replace the sandbox credentials with your live Consumer Key, Consumer Secret, shortcode, and passkey from your approved production Daraja app.

If You're Not Using WooCommerce

If your site isn't an online store, for example if you invoice clients for services rather than sell products directly, a full API integration may be more than you need. Many small businesses simply display their Paybill or Till number on an invoice or contact page and confirm payment manually against their M-Pesa statement. This is slower but needs no plugin or API setup at all.

For help getting your website secured before connecting a payment API, see How to Install a Free SSL Certificate with AutoSSL.