jedtony

newUssdCont

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