Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. <?php
  2. /*
  3. Author: Eric Mutua
  4. */
  5.  
  6. // Reads the variables sent via POST
  7. $sessionId = $_POST["sessionId"];
  8. $serviceCode = $_POST["serviceCode"];
  9. $phoneNumber = $_POST["phoneNumber"];
  10. $text = $_POST["text"];
  11.  
  12. // Global Variables
  13. $till_num = "";
  14. $pin = "";
  15. $amount = "";
  16. $price = 0;
  17. $N = 0;
  18.  
  19. // first response which the user gets
  20. if($text == ""){
  21. $response = "CON Please select a commodity to purchase:\n87 --> back to payment\n";
  22. $response .= "1. WetWipes @ Ksh20\n";
  23. $response .= "2. Always @ Ksh30\n";
  24. $response .= "3. Kortex @ Ksh40\n";
  25. $response .= "4. SunnyGirl @ Ksh10\n";
  26. if($text == "87"){
  27. payment();
  28. }
  29. }
  30. elseif ($text != ""){
  31. switch ($text) {
  32. case 1:
  33. $price = 20;
  34. $N = 12;
  35. payment();
  36. confirmTrasnaction();
  37. checkTransactionValidity();
  38. header('Content-type: text/plain');
  39. echo $response;
  40. break;
  41.  
  42. case 2:
  43. $price = 30;
  44. $N = 8;
  45. payment();
  46. confirmTrasnaction();
  47. checkTransactionValidity();
  48. header('Content-type: text/plain');
  49. echo $response;
  50. break;
  51.  
  52. case 3:
  53. $price = 40;
  54. $N = 11;
  55. payment();
  56. confirmTrasnaction();
  57. checkTransactionValidity();
  58. header('Content-type: text/plain');
  59. echo $response;
  60. break;
  61.  
  62. case 4:
  63. $price = 10;
  64. $N = 7;
  65. payment();
  66. confirmTrasnaction();
  67. checkTransactionValidity();
  68. header('Content-type: text/plain');
  69. echo $response;
  70. break;
  71.  
  72. default:
  73. header('Content-type: text/plain');
  74. $response = "END Invalid Input";
  75. echo $response;
  76. exit(0);
  77. break;
  78. }
  79. }
  80. // Extract data and count from $text (user Response)
  81. function getResponseParameters($user_response){
  82. $response_params = array();
  83. $response_params['count'] = count(explode('*', $user_response));
  84. $response_params['data'] = explode('*', $user_response);
  85. return $response_params;
  86. }
  87.  
  88. function payment(){
  89.  
  90. // Fetch Business till Number
  91. $response = "CON Enter the Till Number:\n";
  92.  
  93. if(getResponseParameters($text)['count'] == 2){
  94. $till_num = getResponseParameters($text)['data'][1];
  95. }
  96.  
  97. // Fetch user mpesa pin
  98. $response = "CON Enter Mpesa pin:\n";
  99.  
  100. if(getResponseParameters($text)['count'] == 3){
  101. $pin = getResponseParameters($text)['data'][2];
  102.  
  103. }
  104.  
  105. // Fetch the amount
  106. if(getResponseParameters($text)['count'] == 4){
  107. $response = "CON Enter the amount: \n 0 --> Items Menu\n";
  108.  
  109. if(getResponseParameters($text)['data'][3] != 0){
  110. $amount = getResponseParameters($text)['data'][3];
  111. }else{
  112.  
  113. //Send the user back to Item menu
  114. $text = "";
  115. }
  116.  
  117. }
  118.  
  119.  
  120. return $response;
  121. }
  122.  
  123. function confirmTrasnaction(){
  124. $response = "CON If you wish to cancel the transaction enter any character, ";
  125. $response .= "Otherwise no input means proceed with transaction:\n";
  126. $response .= "Ksh $price will be debited to account $till_num\n";
  127. sleep(3);
  128. if(getResponseParameters($text)['data'][4] != NULL){
  129. $response = "END Transaction Canceled";
  130. exit(0);
  131. }
  132. return $response;
  133. }
  134.  
  135. function checkTransactionValidity(){
  136. if($amount == $price){
  137. // Send $N and amount payed to VMC server (127.77.110.254:5600)
  138. $response = "END Transaction succesfull\n";
  139. $response .= "Please pick the goods at the dispensing unit Thank you for transacting with YZ-Me int\n";
  140. }
  141. elseif ($amount < $price){
  142. // Change is not expected in Mpesa
  143. $response = "END Unable to proceed with transaction because of Insufficient funds\n";
  144. //Send the user back to Item menu
  145. }
  146. return $response;
  147. }
  148.  
  149. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement