SHARE
TWEET

mtncont

a guest Feb 13th, 2020 157 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.     $firstResponse = $this->client->request('GET', $this->targetUrl . 'send-money',
  596.     [
  597.         'headers' => $this->defaultHeader,
  598.         'form_params'=> [
  599.             "payee_phone_number"=> $transaction->recipient_msisdn,
  600.             "amount" => $transaction->amount,
  601.             "transaction_pin" => $this->_input
  602.         ]
  603.     ]);
  604.     $response = json_decode($firstResponse->getBody()->getContents());
  605.    
  606.     $this->storeLog();
  607.     if($response->status != 200) {
  608.       //  return response("Please enter valid transaction pin", 200)->withHeaders($this->getResponseHeader(false));
  609.       return response()->json([
  610.             "mobile_no" => $this->_msisdn,
  611.             "text" => "Please enter valid transaction pin",
  612.             "begin_end" => "1"
  613.         ]);
  614.     }
  615.  
  616.     $transaction->status = "COMPLETED";
  617.     $transaction->save();
  618.     $this->storeLog();
  619.     //return response("You just sent ".$response->response->amount, 200)->withHeaders($this->getResponseHeader(false));
  620.      return response()->json([
  621.             "mobile_no" => $this->_msisdn,
  622.             "text" => "You just sent ".$response->response->amount,
  623.             "begin_end" => "1"
  624.         ]);
  625. }
  626.  
  627.   private function getUser($msisdn) {
  628.       $user = User::where('msisdn', $msisdn)->first();
  629.       return $user;
  630.   }
  631.  
  632.  
  633.   ////////////////////////////////////////////////
  634.   ////////// TRANSACTION HISTORY ////////////////
  635.   //////////////////////////////////////////////
  636.  
  637.   private function showTransactionHistory() {
  638.     $this->addToTree(1);
  639.     // session(['isNextInputFromMenuList' => true]);
  640.     $this->setNextInput(true);
  641.     //return response("Enter account pin", 200)->withHeaders($this->getResponseHeader());
  642.      return response()->json([
  643.             "mobile_no" => $this->_msisdn,
  644.             "text" => "Enter account pin",
  645.             "begin_end" => "0"
  646.         ]);
  647.   }
  648.  
  649.   private function comparePin() {
  650.       $user = $this->getUser($this->_msisdn);
  651.      
  652.     //   if(!Hash::check($this->_input, $user->pin)) {
  653.     //     return response ("Pin is incorrect ",
  654.     //     200)->withHeaders($this->getResponseHeader());
  655.     //   }
  656.  
  657.     $this->storeLog();
  658.     $pinResponse = $this->postVerifyPin($this->_input);
  659.     if ($pinResponse->status != 200) {
  660.        // return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
  661.         return response()->json([
  662.             "mobile_no" => $this->_msisdn,
  663.             "text" => "Pin is incorrect",
  664.             "begin_end" => "1"
  665.         ]);
  666.     }
  667.    
  668.       $response = $this->fetchRemoteData('/transactions');
  669.  
  670.       $reducedArray = array_reduce($response->response, [$this,"convertTransactionsToString"]);
  671.         // dd($reducedArray);
  672.       //return response("Your transaction history" .$reducedArray, 200)->withHeaders($this->getResponseHeader(false));
  673.       return response()->json([
  674.             "mobile_no" => $this->_msisdn,
  675.             "text" => "Your transaction history\n" .$reducedArray,
  676.             "begin_end" => "1"
  677.         ]);
  678.   }
  679.  
  680.   private static function convertTransactionsToString($prev, $current) {
  681.    
  682.         return $prev ." " .$current->date . " " . $current->amount . " " . $current->type ."\n";
  683.    
  684.   }
  685.  
  686.  
  687.  
  688.  
  689.   //////////////////////////////////////////////
  690.   ///////////  LOAN ///////////////////////////
  691.   ////////////////////////////////////////////
  692.  
  693.  
  694.   private function showLoan() {
  695.    $response =  $this->postLoanEligibility();
  696.     $amount = 12345;
  697.     $this->addToTree(1);
  698.     // session(['isNextInputFromMenuList' => true]);
  699.     $this->setNextInput(true);
  700.   // return response ("You are eligible for N".$amount . " \nEnter amount you would like to apply for" ,  200)->withHeaders($this->getResponseHeader());
  701.     return response()->json([
  702.             "mobile_no" => $this->_msisdn,
  703.             "text" => "You are eligible for N".$amount . " \nEnter amount you would like to apply for",
  704.             "begin_end" => "0"
  705.         ]);
  706.      
  707.   }
  708.  
  709.   private function selectLoanDuration() {
  710.      
  711.     $response =  $this->postValidateLoanAmount($this->_input);
  712.  
  713.     $loan = new Loan();
  714.       $user = $this->getUser($this->_msisdn);
  715.       $loan->user_id = $user->id;
  716.       $loan->amount = $this->_input;
  717.       $loan->save();
  718.    
  719.     //   session(['loan'=> $loan->id]);
  720.     $this->_track->loan_id = $loan->id;
  721.  
  722.       $this->addToTree(2);
  723.     //   session(['isNextInputFromMenuList'=> true]);
  724.     $this->setNextInput(true);
  725.       $this->storeLog();
  726.     //return response ("Enter Loan Tenure \n1. 4 Weeks \n2. 6 Weeks \n3. 12 Weeks" , 200)->withHeaders($this->getResponseHeader());
  727.      return response()->json([
  728.             "mobile_no" => $this->_msisdn,
  729.             "text" => "Enter Loan Tenure \n1. 4 Weeks \n2. 6 Weeks \n3. 12 Weeks",
  730.             "begin_end" => "0"
  731.         ]);
  732.   }
  733.  
  734.   private function enterPinLoan() {
  735.     $loan = Loan::where('id', $this->_track->loan_id)->first();
  736.    
  737.     switch ($this->_input) {
  738.         case '1':
  739.             $loan->duration = '4';
  740.             break;
  741.        
  742.             case '2':
  743.                 $loan->duration = '6';
  744.                 break;
  745.                 case '3':
  746.                     $loan->duration = '12';
  747.                     break;
  748.                
  749.         default:
  750.        // return response ("Not a valid input" ,
  751.          //   200)->withHeaders($this->getResponseHeader(false));
  752.           return response()->json([
  753.             "mobile_no" => $this->_msisdn,
  754.             "text" => "Not a valid input" ,
  755.             "begin_end" => "1"
  756.         ]);
  757.             break;
  758.     }
  759.     // $loan->duration = $this->_input;
  760.     $loan->save();
  761.  
  762.     $this->addToTree(3);
  763.     $this->storeLog();
  764.     // session(['isNextInputFromMenuList'=> true]);  
  765.     $this->setNextInput(true);
  766.     //return response("Enter account pin", 200)->withHeaders($this->getResponseHeader());
  767.     return response()->json([
  768.             "mobile_no" => $this->_msisdn,
  769.             "text" => "Enter account pin" ,
  770.             "begin_end" => "0"
  771.         ]);
  772.   }
  773.  
  774.   private function postLoan() {
  775.  
  776.     $pinResponse = $this->postVerifyPin($this->_input);
  777.     if ($pinResponse->status != 200) {
  778.       //  return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
  779.       return response()->json([
  780.             "mobile_no" => $this->_msisdn,
  781.             "text" => "Pin is incorrect" ,
  782.             "begin_end" => "1"
  783.         ]);
  784.     }
  785.  
  786.     $this->storeLog();
  787.     $loan = Loan::where('id', $this->_track->loan_id)->first();
  788.     $submitResponse = $this->client->request('POST', $this->targetUrl . 'loan-emis',
  789.     [
  790.         'headers' => $this->defaultHeader,
  791.         'form_params'=> [
  792.            'amount' => $loan->amount,
  793.            'duration' => $loan->duration
  794.         ]
  795.     ]);
  796.     $decodedResponse = json_decode($submitResponse->getBody()->getContents());
  797.     if($decodedResponse->status != 200) {
  798.       //  return response ("An error occurred",
  799.         //     200)->withHeaders($this->getResponseHeader(false));
  800.         return response()->json([
  801.             "mobile_no" => $this->_msisdn,
  802.             "text" => "An error occurred",
  803.             "begin_end" => "1"
  804.         ]);
  805.     }
  806.  
  807.     $reducedArray = array_reduce($decodedResponse->response->data, [$this,"convertLoanResponseToString"]);
  808.     //return response("Your request was approved. Your loan information" .$reducedArray, 200)->withHeaders($this->getResponseHeader(false));
  809.     return response()->json([
  810.             "mobile_no" => $this->_msisdn,
  811.             "text" => "Your request was approved. Your loan information" .$reducedArray,
  812.             "begin_end" => "1"
  813.         ]);
  814.   }
  815.  
  816.   private static function convertLoanResponseToString($prev, $current) {
  817.     return $prev ." " .$current->installemt . " " . $current->amount . " " . $current->due_date ."\n";
  818.   }
  819.  
  820.   private function postLoanEligibility() {
  821.     $response = $this->client->request('POST', $this->targetUrl . 'loan-eligible-amount',
  822.     [
  823.         'headers' => $this->defaultHeader,
  824.         'form_params'=> [
  825.            
  826.         ]
  827.     ]);
  828.    return $response = $response->getBody()->getContents();
  829.    
  830.   }
  831.  
  832.   private function postValidateLoanAmount($amount) {
  833.     $response = $this->client->request('POST', $this->targetUrl . 'loan-validate-amount',
  834.     [
  835.         'headers' => $this->defaultHeader,
  836.         'form_params'=> [
  837.            'amount' => $amount
  838.         ]
  839.     ]);
  840.    return $response = $response->getBody()->getContents();
  841.    
  842.   }
  843.  
  844.   private function isPinCorrect($inputPin) {
  845.     $user = $this->getUser($this->_msisdn);
  846.      
  847.     if(!Hash::check($this->_input, $user->pin)) {
  848.       return false;
  849.     }
  850.  
  851.     return true;
  852.   }
  853.  
  854.  
  855.   ///////////////////////////////////////////
  856.   ///////// FINANCE LITERACY  //////////////
  857.   //////////////////////////////////////////
  858.  
  859.  private function getTipOfTheDay() {
  860.     $response = $this->fetchRemoteData('tips');
  861.     // $response = $this->fetchRemoteData('playroute');
  862.  
  863.     $this->storeLog();
  864.     // $decodedResponse = json_decode($response);
  865.  
  866.     // dd(decodedResponse->responsedecodedResponse->response->balance);
  867.    // return response ($response->response->content ,
  868.      //    200)->withHeaders($this->getResponseHeader());
  869.      return response()->json([
  870.             "mobile_no" => $this->_msisdn,
  871.             "text" => $response->response->content ,
  872.             "begin_end" => "1"
  873.         ]);
  874.        
  875.  }
  876.  
  877.  
  878.  ///////////////////////////////////////////////
  879.  ///////// FIND AGENTS ////////////////////////
  880.  /////////////////////////////////////////////
  881.  private function findAgents() {
  882.     // session(['userTree' => '1']);
  883.     $this->addToTree(1);
  884.     $this->storeLog();
  885.     // session(['isNextInputFromMenuList'=> true]);
  886.     $this->setNextInput(true);
  887.     //return response ("Enter your location" ,
  888.       //   200)->withHeaders($this->getResponseHeader());
  889.       return response()->json([
  890.             "mobile_no" => $this->_msisdn,
  891.             "text" => "Enter your location" ,
  892.             "begin_end" => "0"
  893.         ]);
  894.        
  895.  }
  896.  
  897.  private function fetchAgentByCity() {
  898.     $userInput = $this->_input;
  899.     $this->storeLog();
  900.     $response = $this->fetchRemoteData('agents?search='.$userInput);
  901.     // dd($response);
  902.     $totalCount = $response->response->total_count;
  903.     if($totalCount == 0){
  904.        // return response ("There are no agents in your city" ,
  905.        // 200)->withHeaders($this->getResponseHeader(false));
  906.     return response()->json([
  907.         "mobile_no" => $this->_msisdn,
  908.         "text" => "There are no agents in your city" ,
  909.         "begin_end" => "1"
  910.     ]);
  911.     }
  912.     $reducedArray = array_reduce($response->response->status, [$this, "convertAgenListsToString"]);
  913.    // return response ("Agents around are ".$reducedArray , 200)->withHeaders($this->getResponseHeader());
  914.    return response()->json([
  915.         "mobile_no" => $this->_msisdn,
  916.         "text" => "Agents around are ".$reducedArray,
  917.         "begin_end" => "1"
  918.     ]);
  919.        
  920.  }
  921.  
  922.  private static function convertAgenListsToString($prev, $current) {
  923.     return $prev ." " .$current->firstname . " " . $current->lastname . " [" . $current->phone_number ."]\n";
  924.  }
  925.  
  926.  
  927.  ///////////////////////////////////////////////
  928.  ///////////// CLEAR SESSIONS /////////////////
  929.  /////////////////////////////////////////////
  930.  
  931.  public function clearSessions()  {
  932.     session()->forget('userTree');
  933.     session()->forget('sessionStarted');
  934.     session()->forget('loan');
  935.     session()->forget('transaction');
  936.     session()->forget('sessionId');
  937.  }
  938.  
  939.  
  940.  ////////////////////////////////////////////
  941.  /////////// STORE LOGS ////////////////////
  942.  //////////////////////////////////////////
  943.  
  944.  public function storeLog() {
  945.     $user = $this->getUser($this->_msisdn);
  946.     if(!$user) {
  947.         return;
  948.     }
  949.     $log = new Log();
  950.     $log->user_tree = $this->_track->userTree;
  951.     $log->session_id = $this->_track->session_id;
  952.     $log->user_id = $user->id;
  953.     $log->save();
  954.  
  955.     return;
  956.  }
  957.  
  958.   ////////////////////////////////////////
  959.  /////////// Generate token ////////////
  960.  ///////////////////////////////////////
  961.  
  962.     public function generateToken () {
  963.  
  964.         $this->defaultHeader['MSISDN'] = $this->_msisdn;
  965.         $this->defaultHeader['SESSION_ID'] = $this->_sessionId;
  966.         // dd($this->defaultHeader);
  967.     $response = $this->client->request('POST', "http://localhost:9003/api/v1/o/token/",
  968.     [
  969.         'headers' => $this->defaultHeader,
  970.         'form_params'=> [
  971.          //'client_id'=> 'cVsER5pyGvkJ3EhULQX9Edr82gNpMSwinIfx1mpS',
  972.             'client_id' => '4uozrkmCPEJf4Es7b29vCfd9wucw7xBuW1bkyKSy',
  973.             //'client_secret' => 'pmzAvipKUwVQeBDuMdGVvbwMd8faHTkkSHLrlALk8hheoJQsX8u6h0y0Eslt2HJdtaKc8OpZde505BHsHrZiyby5dLSr1gVBDlrx04M8XE0hyIvvbWOMlQTXmrZBobMW',
  974.             'client_secret' => '4Sye9PCXIsSY65XT2FGVrvDl7YhmMIwwW2HsjWFjRrfgLlJZHTRO2DL0OsVWQEH7tVreANNrfYpFoMQl6X5kRwnkl0UuPsnPPjLVhNkhra5TNxRYa7vBLZcmgyamzrVE',
  975.             //'password'=> 'SHZ+wc^Y7b',
  976.             'password'=> '<3nT*mEEd^fGXBR',
  977.             'grant_type'=> 'password',
  978.             'username' => 'ussd',
  979.             "MSISDN" => $this->_msisdn,
  980.             "NETWORK_PROVIDER" => "1",
  981.             "SESSION_ID" => $this->_sessionId,
  982.             "Accept: application/x-www-form-urlencoded",
  983.             "Content-Type: application/x-www-form-urlencoded",
  984.             "cache-control: no-cache"      
  985.         ]
  986.     ]);
  987.  
  988.     $response = json_decode($response->getBody()->getContents());
  989.     return $response;
  990.  }
  991.    
  992.      public function storeAccessToken($accessToken, $refreshToken, $expiresIn) {
  993.      $this->_track->access_token = $accessToken;
  994.      $this->_track->refresh_token = $refreshToken;
  995.      $this->_track->expires = $expiresIn;
  996.  
  997.      $this->_track->save();
  998.  }
  999. }
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