PHP code example of algoetech / openapi_mpesa

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

    

algoetech / openapi_mpesa example snippets


    Route::post('/pay', [PaymentController::class, 'payment'])->name('payments_api');


use Openpesa\Pesa\Facades\Pesa;

// This function will be mapped to web|api route: `your-URL/api/pay`
// assuming you have data captured from a form 
public function payment(PaymentApiRequest $req) {
    $response = Pesa::c2b([
        'input_Amount' => $req->price, // Amount to be charged
        'input_Country' => 'TZN',
        'input_Currency' => 'TZS',
        'input_CustomerMSISDN' => '255'.$req->msisdn, // assuming you capture phone number without country code and zero
        'input_ServiceProviderCode' => '000001', // Replace with your service provider code given by M-Pesa
        'input_ThirdPartyConversationID' => 'mpesatz', // Unique [use a rand function to isolate ConversationId]
        'input_TransactionReference' => 'imethibitishwa', // Unique []
        'input_PurchasedItemsDesc' => $req->item
    ]);
    
    // make your logics to handle responses here
    return $response;
};