DEV Community

Cover image for USSD Code Development by Chris Aydat.
Festus Chris Otopa Ayeh-Datey
Festus Chris Otopa Ayeh-Datey

Posted on • Edited on

USSD Code Development by Chris Aydat.

https://github.com/chrisaydat/ussddevelopment.git

Unstructured Supplementary Service Data (USSD) is a protocol that allows basic feature phones to interact with backend systems. In this article, we will go through a USSD code sample developed by Aydat, Inc. to understand how such applications are built.

Overview

The USSD application developed by (chris) Aydat, Inc for Dyrect. provides the following capabilities:

  • User registration
  • Balance inquiry
  • P2P money transfers
  • Purchases

We will understand how the code implements these features.

Application Flow

The high-level flow is:

  1. User dials USSD code on their phone
  2. Main handler is invoked which displays menu
  3. User input is processed to navigate menu options
  4. Relevant actions like transactions, queries are performed

This is enabled by PHP code and USSD protocol features.

Main Handler

The ussd.php file handles the USSD session:

//ussd.php

$text = $_GET['text']; 

$data = explode("*", $text);

//determine menu level
$level = count($data);

//route user input  
switch($data[1]){

  case 1: 
    registration();
    break;  

  case 2:
    balance();
    break; 

    //...other cases
}
Enter fullscreen mode Exit fullscreen mode

It receives user input, parses it to identify menu level, and routes it accordingly.

Registration Flow

Registration prompts user for details one by one:

//functions.php

function registration(){

  if($level == 1){
    askFirstName();
  }  

  if($level == 2){
    askLastName();
  }

  //...other fields

  if($level == 9){
    saveToDB();
    confirmRegistration(); 
  }

}
Enter fullscreen mode Exit fullscreen mode

ussd_proceed() is used to prompt for next field.

Session Control

ussd_proceed() & ussd_stop() are used to manage session:

function ussd_proceed($text){
  echo "CON $text";
  exit(0);
}

function ussd_stop($text){
  echo "END $text";
  exit(0); 
}
Enter fullscreen mode Exit fullscreen mode

Database Integration

connection.php sets up database connectivity:

//connection.php

$host = "localhost";
$username = "ussd";
$password = "123456";

$db = new mysqli($host, $username, $password, "ussdApp");

//execute queries
Enter fullscreen mode Exit fullscreen mode

Used for registration, balance check etc.

Conclusion

The Next Version of this Article Will explore Database Connections and Final Deployment

Top comments (0)