1. Go to this page and download the library: Download sparors/laravel-ussd 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/ */
sparors / laravel-ussd example snippets
namespace App\Ussd\States;
use App\Ussd\Actions\TransferAccountAction;
use Sparors\Ussd\Attributes\Paginate;
use Sparors\Ussd\Attributes\Transition;
use Sparors\Ussd\Context;
use Sparors\Ussd\Contracts\State;
use Sparors\Ussd\Decisions\Equal;
use Sparors\Ussd\Decisions\Fallback;
use Sparors\Ussd\Decisions\In;
use Sparors\Ussd\Menu;
use Sparors\Ussd\Record;
use Sparors\Ussd\Traits\WithPagination;
#[Transition(to: TransferAccountAction::class, match: new Equal(1))]
#[Transition(to: TransferAmountState::class, match: new In(2, 3), callback: [self::class, 'setTransferType'])]
#[Transition(to: NewAccountNameState::class, match: new Equal(4))]
#[Transition(to: HelplineState::class, match: new Equal(5))]
#[Transition(to: InvalidInputState::class, match: new Fallback())]
#[Paginate(next: new Equal('#'), previous: new Equal('0'))]
class CustomerMenuState implements State
{
use WithPagination;
public function render(): Menu
{
return Menu::build()
->line('Banc')
->listing($this->getItems(), page: $this->currentPage(), perPage: $this->perPage())
->when($this->hasPreviousPage(), fn (Menu $menu) => $menu->line('0. Previous'))
->when($this->hasNextPage(), fn (Menu $menu) => $menu->line('#. Next'))
->text('Powered by Sparors');
}
public function setTransferType(Context $context, Record $record)
{
$transferType = '2' === $context->input() ? 'deposit' : 'withdraw';
$record->set('transfer_type', $transferType);
}
public function getItems(): array
{
return [
'Transfer',
'Deposit',
'Withdraw',
'New Account',
'Helpline',
];
}
public function perPage(): int
{
return 3;
}
}
namespace App\Http\Controllers;
use App\Ussd\Actions\MenuAction;
use App\Ussd\Responses\AfricasTalkingResponse;
use App\Ussd\States\WouldYouLikeToContinueState;
use Illuminate\Http\Request;
use Sparors\Ussd\Context;
use Sparors\Ussd\ContinuingMode;
use Sparors\Ussd\Ussd;
class UssdController extends Controller
{
public function __invoke(Request $request)
{
$lastText = $request->input('text') ?? '';
if (strlen($lastText) > 0) {
$lastText = explode('*', $lastText);
$lastText = end($lastText);
}
return Ussd::build(
Context::create(
$request->input('sessionId'),
$request->input('phoneNumber'),
$lastText
)
->with(['phone_number' => $request->input('phoneNumber')])
)
->useInitialState(MenuAction::class)
->useContinuingState(ContinuingMode::CONFIRM, now()->addMinute(), WouldYouLikeToContinueState::class)
->useResponse(AfricasTalkingResponse::class)
->run();
}
}
namespace App\Http\Ussd\Configurators;
use Sparors\Ussd\Contracts\Configurator;
class Nsano implements Configurator
{
public function configure(Ussd $ussd): void
{
$ussd->setResponse(function (string $message, int $terminating) {
return [
'USSDResp' => [
'action' => $termination ? 'prompt': 'input',
'menus' => '',
'title' => $message
]
];
});
}
}