forked from yvesniyo/ussd-simulator-with-server-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
171 lines (144 loc) · 2.7 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
$number=$_REQUEST['phone'];
$sessionId=$_REQUEST['sessionId'];
$text=$_REQUEST['text'];
$code=$_REQUEST['code'];
$mode="menu";
$level = 1;
$ussd_array = explode ("*",$text);
if(count($ussd_array)==0){
$level=0;
}else{
$level=count($ussd_array);
}
$level=$level+1;
if(trim($text)=="")
$level=1;
switch ($ussd_array[0]) {
case '0':
$mode="menu";
break;
case '':
$mode="menu";
break;
case '1':
$mode="create";
break;
case '2':
$mode="balance";
break;
case '3':
$mode="sendMoney";
break;
default:
# code...
break;
}
if($level==1){
displayMenu();
}else{
if($mode=="menu"){
switch ($text) {
case '0':
displayMenu();
break;
case '1':
createAccount();
break;
case '2':
checkBalance();
break;
case '3':
checkNumber();
break;
case '4':
sendMoney();
break;
default:
displayError();
break;
}
}else if($mode=="create"){
switch ($text) {
case '1':
createAccount();
break;
case '1*1':
echo "END You choose \n Male";
break;
case '1*2':
echo "END You choose \n Female";
break;
case '1*3':
echo "END You choose \n Not Now";
break;
default:
displayError();
break;
}
}else if($mode=="balance"){
checkBalance();
}else if($mode=="sendMoney"){
switch ($text) {
case '3':
sendMoney();
break;
case '3*1':
echo "END You choose \n MTN";
break;
case '3*2':
echo "END You choose \n AIRTEL";
break;
case '3*3':
echo "END You choose \n TIGO";
break;
default:
displayError();
break;
}
}
}
function displayMenu(){
$text="CON USSD Testing \n";
$text.="1. Create Account \n";
$text.="2. Check Balance \n";
$text.="3. Check My Number \n";
$text.="4. Send Money \n";
$text.="5. Buy Books \n";
$text.="44. Next \n";
echo $text;
}
function createAccount(){
$text="CON Creating Account\n";
$text.="1. Male \n";
$text.="2. Woman \n";
$text.="3. Not Now \n";
echo $text;
}
function checkBalance(){
$text="END Account Balance\n";
$text.="Your account Balance is 43,050Rwf \n";
echo $text;
}
function displayError(){
$text="END Error\n";
$text.="Uknown USSD command \n";
echo $text;
}
function checkNumber(){
$text="END Ckeck Number\n";
$number=$_REQUEST['phone'];
$text.="Your number is ".$number." \n";
echo $text;
}
function sendMoney(){
$text="CON Send Money\n";
$text.="1. MTN \n";
$text.="2. TIGO \n";
$text.="3. AIRTEL \n";
echo $text;
}