Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/nbproject/private/
14 changes: 14 additions & 0 deletions class/db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
// 1. Create a database connection
$connection = mysqli_connect("localhost", "root", "", "sessiondb");
// Test if connection succeeded
if (mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>



74 changes: 35 additions & 39 deletions class/operationsClass.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
<?php

class Operations
{

public $session_id='';
public $session_menu='';
public $session_pg=0;
public $session_tel='';
public $session_others='';

public function setSessions($sessions){

$sql_sessions="INSERT INTO `sessions` (`sessionsid`, `tel`, `menu`, `pg`, `created_at`,`others`) VALUES
('".$sessions['sessionid']."', '".$sessions['tel']."', '".$sessions['menu']."', '".$sessions['pg']."', 'CURRENT_TIMESTAMP','".$sessions['others']."')";

$quy_sessions=mysql_query($sql_sessions);
}

public function getSession($sessionid){

$sql_session="SELECT * FROM `sessions` WHERE sessionsid='". $sessionid."'";
$quy_sessions=mysql_query($sql_session);
$fet_sessions=mysql_fetch_array($quy_sessions);
$this->session_others=$fet_sessions['others'];
return $fet_sessions;
}


public function saveSesssion()
{
$sql_session="UPDATE `sessions` SET
`menu` = '".$this->session_menu."',
`pg` = '".$this->session_pg."',
`others` = '".$this->session_others."'
WHERE `sessionsid` = '".$this->session_id."'";
$quy_sessions=mysql_query($sql_session);
}


require 'db.php';

class Operations {

public $session_id = '';
public $session_menu = '';
public $session_pg = 0;
public $session_tel = '';
public $session_others = '';

public function setSessions($sessions) {
global $connection;
$sql_sessions = "INSERT INTO `sessions` (`sessionsid`, `tel`, `menu`, `pg`, `created_at`,`others`) VALUES
('" . $sessions['sessionid'] . "', '" . $sessions['tel'] . "', '" . $sessions['menu'] . "', '" . $sessions['pg'] . "', CURRENT_TIMESTAMP,'" . $sessions['others'] . "')";
// if you have errors in here check sql query near " CURRENT_TIMESTAMP ".
$quy_sessions = mysqli_query($connection, $sql_sessions);
}

public function getSession($sessionid) {

$sql_session = "SELECT * FROM `sessions` WHERE sessionsid='" . $sessionid . "'";
$quy_sessions = mysqli_query($connection, $sql_session);
$fet_sessions = mysqli_fetch_array($quy_sessions);
$this->session_others = $fet_sessions['others'];
return $fet_sessions;
}

public function saveSesssion() {
$sql_session = "UPDATE `sessions` SET
`menu` = '" . $this->session_menu . "',
`pg` = '" . $this->session_pg . "',
`others` = '" . $this->session_others . "'
WHERE `sessionsid` = '" . $this->session_id . "'";
$quy_sessions = mysqli_query($connection, $sql_session);
}

}

Expand Down
7 changes: 0 additions & 7 deletions db.php

This file was deleted.

7 changes: 7 additions & 0 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include.path=${php.global.include.path}
php.version=PHP_54
source.encoding=UTF-8
src.dir=.
tags.asp=false
tags.short=false
web.root=.
9 changes: 9 additions & 0 deletions nbproject/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>USSD-PHP-API</name>
</data>
</configuration>
</project>
166 changes: 81 additions & 85 deletions ussdap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,32 @@
require 'db.php';


$production=false;
$production = false;

if($production==false){
$ussdserverurl ='http://localhost:7000/ussd/send';
}
else{
$ussdserverurl= 'https://api.dialog.lk/ussd/send';
}
if ($production == false) {
$ussdserverurl = 'http://localhost:7000/ussd/send';
} else {
$ussdserverurl = 'https://api.dialog.lk/ussd/send';
}


$receiver = new UssdReceiver();
$sender = new UssdSender($ussdserverurl,'APP_000001','password');
$receiver = new UssdReceiver();
$sender = new UssdSender($ussdserverurl, 'APP_000001', 'password');
$operations = new Operations();

$receiverSessionId = $receiver->getSessionId();
$content = $receiver->getMessage(); // get the message content
$address = $receiver->getAddress(); // get the sender's address
$requestId = $receiver->getRequestID(); // get the request ID
$applicationId = $receiver->getApplicationId(); // get application ID
$encoding = $receiver->getEncoding(); // get the encoding value
$version = $receiver->getVersion(); // get the version
$sessionId = $receiver->getSessionId(); // get the session ID;
$ussdOperation = $receiver->getUssdOperation(); // get the ussd operation
$content = $receiver->getMessage(); // get the message content
$address = $receiver->getAddress(); // get the sender's address
$requestId = $receiver->getRequestID(); // get the request ID
$applicationId = $receiver->getApplicationId(); // get application ID
$encoding = $receiver->getEncoding(); // get the encoding value
$version = $receiver->getVersion(); // get the version
$sessionId = $receiver->getSessionId(); // get the session ID;
$ussdOperation = $receiver->getUssdOperation(); // get the ussd operation


$responseMsg = array(
"main" =>
"main" =>
"T-Shirts
1. Small
2. Medium
Expand All @@ -45,71 +44,68 @@
);


if ($ussdOperation == "mo-init") {

try {

$sessionArrary=array( "sessionid"=>$sessionId,"tel"=>$address,"menu"=>"main","pg"=>"","others"=>"");

$operations->setSessions($sessionArrary);

$sender->ussd($sessionId, $responseMsg["main"],$address );

} catch (Exception $e) {
$sender->ussd($sessionId, 'Sorry error occured try again',$address );
}

}else {

$flag=0;

$sessiondetails= $operations->getSession($sessionId);
$cuch_menu=$sessiondetails['menu'];
$operations->session_id=$sessiondetails['sessionsid'];

switch($cuch_menu ){

case "main": // Following is the main menu
switch ($receiver->getMessage()) {
case "1":
$operations->session_menu="small";
$operations->saveSesssion();
$sender->ussd($sessionId,'Enter Your ID',$address );
break;
case "2":
$operations->session_menu="medium";
$operations->saveSesssion();
$sender->ussd($sessionId,'Enter Your ID',$address );
break;
case "3":
$operations->session_menu="large";
$operations->saveSesssion();
$sender->ussd($sessionId,'Enter Your ID',$address );
break;
default:
$operations->session_menu="main";
$operations->saveSesssion();
$sender->ussd($sessionId, $responseMsg["main"],$address );
break;
}
break;
case "small":
$operations->session_menu="medium";
$operations->session_others=$receiver->getMessage();
$operations->saveSesssion();
$sender->ussd($sessionId,'You Purchased a small T-Shirt Your ID '.$receiver->getMessage(),$address ,'mt-fin');
break;
case "medium":
$sender->ussd($sessionId,'You Purchased a medium T-Shirt Your ID '.$receiver->getMessage(),$address ,'mt-fin');
break;
case "large":
$sender->ussd($sessionId,'You Purchased a large T-Shirt Your ID '.$receiver->getMessage(),$address ,'mt-fin');
break;
default:
$operations->session_menu="main";
$operations->saveSesssion();
$sender->ussd($sessionId,'Incorrect option',$address );
break;
}

if ($ussdOperation == "mo-init") {

try {

$sessionArrary = array("sessionid" => $sessionId, "tel" => $address, "menu" => "main", "pg" => "", "others" => "");

$operations->setSessions($sessionArrary);

$sender->ussd($sessionId, $responseMsg["main"], $address);
} catch (Exception $e) {
$sender->ussd($sessionId, 'Sorry error occured try again', $address);
}
} else {

$flag = 0;

$sessiondetails = $operations->getSession($sessionId);
$cuch_menu = $sessiondetails['menu'];
$operations->session_id = $sessiondetails['sessionsid'];

switch ($cuch_menu) {

case "main": // Following is the main menu
switch ($receiver->getMessage()) {
case "1":
$operations->session_menu = "small";
$operations->saveSesssion();
$sender->ussd($sessionId, 'Enter Your ID', $address);
break;
case "2":
$operations->session_menu = "medium";
$operations->saveSesssion();
$sender->ussd($sessionId, 'Enter Your ID', $address);
break;
case "3":
$operations->session_menu = "large";
$operations->saveSesssion();
$sender->ussd($sessionId, 'Enter Your ID', $address);
break;
default:
$operations->session_menu = "main";
$operations->saveSesssion();
$sender->ussd($sessionId, $responseMsg["main"], $address);
break;
}
break;
case "small":
$operations->session_menu = "medium";
$operations->session_others = $receiver->getMessage();
$operations->saveSesssion();
$sender->ussd($sessionId, 'You Purchased a small T-Shirt Your ID ' . $receiver->getMessage(), $address, 'mt-fin');
break;
case "medium":
$sender->ussd($sessionId, 'You Purchased a medium T-Shirt Your ID ' . $receiver->getMessage(), $address, 'mt-fin');
break;
case "large":
$sender->ussd($sessionId, 'You Purchased a large T-Shirt Your ID ' . $receiver->getMessage(), $address, 'mt-fin');
break;
default:
$operations->session_menu = "main";
$operations->saveSesssion();
$sender->ussd($sessionId, 'Incorrect option', $address);
break;
}
}