Integrate MPESA C2B API to your PHP Website

MediaForce Communications
3 min readMar 17, 2022


MPESA C2B API Integration to your PHP Website

In our previous MPESA integration with the PHP article ( see here), we introduced mpesa C2B integration. We said that Customer-to-Business Integration (C2B) allows businesses to receive payments from their customers directly. Therefore, in this article, we are going to show you mpesa C2B API integration for your PHP Website.

C2B Transaction Process

When a customer initiates a payment request to your Pay Bill or Buy Goods (Till Number) from their phone, M-PESA receives the request. Secondly, it validates the customer’s PIN, Account Balance, Pay Bill entered, etc. In addition, M-PESA also checks if you have enabled External Validation for the C2B. If External Validation is disabled, M-PESA completes the transaction. After that, it sends a Confirmation notification to the Confirmation URL registered. This only happens when the payment is successful.

If External Validation is enabled:

  1. M-PESA Holds the Funds tentatively from the customer’s wallet.
  2. M-PESA then sends a Validation request to the Validation URL registered.
  3. The partner validates the payment request and responds back to M-PESA.
  4. M-PESA receives the response, processes, and completes the transaction then a notification of the payment is sent to your registered confirmation URL.

Finally, M-PESA then sends an SMS notification to both the customer and C2B registered phone number. Learn more here


First, as usual, we will use pure PHP to integrate the C2B API. We hope you have created an account on Safaricom daraja website and have an active sandbox API.

Secondly, obtain an access token that you will be used when calling the Mpesa C2B API. For you to obtain this access_token, you use the consumer key and secret that will be obtained from your developer account.

The next step is to register validation and confirmation URLs(links). Why do we do this? The major reason for integrating Mpesa C2B API with your systems is to allow for communication and the exchange of information between the two systems.

A validation URL is a link where MPESA sends the transaction details to allow you to verify that they are what you intended after your customer initiates an Mpesa transaction using your pay bill number. For example, you might want to validate the amount being paid. Once you have verified, then you can either decline or accept the payment.

A confirmation URL is a link where MPESA sends the details of a completed transaction if you accepted the transaction in the validation stage. At this stage, you might save the data in the database, or perform any other logic for example in e-commerce where you might allow one to access premium products.

Therefore, what does this Mpesa C2B API integration look like in terms of code? Let’s start with vanilla PHP.

First of all, let’s look at the initiating stk push code. We saw this in the previous tutorial but let me just post it here again. See full code for initiateSTK.php here.

The above code initiates the stk push on the user’s phone. However, note that the response on the transaction status (whether the customer PAID, CANCELED, or if there was an ERROR) is sent by Safaricom API to your callback URL. To implement this practice, the callback URL is supposed to receive this response from safaricom. Therefore, you need to set up the code in the callback URL such that it receives the response in form of JSON data and then stores it in your database. If your Callback URL is, response.php should be something like this.

Your database will have the following columns: MerchantRequestID, CheckoutRequestID, ResultCode (0 means successful processing and any other code means an error occurred or the transaction failed.), ResultDesc, MpesaCode, TransactionDate, and PhoneNumber.

Don’t forget to follow us on

Originally published at on March 17, 2022.