A simple package for developers to consume Kopokopo Mpesa API in Laravel 8

v1.0.0 2024-04-03 09:09 UTC

This package is auto-updated.

Last update: 2024-04-06 18:40:02 UTC


A simple package for laravel developers to consume Kopokopo API for laravel 8.


You can easily install this package using composer and an installation command

composer require patrickmwangi017/laravel-kopokopo


Next, after the package has been installed run this to publish config;

php artisan kopokopo:install

This will help in publishing config/kopokopo.php file. This kopokopo config file is where you will add all configurations for Kopokopo APIs. This includes the environment your application is running in(sandbox or production), callback URLs and required credentials.


return [
    // Set this to false if you cannot authenticate due to SSL certificate problem.
    'curl_ssl_verify' => false,

    // When you are testing in sandbox environment this remains true, to go to a live environment set it to false
    'sandbox' => false,

    /* The below values should not be hardcoded for security reason. Add these variables in .env
    # This is the application id acquired after you create an Authorization application on the kopokopo dashboard
    'client_id' => env('KOPOKOPO_CLIENT_ID', 'EXAMPLE_CLIENT_ID'),

    # The kopokopo application client secret
    'client_secret' => env('KOPOKOPO_CLIENT_SECRET', 'EXAMPLE_CLIENT_SECRET'),

    # The kopokopo application api key
    'api_key' => env('KOPOKOPO_API_KEY', 'EXAMPLE_APIKEY'),

    // Define the scope of your applications control on kopokopo transaction. Using company will control transactions for all till numbers regardless
    'scope' => 'till', //i.e company, till

    // The business till number given to you by Kopokopo
    'till_number' => '0000000',

    // The business till number given to you by Kopokopo
    'stk_till_number' => 'K000000',

    // Preferred transacting currency i.e KES, USD, AUD
    'currency' => 'KES',

    // Webhooks are a means of getting notified on your laravel application of events in the Kopokopo application. i.e
    // Below values will be used to register your webhooks on Kopokopo. For it to work, update the values and use Kopokopo::subscribeRegisteredWebhooks() to register
    'webhooks' => [
        'buygoods_transaction_received' => '',
        'buygoods_transaction_reversed' => '',
        'b2b_transaction_received' => '',
        'm2m_transaction_received' => '',
        'settlement_transfer_completed' => '',
        'customer_created' => ''

    // This webhook is used to get notified of a successful Mpesa stk payment
    'stk_payment_received_webhook' => '',


For production you need to replace with production credentials and also set 'sandbox' => false.

For security reasons you may want to define your API credentials in env file. For example;

  // config/kopokopo.php
  'client_id' => env('KOPOKOPO_CLIENT_ID'),
  'client_secret'   => env('KOPOKOPO_CLIENT_SECRET'),
  'api_key' => env('KOPOKOPO_API_KEY'),
# .env
# ...
# ...


First things first...Authorization

N/B: You can skip the authorization step unless you need to carry out a number of operations in a single execution. Otherwise, the rest of the steps automatically acquire the access_token before calling the API>

Kopo Kopo uses Oauth2 to allow access to the Kopo Kopo API so before anything else, Kopokopo API expects the application access token to be used to make calls to the Kopo Kopo API on behalf of the application. To acquire the token, you need to call the token service like below:

use Kopokopo;
// Get the token

//introspect token

//revoke access token

Webhook Subscription

This API enables you to register the callback URLs via which you will receive payment notifications for payments and transaction that happen in your Kopo kopo app. To register Urls ensure all urls under webhooks are filled with valid urls in config/kopokopo.php. Checkout to understand what each webhook does.
When testing on sandbox you can use ngrok to expose your callbacks to the internet. Then call registerWebhooks() on Kopokopo facade. i.e

Webhook subscription via terminal commands:

Publish any webhook by running the command below in your terminal:

php artisan kopokopo:subscribe


$ php artisan kopokopo:subscribe

  Enter Event Type:
  > stk_payment_received_webhook
  Callback URL i.e
  Scope: i.e till,business [till]:
  > till
  STK Enabled Till Number [K000000]:
  > K000000

You may use the --all option to subscribe to all webhook specified in kopokopo webhooks config i.e

php artisan kopokopo:subscribe --all


Remember to first import Kopokopo facade with:
use Kopokopo;


// subscribe all webhooks registered in config file
$response = Kopokopo::authenticate($token)->subscribeRegisteredWebhooks();
 //subscribe a specific webhook
 $res = Kopokopo::authenticate($token)->subscribeWebhook(
    event_type: 'buygoods_transaction_received',
    url: '',
    scope: 'till',
    till: 7777777,
// HTTP/1.1 201 Created Location:

Upon successful creation of the webhook subscription, the api will return true, else you will get the exact error as json HTTP response;

Initiate Mpesa Payment (STK Push)

You can receive payments from M-PESA users via STK Push. You can initiate payment like this;

$res= Kopokopo::authenticate('my_access_token')->stkPush(
    amount:  2230,
    phone: '+254799999999',
    first_name: 'Michael',//optional
    last_name: 'Gatuma',//optional
    email: '',//optional
    metadata: [

if($res['status'] == 'success')
    dump ("The resource location is:" . json_encode($res['location']));
    // => ''

Upon a successful request a HTTP Status 201 will be returned and the location HTTP Header will contain the URL of the newly created Incoming Payment.

HTTP/1.1 201 Created Location:

Query Incoming Payment Status

Coming Soon


Create Payment Recipients

You can add external recipient that will be the destination of your payments in the future, sort of creating contacts. The following are the different types of pay recipients you can create with examples.

(a). Mobile Wallet

Create Pay Recipient for a Mobile Wallet recipient;

$create_recipient_response = Kopokopo::PayService()->addPayRecipient([
  'type' => 'mobile_wallet',
  'firstName'=> 'Michael',
  'lastName'=> 'Gatuma',
  'email'=> '',
  'phoneNumber'=> '+254999999999',
  'network'=> 'Safaricom',
  'accessToken' => 'myRand0mAcc3ssT0k3n'

if($create_recipient_response['status'] == 'success')
    echo "The resource location is:" . json_encode($create_recipient_response['location']);

A HTTP response code of 201 is returned upon successful creation of the PAY recipient. The URL of the recipient resource is also returned in the HTTP Location Header

HTTP/1.1 201 Created Location:

(b). Bank Account

Coming Soon

(c). External Till

Coming Soon

(c). Paybill

Coming Soon

Make Outgoing Payments

Coming Soon

Query Outgoing Payment Status

Coming Soon

Transfer to your settling account(s)

Transfer funds to your pre-approved settlement accounts (bank accounts or mobile wallets).

Coming Soon


Poll Buygoods Transactions between the specified dates for a particular till or for the whole company.

Coming Soon

Transaction SMS Notifications API Requests

Send sms notifications to your customer after you have received a payment from them.

Coming Soon

Change log

Please see the changelog for more information on what has changed recently.


$ composer test


Thank you for considering contributing to laravel-kopokopo. Pull requests and issues welcome. Be sure to check open issues and PRs before continuing. Take a look at to see a to do list.

Security Vulnerabilities

If you discover a security vulnerability within laravel-kopokopo, please send an e-mail to Michael Gatuma via All security vulnerabilities will be promptly addressed.


The laravel-kopokopo package is open-source software licensed under the MIT license


laravel, mpesa, kopokopo, k2-connect,k2-connect-php, laravel-mpesa, php, stk-push