├── README.md ├── class └── operationsClass.php ├── db.php ├── libs ├── Log.php ├── MoUssdReceiver.php ├── MtUssdSender.php └── core.php ├── sessions.sql └── ussdap.php /README.md: -------------------------------------------------------------------------------- 1 | USSD-PHP-API 2 | ============ 3 | 4 | Ideamart USSD PHP API 5 | -------------------------------------------------------------------------------- /class/operationsClass.php: -------------------------------------------------------------------------------- 1 | session_others=$fet_sessions['others']; 26 | return $fet_sessions; 27 | } 28 | 29 | 30 | public function saveSesssion() 31 | { 32 | $sql_session="UPDATE `sessions` SET 33 | `menu` = '".$this->session_menu."', 34 | `pg` = '".$this->session_pg."', 35 | `others` = '".$this->session_others."' 36 | WHERE `sessionsid` = '".$this->session_id."'"; 37 | $quy_sessions=mysql_query($sql_session); 38 | } 39 | 40 | 41 | 42 | } 43 | 44 | ?> -------------------------------------------------------------------------------- /db.php: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /libs/Log.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /libs/MoUssdReceiver.php: -------------------------------------------------------------------------------- 1 | thejson = json_decode(file_get_contents('php://input'), true); 30 | $this->sourceAddress = $array['sourceAddress']; 31 | $this->message = $array['message']; 32 | $this->requestId = $array['requestId']; 33 | $this->applicationId = $array['applicationId']; 34 | $this->encoding = $array['encoding']; 35 | $this->version = $array['version']; 36 | $this->sessionId = $array['sessionId']; 37 | $this->ussdOperation = $array['ussdOperation']; 38 | 39 | if (!((isset($this->sourceAddress) && isset($this->message)))) { 40 | throw new Exception("Some of the required parameters are not provided"); 41 | } else { 42 | $responses = array("statusCode" => "S1000", "statusDetail" => "Success"); 43 | } 44 | } 45 | 46 | public function getthejson(){ 47 | return $this->thejson; 48 | } 49 | 50 | public function getAddress(){ 51 | return $this->sourceAddress; 52 | } 53 | 54 | public function getMessage(){ 55 | return $this->message; 56 | } 57 | 58 | public function getRequestID(){ 59 | return $this->requestId; 60 | } 61 | 62 | public function getApplicationId(){ 63 | return $this->applicationId; 64 | } 65 | 66 | public function getEncoding(){ 67 | return $this->encoding; 68 | } 69 | 70 | public function getVersion(){ 71 | return $this->version; 72 | } 73 | 74 | public function getSessionId(){ 75 | return $this->sessionId; 76 | } 77 | 78 | public function getUssdOperation(){ 79 | return $this->ussdOperation; 80 | } 81 | 82 | } 83 | 84 | ?> -------------------------------------------------------------------------------- /libs/MtUssdSender.php: -------------------------------------------------------------------------------- 1 | serverURL = $server; 17 | $this->applicationId = $applicationId; 18 | $this->password = $password; 19 | } 20 | 21 | public function ussd( $sessionId, $message, $destinationAddress, $ussdOperation='mo-cont'){ 22 | 23 | if (is_array($destinationAddress)) { 24 | return $this->ussdMany($message,$sessionId, $ussdOperation, $destinationAddress); 25 | 26 | } else if (is_string($destinationAddress) && trim($destinationAddress) != "") { 27 | return $this->ussdMany($message,$sessionId, $ussdOperation, $destinationAddress); 28 | } else { 29 | throw new Exception("address should a string or a array of strings"); 30 | } 31 | } 32 | 33 | private function ussdMany($message,$sessionId, $ussdOperation, $destinationAddress) 34 | { 35 | 36 | $arrayField = array("applicationId" => $this->applicationId, 37 | "password" => $this->password, 38 | "message" => $message, 39 | "destinationAddress" => $destinationAddress, 40 | "sessionId" => $sessionId, 41 | "ussdOperation" => $ussdOperation, 42 | "encoding" => "440" 43 | ); 44 | 45 | $jsonObjectFields = json_encode($arrayField); 46 | return $this->sendRequest($jsonObjectFields,$this->serverURL); 47 | } 48 | 49 | private function handleResponse($resp){ 50 | if ($resp == "") { 51 | throw new UssdException 52 | ("Server URL is invalid", '500'); 53 | } else { 54 | echo $resp; 55 | } 56 | } 57 | 58 | } 59 | 60 | 61 | 62 | class UssdException extends Exception{ // Ussd Exception Handler 63 | 64 | var $code; 65 | var $response; 66 | var $statusMessage; 67 | 68 | public function __construct($message, $code, $response = null){ 69 | parent::__construct($message); 70 | $this->statusMessage = $message; 71 | $this->code = $code; 72 | $this->response = $response; 73 | } 74 | 75 | public function getStatusCode(){ 76 | return $this->code; 77 | } 78 | 79 | public function getStatusMessage(){ 80 | return $this->statusMessage; 81 | } 82 | 83 | public function getRawResponse(){ 84 | return $this->response; 85 | } 86 | 87 | } 88 | 89 | ?> -------------------------------------------------------------------------------- /libs/core.php: -------------------------------------------------------------------------------- 1 | 31 | -------------------------------------------------------------------------------- /sessions.sql: -------------------------------------------------------------------------------- 1 | -- -------------------------------------------------------- 2 | -- Host: 127.0.0.1 3 | -- Server version: 5.5.25a - MySQL Community Server (GPL) 4 | -- Server OS: Win32 5 | -- HeidiSQL Version: 7.0.0.4389 6 | -- -------------------------------------------------------- 7 | 8 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 9 | /*!40101 SET NAMES utf8 */; 10 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 11 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 12 | 13 | -- Dumping structure for table celebrity.sessions 14 | DROP TABLE IF EXISTS `sessions`; 15 | CREATE TABLE IF NOT EXISTS `sessions` ( 16 | `sessionsid` varchar(100) NOT NULL, 17 | `tel` varchar(50) DEFAULT NULL, 18 | `menu` varchar(50) DEFAULT NULL, 19 | `pg` varchar(50) DEFAULT NULL, 20 | `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, 21 | `others` varchar(50) DEFAULT NULL, 22 | PRIMARY KEY (`sessionsid`) 23 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Handle the sessions'; 24 | 25 | -- Data exporting was unselected. 26 | /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; 27 | /*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */; 28 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 29 | -------------------------------------------------------------------------------- /ussdap.php: -------------------------------------------------------------------------------- 1 | getSessionId(); 27 | $content = $receiver->getMessage(); // get the message content 28 | $address = $receiver->getAddress(); // get the sender's address 29 | $requestId = $receiver->getRequestID(); // get the request ID 30 | $applicationId = $receiver->getApplicationId(); // get application ID 31 | $encoding = $receiver->getEncoding(); // get the encoding value 32 | $version = $receiver->getVersion(); // get the version 33 | $sessionId = $receiver->getSessionId(); // get the session ID; 34 | $ussdOperation = $receiver->getUssdOperation(); // get the ussd operation 35 | 36 | 37 | $responseMsg = array( 38 | "main" => 39 | "T-Shirts 40 | 1. Small 41 | 2. Medium 42 | 3. Large 43 | 44 | 99. Exit" 45 | ); 46 | 47 | 48 | if ($ussdOperation == "mo-init") { 49 | 50 | try { 51 | 52 | $sessionArrary=array( "sessionid"=>$sessionId,"tel"=>$address,"menu"=>"main","pg"=>"","others"=>""); 53 | 54 | $operations->setSessions($sessionArrary); 55 | 56 | $sender->ussd($sessionId, $responseMsg["main"],$address ); 57 | 58 | } catch (Exception $e) { 59 | $sender->ussd($sessionId, 'Sorry error occured try again',$address ); 60 | } 61 | 62 | }else { 63 | 64 | $flag=0; 65 | 66 | $sessiondetails= $operations->getSession($sessionId); 67 | $cuch_menu=$sessiondetails['menu']; 68 | $operations->session_id=$sessiondetails['sessionsid']; 69 | 70 | switch($cuch_menu ){ 71 | 72 | case "main": // Following is the main menu 73 | switch ($receiver->getMessage()) { 74 | case "1": 75 | $operations->session_menu="small"; 76 | $operations->saveSesssion(); 77 | $sender->ussd($sessionId,'Enter Your ID',$address ); 78 | break; 79 | case "2": 80 | $operations->session_menu="medium"; 81 | $operations->saveSesssion(); 82 | $sender->ussd($sessionId,'Enter Your ID',$address ); 83 | break; 84 | case "3": 85 | $operations->session_menu="large"; 86 | $operations->saveSesssion(); 87 | $sender->ussd($sessionId,'Enter Your ID',$address ); 88 | break; 89 | default: 90 | $operations->session_menu="main"; 91 | $operations->saveSesssion(); 92 | $sender->ussd($sessionId, $responseMsg["main"],$address ); 93 | break; 94 | } 95 | break; 96 | case "small": 97 | $operations->session_menu="medium"; 98 | $operations->session_others=$receiver->getMessage(); 99 | $operations->saveSesssion(); 100 | $sender->ussd($sessionId,'You Purchased a small T-Shirt Your ID '.$receiver->getMessage(),$address ,'mt-fin'); 101 | break; 102 | case "medium": 103 | $sender->ussd($sessionId,'You Purchased a medium T-Shirt Your ID '.$receiver->getMessage(),$address ,'mt-fin'); 104 | break; 105 | case "large": 106 | $sender->ussd($sessionId,'You Purchased a large T-Shirt Your ID '.$receiver->getMessage(),$address ,'mt-fin'); 107 | break; 108 | default: 109 | $operations->session_menu="main"; 110 | $operations->saveSesssion(); 111 | $sender->ussd($sessionId,'Incorrect option',$address ); 112 | break; 113 | } 114 | 115 | } --------------------------------------------------------------------------------