1. Go to this page and download the library: Download njoguamos/laravel-pesapal 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/ */
njoguamos / laravel-pesapal example snippets
# Laravel 10 -> app/Console/Kernel.php
use NjoguAmos\Pesapal\Models\PesapalToken;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule): void
{
# Other scheduled commands
$schedule->command('pesapal:auth')->everyFourMinutes();
Schedule::command('model:prune', ['--model' => [PesapalToken::class]])->everyFiveMinutes();
}
}
# Laravel 11 -> routes/console.php
use Illuminate\Support\Facades\Schedule;
use NjoguAmos\Pesapal\Models\PesapalToken;
Schedule::command('pesapal:auth')->everyFourMinutes();
Schedule::command('model:prune', ['--model' => [PesapalToken::class]])->everyFiveMinutes();
use NjoguAmos\Pesapal\Pesapal;
$token = Pesapal::createToken();
use NjoguAmos\Pesapal\Pesapal;
$transaction = Pesapal::getTransactionStatus(
orderTrackingId: 'b945e4af-80a5-4ec1-8706-e03f8332fb04',
);
// $transaction either an array or an instance of Saloon Response
use NjoguAmos\Pesapal\Pesapal;
$redirectUrl = Pesapal::getRedirectUrl(orderTrackingId: $orderTrackingId);
// https://pay.pesapal.com/iframe/PesapalIframe3/Index?OrderTrackingId=db80f574-a759-40b3-a6ec-dc68ef3dc1e6
use NjoguAmos\Pesapal\Pesapal;
$transaction = Pesapal::getTransactionStatus(
orderTrackingId: 'b945e4af-80a5-4ec1-8706-e03f8332fb04',
);
if (is_array($transaction)) {
// The API call was successful and response is an array
// [
// "payment_method" => "MpesaKE"
// "amount" => 6.0
// "created_date" => "2024-03-19T20:08:46.39"
// "confirmation_code" => "SCJ8JQ26SW"
// "....more field"
// ]
} else {
// The API call was not successful. The response is an instance of Saloon Response
// $transaction->status() ---> response status code.
// $transaction->headers() ---> Returns all response headers
// $transaction->getPendingRequest() ---> PendingRequest class that was built up for the request.
}