Skip to content
lawrence615Public

About

A Laravel ussd application demonstrating how to create a ussd application

Topics

Resources

Stars

9 stars

Watchers

2 watching

Forks

Repository files navigation

Laravel USSD Application

A Laravel USSD application demonstrating how to create a USSD application. It is intended as a template/starting point for building your own USSD menus on top of the Wasiliana USSD gateway - see their USSD integration docs. It uses the standard sessionId/serviceCode/phoneNumber/text + CON/END callback contract, so it also works with any other gateway that follows the same format.

Note: This codebase was built on Laravel 5.2 (PHP >= 5.5.9) and is kept for reference and learning purposes. The patterns it demonstrates - CON/END responses, session tracking, and menu navigation - still apply, but consider porting them to a modern Laravel release for production use.

What's Inside

The app exposes a set of USSD callback endpoints under ussd/serve/ (see app/Http/routes.php):

Endpoint Controller Demonstrates
ussd/serve/main UssdController A root menu that routes users to sub-applications
ussd/serve/kplc KplcStaffVerificationController A simple two-step flow: prompt for input, look it up in the database, respond
ussd/serve/bundles BundlesController A database-driven menu system (menus, menu items, user session/progress tracking)
ussd/serve/mpesa MpesaController A level-based menu driven by the *-separated text parameter
ussd/serve/mail MailerController Sending email via the Mailgun API

How the USSD Flow Works

The USSD gateway sends four parameters to your callback URL on every step of the session:

  • sessionId - unique ID for the USSD session
  • serviceCode - the USSD code dialed, e.g. *384*2014#
  • phoneNumber - the subscriber's phone number
  • text - the user's input so far, *-separated (empty on the first request)

Your response is plain text prefixed with:

  • CON - continue the session and show the user the message/menu
  • END - terminate the session with a final message

Example:

if ($text == '') {
    $this->sendResponse("Please enter staff ID e.g ekp1111", 1); // CON
} else {
    $this->sendResponse("ID is valid and it belongs to John Doe", 2); // END
}

Session state (where the user is in the flow) is persisted in the ussd_users table. See BundlesController for a menu/progress-based approach, and MpesaController for a pure text-parsing approach.

Requirements

  • PHP >= 5.5.9
  • Composer
  • MySQL (or another database supported by Laravel)

Installation

To actually run the app you'll need a PHP version from its era - PHP 5.6 to 7.x. Laravel 5.2 predates PHP 8 and uses functions that have since been removed, so it will not boot on PHP 8+.

git clone https://github.com/lawrence615/ussd.git
cd ussd
composer install
cp .env.example .env
php artisan key:generate

Installing on modern PHP (8.x)

On a modern PHP version, composer install fails platform requirements and the post-install scripts would break. To install the locked dependencies anyway - useful for browsing the code or getting IDE autocomplete - run:

composer install --no-scripts --ignore-platform-reqs

Note this only installs the dependencies for inspection; the app still won't run on PHP 8. --no-scripts also skips the automatic php artisan key:generate, so run it yourself if you're on a compatible PHP version.

Configure your database credentials in .env, then:

php artisan migrate
php artisan db:seed
php artisan serve

The USSD endpoints will then be available at http://localhost:8000/ussd/serve/{main,kplc,bundles,mpesa}.

Testing Locally

Simulate a gateway request with curl:

curl "http://localhost:8000/ussd/serve/mpesa?sessionId=123&serviceCode=*384*2014%23&phoneNumber=254700000000&text="
# CON Safaricom:
# 1.Safaricom+
# 2.M-PESA

Pass the accumulated input in text to move deeper into a flow, e.g. text=2*1.

To connect a real Wasiliana shortcode, expose your local server with a tunneling tool such as ngrok and set the callback URL in your Wasiliana account - see the Wasiliana USSD docs for details.

Going Live

A USSD application is only reachable by the gateway if it has a public URL. To run it for real:

  1. Deploy on a server running PHP 5.6-7.x. Laravel 5.2 will not boot on PHP 8+, so on modern infrastructure the easiest option is a container (e.g. php:7.1-apache) or a VM with an older PHP version. Install dependencies on the server with composer install.
  2. Point the web server at public/. php artisan serve is for local development only - use Apache or nginx and set the document root to the public/ directory.
  3. Configure .env for production: APP_ENV=production, APP_DEBUG=false, and your production database credentials. Then run php artisan migrate and php artisan db:seed.
  4. Get a USSD service code and register your callback URL. With a gateway such as Wasiliana (or Africa's Talking), register the URL the gateway should call for each session step, e.g. https://your-domain.com/ussd/serve/main. Gateways typically POST the sessionId/serviceCode/phoneNumber/text parameters - these endpoints accept both GET and POST.
  5. Dial the code on a supported network - the gateway now calls your server for each step of the session.

For testing a real shortcode against your local machine, use a tunnel like ngrok (ngrok http 8000) and register the generated https://*.ngrok.io URL as the callback instead.

License

Open-sourced software licensed under the MIT license.


No-Code Alternative: UFlow

Don't want to write code? UFlow is the premier free no-code flow builder for USSD and WhatsApp, allowing anyone to create complex menus and flows without writing a single line of code.

About

A Laravel ussd application demonstrating how to create a ussd application

Topics

Resources

Stars

9 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages