PHP code example of tfs / mpesa

1. Go to this page and download the library: Download tfs/mpesa library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tfs / mpesa example snippets


use TFS\Mpesa\Mpesa;

...
$response = Mpesa::mpesa_express($phone, $amount, $AccountReference, $TransactionDesc, $callback = null);
...

eg.

$response = Mpesa::mpesa_express("254723077827", 1, "AccountReference", "TransactionDesc");

use TFS\Mpesa\Mpesa;

...
$response = Mpesa::b2c($phone, $amount, $occassion, $remarks, $callback = null, $command_id = null);
...

eg.

$response = Mpesa::b2c("254708374149", 10, "Test occassion", "Test remarks");

use TFS\Mpesa\Mpesa;

...

$result = Mpesa::balance($partyA, $remarks, $callback, $identifierType, $consumer_key, $consumer_secret, $initiator_name, $initiator_password, $queueTimeOutURL = null);
...

eg.

$partyA = config('mpesa.live.b2c_shortcode');
$remarks = "Balance";
$callback = config('mpesa.balance_callback_url');
$identifierType = 4; //1 – MSISDN, 2 – Till Number, 4 – Organization short code!
$consumer_key = config("mpesa." . config('mpesa.mode') . ".b2c_consumer_key");
$consumer_secret = config("mpesa." . config('mpesa.mode') . ".b2c_consumer_secret");
$consumer_key = config("mpesa." . config('mpesa.mode') . ".b2c_consumer_key");
$initiator_name = config("mpesa." . config('mpesa.mode') . ".initiator_name");
$initiator_password = config("mpesa." . config('mpesa.mode') . ".initiator_password");

$result = Mpesa::balance($partyA, $remarks, $callback, $identifierType, $consumer_key, $consumer_secret, $initiator_name, $initiator_password);

use TFS\Mpesa\Mpesa;

...

$result = Mpesa::c2b_register_url($ValidationURL, $ConfirmationURL, $ResponseType, $ShortCode, $consumer_key = null, $consumer_secret = null)
...

eg.

$ValidationURL = "https://24seven.co.ke/api/payments/c2b-validation";
$ConfirmationURL = "https://24seven.co.ke/api/payments/c2b-confirmation";
$ResponseType = "Completed";//Canceled
$ShortCode = config("mpesa." . config('mpesa.mode') . ".shortcode");

$result = Mpesa::balance($partyA, $remarks, $callback, $identifierType, $consumer_key, $consumer_secret, $initiator_name, $initiator_password, $queueTimeOutURL = null);
bash
php artisan vendor:publish --provider="TFS\Mpesa\MpesaServiceProvider"