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::stkPush([
    'phone' => '254712345678',
    'amount' => '100',
    'account_reference' => 'INV-12345',
    'transaction_desc' => 'Payment for order',
]);

use TFS\Mpesa\Mpesa;

$response = Mpesa::stkPush(
    phone: '254712345678',
    amount: '100',
    accountReference: 'INV-12345',
    transactionDesc: 'Payment for order'
);

$response = Mpesa::stkPush('254712345678', '100', 'INV-12345', 'Payment');

// PHP 7.4+ (Array)
$response = Mpesa::stkPush([
    'phone' => '254712345678',
    'amount' => '100',
    'account_reference' => 'INV-12345',
    'transaction_desc' => 'Payment for order',
    'callback' => 'https://yourdomain.com/callback', // optional
]);

// PHP 8.0+ (Named Arguments)
$response = Mpesa::stkPush(
    phone: '254712345678',
    amount: '100',
    accountReference: 'INV-12345',
    transactionDesc: 'Payment for order',
    callback: 'https://yourdomain.com/callback'
);

// PHP 7.4+
$response = Mpesa::stkQuery(['checkout_request_id' => 'ws_CO_123456']);

// PHP 8.0+
$response = Mpesa::stkQuery(checkoutRequestId: 'ws_CO_123456');

// Positional
$response = Mpesa::stkQuery('ws_CO_123456');

// PHP 7.4+ (Array)
$response = Mpesa::b2c([
    'phone' => '254712345678',
    'amount' => '1000',
    'occasion' => 'Salary',
    'remarks' => 'Monthly salary payment',
    'command_id' => 'SalaryPayment', // SalaryPayment | BusinessPayment | PromotionPayment
]);

// PHP 8.0+ (Named Arguments)
$response = Mpesa::b2c(
    phone: '254712345678',
    amount: '1000',
    occasion: 'Salary',
    remarks: 'Monthly salary payment',
    commandId: 'SalaryPayment'
);

// PHP 7.4+
$response = Mpesa::b2bPayBill([
    'party_b' => '888880',
    'amount' => '1000',
    'account_reference' => 'ACC-12345',
    'remarks' => 'Payment for services',
    'requester' => '254712345678', // optional - on behalf of
]);

// PHP 8.0+
$response = Mpesa::b2bPayBill(
    partyB: '888880',
    amount: '1000',
    accountReference: 'ACC-12345',
    remarks: 'Payment for services'
);

// PHP 7.4+
$response = Mpesa::b2bBuyGoods([
    'party_b' => '888880',
    'amount' => '500',
    'account_reference' => 'ORDER-789',
    'remarks' => 'Payment for goods',
]);

// PHP 8.0+
$response = Mpesa::b2bBuyGoods(
    partyB: '888880',
    amount: '500',
    accountReference: 'ORDER-789',
    remarks: 'Payment for goods'
);

// PHP 7.4+
$response = Mpesa::b2Pochi([
    'party_b' => '254712345678',
    'amount' => '1000',
    'occasion' => 'Payment',
    'remarks' => 'Business payment',
    'originator_conversation_id' => 'CONV-' . time(),
]);

// PHP 8.0+
$response = Mpesa::b2Pochi(
    partyB: '254712345678',
    amount: '1000',
    occasion: 'Payment',
    remarks: 'Business payment',
    originatorConversationId: 'CONV-' . time()
);

// PHP 7.4+
$response = Mpesa::balance([
    'party_a' => '600000',
    'remarks' => 'Balance inquiry',
    'identifier_type' => 4, // 1=MSISDN, 2=Till, 4=Shortcode
]);

// PHP 8.0+
$response = Mpesa::balance(
    partyA: '600000',
    remarks: 'Balance inquiry',
    identifierType: 4
);

// PHP 7.4+
$response = Mpesa::registerC2BUrl([
    'validation_url' => 'https://yourdomain.com/api/c2b/validation',
    'confirmation_url' => 'https://yourdomain.com/api/c2b/confirmation',
    'response_type' => 'Completed', // Completed | Canceled
]);

// PHP 8.0+
$response = Mpesa::registerC2BUrl(
    validationUrl: 'https://yourdomain.com/api/c2b/validation',
    confirmationUrl: 'https://yourdomain.com/api/c2b/confirmation',
    responseType: 'Completed'
);

// PHP 7.4+ (Mpesa::stkPush([
    'phone' => '254712345678',
    'amount' => '100',
    'account_reference' => 'INV-123',
    'transaction_desc' => 'Payment',
    'auth' => [
        'consumer_key' => 'different_key',
        'consumer_secret' => 'different_secret',
        'shortcode' => '123456',
        'passkey' => 'different_passkey',
    ],
]);

// PHP 8.0+
$response = Mpesa::stkPush(
    phone: '254712345678',
    amount: '100',
    accountReference: 'INV-123',
    transactionDesc: 'Payment',
    auth: [
        'consumer_key' => 'different_key',
        'consumer_secret' => 'different_secret',
    ]
);
bash
php artisan vendor:publish --provider="TFS\Mpesa\MpesaServiceProvider"