How To Create USSD Service
How To Create USSD Service
Table of Contents
1 Introduction ..................................................................................................................... 3
2 What Is Mobile Service?.................................................................................................. 4
3 Creating a Mobile Service ............................................................................................... 5
3.1 Description of Service Structure and Logic ..................................................................... 5
3.1.1 Example of Financial Service .................................................................................................. 5
3.2 Static Service in XML ..................................................................................................... 6
3.3 Dynamic service ............................................................................................................ 9
3.4 Combining the Static and Dynamic Parts in XML.......................................................... 14
4 Connecting Service ....................................................................................................... 16
4.1 C2S number renting ..................................................................................................... 16
5 C2S number management ............................................................................................ 18
Table of Contents – 2
MiniApps Documentation – How to create USSD service
1 Introduction
Creating mobile services is a complex process involving the following issues:
• variety of mobile devices;
• lack of uniform standards and, consequently, very different interfaces and ways of
processing and displaying information on mobile devices.
MiniApps platform is a solution that helps to save time and money needed for mobile service
creation. There is no need for implementing low-level functionality to communicate with the
network infrastructure (SMS centers, USSD centers, etc.) as well as mobile operators.Mobile
service using MiniApps can be created in two different ways. The original service structure and
its information content can be easy created via MiniApps platform constructor which allows
creating in a graphical mode.The other way is to use a site of XML pages with additional
functionality programmed in any convenient server side programming language. This option is
used in the cases when the service needs to access databases, perform calculations, make
choices depending on user data, etc.This document describes the process of developing USSD
services with MiniApps platform.
Introduction – 3
MiniApps Documentation – How to create USSD service
• pageId; this is a file describing the page which will be reached through this menu item.
• protocol; it defines a list of access points in which this menu item will be visible. If this
parameter is not specified, the item will be visible in all access points.
In the phone this page will look as follows:
1>Special Offers
4>Contact Us
Parameter type=”back” in the tag <link> sets type back for the menu item “Back.” This
parameter is optional; it is used to determine the instruction for return, which will be executed
when the user clicks the button of return on the telephone.
The text from the first block <div></div> will be displayed on the mobile device as plain text.The
text from the second block <div></div> will be delivered in the form of a SMS simultaneously
upon entering this page; the parameter type=”sms” is specified for this.It should be noted that
the link in the text for SMS is not enclosed in the tag <a></a>, that is, it will be displayed as
plain text. However, many phone models support recognition of links in SMS, thus it can
possibly be displayed not as plain text, but as a link.
The static part of the service is defined. The entire set of created files is uploaded on the web
server, and we can start use the service.The appearance of the uploaded files:
../service_site_directory/
b01.xml
b04.xml
c01.xml
index.xml
<?php
// ** Variables definition ** //
define('DB_NAME', 'wp_fs'); // The name of the database
define('DB_USER', 'root'); // Your MySQL username
define('DB_PASSWORD', 'root'); // ...and password
define('DB_HOST', 'localhost'); // 99% chance you won't need to change
this value
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('DEFAULT_BALANCE', 100);
?>
Common.php describes the functions required for working with the balance.
<?php
require_once 'config.php';
function getConnection()
{
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die('Could not connect: ' . mysql_error());
if(!mysql_select_db(DB_NAME))
die('Could not select database');
return $con;
}
function createAccount($abonent)
{
$con = getConnection();
$query = "INSERT INTO `accounts`(`abonent`,
`balance`) values($abonent,
".DEFAULT_BALANCE.")";
if(!mysql_query($query, $con))
die('createAccount: Query failed: ' . mysql_error());
}
function getBallance($abonent)
{
$con = getConnection();
$query = "SELECT `balance` FROM `accounts` WHERE `abonent` =
$abonent";
$result = mysql_query($query, $con)
or die('getBallance: Query failed: ' . mysql_error());
if(mysql_num_rows($result) == 0)
{
return -1;
}
$data = mysql_fetch_array($result);
return $data['balance'];
}
function setBalance($abonent, $balance)
{
$con = getConnection();
$query = "UPDATE `accounts` SET `balance`= $balance WHERE `abonent` =
$abonent";
if(!mysql_query($query, $con))
die('setBalance: Query failed: ' . mysql_error());
}
function sendMoney($abonent, $amount, $dest)
{
$con = getConnection();
$curBalance = getBallance($abonent);
if($dest === $abonent)
{
return "You can't send money to yourself";
}
if($curBalance < $amount)
{
return "Not enough money";
}
$newBalance = $curBalance - $amount;
if($dest !== "m" && $dest !== "M")
{
$destBalance = getBallance($dest);
if($destBalance == -1)
{
return "Account $dest not found";
}
setBalance($dest, $destBalance + $amount);
}
$message = "Operation successful.";
if($newBalance == 0)
{
$newBalance = DEFAULT_BALANCE;
$message .= " Your account has been recharged.";
}
setBalance($abonent, $newBalance);
$message .= " Your balance is $newBalance";
return $message;
}
?>
Now it is necessary to describe service pages for showing balance and transferring money.
<?php
require_once 'common.php';
if(!isset($_POST['abonent']))
die("Not enough parameters");
$abonent = $_POST['abonent'];
$balance = getBallance($abonent);
if($balance == -1)
{
createAccount($abonent);
$balance = DEFAULT_BALANCE;
}
header('Content-Type: text/xml');
print '<?xml version="1.0" encoding="UTF-8"?>';
?>
<page version="2.0">
<title>Balance</title>
<div>
Your balance is <?=$balance?>
</div>
<navigation>
<link accesskey="0" pageId="a01.xml" type="back">Back</link>
</navigation>
</page>
MiniApps sends the subscriber number and the protocol (USSD) used by the subscriber for
access, on the part of the service.The balance is requested from the database using the
subscriber number. If the record needed is not there yet, a record is created [subscriber
number; default balance]. On the service page we draw the value of balance.The link Back
leads to the file a01.xml, described in Static Service in XML of this document. This file specifies
the main service menu.On a mobile device, this page will appear as follows:
BalanceYour balance is 1000>Back
For transferring money, the subscriber should enter the number of another subscriber who has
the entry in the database, or the letter “m” for a predefined transfer, as well as the transfer
amount.Since USSD supports the input of only one answer on each page, for entering the
values Destination and Amount, we will create two different pages, consecutive to each other,
using the protocol USSD.
Code Block 9 send-money.php
<?php
header('Content-Type: text/xml');
print '<?xml version="1.0" encoding="UTF-8"?>';
?>
<page version="2.0">
<div protocol="ussd">
<input navigationId="form" title="Enter Destination" name="dest"/>
</div>
<navigation id="form" protocol="ussd">
<link pageId="amount.php"></link>
</navigation>
<navigation>
<link accesskey="0" pageId="a01.xml" type="back">Back</link>
</navigation>
</page>
<?php
$dest = $_REQUEST['dest'];
header('Content-Type: text/xml');
print '<?xml version="1.0" encoding="UTF-8"?>';
?>
<page version="2.0">
<div protocol="ussd">
<input navigationId="form" title="Enter Destination" name="dest"
value="<?php echo
$dest?>" type="hidden"/><br/>
<input navigationId="form" title="Enter Amount" name="amount"
type="Number"/>
</div>
<navigation id="form" protocol="ussd">
<link pageId="transaction.php" ></link>
</navigation>
<navigation>
<link accesskey="0" pageId="send-money.php"
type="back">Back</link>
<link accesskey="1" pageId="a01.xml">Main menu</link>
</navigation>
</page>
To avoid losing the value Destination, a hidden field type=”hidden” is used; you can use a
session mechanism or both. After entering the value “Amount” you are transferred to the page
transaction.php.
Enter Amount:0>Back
<?php
require_once 'common.php';
$abonent = $_REQUEST['abonent'];
$amount = $_REQUEST['amount'];
$dest = $_REQUEST['dest'];
$message = sendMoney($abonent, $amount, $dest);
header('Content-Type: text/xml');
print '<?xml version="1.0" encoding="UTF-8"?>';
?>
<page version="2.0">
<title protocol="wap java">Send money</title>
<div><?php echo $message?></div>
<navigation>
<link accesskey="0" pageId="send-money.php"
type="back">Back</link>
<link accesskey="1" pageId="a01.xml">Main menu</link>
</navigation>
</page>
../service_site_directory/
amount.php
common.php
config.php
send-money.php
show-balance.php
transaction.php
other files
...
It is possible to combine the static and the dynamic service files and upload them on the web
server. The service source is ready and it is possible to start its using.
../service_site_directory/
amount.php
common.php
config.php
send-money.php
show-balance.php
transaction.php
b01.xml
b04.xml
c01.xml
index.xml
4 Connecting Service
This chapter describes how to connect C2S number and then use it as user's access point to
service. C2S number is a Call to Service phone number. By dialing this number a user
receives USSD menu for communication with a service.
You will see a gray handset icon with Not connected to USSD text hint next to it. Click on the
icon and a window, where you can rent C2S numbers, will appear (please notice that you must
have a positive balance in your wallet to rent a C2S number. You can always check the amount
Connecting Service – 16
MiniApps Documentation – How to create USSD service
You can see the price for every available number. Find a suitable number and click Rent &
Assign button. Then confirm renting with OK button.
A message: Call2Service number was successfully assigned will appear. The bot
information page is updated. Now you can see that the handset icon is active:
From now on, your bot can use USSD channel to communicate with users.
Connecting Service – 17
MiniApps Documentation – How to create USSD service
In the Bot column you will see if a number is assigned to a bot. If you don't the number
anymore, click on the Cancel rent red button opposite the number:
The number will be available for renting again only until the date stated in the column Rental
until.