Lipa na MPESA STK-PUSH API Integration to your PHP Website
We are going to take you through a step-by-step tutorial for mpesa STK API integration. Mpesa is one of the most convenient ways people make transactions in Kenya. MPESA has come a long way from just sending and receiving the money to making payments to businesses. With the rapid transformation in the e-commerce industry, combined with the COVID-19 pandemic affecting businesses across the world, businesses have moved online. You can now integrate MPESA into your online business for faster transactions with your customers. In this article, we are going to learn how to do the MPESA STK Push API using PHP.
Types of Mpesa Integration Services
- Business-to-Customer Integration (B2C) — This enables businesses to pay customers directly.
- Business-to-Business Integration (B2B) — This allows businesses to send money to another business directly from their paybill.
- Customer-to-Business Integration (C2B) — This allows businesses to receive payments from their customers directly.
- Lipa an MPESA Online — This allows initiating an M-Pesa transaction on behalf of a customer using STK Push
MPESA STK Integration on Daraja
Firstly, What is STK push? STK Push is a prefilled pop-up notification on a customer’s MPESA Menu to confirm the initiated transaction by the merchant by inputting their PIN.
In MPESA STK API integration, the M-Pesa registered customer gets a pop-up notification to confirm the initiated transaction by the merchant. The transaction is then processed after the customer keys in their MPESA PIN and confirms the transaction. Both the customer and merchant get transaction confirmation messages. Learn more about STK push M-PESA API.
Now that we know the types of MPESA Integration services, the next step is to make sure that we have an account with Safaricom Developers Account. To create an account visit the Daraja Safaricom website. If you have an account with Safaricom Daraja you can log in else sign up.
The next step is to create a new sandbox app by clicking on the Add a New App button and give it a name. Ensure you select both Lipa na Mpesa Sandbox and Mpesa Sandbox and hit Create App button. You will get the following success message. Awesome! Now that we have our app created successfully. Click on your newly created app.
The Consumer Key and Consumer Secret are personal and therefore should be secured and not shared. Learn more here
How to Integrate Lipa na Mpesa STK Push in PHP
Firstly, let’s set up your MPESA API Folder: mpesa.
checkout.php — This is a sample checkout page for testing the API.
express-stk.php — This is the code used to initiate the stk push.
confirm-payment.php — This is the page where the user confirms their transaction
callback.php — This has the code that receives the result from Safaricom.
status.php — This has the API code that checks if the transaction was a success
transaction_log — This file will help us log the result.
I. Checkout Page
Your checkout page (checkout.php) should be something like this: See the code here.
II. Express STK API file
When the customer enters the phone number and presses SEND, the POST request will be handled by an external PHP file in the API folder: express-stk.php: See the code here. At the end of the transaction, the user is redirected to the page confirm-payment.php which confirms if the transaction was successful or not.
III. Callback file
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 in ou case, callback.php. To implement this, 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. It should be something like this: See the code here.
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.
IV. Payment Confirmation page
Back to the user end, after the stk push is initiated, the user is redirected to the confirmation page confirmation-payment.php which should be close to something like this:
When the user clicks “Confirm Payment”, we initiate an MPESA Transaction status query to check the status of the transaction (To see if the user actually PAID, CANCELED or if there was an ERROR). This is handled by: status.php (See the code here)
See also how to integrate it on Laravel: MPESA API Integration in Laravel
Don’t forget to follow us on mediaforcecommunications.medium.com