PHP code example of james-kabz / mpesapkg

1. Go to this page and download the library: Download james-kabz/mpesapkg 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/ */

    

james-kabz / mpesapkg example snippets


use JamesKabz\MpesaPkg\Services\MpesaConfig;

class Example
{
    public function __construct(private MpesaConfig $config) {}

    public function handle(): void
    {
        $prefix = $this->config->routePrefix();
        $stkShortCode = $this->config->stkShortCode();
    }
}

use JamesKabz\MpesaPkg\MpesaClient;

class PaymentsController
{
    public function __construct(private MpesaClient $mpesa) {}
}
bash
php artisan vendor:publish --tag=mpesa-config
bash
php artisan migrate
bash
php artisan mpesa:security-credential
bash
curl -X POST "$BASE/$PREFIX/c2b/register" \
  -H "Content-Type: application/json" \
  -d '{
    "short_code": "600997",
    "confirmation_url": "'"$BASE"'/'"$PREFIX"'/c2b/confirmation",
    "validation_url": "'"$BASE"'/'"$PREFIX"'/c2b/validation",
    "response_type": "Completed"
  }'
bash
curl -X POST "$BASE/$PREFIX/account/balance" \
  -H "Content-Type: application/json" \
  -d '{
    "short_code": "600997",
    "identifier_type": "4",
    "remarks": "Balance Check"
  }'