Payment Plugins

Mpesa C2B API Integration on PHP website | How to integrate Tutorials

You have heard about tutorials on Mpesa C2B API Integration from safaricom Daraja and the fact that you can start accepting Mpesa payments on your website. These trainings and blogs are all over the internet. This is true and actually very easy if you follow the right steps and get the best source code or expertise. I emphasize, there is a technical skill needed to complete a successful Mpesa API integration and that is what most tutorials do not tell you.

In this post, I will dive in explaining every detail like to a 2 year old baby on how to integrate your core PHP website with Mpesa C2B API.

What is core PHP?

PHP is a recursive acronymn for PHP Hypertext Preprocessor. It is used in developing the server side of most websites and runs most common frameworks like Laravel and CMS like Drupal and wordpress. However, to be specific, when we talk about core PHP, we are referring to something like below:

        <?php

        //php code here
        /* so how exactly can a web app
*developed in this php code be
*integrated with Mpesa API*/ ?>

Actually, when we were in college, a lecturer used to call this Vanilla PHP, to mean, that PHP without anything else.

What is Mpesa API?

Mpesa is a mobile payment gateway owned by safaricom and first launched in Kenya. Later it was opened in many other countries across the world. Safaricom mobile sim card holders are able to send money from one phone number to another with a confirmation of the money sent done via an SMS received by both the sender and recipient after a transaction is completed.

When we talk generally about Mpesa API integration, we are talking about developing a software script or code that communicated between the Mpesa servers and a third-party app or website or software. In this specifically, we will address Mpesa C2B API integration which is the mpesa API which bridges between a Business (B) to(2) a Customer(C) mpesa transaction.
Mpesa C2B API PHP website integration

More specific about Mpesa C2B API integration

In this Mpesa API integration tutorial, we will deal with two types of web developers:

You can always hire us as your Mpesa API integration experts to sort the work very fast. We are always a call or whatsapp DM away (+25406745202).

Mpesa C2B API for Novice or starters.

Note:, in this category, I have decided not to use code here because it is very possible for you to start accepting Mpesa payments or to add Mpesa Payment gateway on your website without you knowing how to write a single code line.

So, how exactly can you Integrate Mpesa API on your PHP website without writing any code. This is the simplest way to Integrate any PHP website with Mpesa.

But before that, let us know a few things so that we make it even simpler to complete this important ecommerce task.

What is C2B Mpesa integration?

Simple. It is that Mpesa linking feature that will help customers pay you automatically then you can process their orders or continue with whatever business logic your website does using other payment options.

What is a core PHP website?

Once more. Simple. It is a website that is built in PHP without those other stuff you hear known as CMS, Frameworks etc. Just plain PHP opening your notepad or any text editor and writing your website from there and processing data using PHP to maybe a Mysql database.

Note: Some CMS's like wordpress, Drupal and Joomla and the Moodle LMS are developed in PHP. Because of the way they are developed,they have special ways to do their Mpesa API Integration through the use of plugins, components and modules. For those, you will benefit from below posts on how to add Mpesa Payment gateways on them:

Types of Mpesa C2B API integration to have in mind before deciding on Integrating your website with an Mpesa Payment option.

1. Mpesa C2B General API:

This is whereby users pay using their phones without having to touch your website or app and everything else is processed by your website/server automatically. In this case, your website users do not need to visit your website or app or be online to make a payment. All they need is to know how to pay you and the account that identifies their payment in your website or software or servers.

The technical aspects involved here are:

a) . Url registration: ==> This involves telling Mpesa servers the urls which they will need to communicate to in case a payment is received on a your Mpesa  Paybill or Till Number.

b). Validation : ===> The transaction received is validated on its legitimacy and your own conditions.

c). Confirmation:===> If valid, the Mpesa transaction is confirmed and the business logic is executed.

So now, let us integrate a C2B Mpesa gateway to process transactions from your clients.

Before I dive too deep, here is an Mpesa C2B Integration Assistant that through a few Clicks will help integrate Mpesa C2B API in your website within minutes without any coding skills Mpesa API integration assistant.

Requirements to Integrate your website with Mpesa C2B API:

1. The above Mpesa API integration assistant.

2. A C2B Paybill from Safaricom.

3. A developer account from Safaricom.

4. Webmaster knowledge.

Process on how to integrate C2B API.

a) Set up the Mpesa API integration Assistant in your server/Hosting Cpanel. Since most people use Cpanel these days, I will explain using a Cpanel. The Apache server should be running PHP and Mysql and support PHP 7+. Let this be set in its own folder in your cpanel. If your main website is running in the main directory, consider a folder like this maindirectory/payments. You will note that a folder like maindirectory/mpesa wil be rejected by Mpesa API when you try registering your Mpesa validation and confirmation urls. Therefore, be sure you do not use that or the word "mpesa" in the urls.

b) Add the C2B Mpesa integration plugin to the above Payments processor.

After the above procedure, you are now ready with a payment gateway which can automatically process Mpesa payments from Clients. However, you are not yet ready to process the orders against these transactions.

c) Now set the Core PHP Payments Mpesa integration plugin. This is the plugin that connects your order process to above processed payments.

Open your "Payment successful page" and include code to factor in any Payments from Mpesa. This is upon you as per the functionality of your Core PHP website.

Now, it is time to run and enter important Mpesa API credentials that will make your API integration work. These are as follows:

Now that you are fully set, save the above credentials settings and ensure you select either "sandbox" or live depending on the credentials you just set. The credentials page on Daraja has the sandbox while the Live credentials are obtained when you GO live on Daraja API portal.

You now need to run the main url where you uploaded the Mpesa API integration assistant and click all NEXT steps until you get to the "Integration successful" page. Your Mpes C2B integration is now complete and can now be tested.

Recall that all Mpesa Payments are deposited on the Payments core Gateway and you can view them from the admin end under Transactions--->Mpesa. You can also view these on the mpesa_payments table in your Database. Make payments directly to your Paybill or Till number to test if they get saved in the Database and everything should actually be automated.

Now you are done. Test your work.

You have successfully integrated C2B Mpesa payments with your Core script.

2. Lipa na Mpesa Online API - The Mpesa Express Api.

This is also a form of Mpesa C2B API. Customers will pay your business. However, unlike the General C2B Mpesa API, customers will need to do an online Checkout of whatever they are buying or paying for, then, a popup will appear will appear on their Phones with the payment details and a form to just enter the Mpesa PIN and click OK and fuaaaahhhhh, the Mpesa payment is completed and the item processed. This type of Mpesa API is what most people call STK push because it pushes a popup form with order payment details to the customer phone and the customer only has to input the Mpesa PIN and the transaction is completed.

Mpesa API Integration - stk push

Fig 1: An illustration showing the POPUP form that will appear on the customers phone after checkout ready to be paid when PHP website is integration with the Mpesa STK push or Express API.