Advertisement
jedtony

ussdcont

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