1. Go to this page and download the library: Download bmatovu/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/ */
bmatovu / laravel-ussd example snippets
namespace App\Http\Controllers;
use Bmatovu\Ussd\Exceptions\FlowBreakException;
use Bmatovu\Ussd\Ussd;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
/**
* @see https://developers.africastalking.com/docs/ussd/overview
*/
class UssdController extends Controller
{
public function __invoke(Request $request): Response
{
try {
$output = Ussd::make('menu.xml', $request->sessionId)->handle($request->text);
} catch(FlowBreakException $ex) {
return response('END ' . $ex->getMessage());
} catch(\Exception $ex) {
return response('END ' . get_class($ex));
}
return response('CON ' . $output);
}
}
use App\Http\Controllers\UssdController;
use Illuminate\Support\Facades\Route;
Route::post('/ussd', [UssdController::class, '__invoke']);
xml
<question name="username" text="Enter username: "/>
xml
<response text="Thank you for banking with us."/>
xml
<!-- Read from cache -->
<!-- $msisdn = $this->store->get('msisdn'); -->
<action name="get_user_info"/>
<!-- Pass as attribute -->
<action name="get_user_info" msisdn="{{msisdn}}"/>
<!-- Pass as variable -->
<action name="get_user_info">
<variable name="msisdn" value="{{msisdn}}"/>
</action>
xml
<!-- Approach #1 - user input handled by a qn tag -->
<question name="pin" text="To check balance, enter PIN: "/>
<action name="validate_pin"/>
<!-- Approach #2 - user input handled by the action -->
<action name="validate_pin" text="To check balance, enter PIN: "/>