SHARE
TWEET

fourthCont

a guest Feb 14th, 2020 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Loan;
  6. use App\Log;
  7. use App\Track;
  8. use App\Transaction;
  9. use App\User;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Hash;
  12.  
  13.  
  14. class MtnController extends Controller
  15. {
  16.     //
  17.     protected $client = null;
  18.     protected $targetUrl = 'http://localhost:9003/api/v1/ussd/';
  19.     // protected $targetUrl = 'https://2e382cf6-6610-4d72-8795-c28e77a479e8.mock.pstmn.io/';
  20.     // protected $targetUrl = 'https://f2ba5a25-3fda-4ec2-a59e-4d96b59af7f4.mock.pstmn.io/';
  21.  
  22.     protected $_msisdn = null;
  23.     protected $_input = null;
  24.     protected $_sessionId = null;
  25.     protected $defaultHeader = [];
  26.     protected $_track = null;
  27.  
  28.     public function __construct()
  29.     {
  30.         $this->client = new \GuzzleHttp\Client();
  31.         $this->defaultHeader = [
  32.             //'client_id'=> 'cVsER5pyGvkJ3EhULQX9Edr82gNpMSwinIfx1mpS',
  33.   'client_id' => '4uozrkmCPEJf4Es7b29vCfd9wucw7xBuW1bkyKSy',
  34.             //'client_secret' => 'pmzAvipKUwVQeBDuMdGVvbwMd8faHTkkSHLrlALk8hheoJQsX8u6h0y0Eslt2HJdtaKc8OpZde505BHsHrZiyby5dLSr1gVBDlrx04M8XE0hyIvvbWOMlQTXmrZBobMW',
  35.             'client_secret' => '4Sye9PCXIsSY65XT2FGVrvDl7YhmMIwwW2HsjWFjRrfgLlJZHTRO2DL0OsVWQEH7tVreANNrfYpFoMQl6X5kRwnkl0UuPsnPPjLVhNkhra5TNxRYa7vBLZcmgyamzrVE',
  36.             //'password'=> 'SHZ+wc^Y7b',
  37.             'password'=> '<3nT*mEEd^fGXBR',
  38.             'grant_type'=> 'password',
  39.             'username' => 'ussd',
  40.             "MSISDN" => '+'.$this->_msisdn,
  41.             "NETWORK_PROVIDER" => "1",
  42.             "SESSION_ID" => $this->_sessionId,
  43.             "Accept: application/x-www-form-urlencoded",
  44.             "Content-Type: application/x-www-form-urlencoded",
  45.             "cache-control: no-cache"            
  46.         ];
  47.  
  48.        
  49.     }
  50.  
  51.     public function trackInit($sessionId, $msisdn) {
  52.         $user = $this->getUser($msisdn);
  53.         $tempTrack = Track::where('session_id', $sessionId)->first();
  54.  
  55.         if($tempTrack) {
  56.             $this->_track = $tempTrack;
  57.         }
  58.         else {
  59.             $this->_track = new Track();
  60.             $this->_track->session_id = $sessionId;
  61.             // $this->_track->user_id = $user->id;
  62.             $this->_track->save();
  63.  
  64.         }
  65.     }
  66.  
  67.  
  68.  public function processRoutes(Request $request) {
  69.         //$this->_msisdn = $request->input('phone');
  70.          $this->_msisdn = $request->input('phone');
  71.         $this->_input = $request->input('text');
  72.         $this->_sessionId = $request->input('session');
  73.  
  74.         $this->trackInit($this->_sessionId, $this->_msisdn);
  75.  
  76.         $this->getNextInputType($request->input('text'));
  77.  
  78.  
  79.         $this->defaultHeader['MSISDN'] = '+'. $this->_msisdn;
  80.         $this->defaultHeader['SESSION_ID'] = $this->_sessionId;
  81.        
  82.         // $userTree = session('userTree');
  83.         $userTree = $this->_track->userTree;
  84.  
  85.         switch ($userTree) {
  86.             case '10':
  87.                 return  $this->showEnterFirstName();
  88.                 break;
  89.            
  90.             case '101':
  91.                 return  $this->showEnterLastName();
  92.                 break;
  93.             case '1012':
  94.                 return  $this->showEnterTransactionPin();
  95.                 break;
  96.            
  97.             case '10123':
  98.                 return  $this->showEnterPassword();
  99.                 break;
  100.             case '101234':
  101.                 return  $this->storePassword();
  102.                 break;
  103.            
  104.             case '1':
  105.                 return $this->showBalanceMenu();
  106.             break;
  107.  
  108.             case '11':
  109.                 return $this->getWalletBalance();
  110.             break;
  111.  
  112.             case '12':
  113.                 return $this->getRewardBalance();
  114.             break;
  115.  
  116.             case '112':
  117.                 return $this->confirmPinForWalletBalance();
  118.             break;
  119.  
  120.             case '122':
  121.                 return $this->confirmPinForRewardBalance();
  122.             break;
  123.  
  124.             case '1123':
  125.                 return $this->confirmPinForWalletBalance();
  126.             break;
  127.  
  128.             case '2':
  129.                 return $this->getSendMoney();
  130.             break;
  131.  
  132.             case '21':
  133.                 return $this->confirmMSISDN();
  134.             break;
  135.  
  136.             case '212':
  137.                 return $this->enterAmountToSend();
  138.             break;
  139.  
  140.             case '2123':
  141.                 return $this->enterPinSendMoney();
  142.             break;
  143.  
  144.             case '21234':
  145.                 return $this->confirmPinSendMoney();
  146.             break;
  147.  
  148.             case '3':
  149.                 return $this->showTransactionHistory();
  150.             break;
  151.  
  152.             case '31':
  153.                 return $this->comparePin();
  154.             break;
  155.  
  156.             case '4':
  157.                 return $this->showLoan();
  158.             break;
  159.  
  160.             case '41':
  161.                 return $this->selectLoanDuration();
  162.             break;
  163.  
  164.             case '412':
  165.                 return $this->enterPinLoan();
  166.             break;
  167.  
  168.             case '4123':
  169.                 return $this->postLoan();
  170.             break;
  171.  
  172.             case '5' :
  173.                 return $this->getTipOfTheDay();
  174.             break;
  175.  
  176.             case '6' :
  177.                 return $this->findAgents();
  178.             break;
  179.  
  180.             case '61' :
  181.                 return $this->fetchAgentByCity();
  182.             break;
  183.             default:
  184.               return $this->getMenu();
  185.                 break;
  186.         }
  187.      
  188.  
  189.     }
  190.  
  191.     private function getMenu() {
  192.         if(!$this->isNewMSISDN()) {
  193.             // session(['userTree' => '10']);
  194.             // session(['isNextInputFromMenuList'=> true]);
  195.             $this->_track->userTree = '10';
  196.             $this->_track->isNextInputFromMenuList = true;
  197.             $this->_track->save();
  198.             $this->storeLog();
  199.             return response()->json([
  200.                 "mobile_no" => $this->_msisdn,
  201.                 "text" => "Register to Tanadi. Press 1 to continue",
  202.                 "begin_end" => "0"
  203.             ]);
  204.        
  205.         }
  206.         else {
  207.  
  208.             // session(['isNextInputFromMenuList'=> false]);
  209.             $this->setNextInput(false);
  210.             $this->storeLog();
  211.         //     return response("Welcome to Tanadi\n1.Check Balance\n2.Send Money\n3.Transaction history\n4.Loan\n5.Finance Literacy\n6.Find agents
  212.         // ", 200)->withHeaders($this->getResponseHeader());
  213.         return response()->json([
  214.             "mobile_no" => $this->_msisdn,
  215.             "text" => "Welcome to Tanadi\n1.Check Balance\n2.Send Money\n3.Transaction history\n4.Loan\n5.Finance Literacy\n6.Find agents",
  216.             "begin_end" => "0"
  217.         ]);
  218.         }
  219.     }
  220.  
  221.     private function showEnterFirstName() {
  222.         $this->addToTree(1);
  223.         $user = new User();
  224.         $user->msisdn = $this->_msisdn;
  225.         $user->save();
  226.         $this->_track->user_id = $user->id;
  227.         $this->_track->save();
  228.         $this->storeLog();
  229.        // return response("Enter your first name", 200)->withHeaders($this->getResponseHeader());
  230.        return response()->json([
  231.             "mobile_no" => $this->_msisdn,
  232.             "text" => "Enter your first name",
  233.             "begin_end" => "0"
  234.         ]);
  235.     }
  236.  
  237.     private function showEnterLastName() {
  238.         $this->addToTree(2);
  239.         $user = User::where('msisdn',  $this->_msisdn)->first();
  240.         $user->firstname = $this->_input;
  241.         $user->save();
  242.         $this->storeLog();
  243.         //return response("Enter your last name", 200)->withHeaders($this->getResponseHeader());
  244.                return response()->json([
  245.             "mobile_no" => $this->_msisdn,
  246.             "text" => "Enter your last name",
  247.             "begin_end" => "0"
  248.         ]);
  249.     }
  250.  
  251.     private function showEnterTransactionPin() {
  252.         $this->addToTree(3);
  253.         $user = User::where('msisdn', $this->_msisdn)->first();
  254.         $user->lastname = $this->_input;
  255.         $user->save();
  256.         $this->storeLog();
  257.         // return response("Enter your pin", 200)->withHeaders($this->getResponseHeader());
  258.            return response()->json([
  259.             "mobile_no" => $this->_msisdn,
  260.             "text" => "Enter your pin",
  261.             "begin_end" => "0"
  262.         ]);
  263.     }
  264.    
  265.     private function showEnterPassword() {
  266.         $this->addToTree(4);
  267.         $user = User::where('msisdn', $this->_msisdn)->first();
  268.         $user->pin =  $this->_input;
  269.         $user->save();
  270.         $this->storeLog();
  271.         //return response("Enter your password", 200)->withHeaders($this->getResponseHeader());
  272.          return response()->json([
  273.             "mobile_no" => $this->_msisdn,
  274.             "text" => "Enter your password",
  275.             "begin_end" => "0"
  276.         ]);
  277.     }
  278.  
  279.     private function storePassword() {
  280.        // $this->addToTree(5);
  281.         $user = User::where('msisdn', $this->_msisdn)->first();
  282.         $user->password = $this->_input;
  283.         $user->save();
  284.         $this->storeLog();
  285.        
  286.         $token = $this->generateToken();
  287.         //dd($token);
  288.         //dd($user->pin);
  289.         $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
  290.         $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
  291.         $response = $this->client->request('POST', $this->targetUrl . 'register',
  292.         [
  293.             'headers' => $this->defaultHeader,
  294.             'form_params'=> [
  295.                 'phone_number' => "+".$user->msisdn,
  296.                 'password'=> $this->_input,
  297.                 'firstname'=> $user->firstname,
  298.                 'lastname'=> $user->lastname,
  299.                 'transaction_pin'=> $user->pin,
  300.             ]
  301.         ]);
  302.  
  303.         $response = $response->getBody()->getContents();
  304.         //dd($response);
  305.         //Todo:: this next line is to be executed after a successful submission
  306.      
  307.         //return response("Registered successfully", 200)->withHeaders($this->getResponseHeader(false));
  308.          return response()->json([
  309.             "mobile_no" => $this->_msisdn,
  310.             "text" => "Registered successfully",
  311.             "begin_end" => "1"
  312.         ]);
  313.     }
  314.    
  315.  
  316.  
  317.     public function getResponseHeader($continue = true) {
  318.  
  319.         $defaultParameters =  [
  320.             "Set-Cookie"=> "PHPSESSID=i45681p3m5s4o8ns7603jrnvg3; path=/",
  321.             "Expires"=> -1,
  322.             "Cache-Control"=> "max-age=0",
  323.             "Pragma"=> "no-cache",
  324.             "Path"=> "/Airtelussd/fcmb/index.php",
  325.             "Charge"=> "Y",
  326.             "Amount"=> "10",
  327.             "Connection"=> "close",
  328.             "Content-Type"=> "application/json"
  329.         ];
  330.         if($continue) {
  331.             $flowArray = [ "Freeflow"=> "FC"];
  332.             return array_merge($defaultParameters, $flowArray);
  333.         }
  334.         $flowArray = [ "Freeflow"=> "FB"];
  335.         $this->clearSessions();
  336.         return array_merge($defaultParameters, $flowArray);
  337.     }
  338.  
  339.     private function addToTree($value) {
  340.        
  341.         $currentTree = $this->_track->userTree;
  342.         $convertTreeToString = ''.$currentTree .''.$value;
  343.         $this->storeLog();
  344.        
  345.         $this->_track->userTree = $convertTreeToString;
  346.         $this->_track->save();
  347.     }
  348.  
  349.     private function setNextInput($value) {
  350.         $this->_track->isNextInputFromMenuList = $value;
  351.         $this->_track->save();
  352.     }
  353.  
  354.     private function showBalanceMenu() {
  355.         // $this->addToTree(1);
  356.         // session(['isNextInputFromMenuList' => false]);
  357.         $this->setNextInput(false);
  358.         $this->storeLog();
  359.        // return response("Select Wallet type\n1.User wallet balance\n2.Reward balance", 200)->withHeaders($this->getResponseHeader());
  360.        return response()->json([
  361.             "mobile_no" => $this->_msisdn,
  362.             "text" => "Select Wallet type\n1.User wallet balance\n2.Reward balance",
  363.             "begin_end" => "0"
  364.         ]);
  365.        
  366.     }
  367.  
  368.     private function getWalletBalance() {
  369.         $this->addToTree(2);
  370.         // session(['isNextInputFromMenuList'=> true]);
  371.         $this->setNextInput(true);
  372.         $this->storeLog();
  373.         //return response("Enter transaction pin", 200)->withHeaders($this->getResponseHeader());
  374.          return response()->json([
  375.             "mobile_no" => $this->_msisdn,
  376.             "text" => "Enter transaction pin",
  377.             "begin_end" => "0"
  378.         ]);
  379.         // dd(session('userTree'));
  380.     }
  381.  
  382.  
  383.     private function getRewardBalance() {
  384.         $this->addToTree(2);
  385.         // session(['isNextInputFromMenuList'=> true]);
  386.         $this->setNextInput(true);
  387.         $this->storeLog();  
  388.        // return response("Enter transaction pin for reward balance", 200)->withHeaders($this->getResponseHeader());
  389.          return response()->json([
  390.             "mobile_no" => $this->_msisdn,
  391.             "text" => "Enter transaction pin for reward balance",
  392.             "begin_end" => "0"
  393.         ]);
  394.         // dd(session('userTree'));
  395.     }
  396.  
  397.     private function confirmPinForWalletBalance() {
  398.  
  399.         $response = $this->postVerifyPin($this->_input);
  400.         // dd($response);
  401.         $this->storeLog();
  402.         if ($response->status != 200) {
  403.            // return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
  404.            return response()->json([
  405.             "mobile_no" => $this->_msisdn,
  406.             "text" => "Pin is incorrect",
  407.             "begin_end" => "1"
  408.         ]);
  409.         }
  410.         $fetchBalanceResult = $this->fetchRemoteData('/wallet-balance');
  411.  
  412.         // dd($fetchBalanceResult);
  413.         //return response("Your wallet balance is " .$fetchBalanceResult->response->balance, 200)->withHeaders($this->getResponseHeader(false));
  414.          return response()->json([
  415.             "mobile_no" => $this->_msisdn,
  416.             "text" => "Your wallet balance is " .$fetchBalanceResult->response->balance,
  417.             "begin_end" => "1"
  418.         ]);
  419.     }
  420.  
  421.     private function confirmPinForRewardBalance() {
  422.  
  423.         $response = $this->postVerifyPin($this->_input);
  424.         $this->storeLog();
  425.         if ($response->status != 200) {
  426.            // return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
  427.            return response()->json([
  428.             "mobile_no" => $this->_msisdn,
  429.             "text" => "Pin is incorrect",
  430.             "begin_end" => "1"
  431.         ]);
  432.         }
  433.         $fetchBalanceResult = $this->fetchRemoteData('/rewards');
  434.         //dd($fetchBalanceResult);
  435.         if($fetchBalanceResult->status != 200){
  436.              return response()->json([
  437.             "mobile_no" => $this->_msisdn,
  438.             "text" => "".$fetchBalanceResult->message->detail,
  439.             "begin_end" => "1"
  440.         ]);
  441.         }
  442.         //return response("Your reward balance is " .$fetchBalanceResult->response->balance, 200)->withHeaders($this->getResponseHeader(false));
  443.            return response()->json([
  444.             "mobile_no" => $this->_msisdn,
  445.             "text" => "Your reward balance is " .$fetchBalanceResult->response->balance,
  446.             "begin_end" => "1"
  447.         ]);
  448.     }
  449.  
  450.  
  451.     private function postVerifyPin($pin) {
  452.        //dd($this->defaultHeader);
  453.        $token = $this->generateToken();
  454.        //dd($token);
  455.        $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
  456.        $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
  457.        $this->defaultHeader['MSISDN'] = '+'.$this->_msisdn;
  458.        //dd($pin);
  459.         $response = $this->client->request('POST', $this->targetUrl . 'verify-user-pin',
  460.         [
  461.             'headers' => $this->defaultHeader,
  462.             'form_params'=> [
  463.                 "pin"=> $pin
  464.             ]
  465.         ]);
  466.        $response = $response->getBody()->getContents();
  467.  
  468.        $decodedResponse = json_decode($response);
  469.        //dd($decodedResponse);
  470.         return $decodedResponse;
  471.     }
  472.  
  473.     private function isNewMSISDN() {
  474.      $user = User::where('msisdn', $this->_msisdn)->first();
  475.      
  476.      if($user){
  477.          return true;
  478.      }
  479.      return false;
  480.     }
  481.  
  482.     private function getNextInputType($input) {
  483.        
  484.         if(!$this->_track->isNextInputFromMenuList && $this->_track->sessionStarted) {
  485.             $this->addToTree($input);
  486.         }
  487.  
  488.         // if(!session('sessionId')){
  489.         //     session(['sessionId' => rand(0, 100000)]);
  490.         // }
  491.         // session(['sessionStarted'=> true]);
  492.  
  493.         $this->_track->sessionStarted = true;
  494.         $this->_track->save();
  495.     }
  496.  
  497.     private function fetchRemoteData($appendedUrl) {
  498.         $request = $this->client->get('' .$this->targetUrl . ''. $appendedUrl, [
  499.             'headers' => $this->defaultHeader,
  500.            
  501.         ]);
  502.         $response = $request->getBody()->getContents();
  503.         // $response = json _decode($this->client->get('' .$this->targetUrl . ''. $appendedUrl)->getBody());
  504.           $decodedResponse = json_decode($response);
  505.         return $decodedResponse;
  506.     }
  507.  
  508.  
  509.     ////////////////////////////////////////
  510.     ///////// For sending money case 2 /////
  511.   ////////////////////////////////////////
  512.  
  513.   private function getSendMoney() {
  514.     $this->addToTree(1);
  515.     $this->storeLog();
  516.     // session(['isNextInputFromMenuList' => true]);
  517.     $this->setNextInput(true);
  518.     //return response("Enter recipient number", 200)->withHeaders($this->getResponseHeader());
  519.       return response()->json([
  520.             "mobile_no" => $this->_msisdn,
  521.             "text" => "Enter recipient number",
  522.             "begin_end" => "0"
  523.         ]);
  524.   }
  525.  
  526.   private function confirmMSISDN() {
  527.    
  528.       $user = $this->getUser($this->_input);
  529.       $this->storeLog();
  530.       if (!$user){
  531.         //return response("Recipient does not exists", 200)->withHeaders($this->getResponseHeader(false));
  532.          return response()->json([
  533.             "mobile_no" => $this->_msisdn,
  534.             "text" => "Recipient does not exists",
  535.             "begin_end" => "1"
  536.         ]);
  537.       }
  538.       $transaction = new Transaction();
  539.       $transaction->recipient_msisdn = $this->_input;
  540.       $transaction->user_id = $user->id;
  541.       $transaction->save();
  542.  
  543.       $this->storeLog();
  544.     //   session(['transaction' => $transaction->id]);
  545.     $this->_track->transaction_id = $transaction->id;
  546.         $this->addToTree(2);
  547.     //   session(['isNextInputFromMenuList' => true]);
  548.     $this->setNextInput(true);
  549.      // return response("Confirm if this recipient ". $transaction->recipient_msisdn ."\n Press any key to confirm", 200)->withHeaders($this->getResponseHeader());
  550.       return response()->json([
  551.             "mobile_no" => $this->_msisdn,
  552.             "text" => "Confirm if this recipient ". $transaction->recipient_msisdn ."\n Press any key to confirm",
  553.             "begin_end" => "0"
  554.         ]);
  555.   }
  556.  
  557.  
  558.   private function enterAmountToSend() {
  559.        $this->addToTree(3);
  560.     //   session(['isNextInputFromMenuList' => true]);
  561.     $this->setNextInput(true);
  562.       $this->storeLog();
  563.       //return response("Enter Amount", 200)->withHeaders($this->getResponseHeader());
  564.       return response()->json([
  565.             "mobile_no" => $this->_msisdn,
  566.             "text" => "Enter Amount",
  567.             "begin_end" => "0"
  568.         ]);
  569.   }
  570.  
  571.  
  572.   private function enterPinSendMoney() {
  573.     $transaction = Transaction::where('id', $this->_track->transaction_id)->first();
  574.    
  575.     // dd($this->input);
  576.     $transaction->amount = $this->_input;
  577.     $transaction->save();
  578.  
  579.      $this->addToTree(4);
  580.      $this->storeLog();
  581.     // session(['isNextInputFromMenuList' => true]);
  582.     $this->setNextInput(true);
  583.     //return response("Enter pin", 200)->withHeaders($this->getResponseHeader());
  584.     return response()->json([
  585.             "mobile_no" => $this->_msisdn,
  586.             "text" => "Enter pin",
  587.             "begin_end" => "0"
  588.         ]);
  589. }
  590.  
  591.  
  592. private function confirmPinSendMoney() {
  593.     $transaction = Transaction::where('id', $this->_track->transaction_id)->first();
  594.  
  595.     $token = $this->generateToken();
  596.     //dd($token);
  597.     //dd($user->pin);
  598.     $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
  599.     $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
  600.    $this->defaultHeader['MSISDN'] = '+' . $this->_msisdn;
  601.             $firstResponse = $this->client->request('POST', $this->targetUrl . 'send-money',
  602.     [
  603.         'headers' => $this->defaultHeader,
  604.         'form_params'=> [
  605.             "payee_phone_number"=> '+'.$transaction->recipient_msisdn,
  606.             "amount" => $transaction->amount,
  607.             "transaction_pin" => $this->_input
  608.         ]
  609.     ]);
  610.     $response = json_decode($firstResponse->getBody()->getContents());
  611.     //dd($response);
  612.     $this->storeLog();
  613.     if($response->status != 200) {
  614.       //  return response("Please enter valid transaction pin", 200)->withHeaders($this->getResponseHeader(false));
  615.       return response()->json([
  616.             "mobile_no" => $this->_msisdn,
  617.             "text" => "Please enter valid transaction pin",
  618.             "begin_end" => "1"
  619.         ]);
  620.     }
  621.  
  622.     $transaction->status = "COMPLETED";
  623.     $transaction->save();
  624.     $this->storeLog();
  625.     //return response("You just sent ".$response->response->amount, 200)->withHeaders($this->getResponseHeader(false));
  626.      return response()->json([
  627.             "mobile_no" => $this->_msisdn,
  628.             "text" => "You just sent ".$response->response->amount,
  629.             "begin_end" => "1"
  630.         ]);
  631. }
  632.  
  633.   private function getUser($msisdn) {
  634.       $user = User::where('msisdn', $msisdn)->first();
  635.       return $user;
  636.   }
  637.  
  638.  
  639.   ////////////////////////////////////////////////
  640.   ////////// TRANSACTION HISTORY ////////////////
  641.   //////////////////////////////////////////////
  642.  
  643.   private function showTransactionHistory() {
  644.     $this->addToTree(1);
  645.     // session(['isNextInputFromMenuList' => true]);
  646.     $this->setNextInput(true);
  647.     //return response("Enter account pin", 200)->withHeaders($this->getResponseHeader());
  648.      return response()->json([
  649.             "mobile_no" => $this->_msisdn,
  650.             "text" => "Enter account pin",
  651.             "begin_end" => "0"
  652.         ]);
  653.   }
  654.  
  655.   private function comparePin() {
  656.       $user = $this->getUser($this->_msisdn);
  657.      
  658.     //   if(!Hash::check($this->_input, $user->pin)) {
  659.     //     return response ("Pin is incorrect ",
  660.     //     200)->withHeaders($this->getResponseHeader());
  661.     //   }
  662.  
  663.     $this->storeLog();
  664.     $pinResponse = $this->postVerifyPin($this->_input);
  665.     if ($pinResponse->status != 200) {
  666.        // return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
  667.         return response()->json([
  668.             "mobile_no" => $this->_msisdn,
  669.             "text" => "Pin is incorrect",
  670.             "begin_end" => "1"
  671.         ]);
  672.     }
  673.    
  674.       $response = $this->fetchRemoteData('/transactions');
  675.  
  676.       $reducedArray = array_reduce($response->response, [$this,"convertTransactionsToString"]);
  677.         // dd($reducedArray);
  678.       //return response("Your transaction history" .$reducedArray, 200)->withHeaders($this->getResponseHeader(false));
  679.       return response()->json([
  680.             "mobile_no" => $this->_msisdn,
  681.             "text" => "Your transaction history\n" .$reducedArray,
  682.             "begin_end" => "1"
  683.         ]);
  684.   }
  685.  
  686.   private static function convertTransactionsToString($prev, $current) {
  687.    
  688.         return $prev ." " .$current->date . " " . $current->amount . " " . $current->type ."\n";
  689.    
  690.   }
  691.  
  692.  
  693.  
  694.  
  695.   //////////////////////////////////////////////
  696.   ///////////  LOAN ///////////////////////////
  697.   ////////////////////////////////////////////
  698.  
  699.  
  700.   private function showLoan() {
  701.    $response =  $this->postLoanEligibility();
  702.     $amount = 12345;
  703.     $this->addToTree(1);
  704.     // session(['isNextInputFromMenuList' => true]);
  705.     $this->setNextInput(true);
  706.   // return response ("You are eligible for N".$amount . " \nEnter amount you would like to apply for" ,  200)->withHeaders($this->getResponseHeader());
  707.     return response()->json([
  708.             "mobile_no" => $this->_msisdn,
  709.             "text" => "You are eligible for N".$amount . " \nEnter amount you would like to apply for",
  710.             "begin_end" => "0"
  711.         ]);
  712.      
  713.   }
  714.  
  715.   private function selectLoanDuration() {
  716.      
  717.     $response =  $this->postValidateLoanAmount($this->_input);
  718.  
  719.     $loan = new Loan();
  720.       $user = $this->getUser($this->_msisdn);
  721.       $loan->user_id = $user->id;
  722.       $loan->amount = $this->_input;
  723.       $loan->save();
  724.    
  725.     //   session(['loan'=> $loan->id]);
  726.     $this->_track->loan_id = $loan->id;
  727.  
  728.       $this->addToTree(2);
  729.     //   session(['isNextInputFromMenuList'=> true]);
  730.     $this->setNextInput(true);
  731.       $this->storeLog();
  732.     //return response ("Enter Loan Tenure \n1. 4 Weeks \n2. 6 Weeks \n3. 12 Weeks" , 200)->withHeaders($this->getResponseHeader());
  733.      return response()->json([
  734.             "mobile_no" => $this->_msisdn,
  735.             "text" => "Enter Loan Tenure \n1. 4 Weeks \n2. 6 Weeks \n3. 12 Weeks",
  736.             "begin_end" => "0"
  737.         ]);
  738.   }
  739.  
  740.   private function enterPinLoan() {
  741.     $loan = Loan::where('id', $this->_track->loan_id)->first();
  742.    
  743.     switch ($this->_input) {
  744.         case '1':
  745.             $loan->duration = '4';
  746.             break;
  747.        
  748.             case '2':
  749.                 $loan->duration = '6';
  750.                 break;
  751.                 case '3':
  752.                     $loan->duration = '12';
  753.                     break;
  754.                
  755.         default:
  756.        // return response ("Not a valid input" ,
  757.          //   200)->withHeaders($this->getResponseHeader(false));
  758.           return response()->json([
  759.             "mobile_no" => $this->_msisdn,
  760.             "text" => "Not a valid input" ,
  761.             "begin_end" => "1"
  762.         ]);
  763.             break;
  764.     }
  765.     // $loan->duration = $this->_input;
  766.     $loan->save();
  767.  
  768.     $this->addToTree(3);
  769.     $this->storeLog();
  770.     // session(['isNextInputFromMenuList'=> true]);  
  771.     $this->setNextInput(true);
  772.     //return response("Enter account pin", 200)->withHeaders($this->getResponseHeader());
  773.     return response()->json([
  774.             "mobile_no" => $this->_msisdn,
  775.             "text" => "Enter account pin" ,
  776.             "begin_end" => "0"
  777.         ]);
  778.   }
  779.  
  780.   private function postLoan() {
  781.  
  782.     $pinResponse = $this->postVerifyPin($this->_input);
  783.     if ($pinResponse->status != 200) {
  784.       //  return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
  785.       return response()->json([
  786.             "mobile_no" => $this->_msisdn,
  787.             "text" => "Pin is incorrect" ,
  788.             "begin_end" => "1"
  789.         ]);
  790.     }
  791.  
  792.     $this->storeLog();
  793.     $loan = Loan::where('id', $this->_track->loan_id)->first();
  794.     $submitResponse = $this->client->request('POST', $this->targetUrl . 'loan-emis',
  795.     [
  796.         'headers' => $this->defaultHeader,
  797.         'form_params'=> [
  798.            'amount' => $loan->amount,
  799.            'duration' => $loan->duration
  800.         ]
  801.     ]);
  802.     $decodedResponse = json_decode($submitResponse->getBody()->getContents());
  803.     if($decodedResponse->status != 200) {
  804.       //  return response ("An error occurred",
  805.         //     200)->withHeaders($this->getResponseHeader(false));
  806.         return response()->json([
  807.             "mobile_no" => $this->_msisdn,
  808.             "text" => "An error occurred",
  809.             "begin_end" => "1"
  810.         ]);
  811.     }
  812.  
  813.     $reducedArray = array_reduce($decodedResponse->response->data, [$this,"convertLoanResponseToString"]);
  814.     //return response("Your request was approved. Your loan information" .$reducedArray, 200)->withHeaders($this->getResponseHeader(false));
  815.     return response()->json([
  816.             "mobile_no" => $this->_msisdn,
  817.             "text" => "Your request was approved. Your loan information" .$reducedArray,
  818.             "begin_end" => "1"
  819.         ]);
  820.   }
  821.  
  822.   private static function convertLoanResponseToString($prev, $current) {
  823.     return $prev ." " .$current->installemt . " " . $current->amount . " " . $current->due_date ."\n";
  824.   }
  825.  
  826.   private function postLoanEligibility() {
  827.     $response = $this->client->request('POST', $this->targetUrl . 'loan-eligible-amount',
  828.     [
  829.         'headers' => $this->defaultHeader,
  830.         'form_params'=> [
  831.            
  832.         ]
  833.     ]);
  834.    return $response = $response->getBody()->getContents();
  835.    
  836.   }
  837.  
  838.   private function postValidateLoanAmount($amount) {
  839.     $response = $this->client->request('POST', $this->targetUrl . 'loan-validate-amount',
  840.     [
  841.         'headers' => $this->defaultHeader,
  842.         'form_params'=> [
  843.            'amount' => $amount
  844.         ]
  845.     ]);
  846.    return $response = $response->getBody()->getContents();
  847.    
  848.   }
  849.  
  850.   private function isPinCorrect($inputPin) {
  851.     $user = $this->getUser($this->_msisdn);
  852.      
  853.     if(!Hash::check($this->_input, $user->pin)) {
  854.       return false;
  855.     }
  856.  
  857.     return true;
  858.   }
  859.  
  860.  
  861.   ///////////////////////////////////////////
  862.   ///////// FINANCE LITERACY  //////////////
  863.   //////////////////////////////////////////
  864.  
  865.  private function getTipOfTheDay() {
  866.     $response = $this->fetchRemoteData('tips');
  867.     // $response = $this->fetchRemoteData('playroute');
  868.  
  869.     $this->storeLog();
  870.     // $decodedResponse = json_decode($response);
  871.  
  872.     // dd(decodedResponse->responsedecodedResponse->response->balance);
  873.    // return response ($response->response->content ,
  874.      //    200)->withHeaders($this->getResponseHeader());
  875.      return response()->json([
  876.             "mobile_no" => $this->_msisdn,
  877.             "text" => $response->response->content ,
  878.             "begin_end" => "1"
  879.         ]);
  880.        
  881.  }
  882.  
  883.  
  884.  ///////////////////////////////////////////////
  885.  ///////// FIND AGENTS ////////////////////////
  886.  /////////////////////////////////////////////
  887.  private function findAgents() {
  888.     // session(['userTree' => '1']);
  889.     $this->addToTree(1);
  890.     $this->storeLog();
  891.     // session(['isNextInputFromMenuList'=> true]);
  892.     $this->setNextInput(true);
  893.     //return response ("Enter your location" ,
  894.       //   200)->withHeaders($this->getResponseHeader());
  895.       return response()->json([
  896.             "mobile_no" => $this->_msisdn,
  897.             "text" => "Enter your location" ,
  898.             "begin_end" => "0"
  899.         ]);
  900.        
  901.  }
  902.  
  903.  private function fetchAgentByCity() {
  904.     $userInput = $this->_input;
  905.     $this->storeLog();
  906.     $response = $this->fetchRemoteData('agents?search='.$userInput);
  907.     // dd($response);
  908.     $totalCount = $response->response->total_count;
  909.     if($totalCount == 0){
  910.        // return response ("There are no agents in your city" ,
  911.        // 200)->withHeaders($this->getResponseHeader(false));
  912.     return response()->json([
  913.         "mobile_no" => $this->_msisdn,
  914.         "text" => "There are no agents in your city" ,
  915.         "begin_end" => "1"
  916.     ]);
  917.     }
  918.     $reducedArray = array_reduce($response->response->status, [$this, "convertAgenListsToString"]);
  919.    // return response ("Agents around are ".$reducedArray , 200)->withHeaders($this->getResponseHeader());
  920.    return response()->json([
  921.         "mobile_no" => $this->_msisdn,
  922.         "text" => "Agents around are ".$reducedArray,
  923.         "begin_end" => "1"
  924.     ]);
  925.        
  926.  }
  927.  
  928.  private static function convertAgenListsToString($prev, $current) {
  929.     return $prev ." " .$current->firstname . " " . $current->lastname . " [" . $current->phone_number ."]\n";
  930.  }
  931.  
  932.  
  933.  ///////////////////////////////////////////////
  934.  ///////////// CLEAR SESSIONS /////////////////
  935.  /////////////////////////////////////////////
  936.  
  937.  public function clearSessions()  {
  938.     session()->forget('userTree');
  939.     session()->forget('sessionStarted');
  940.     session()->forget('loan');
  941.     session()->forget('transaction');
  942.     session()->forget('sessionId');
  943.  }
  944.  
  945.  
  946.  ////////////////////////////////////////////
  947.  /////////// STORE LOGS ////////////////////
  948.  //////////////////////////////////////////
  949.  
  950.  public function storeLog() {
  951.     $user = $this->getUser($this->_msisdn);
  952.     if(!$user) {
  953.         return;
  954.     }
  955.     $log = new Log();
  956.     $log->user_tree = $this->_track->userTree;
  957.     $log->session_id = $this->_track->session_id;
  958.     $log->user_id = $user->id;
  959.     $log->save();
  960.  
  961.     return;
  962.  }
  963.  
  964.   ////////////////////////////////////////
  965.  /////////// Generate token ////////////
  966.  ///////////////////////////////////////
  967.  
  968.     public function generateToken () {
  969.  
  970.         $this->defaultHeader['MSISDN'] = $this->_msisdn;
  971.         $this->defaultHeader['SESSION_ID'] = $this->_sessionId;
  972.         // dd($this->defaultHeader);
  973.     $response = $this->client->request('POST', "http://localhost:9003/api/v1/o/token/",
  974.     [
  975.         'headers' => $this->defaultHeader,
  976.         'form_params'=> [
  977.          //'client_id'=> 'cVsER5pyGvkJ3EhULQX9Edr82gNpMSwinIfx1mpS',
  978.             'client_id' => '4uozrkmCPEJf4Es7b29vCfd9wucw7xBuW1bkyKSy',
  979.             //'client_secret' => 'pmzAvipKUwVQeBDuMdGVvbwMd8faHTkkSHLrlALk8hheoJQsX8u6h0y0Eslt2HJdtaKc8OpZde505BHsHrZiyby5dLSr1gVBDlrx04M8XE0hyIvvbWOMlQTXmrZBobMW',
  980.             'client_secret' => '4Sye9PCXIsSY65XT2FGVrvDl7YhmMIwwW2HsjWFjRrfgLlJZHTRO2DL0OsVWQEH7tVreANNrfYpFoMQl6X5kRwnkl0UuPsnPPjLVhNkhra5TNxRYa7vBLZcmgyamzrVE',
  981.             //'password'=> 'SHZ+wc^Y7b',
  982.             'password'=> '<3nT*mEEd^fGXBR',
  983.             'grant_type'=> 'password',
  984.             'username' => 'ussd',
  985.             "MSISDN" => $this->_msisdn,
  986.             "NETWORK_PROVIDER" => "1",
  987.             "SESSION_ID" => $this->_sessionId,
  988.             "Accept: application/x-www-form-urlencoded",
  989.             "Content-Type: application/x-www-form-urlencoded",
  990.             "cache-control: no-cache"      
  991.         ]
  992.     ]);
  993.  
  994.     $response = json_decode($response->getBody()->getContents());
  995.     return $response;
  996.  }
  997.    
  998.      public function storeAccessToken($accessToken, $refreshToken, $expiresIn) {
  999.      $this->_track->access_token = $accessToken;
  1000.      $this->_track->refresh_token = $refreshToken;
  1001.      $this->_track->expires = $expiresIn;
  1002.  
  1003.      $this->_track->save();
  1004.  }
  1005. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top