Advertisement
jedtony

thirdcont

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