Created
March 16, 2017 04:16
-
-
Save JaniKibichi/f8fabc3b988eb2c994a509070c44777a to your computer and use it in GitHub Desktop.
An example of a USSD switch statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//1. Ensure ths code runs only after a POST from AT | |
if(!empty($_POST) && !empty($_POST['phoneNumber'])){ | |
require_once('dbConnector.php'); | |
require_once('AfricasTalkingGateway.php'); | |
require_once('config.php'); | |
//2. receive the POST from AT | |
$sessionId =$_POST['sessionId']; | |
$serviceCode =$_POST['serviceCode']; | |
$phoneNumber =$_POST['phoneNumber']; | |
$text =$_POST['text']; | |
//3. Explode the text to get the value of the latest interaction - think 1*1 | |
$textArray=explode('*', $text); | |
$userResponse=trim(end($textArray)); | |
//4. Switch the user responses | |
switch ($userResponse) { | |
case "": | |
if($level==0){ | |
//9b. Graduate user to next level & Serve Main Menu | |
$sql9b = "INSERT INTO `session_levels`(`session_id`,`phoneNumber`,`level`) VALUES('".$sessionId."','".$phoneNumber."',1)"; | |
$db->query($sql9b); | |
//Serve our services menu | |
$response = "CON Welcome to Nerd Microfinance, " . $userAvailable['name'] . ". Choose a service.\n"; | |
$response .= " 1. Please call me.\n"; | |
$response .= " 2. Deposit Money\n"; | |
$response .= " 3. Withdraw Money\n"; | |
$response .= " 4. Send Money\n"; | |
$response .= " 5. Buy Airtime\n"; | |
$response .= " 6. Repay Loan\n"; | |
$response .= " 7. Account Balance\n"; | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
break; | |
case "0": | |
if($level==0){ | |
//9b. Graduate user to next level & Serve Main Menu | |
$sql9b = "INSERT INTO `session_levels`(`session_id`,`phoneNumber`,`level`) VALUES('".$sessionId."','".$phoneNumber."',1)"; | |
$db->query($sql9b); | |
//Serve our services menu | |
$response = "CON Welcome to Nerd Microfinance, " . $userAvailable['username'] . ". Choose a service.\n"; | |
$response .= " 1. Please call me.\n"; | |
$response .= " 2. Deposit\n"; | |
$response .= " 3. Withdraw\n"; | |
$response .= " 4. Send Money\n"; | |
$response .= " 5. Buy Airtime\n"; | |
$response .= " 6. Repay Loan\n"; | |
$response .= " 7. Account Balance\n"; | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
break; | |
case "1": | |
if($level==1){ | |
//9d. Call the user and bridge to a sales person | |
$response = "END Please wait while we place your call.\n"; | |
//Make a call | |
$from="+254722000000"; $to=$phoneNumber; | |
// Create a new instance of our awesome gateway class | |
$gateway = new AfricasTalkingGateway($username, $apikey, "sandbox"); | |
try { $gateway->call($from, $to); } | |
catch ( AfricasTalkingGatewayException $e ){echo "Encountered an error when calling: ".$e->getMessage();} | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
break; | |
case "2": | |
if($level==1){ | |
//9e. Ask how much and Launch the Mpesa Checkout to the user | |
$response = "CON How much are you depositing?\n"; | |
$response .= " 1. 5 Shillings.\n"; | |
$response .= " 2. 6 Shillings.\n"; | |
$response .= " 3. 7 Shillings.\n"; | |
//Update sessions to level 9 | |
$sqlLvl9="UPDATE `session_levels` SET `level`=9 where `session_id`='".$sessionId."'"; | |
$db->query($sqlLvl9); | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
break; | |
case "3": | |
if($level==1){ | |
//9e. Ask how much and Launch B2C to the user | |
$response = "CON How much are you withdrawing?\n"; | |
$response .= " 1. 5 Shillings.\n"; | |
$response .= " 2. 6 Shillings.\n"; | |
$response .= " 3. 7 Shillings.\n"; | |
//Update sessions to level 10 | |
$sqlLvl10="UPDATE `session_levels` SET `level`=10 where `session_id`='".$sessionId."'"; | |
$db->query($sqlLvl10); | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
break; | |
case "4": | |
if($level==1){ | |
//9g. Send Another User Some Money | |
$response = "CON You can only send 5 shillings.\n"; | |
$response .= " Enter a valid phonenumber (like 0722122122)\n"; | |
//Update sessions to level 11 | |
$sqlLvl11="UPDATE `session_levels` SET `level`=11 where `session_id`='".$sessionId."'"; | |
$db->query($sqlLvl11); | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
break; | |
case "5": | |
if($level==1){ | |
//Find account | |
$sql10a = "SELECT * FROM account WHERE phoneNumber LIKE '%".$phoneNumber."%' LIMIT 1"; | |
$balQuery=$db->query($sql10a); | |
$balAvailable=$balQuery->fetch_assoc(); | |
if($balAvailable=$balQuery->fetch_assoc()){ | |
// Reduce balance | |
$newBal = $balAvailable['balance']; | |
$newBal -= 5; | |
if($newBal > 0){ | |
//9e. Send user airtime | |
$response = "END Please wait while we load your account.\n"; | |
// Search DB and the Send Airtime | |
$recipients = array( array("phoneNumber"=>"".$phoneNumber."", "amount"=>"KES 5") ); | |
//JSON encode | |
$recipientStringFormat = json_encode($recipients); | |
//Create an instance of our gateway class, pass your credentials | |
$gateway = new AfricasTalkingGateway($username, $apikey, "sandbox"); | |
try { $results = $gateway->sendAirtime($recipientStringFormat);} | |
catch(AfricasTalkingGatewayException $e){ echo $e->getMessage(); } | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} else { | |
//Alert user of insufficient funds | |
$response = "END Sorry, you dont have sufficient\n"; | |
$response .= " funds in your account \n"; | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
} | |
} | |
break; | |
case "6": | |
if($level==1){ | |
//9e. Ask how much and Launch the Mpesa Checkout to the user | |
$response = "CON How much are you depositing?\n"; | |
$response .= " 4. 5 Shilling.\n"; | |
$response .= " 5. 6 Shillings.\n"; | |
$response .= " 6. 7 Shillings.\n"; | |
//Update sessions to level 9 | |
$sqlLvl12="UPDATE `session_levels` SET `level`=12 where `session_id`='".$sessionId."'"; | |
$db->query($sqlLvl12); | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
break; | |
case "7": | |
if($level==1){ | |
// Find the user in the db | |
$sql7 = "SELECT * FROM microfinance WHERE phoneNumber LIKE '%".$phoneNumber."%' LIMIT 1"; | |
$userQuery=$db->query($sql7); | |
$userAvailable=$userQuery->fetch_assoc(); | |
// Find the account | |
$sql7a = "SELECT * FROM account WHERE phoneNumber LIKE '%".$phoneNumber."%' LIMIT 1"; | |
$BalQuery=$db->query($sql7a); | |
$newBal = 0.00; $newLoan = 0.00; | |
if($BalAvailable=$BalQuery->fetch_assoc()){ | |
$newBal = $BalAvailable['balance']; | |
$newLoan = $BalAvailable['loan']; | |
} | |
//Respond with user Balance | |
$response = "END Your account statement.\n"; | |
$response .= "Nerd Microfinance.\n"; | |
$response .= "Name: ".$userAvailable['name']."\n"; | |
$response .= "City: ".$userAvailable['city']."\n"; | |
$response .= "Balance: ".$newBal."\n"; | |
$response .= "Loan: ".$newLoan."\n"; | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
break; | |
default: | |
if($level==1){ | |
// Return user to Main Menu & Demote user's level | |
$response = "CON You have to choose a service.\n"; | |
$response .= "Press 0 to go back.\n"; | |
//demote | |
$sqlLevelDemote="UPDATE `session_levels` SET `level`=0 where `session_id`='".$sessionId."'"; | |
$db->query($sqlLevelDemote); | |
// Print the response onto the page so that our gateway can read it | |
header('Content-type: text/plain'); | |
echo $response; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment