Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Loan;
- use App\Log;
- use App\Track;
- use App\Transaction;
- use App\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
- class MtnController extends Controller
- {
- //
- protected $client = null;
- protected $targetUrl = 'http://localhost:9003/api/v1/ussd/';
- // protected $targetUrl = 'https://2e382cf6-6610-4d72-8795-c28e77a479e8.mock.pstmn.io/';
- // protected $targetUrl = 'https://f2ba5a25-3fda-4ec2-a59e-4d96b59af7f4.mock.pstmn.io/';
- protected $_msisdn = null;
- protected $_input = null;
- protected $_sessionId = null;
- protected $defaultHeader = [];
- protected $_track = null;
- public function __construct()
- {
- $this->client = new \GuzzleHttp\Client();
- $this->defaultHeader = [
- //'client_id'=> 'cVsER5pyGvkJ3EhULQX9Edr82gNpMSwinIfx1mpS',
- 'client_id' => '4uozrkmCPEJf4Es7b29vCfd9wucw7xBuW1bkyKSy',
- //'client_secret' => 'pmzAvipKUwVQeBDuMdGVvbwMd8faHTkkSHLrlALk8hheoJQsX8u6h0y0Eslt2HJdtaKc8OpZde505BHsHrZiyby5dLSr1gVBDlrx04M8XE0hyIvvbWOMlQTXmrZBobMW',
- 'client_secret' => '4Sye9PCXIsSY65XT2FGVrvDl7YhmMIwwW2HsjWFjRrfgLlJZHTRO2DL0OsVWQEH7tVreANNrfYpFoMQl6X5kRwnkl0UuPsnPPjLVhNkhra5TNxRYa7vBLZcmgyamzrVE',
- //'password'=> 'SHZ+wc^Y7b',
- 'password'=> '<3nT*mEEd^fGXBR',
- 'grant_type'=> 'password',
- 'username' => 'ussd',
- "MSISDN" => '+'.$this->_msisdn,
- "NETWORK_PROVIDER" => "1",
- "SESSION_ID" => $this->_sessionId,
- "Accept: application/x-www-form-urlencoded",
- "Content-Type: application/x-www-form-urlencoded",
- "cache-control: no-cache"
- ];
- }
- public function trackInit($sessionId, $msisdn) {
- $user = $this->getUser($msisdn);
- $tempTrack = Track::where('session_id', $sessionId)->first();
- if($tempTrack) {
- $this->_track = $tempTrack;
- }
- else {
- $this->_track = new Track();
- $this->_track->session_id = $sessionId;
- // $this->_track->user_id = $user->id;
- $this->_track->save();
- }
- }
- public function processRoutes(Request $request) {
- //$this->_msisdn = $request->input('phone');
- $this->_msisdn = $request->input('phone');
- $this->_input = $request->input('text');
- $this->_sessionId = $request->input('session');
- $this->trackInit($this->_sessionId, $this->_msisdn);
- $this->getNextInputType($request->input('text'));
- $this->defaultHeader['MSISDN'] = '+'. $this->_msisdn;
- $this->defaultHeader['SESSION_ID'] = $this->_sessionId;
- // $userTree = session('userTree');
- $userTree = $this->_track->userTree;
- switch ($userTree) {
- case '10':
- return $this->showEnterFirstName();
- break;
- case '101':
- return $this->showEnterLastName();
- break;
- case '1012':
- return $this->showEnterTransactionPin();
- break;
- case '10123':
- return $this->showEnterPassword();
- break;
- case '101234':
- return $this->storePassword();
- break;
- case '1':
- return $this->showBalanceMenu();
- break;
- case '11':
- return $this->getWalletBalance();
- break;
- case '12':
- return $this->getRewardBalance();
- break;
- case '112':
- return $this->confirmPinForWalletBalance();
- break;
- case '122':
- return $this->confirmPinForRewardBalance();
- break;
- case '1123':
- return $this->confirmPinForWalletBalance();
- break;
- case '2':
- return $this->getSendMoney();
- break;
- case '21':
- return $this->confirmMSISDN();
- break;
- case '212':
- return $this->enterAmountToSend();
- break;
- case '2123':
- return $this->enterPinSendMoney();
- break;
- case '21234':
- return $this->confirmPinSendMoney();
- break;
- case '3':
- return $this->showTransactionHistory();
- break;
- case '31':
- return $this->comparePin();
- break;
- case '4':
- return $this->showLoan();
- break;
- case '41':
- return $this->selectLoanDuration();
- break;
- case '412':
- return $this->enterPinLoan();
- break;
- case '4123':
- return $this->postLoan();
- break;
- case '5' :
- return $this->getTipOfTheDay();
- break;
- case '6' :
- return $this->findAgents();
- break;
- case '61' :
- return $this->fetchAgentByCity();
- break;
- default:
- return $this->getMenu();
- break;
- }
- }
- private function getMenu() {
- if(!$this->isNewMSISDN()) {
- // session(['userTree' => '10']);
- // session(['isNextInputFromMenuList'=> true]);
- $this->_track->userTree = '10';
- $this->_track->isNextInputFromMenuList = true;
- $this->_track->save();
- $this->storeLog();
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Register to Tanadi. Press 1 to continue",
- "begin_end" => "0"
- ]);
- }
- else {
- // session(['isNextInputFromMenuList'=> false]);
- $this->setNextInput(false);
- $this->storeLog();
- // return response("Welcome to Tanadi\n1.Check Balance\n2.Send Money\n3.Transaction history\n4.Loan\n5.Finance Literacy\n6.Find agents
- // ", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Welcome to Tanadi\n1.Check Balance\n2.Send Money\n3.Transaction history\n4.Loan\n5.Finance Literacy\n6.Find agents",
- "begin_end" => "0"
- ]);
- }
- }
- private function showEnterFirstName() {
- $this->addToTree(1);
- $user = new User();
- $user->msisdn = $this->_msisdn;
- $user->save();
- $this->_track->user_id = $user->id;
- $this->_track->save();
- $this->storeLog();
- // return response("Enter your first name", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter your first name",
- "begin_end" => "0"
- ]);
- }
- private function showEnterLastName() {
- $this->addToTree(2);
- $user = User::where('msisdn', $this->_msisdn)->first();
- $user->firstname = $this->_input;
- $user->save();
- $this->storeLog();
- //return response("Enter your last name", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter your last name",
- "begin_end" => "0"
- ]);
- }
- private function showEnterTransactionPin() {
- $this->addToTree(3);
- $user = User::where('msisdn', $this->_msisdn)->first();
- $user->lastname = $this->_input;
- $user->save();
- $this->storeLog();
- // return response("Enter your pin", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter your pin",
- "begin_end" => "0"
- ]);
- }
- private function showEnterPassword() {
- $this->addToTree(4);
- $user = User::where('msisdn', $this->_msisdn)->first();
- $user->pin = $this->_input;
- $user->save();
- $this->storeLog();
- //return response("Enter your password", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter your password",
- "begin_end" => "0"
- ]);
- }
- private function storePassword() {
- // $this->addToTree(5);
- $user = User::where('msisdn', $this->_msisdn)->first();
- $user->password = $this->_input;
- $user->save();
- $this->storeLog();
- $token = $this->generateToken();
- //dd($token);
- //dd($user->pin);
- $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
- $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
- $response = $this->client->request('POST', $this->targetUrl . 'register',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- 'phone_number' => "+".$user->msisdn,
- 'password'=> $this->_input,
- 'firstname'=> $user->firstname,
- 'lastname'=> $user->lastname,
- 'transaction_pin'=> $user->pin,
- ]
- ]);
- $response = $response->getBody()->getContents();
- //dd($response);
- //Todo:: this next line is to be executed after a successful submission
- //return response("Registered successfully", 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Registered successfully",
- "begin_end" => "1"
- ]);
- }
- public function getResponseHeader($continue = true) {
- $defaultParameters = [
- "Set-Cookie"=> "PHPSESSID=i45681p3m5s4o8ns7603jrnvg3; path=/",
- "Expires"=> -1,
- "Cache-Control"=> "max-age=0",
- "Pragma"=> "no-cache",
- "Path"=> "/Airtelussd/fcmb/index.php",
- "Charge"=> "Y",
- "Amount"=> "10",
- "Connection"=> "close",
- "Content-Type"=> "application/json"
- ];
- if($continue) {
- $flowArray = [ "Freeflow"=> "FC"];
- return array_merge($defaultParameters, $flowArray);
- }
- $flowArray = [ "Freeflow"=> "FB"];
- $this->clearSessions();
- return array_merge($defaultParameters, $flowArray);
- }
- private function addToTree($value) {
- $currentTree = $this->_track->userTree;
- $convertTreeToString = ''.$currentTree .''.$value;
- $this->storeLog();
- $this->_track->userTree = $convertTreeToString;
- $this->_track->save();
- }
- private function setNextInput($value) {
- $this->_track->isNextInputFromMenuList = $value;
- $this->_track->save();
- }
- private function showBalanceMenu() {
- // $this->addToTree(1);
- // session(['isNextInputFromMenuList' => false]);
- $this->setNextInput(false);
- $this->storeLog();
- // return response("Select Wallet type\n1.User wallet balance\n2.Reward balance", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Select Wallet type\n1.User wallet balance\n2.Reward balance",
- "begin_end" => "0"
- ]);
- }
- private function getWalletBalance() {
- $this->addToTree(2);
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- $this->storeLog();
- //return response("Enter transaction pin", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter transaction pin",
- "begin_end" => "0"
- ]);
- // dd(session('userTree'));
- }
- private function getRewardBalance() {
- $this->addToTree(2);
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- $this->storeLog();
- // return response("Enter transaction pin for reward balance", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter transaction pin for reward balance",
- "begin_end" => "0"
- ]);
- // dd(session('userTree'));
- }
- private function confirmPinForWalletBalance() {
- $response = $this->postVerifyPin($this->_input);
- // dd($response);
- $this->storeLog();
- if ($response->status != 200) {
- // return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Pin is incorrect",
- "begin_end" => "1"
- ]);
- }
- $fetchBalanceResult = $this->fetchRemoteData('/wallet-balance');
- // dd($fetchBalanceResult);
- //return response("Your wallet balance is " .$fetchBalanceResult->response->balance, 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Your wallet balance is " .$fetchBalanceResult->response->balance,
- "begin_end" => "1"
- ]);
- }
- private function confirmPinForRewardBalance() {
- $response = $this->postVerifyPin($this->_input);
- $this->storeLog();
- if ($response->status != 200) {
- // return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Pin is incorrect",
- "begin_end" => "1"
- ]);
- }
- $fetchBalanceResult = $this->fetchRemoteData('/rewards');
- //dd($fetchBalanceResult);
- if($fetchBalanceResult->status != 200){
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "".$fetchBalanceResult->message->detail,
- "begin_end" => "1"
- ]);
- }
- //return response("Your reward balance is " .$fetchBalanceResult->response->balance, 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Your reward balance is " .$fetchBalanceResult->response->balance,
- "begin_end" => "1"
- ]);
- }
- private function postVerifyPin($pin) {
- //dd($this->defaultHeader);
- $token = $this->generateToken();
- //dd($token);
- $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
- $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
- $this->defaultHeader['MSISDN'] = '+'.$this->_msisdn;
- //dd($pin);
- $response = $this->client->request('POST', $this->targetUrl . 'verify-user-pin',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- "pin"=> $pin
- ]
- ]);
- $response = $response->getBody()->getContents();
- $decodedResponse = json_decode($response);
- //dd($decodedResponse);
- return $decodedResponse;
- }
- private function isNewMSISDN() {
- $user = User::where('msisdn', $this->_msisdn)->first();
- if($user){
- return true;
- }
- return false;
- }
- private function getNextInputType($input) {
- if(!$this->_track->isNextInputFromMenuList && $this->_track->sessionStarted) {
- $this->addToTree($input);
- }
- // if(!session('sessionId')){
- // session(['sessionId' => rand(0, 100000)]);
- // }
- // session(['sessionStarted'=> true]);
- $this->_track->sessionStarted = true;
- $this->_track->save();
- }
- private function fetchRemoteData($appendedUrl) {
- $request = $this->client->get('' .$this->targetUrl . ''. $appendedUrl, [
- 'headers' => $this->defaultHeader,
- ]);
- $response = $request->getBody()->getContents();
- // $response = json _decode($this->client->get('' .$this->targetUrl . ''. $appendedUrl)->getBody());
- $decodedResponse = json_decode($response);
- return $decodedResponse;
- }
- ////////////////////////////////////////
- ///////// For sending money case 2 /////
- ////////////////////////////////////////
- private function getSendMoney() {
- $this->addToTree(1);
- $this->storeLog();
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- //return response("Enter recipient number", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter recipient number",
- "begin_end" => "0"
- ]);
- }
- private function confirmMSISDN() {
- $user = $this->getUser($this->_input);
- $this->storeLog();
- if (!$user){
- //return response("Recipient does not exists", 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Recipient does not exists",
- "begin_end" => "1"
- ]);
- }
- $transaction = new Transaction();
- $transaction->recipient_msisdn = $this->_input;
- $transaction->user_id = $user->id;
- $transaction->save();
- $this->storeLog();
- // session(['transaction' => $transaction->id]);
- $this->_track->transaction_id = $transaction->id;
- $this->addToTree(2);
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- // return response("Confirm if this recipient ". $transaction->recipient_msisdn ."\n Press any key to confirm", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Confirm if this recipient ". $transaction->recipient_msisdn ."\n Press any key to confirm",
- "begin_end" => "0"
- ]);
- }
- private function enterAmountToSend() {
- $this->addToTree(3);
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- $this->storeLog();
- //return response("Enter Amount", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter Amount",
- "begin_end" => "0"
- ]);
- }
- private function enterPinSendMoney() {
- $transaction = Transaction::where('id', $this->_track->transaction_id)->first();
- // dd($this->input);
- $transaction->amount = $this->_input;
- $transaction->save();
- $this->addToTree(4);
- $this->storeLog();
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- //return response("Enter pin", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter pin",
- "begin_end" => "0"
- ]);
- }
- private function confirmPinSendMoney() {
- $transaction = Transaction::where('id', $this->_track->transaction_id)->first();
- $firstResponse = $this->client->request('GET', $this->targetUrl . 'send-money',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- "payee_phone_number"=> $transaction->recipient_msisdn,
- "amount" => $transaction->amount,
- "transaction_pin" => $this->_input
- ]
- ]);
- $response = json_decode($firstResponse->getBody()->getContents());
- $this->storeLog();
- if($response->status != 200) {
- // return response("Please enter valid transaction pin", 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Please enter valid transaction pin",
- "begin_end" => "1"
- ]);
- }
- $transaction->status = "COMPLETED";
- $transaction->save();
- $this->storeLog();
- //return response("You just sent ".$response->response->amount, 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "You just sent ".$response->response->amount,
- "begin_end" => "1"
- ]);
- }
- private function getUser($msisdn) {
- $user = User::where('msisdn', $msisdn)->first();
- return $user;
- }
- ////////////////////////////////////////////////
- ////////// TRANSACTION HISTORY ////////////////
- //////////////////////////////////////////////
- private function showTransactionHistory() {
- $this->addToTree(1);
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- //return response("Enter account pin", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter account pin",
- "begin_end" => "0"
- ]);
- }
- private function comparePin() {
- $user = $this->getUser($this->_msisdn);
- // if(!Hash::check($this->_input, $user->pin)) {
- // return response ("Pin is incorrect ",
- // 200)->withHeaders($this->getResponseHeader());
- // }
- $this->storeLog();
- $pinResponse = $this->postVerifyPin($this->_input);
- if ($pinResponse->status != 200) {
- // return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Pin is incorrect",
- "begin_end" => "1"
- ]);
- }
- $response = $this->fetchRemoteData('/transactions');
- $reducedArray = array_reduce($response->response, [$this,"convertTransactionsToString"]);
- // dd($reducedArray);
- //return response("Your transaction history" .$reducedArray, 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Your transaction history\n" .$reducedArray,
- "begin_end" => "1"
- ]);
- }
- private static function convertTransactionsToString($prev, $current) {
- return $prev ." " .$current->date . " " . $current->amount . " " . $current->type ."\n";
- }
- //////////////////////////////////////////////
- /////////// LOAN ///////////////////////////
- ////////////////////////////////////////////
- private function showLoan() {
- $response = $this->postLoanEligibility();
- $amount = 12345;
- $this->addToTree(1);
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- // return response ("You are eligible for N".$amount . " \nEnter amount you would like to apply for" , 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "You are eligible for N".$amount . " \nEnter amount you would like to apply for",
- "begin_end" => "0"
- ]);
- }
- private function selectLoanDuration() {
- $response = $this->postValidateLoanAmount($this->_input);
- $loan = new Loan();
- $user = $this->getUser($this->_msisdn);
- $loan->user_id = $user->id;
- $loan->amount = $this->_input;
- $loan->save();
- // session(['loan'=> $loan->id]);
- $this->_track->loan_id = $loan->id;
- $this->addToTree(2);
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- $this->storeLog();
- //return response ("Enter Loan Tenure \n1. 4 Weeks \n2. 6 Weeks \n3. 12 Weeks" , 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter Loan Tenure \n1. 4 Weeks \n2. 6 Weeks \n3. 12 Weeks",
- "begin_end" => "0"
- ]);
- }
- private function enterPinLoan() {
- $loan = Loan::where('id', $this->_track->loan_id)->first();
- switch ($this->_input) {
- case '1':
- $loan->duration = '4';
- break;
- case '2':
- $loan->duration = '6';
- break;
- case '3':
- $loan->duration = '12';
- break;
- default:
- // return response ("Not a valid input" ,
- // 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Not a valid input" ,
- "begin_end" => "1"
- ]);
- break;
- }
- // $loan->duration = $this->_input;
- $loan->save();
- $this->addToTree(3);
- $this->storeLog();
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- //return response("Enter account pin", 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter account pin" ,
- "begin_end" => "0"
- ]);
- }
- private function postLoan() {
- $pinResponse = $this->postVerifyPin($this->_input);
- if ($pinResponse->status != 200) {
- // return response("Pin is incorrect", 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Pin is incorrect" ,
- "begin_end" => "1"
- ]);
- }
- $this->storeLog();
- $loan = Loan::where('id', $this->_track->loan_id)->first();
- $submitResponse = $this->client->request('POST', $this->targetUrl . 'loan-emis',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- 'amount' => $loan->amount,
- 'duration' => $loan->duration
- ]
- ]);
- $decodedResponse = json_decode($submitResponse->getBody()->getContents());
- if($decodedResponse->status != 200) {
- // return response ("An error occurred",
- // 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "An error occurred",
- "begin_end" => "1"
- ]);
- }
- $reducedArray = array_reduce($decodedResponse->response->data, [$this,"convertLoanResponseToString"]);
- //return response("Your request was approved. Your loan information" .$reducedArray, 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Your request was approved. Your loan information" .$reducedArray,
- "begin_end" => "1"
- ]);
- }
- private static function convertLoanResponseToString($prev, $current) {
- return $prev ." " .$current->installemt . " " . $current->amount . " " . $current->due_date ."\n";
- }
- private function postLoanEligibility() {
- $response = $this->client->request('POST', $this->targetUrl . 'loan-eligible-amount',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- ]
- ]);
- return $response = $response->getBody()->getContents();
- }
- private function postValidateLoanAmount($amount) {
- $response = $this->client->request('POST', $this->targetUrl . 'loan-validate-amount',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- 'amount' => $amount
- ]
- ]);
- return $response = $response->getBody()->getContents();
- }
- private function isPinCorrect($inputPin) {
- $user = $this->getUser($this->_msisdn);
- if(!Hash::check($this->_input, $user->pin)) {
- return false;
- }
- return true;
- }
- ///////////////////////////////////////////
- ///////// FINANCE LITERACY //////////////
- //////////////////////////////////////////
- private function getTipOfTheDay() {
- $response = $this->fetchRemoteData('tips');
- // $response = $this->fetchRemoteData('playroute');
- $this->storeLog();
- // $decodedResponse = json_decode($response);
- // dd(decodedResponse->responsedecodedResponse->response->balance);
- // return response ($response->response->content ,
- // 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => $response->response->content ,
- "begin_end" => "1"
- ]);
- }
- ///////////////////////////////////////////////
- ///////// FIND AGENTS ////////////////////////
- /////////////////////////////////////////////
- private function findAgents() {
- // session(['userTree' => '1']);
- $this->addToTree(1);
- $this->storeLog();
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- //return response ("Enter your location" ,
- // 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Enter your location" ,
- "begin_end" => "0"
- ]);
- }
- private function fetchAgentByCity() {
- $userInput = $this->_input;
- $this->storeLog();
- $response = $this->fetchRemoteData('agents?search='.$userInput);
- // dd($response);
- $totalCount = $response->response->total_count;
- if($totalCount == 0){
- // return response ("There are no agents in your city" ,
- // 200)->withHeaders($this->getResponseHeader(false));
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "There are no agents in your city" ,
- "begin_end" => "1"
- ]);
- }
- $reducedArray = array_reduce($response->response->status, [$this, "convertAgenListsToString"]);
- // return response ("Agents around are ".$reducedArray , 200)->withHeaders($this->getResponseHeader());
- return response()->json([
- "mobile_no" => $this->_msisdn,
- "text" => "Agents around are ".$reducedArray,
- "begin_end" => "1"
- ]);
- }
- private static function convertAgenListsToString($prev, $current) {
- return $prev ." " .$current->firstname . " " . $current->lastname . " [" . $current->phone_number ."]\n";
- }
- ///////////////////////////////////////////////
- ///////////// CLEAR SESSIONS /////////////////
- /////////////////////////////////////////////
- public function clearSessions() {
- session()->forget('userTree');
- session()->forget('sessionStarted');
- session()->forget('loan');
- session()->forget('transaction');
- session()->forget('sessionId');
- }
- ////////////////////////////////////////////
- /////////// STORE LOGS ////////////////////
- //////////////////////////////////////////
- public function storeLog() {
- $user = $this->getUser($this->_msisdn);
- if(!$user) {
- return;
- }
- $log = new Log();
- $log->user_tree = $this->_track->userTree;
- $log->session_id = $this->_track->session_id;
- $log->user_id = $user->id;
- $log->save();
- return;
- }
- ////////////////////////////////////////
- /////////// Generate token ////////////
- ///////////////////////////////////////
- public function generateToken () {
- $this->defaultHeader['MSISDN'] = $this->_msisdn;
- $this->defaultHeader['SESSION_ID'] = $this->_sessionId;
- // dd($this->defaultHeader);
- $response = $this->client->request('POST', "http://localhost:9003/api/v1/o/token/",
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- //'client_id'=> 'cVsER5pyGvkJ3EhULQX9Edr82gNpMSwinIfx1mpS',
- 'client_id' => '4uozrkmCPEJf4Es7b29vCfd9wucw7xBuW1bkyKSy',
- //'client_secret' => 'pmzAvipKUwVQeBDuMdGVvbwMd8faHTkkSHLrlALk8hheoJQsX8u6h0y0Eslt2HJdtaKc8OpZde505BHsHrZiyby5dLSr1gVBDlrx04M8XE0hyIvvbWOMlQTXmrZBobMW',
- 'client_secret' => '4Sye9PCXIsSY65XT2FGVrvDl7YhmMIwwW2HsjWFjRrfgLlJZHTRO2DL0OsVWQEH7tVreANNrfYpFoMQl6X5kRwnkl0UuPsnPPjLVhNkhra5TNxRYa7vBLZcmgyamzrVE',
- //'password'=> 'SHZ+wc^Y7b',
- 'password'=> '<3nT*mEEd^fGXBR',
- 'grant_type'=> 'password',
- 'username' => 'ussd',
- "MSISDN" => $this->_msisdn,
- "NETWORK_PROVIDER" => "1",
- "SESSION_ID" => $this->_sessionId,
- "Accept: application/x-www-form-urlencoded",
- "Content-Type: application/x-www-form-urlencoded",
- "cache-control: no-cache"
- ]
- ]);
- $response = json_decode($response->getBody()->getContents());
- return $response;
- }
- public function storeAccessToken($accessToken, $refreshToken, $expiresIn) {
- $this->_track->access_token = $accessToken;
- $this->_track->refresh_token = $refreshToken;
- $this->_track->expires = $expiresIn;
- $this->_track->save();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement