jedtony

mtncont

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