Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Author: Eric Mutua
- */
- // Reads the variables sent via POST
- $sessionId = $_POST["sessionId"];
- $serviceCode = $_POST["serviceCode"];
- $phoneNumber = $_POST["phoneNumber"];
- $text = $_POST["text"];
- // Global Variables
- $till_num = "";
- $pin = "";
- $amount = "";
- $price = 0;
- $N = 0;
- // first response which the user gets
- if($text == ""){
- $response = "CON Please select a commodity to purchase:\n87 --> back to payment\n";
- $response .= "1. WetWipes @ Ksh20\n";
- $response .= "2. Always @ Ksh30\n";
- $response .= "3. Kortex @ Ksh40\n";
- $response .= "4. SunnyGirl @ Ksh10\n";
- if($text == "87"){
- payment();
- }
- }
- elseif ($text != ""){
- switch ($text) {
- case 1:
- $price = 20;
- $N = 12;
- payment();
- confirmTrasnaction();
- checkTransactionValidity();
- header('Content-type: text/plain');
- echo $response;
- break;
- case 2:
- $price = 30;
- $N = 8;
- payment();
- confirmTrasnaction();
- checkTransactionValidity();
- header('Content-type: text/plain');
- echo $response;
- break;
- case 3:
- $price = 40;
- $N = 11;
- payment();
- confirmTrasnaction();
- checkTransactionValidity();
- header('Content-type: text/plain');
- echo $response;
- break;
- case 4:
- $price = 10;
- $N = 7;
- payment();
- confirmTrasnaction();
- checkTransactionValidity();
- header('Content-type: text/plain');
- echo $response;
- break;
- default:
- header('Content-type: text/plain');
- $response = "END Invalid Input";
- echo $response;
- exit(0);
- break;
- }
- }
- // Extract data and count from $text (user Response)
- function getResponseParameters($user_response){
- $response_params = array();
- $response_params['count'] = count(explode('*', $user_response));
- $response_params['data'] = explode('*', $user_response);
- return $response_params;
- }
- function payment(){
- // Fetch Business till Number
- $response = "CON Enter the Till Number:\n";
- if(getResponseParameters($text)['count'] == 2){
- $till_num = getResponseParameters($text)['data'][1];
- }
- // Fetch user mpesa pin
- $response = "CON Enter Mpesa pin:\n";
- if(getResponseParameters($text)['count'] == 3){
- $pin = getResponseParameters($text)['data'][2];
- }
- // Fetch the amount
- if(getResponseParameters($text)['count'] == 4){
- $response = "CON Enter the amount: \n 0 --> Items Menu\n";
- if(getResponseParameters($text)['data'][3] != 0){
- $amount = getResponseParameters($text)['data'][3];
- }else{
- //Send the user back to Item menu
- $text = "";
- }
- }
- return $response;
- }
- function confirmTrasnaction(){
- $response = "CON If you wish to cancel the transaction enter any character, ";
- $response .= "Otherwise no input means proceed with transaction:\n";
- $response .= "Ksh $price will be debited to account $till_num\n";
- sleep(3);
- if(getResponseParameters($text)['data'][4] != NULL){
- $response = "END Transaction Canceled";
- exit(0);
- }
- return $response;
- }
- function checkTransactionValidity(){
- if($amount == $price){
- // Send $N and amount payed to VMC server (127.77.110.254:5600)
- $response = "END Transaction succesfull\n";
- $response .= "Please pick the goods at the dispensing unit Thank you for transacting with YZ-Me int\n";
- }
- elseif ($amount < $price){
- // Change is not expected in Mpesa
- $response = "END Unable to proceed with transaction because of Insufficient funds\n";
- //Send the user back to Item menu
- }
- return $response;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement