Table of Contents
Overview
Build USSD menus with ease.
Instead of having tonnes of nested, complex PHP files, this package give you the ability to construct your menus in XML and execute them as though they were plain PHP files.
This approach greatly shrinks the code footprint as well as increase readability.
Let's see an example for a simple would be SACCO USSD application.
xml version="1.0" encoding="UTF-8"?>
<menu name="sacco">
<action name="check-user"/>
<options header="SACCO Services" noback="no">
<option text="Savings">
<list header="Saving Accounts" provider="saving-accounts" prefix="account"/>
<options header="Savings">
<option text="Deposit">
<options header="Deposit From:">
<option text="My Number">
<variable name="sender" value="{{phone_number}}"/>
option>
<option text="Another Number">
<question name="sender" text="Enter Phone Number: "/>
option>
options>
<question name="amount" text="Enter Amount: "/>
<action name="deposit"/>
option>
<option text="Withdraw">
<options header="Withdraw To:">
<option text="My Number">
<variable name="receiver" value="{{phone_number}}"/>
option>
<option text="Another Number">
<question name="receiver" text="Enter Phone Number: "/>
option>
options>
<question name="amount" text="Enter Amount: "/>
<action name="withdraw"/>
option>
<option text="Check Balance">
<action name="check-balance" text="To see your balance, enter PIN: "/>
option>
<option text="Check Transaction">
<question name="transaction_id" text="Enter Transaction ID: "/>
<action name="check-transaction"/>
option>
options>
option>
<option text="Loans">
<response text="Coming soon."/>
option>
options>
menu>
Installation
Install the package via the Composer.
composer require bmatovu/laravel-ussd
Configurations
php artisan vendor:publish --provider="Bmatovu\Ussd\UssdServiceProvider" --tag="config"
Usage
Example
menus/menu.xml
xml version="1.0" encoding="UTF-8" ?>
<menu name="demo">
<question name="guest" text="Enter Name: "/>
<response text="Hello {{guest}}."/>
menu>
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 {
$ussd = new Ussd('menu.xml', $request->session_id);
$output = $ussd->handle($request->text);
} catch(\Exception $ex) {
return response('END ' . $ex->getMessage());
}
return response('CON ' . $output);
}
}
Validation
Publish the menu schema (optional). Defaults to using the schema bundled within the package if none is present in your menus path, usually menus/menu.xsd
.
php artisan vendor:publish --provider="Bmatovu\Ussd\UssdServiceProvider" --tag="schema"
Validate your menu files against the schema
php artisan ussd:validate
Simulator
The package comes with a CLI USSD simulator supporting a handful of populator aggregators.
Publish the simulator config file to get started. Update the aggregator and the USSD service endpoint in the config file.
php artisan vendor:publish --provider="Bmatovu\Ussd\UssdServiceProvider" --tag="simulator"
Usage:
./vendor/bin/ussd --help
./vendor/bin/ussd 256772100103
If you're an aggregator missing from the current list reachout to have you added. Or simply send a pull request
Constructs
Variable
$color = 'blue';
<variable name="color" value="blue"/>