-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathindex.php
96 lines (85 loc) · 3.26 KB
/
index.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
<?php
// We begin by reading the HTTP request body contents.
// Since we expect is to be in JSON format, let's parse as well.
$ussdRequest = json_decode(@file_get_contents('php://input'));
// Our response object. We shall use PHP's json_encode function
// to convert the various properties (we'll set later) into JSON.
$ussdResponse = new stdclass;
// Check if the decoding of the HTTP request body into JSON was
// successful. You can use json_last_error to get the exact nature of
// the error in event the decoding failed. I'll skip that here.
if ($ussdRequest != NULL)
switch ($ussdRequest->Type) {
// Initiation request. This is the first type of request every
// USSD application will receive. So let's display our main menu.
case 'Initiation':
$ussdResponse->Message =
"Welcome to Freebie Service.\n" .
"1. Free Food\n2. Free Drink\n3. Free Airtime";
$ussdResponse->Type = 'Response';
break;
// Response request. This is where all other interactions occur.
// Every time the mobile subscriber responds to any of our menus,
// this will be the type of request we shall receive.
case 'Response':
switch ($ussdRequest->Sequence) {
// Menu selection. Note that everytime we receive a request
// in a particular session, the Sequence will increase by 1.
// Sequence number 1 was that of the initiation request.
case 2:
$items = array('1' => 'food', '2' => 'drink', '3' => 'airtime');
if (isset($items[$ussdRequest->Message])) {
$ussdResponse->Message = 'Are you sure you want free '
. $items[$ussdRequest->Message] . "?\n1. Yes\n2. No";
$ussdResponse->Type = 'Response';
$ussdResponse->ClientState = $items[$ussdRequest->Message];
} else {
$ussdResponse->Message = 'Invalid option.';
$ussdResponse->Type = 'Release';
}
break;
// Order confirmation. Here the user has responded to our
// previously sent menu (i.e. Are you sure you want...)
// Note that we saved the option the user selected in our
// previous dialog into the ClientState property.
case 3:
switch ($ussdRequest->Message) {
case '1':
$ussdResponse->Message =
'Thank you. You will receive your free '
. $ussdRequest->ClientState . ' shortly.';
break;
case '2':
$ussdResponse->Message = 'Order cancelled.';
break;
default:
$ussdResponse->Message = 'Invalid selection.';
break;
}
$ussdResponse->Type = "Release";
break;
// Unexpected request. If the code here should ever
// execute, it means the request is probably forged.
default:
$ussdResponse->Message = 'Unexpected request.';
$ussdResponse->Type = 'Release';
break;
}
break;
// Session cleanup.
// Not much to do here.
default:
$ussdResponse->Message = 'Duh.';
$ussdResponse->Type = 'Release';
break;
}
// An error has occured.
// Probably the request JSON could not be parsed.
else {
$ussdResponse->Message = 'Invalid USSD request.';
$ussdResponse->Type = 'Release';
}
// Let's set the HTTP content-type of our response, encode our
// USSD response object into JSON, and flush the output.
header('Content-type: application/json; charset=utf-8');
echo json_encode($ussdResponse);