PHP code example of drh / mpesa

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

    

drh / mpesa example snippets


use DrH\Mpesa\Facades\STK;

// Simple push
$result = STK::push(amount: 100, phone: '254712345678', ref: 'Order001', description: 'Payment for order');

// Fluent API
$result = STK::amount(100)
    ->from('254712345678')
    ->usingReference('Order001', 'Payment for order')
    ->push();

// Check STK transaction status
$status = STK::status($checkoutRequestId);

use DrH\Mpesa\Facades\Registrar;

Registrar::shortcode(174379)
    ->onConfirmation('https://example.com/payments/callbacks/c2b-confirmation')
    ->onValidation('https://example.com/payments/callbacks/c2b-validation')
    ->submit();

use DrH\Mpesa\Facades\B2C;

// Send payment
$result = B2C::send(number: '254712345678', amount: 500, remarks: 'Salary payment');

// Fluent API
$result = B2C::to('254712345678')
    ->amount(500)
    ->withRemarks('Salary payment')
    ->send();

// Check balance
$balance = B2C::balance();

// Check transaction status
$status = B2C::status($transactionId);

use DrH\Mpesa\Facades\B2B;

// Send payment
$result = B2B::pay(
    type: 'BusinessPayBill',
    shortcode: '600000',
    amount: 1000,
    reference: 'INV001',
    phone: '254712345678'
);

// Check transaction status
$status = B2B::status($request);

use DrH\Mpesa\Facades\Identity;

$result = Identity::validate('254712345678');
bash
php artisan vendor:publish --provider="DrH\Mpesa\MpesaServiceProvider"
bash
php artisan migrate