Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\AccessToken;
- use App\Loan;
- use App\Log;
- use App\Track;
- use App\Transaction;
- use App\User;
- use Carbon\Carbon;
- use Exception;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
- class UssdController extends Controller
- {
- //
- protected $client = null;
- protected $targetUrl = 'http://localhost:9003/api/v1/ussd/';
- // protected $targetUrl = 'https://42a2e52b-a432-4b4e-9c5c-730a983990c0.mock.pstmn.io/';
- // protected $targetUrl = 'https://2e382cf6-6610-4d72-8795-c28e77a479e8.mock.pstmn.io/';
- //protected $targetUrl = 'https://f2ba5a25-3fda-4ec2-a59e-4d96b59af7f4.mock.pstmn.io/';
- protected $_msisdn = null;
- protected $_input = null;
- protected $_sessionId = null;
- protected $defaultHeader = [];
- protected $_track = null;
- public function __construct()
- {
- $this->client = new \GuzzleHttp\Client();
- $this->defaultHeader = [
- "MSISDN" => $this->_msisdn,
- "NETWORK_PROVIDER" => "1",
- "SESSION_ID" => $this->_sessionId,
- "Authorization" => ''
- ];
- }
- public function trackInit($sessionId, $msisdn) {
- $user = $this->getUser($msisdn);
- $tempTrack = Track::where('session_id', $sessionId)->first();
- if($tempTrack) {
- $this->_track = $tempTrack;
- }
- else {
- $this->_track = new Track();
- $this->_track->session_id = $sessionId;
- // $this->_track->user_id = $user->id;
- $this->_track->save();
- }
- }
- public function processRoutes(Request $request) {
- $this->_msisdn = $request->input('MSISDN');
- if($request->input('msisdn')) {
- $this->_msisdn = $request->input('msisdn');
- }
- $this->_sessionId = $request->input('sessionid');
- if($request->input('SessionId')) {
- $this->_sessionId = $request->input('SessionId');
- }
- $this->_msisdn = $this->formatMsisdn($this->_msisdn);
- $this->_input = $request->input('input');
- $this->trackInit($this->_sessionId, $this->_msisdn);
- $this->getNextInputType($request->input('input'));
- // $userTree = session('userTree');
- $userTree = $this->_track->userTree;
- $this->defaultHeader['MSISDN'] = $this->_msisdn;
- $this->defaultHeader['SESSION_ID'] = $this->_sessionId;
- switch ($userTree) {
- case '1':
- return $this->showEnterFirstName();
- break;
- case '11':
- return $this->showEnterLastName();
- break;
- case '112':
- return $this->showEnterTransactionPin();
- break;
- case '1123':
- return $this->showEnterPassword();
- break;
- case '11234':
- return $this->storePassword();
- break;
- case '2':
- return $this->showBalanceMenu();
- break;
- case '21':
- return $this->getWalletBalance();
- break;
- case '22':
- return $this->getRewardBalance();
- break;
- case '212':
- return $this->confirmPinForWalletBalance();
- break;
- case '222':
- return $this->confirmPinForRewardBalance();
- break;
- case '2123':
- return $this->confirmPinForWalletBalance();
- break;
- case '3':
- return $this->getSendMoney();
- break;
- case '31':
- return $this->confirmMSISDN();
- break;
- case '312':
- return $this->enterAmountToSend();
- break;
- case '3123':
- return $this->enterPinSendMoney();
- break;
- case '31234':
- return $this->confirmPinSendMoney();
- break;
- case '4':
- return $this->showTransactionHistory();
- break;
- case '41':
- return $this->comparePin();
- break;
- case '5':
- return $this->showLoan();
- break;
- case '51':
- return $this->selectLoanDuration();
- break;
- case '512':
- return $this->enterPinLoan();
- break;
- case '5123':
- return $this->postLoan();
- break;
- case '6' :
- return $this->getTipOfTheDay();
- break;
- case '7' :
- return $this->findAgents();
- break;
- case '71' :
- return $this->fetchAgentByCity();
- break;
- default:
- $this->clearTree();
- return $this->getMenu();
- break;
- }
- $this->clearTree();
- return $this->getMenu();
- }
- private function getMenu() {
- // session(['isNextInputFromMenuList'=> false]);
- $this->setNextInput(false);
- $this->storeLog();
- 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());
- }
- private function showEnterFirstName() {
- $this->setNextInput(true);
- $this->addToTree(1);
- // $user = new User();
- // $user->msisdn = $this->_msisdn;
- // try{
- // $user->save();
- // } catch(Exception $e) {
- // return response("You are already registered", 200)->withHeaders($this->getResponseHeader(false));
- // }
- $user = $this->getUser($this->_msisdn);
- if($user) {
- return response("You are already registered", 200)->withHeaders($this->getResponseHeader(false));
- }
- $user = new User();
- $user->msisdn = $this->_msisdn;
- $user->save();
- $this->_track->user_id = $user->id;
- $this->_track->save();
- $this->storeLog();
- return response("Enter your first name", 200)->withHeaders($this->getResponseHeader());
- }
- private function showEnterLastName() {
- $this->setNextInput(true);
- $this->addToTree(2);
- $user = User::where('msisdn', $this->_msisdn)->first();
- $user->firstname = $this->_input;
- $user->save();
- $this->storeLog();
- return response("Enter your last name", 200)->withHeaders($this->getResponseHeader());
- }
- private function showEnterTransactionPin() {
- $this->setNextInput(true);
- $this->addToTree(3);
- $user = User::where('msisdn', $this->_msisdn)->first();
- $user->lastname = $this->_input;
- $user->save();
- $this->storeLog();
- return response("Enter your pin", 200)->withHeaders($this->getResponseHeader());
- }
- private function showEnterPassword() {
- $this->setNextInput(true);
- $this->addToTree(4);
- $user = User::where('msisdn', $this->_msisdn)->first();
- $user->pin = $this->_input;
- $user->save();
- $this->storeLog();
- return response("Enter your password", 200)->withHeaders($this->getResponseHeader());
- }
- private function storePassword() {
- $this->setNextInput(true);
- $this->addToTree(5);
- $user = User::where('msisdn', $this->_msisdn)->first();
- $user->password = $this->_input;
- $user->save();
- $this->storeLog();
- $token = $this->generateToken();
- $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
- $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
- $response = $this->client->request('POST', $this->targetUrl . 'register',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- 'phone_number' => $user->msisdn,
- 'password'=> $this->_input,
- 'firstname'=> $user->firstname,
- 'lastname'=> $user->lastname,
- 'transaction_pin'=> $user->pin,
- ]
- ]);
- $response = $response->getBody()->getContents();
- //Todo:: this next line is to be executed after a successful submission
- return response("Registered successfully", 200)->withHeaders($this->getResponseHeader(false));
- }
- public function getResponseHeader($continue = true) {
- $flowValue = 'FB';
- if($continue) {
- $flowValue = 'FC';
- }
- $defaultParameters = [
- "Set-Cookie"=> "PHPSESSID=i45681p3m5s4o8ns7603jrnvg3; path=/",
- "Expires"=> -1,
- "Cache-Control"=> "max-age=0",
- "Pragma"=> "no-cache",
- "Path"=> "/ussdapp/public/index.php",
- "Charge"=> "Y",
- "Amount"=> "100",
- "Connection"=> "close",
- "Content-Type"=> "text/plain; charset=UTF-8",
- "Freeflow" => $flowValue,
- "Content-Length" => 170,
- ];
- return $defaultParameters;
- }
- private function addToTree($value) {
- $currentTree = $this->_track->userTree;
- $convertTreeToString = ''.$currentTree .''.$value;
- $this->storeLog();
- $this->_track->userTree = $convertTreeToString;
- $this->_track->save();
- }
- private function clearTree(){
- $this->_track->userTree = '';
- $this->_track->save();
- }
- private function setNextInput($value) {
- $this->_track->isNextInputFromMenuList = $value;
- $this->_track->save();
- }
- private function showBalanceMenu() {
- // $this->addToTree(1);
- // session(['isNextInputFromMenuList' => false]);
- $this->setNextInput(false);
- $this->storeLog();
- return response("Select Wallet type\n 1.User wallet balance\n 2.Reward balance \n 9. Back to menu", 200)->withHeaders($this->getResponseHeader());
- }
- private function getWalletBalance() {
- $this->addToTree(2);
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- $this->storeLog();
- return response("Enter transaction pin", 200)->withHeaders($this->getResponseHeader());
- }
- private function getRewardBalance() {
- $this->addToTree(2);
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- $this->storeLog();
- return response("Enter transaction pin for reward balance", 200)->withHeaders($this->getResponseHeader());
- // dd(session('userTree'));
- }
- private function confirmPinForWalletBalance() {
- $response = $this->postVerifyPin($this->_input);
- $this->storeLog();
- if ($response->status != 200) {
- return response($response->message->detail, 200)->withHeaders($this->getResponseHeader(false));
- }
- $fetchBalanceResult = $this->fetchRemoteData('/wallet-balance');
- // dd($fetchBalanceResult);
- return response("Your wallet balance is " .$fetchBalanceResult->response->balance, 200)->withHeaders($this->getResponseHeader(false));
- }
- private function confirmPinForRewardBalance() {
- $response = $this->postVerifyPin($this->_input);
- $this->storeLog();
- if ($response->status != 200) {
- return response($response->message->detail, 200)->withHeaders($this->getResponseHeader(false));
- }
- $fetchBalanceResult = $this->fetchRemoteData('/rewards');
- if($fetchBalanceResult->status != 200){
- return response($fetchBalanceResult->message->detail, 200)->withHeaders($this->getResponseHeader(false));
- }
- return response("Your reward balance is " .$fetchBalanceResult->response->balance, 200)->withHeaders($this->getResponseHeader(false));
- }
- private function postVerifyPin($pin) {
- $token = $this->generateToken();
- //dd($token);
- $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
- $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
- $this->defaultHeader['MSISDN'] = $this->_msisdn;
- $response = $this->client->request('POST', $this->targetUrl . 'verify-user-pin',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- "pin"=> $pin
- ]
- ]);
- $response = $response->getBody()->getContents();
- $decodedResponse = json_decode($response);
- return $decodedResponse;
- }
- private function isNewMSISDN() {
- $user = User::where('msisdn', $this->_msisdn)->first();
- // return response("Checking if the user is new", 200)->withHeaders($this->getResponseHeader());
- if($user){
- return true;
- }
- return false;
- }
- private function getNextInputType($input) {
- if(!$this->_track->isNextInputFromMenuList && $this->_track->sessionStarted) {
- $this->addToTree($input);
- }
- $this->_track->sessionStarted = true;
- $this->_track->save();
- }
- private function fetchRemoteData($appendedUrl) {
- $token = $this->generateToken();
- $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
- $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
- $this->defaultHeader['MSISDN'] = $this->_msisdn;
- $request = $this->client->get('' .$this->targetUrl . ''. $appendedUrl, [
- 'headers' => $this->defaultHeader
- ]);
- $response = $request->getBody()->getContents();
- // $response = json _decode($this->client->get('' .$this->targetUrl . ''. $appendedUrl)->getBody());
- $decodedResponse = json_decode($response);
- return $decodedResponse;
- }
- ////////////////////////////////////////
- ///////// For sending money case 2 /////
- ////////////////////////////////////////
- private function getSendMoney() {
- $this->addToTree(1);
- $this->storeLog();
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- return response("Enter recipient number
- ", 200)->withHeaders($this->getResponseHeader());
- }
- private function confirmMSISDN() {
- $user = $this->getUser($this->_msisdn);
- $this->storeLog();
- // if (!$user){
- // return response("Recipient does not exists
- // ", 200)->withHeaders($this->getResponseHeader(false));
- // }
- $transaction = new Transaction();
- $transaction->recipient_msisdn = $this->_input;
- $transaction->user_id = $user->id;
- $transaction->save();
- $this->storeLog();
- $this->_track->transaction_id = $transaction->id;
- $this->addToTree(2);
- $this->setNextInput(true);
- return response("Confirm if this recipient ". $transaction->recipient_msisdn ."\n Press any key to confirm"
- , 200)->withHeaders($this->getResponseHeader());
- }
- private function enterAmountToSend() {
- $this->addToTree(3);
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- $this->storeLog();
- return response("Enter Amount
- ", 200)->withHeaders($this->getResponseHeader());
- }
- private function enterPinSendMoney() {
- $transaction = Transaction::where('id', $this->_track->transaction_id)->first();
- // dd($this->input);
- $transaction->amount = $this->_input;
- $transaction->save();
- $this->addToTree(4);
- $this->storeLog();
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- return response("Enter pin
- ", 200)->withHeaders($this->getResponseHeader());
- }
- private function confirmPinSendMoney() {
- $transaction = Transaction::where('id', $this->_track->transaction_id)->first();
- $token = $this->generateToken();
- $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
- $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
- $this->defaultHeader['MSISDN'] = $this->_msisdn;
- $firstResponse = $this->client->request('POST', $this->targetUrl . 'send-money',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- "payee_phone_number"=> $transaction->recipient_msisdn,
- "amount" => $transaction->amount,
- "transaction_pin" => $this->_input
- ]
- ]);
- $response = json_decode($firstResponse->getBody()->getContents());
- $this->storeLog();
- if($response->status != 200) {
- return response($response->message->detail, 200)->withHeaders($this->getResponseHeader(false));
- }
- $transaction->status = "COMPLETED";
- $transaction->save();
- $this->storeLog();
- return response("You just sent ".$response->response->amount, 200)->withHeaders($this->getResponseHeader(false));
- }
- private function getUser($msisdn) {
- $user = User::where('msisdn', $msisdn)->first();
- if(!$user) {
- $response = $this->fetchRemoteData('confirm-user?phone_number='. $msisdn);
- if($response->status != 200) {
- return false;
- }
- $explodedName = explode (' ', $response->response->name);
- $user = User::create([
- 'firstname' => $explodedName[0],
- 'lastname' => $explodedName[1],
- 'msisdn' => $msisdn
- ]) ;
- return $user;
- }
- return $user;
- }
- ////////////////////////////////////////////////
- ////////// TRANSACTION HISTORY ////////////////
- //////////////////////////////////////////////
- private function showTransactionHistory() {
- $this->addToTree(1);
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- return response("Enter account pin", 200)->withHeaders($this->getResponseHeader());
- }
- private function comparePin() {
- $user = $this->getUser($this->_msisdn);
- $this->storeLog();
- $pinResponse = $this->postVerifyPin($this->_input);
- if ($pinResponse->status != 200) {
- return response($pinResponse->message->detail, 200)->withHeaders($this->getResponseHeader(false));
- }
- $response = $this->fetchRemoteData('/transactions');
- $reducedArray = array_reduce($response->response, [$this,"convertTransactionsToString"]);
- // dd($reducedArray);
- return response("Your transaction history
- " .$reducedArray, 200)->withHeaders($this->getResponseHeader(false));
- }
- private static function convertTransactionsToString($prev, $current) {
- return $prev ." " .$current->date . " " . $current->amount . " " . $current->type ."\n";
- }
- //////////////////////////////////////////////
- /////////// LOAN ///////////////////////////
- ////////////////////////////////////////////
- private function showLoan() {
- $response = $this->postLoanEligibility();
- // dd($response);
- if($response->status != 200){
- return response ($response->message->detail,
- 200)->withHeaders($this->getResponseHeader(false));
- }
- $amount = $response->response->amount;
- $this->addToTree(1);
- // session(['isNextInputFromMenuList' => true]);
- $this->setNextInput(true);
- return response ("You are eligible for N".$amount . " \n
- Enter amount you would like to apply for" ,
- 200)->withHeaders($this->getResponseHeader());
- }
- private function selectLoanDuration() {
- $response = $this->postValidateLoanAmount($this->_input);
- $loan = new Loan();
- $user = $this->getUser($this->_msisdn);
- $loan->user_id = $user->id;
- $loan->amount = $this->_input;
- $loan->save();
- // session(['loan'=> $loan->id]);
- $this->_track->loan_id = $loan->id;
- $this->addToTree(2);
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- $this->storeLog();
- return response ("Enter Loan Tenure \n 1. 4 Weeks \n 2. 6 Weeks \n 3. 12 Weeks" ,
- 200)->withHeaders($this->getResponseHeader());
- }
- private function enterPinLoan() {
- $loan = Loan::where('id', $this->_track->loan_id)->first();
- switch ($this->_input) {
- case '1':
- $loan->duration = '4';
- break;
- case '2':
- $loan->duration = '6';
- break;
- case '3':
- $loan->duration = '12';
- break;
- default:
- return response ("Not a valid input" ,
- 200)->withHeaders($this->getResponseHeader(false));
- break;
- }
- // $loan->duration = $this->_input;
- $loan->save();
- $this->addToTree(3);
- $this->storeLog();
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- return response("Enter account pin", 200)->withHeaders($this->getResponseHeader());
- }
- private function postLoan() {
- $pinResponse = $this->postVerifyPin($this->_input);
- if ($pinResponse->status != 200) {
- return response($pinResponse->message->detail, 200)->withHeaders($this->getResponseHeader(false));
- }
- $this->storeLog();
- $loan = Loan::where('id', $this->_track->loan_id)->first();
- $submitResponse = $this->client->request('POST', $this->targetUrl . 'loan-emis',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- 'amount' => $loan->amount,
- 'duration' => $loan->duration
- ]
- ]);
- $decodedResponse = json_decode($submitResponse->getBody()->getContents());
- if($decodedResponse->status != 200) {
- return response ("An error occurred",
- 200)->withHeaders($this->getResponseHeader(false));
- }
- $reducedArray = array_reduce($decodedResponse->response->data, [$this,"convertLoanResponseToString"]);
- return response("Your request was approved. Your loan information
- " .$reducedArray, 200)->withHeaders($this->getResponseHeader(false));
- }
- private static function convertLoanResponseToString($prev, $current) {
- return $prev ." " .$current->installemt . " " . $current->amount . " " . $current->due_date ."\n";
- }
- private function postLoanEligibility() {
- $token = $this->generateToken();
- $this->storeAccessToken($token->access_token, $token->refresh_token, $token->expires_in);
- $this->defaultHeader['Authorization'] = 'Bearer '.$this->_track->access_token;
- $this->defaultHeader['MSISDN'] = $this->_msisdn;
- $response = $this->client->request('GET', $this->targetUrl . 'loan-eligible-amount',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- ]
- ]);
- return $response = json_decode($response->getBody()->getContents());
- }
- private function postValidateLoanAmount($amount) {
- $response = $this->client->request('POST', $this->targetUrl . 'loan-validate-amount',
- [
- 'headers' => $this->defaultHeader,
- 'form_params'=> [
- 'amount' => $amount
- ]
- ]);
- return $response = $response->getBody()->getContents();
- }
- private function isPinCorrect($inputPin) {
- $user = $this->getUser($this->_msisdn);
- if(strcasecmp( $this->_input, $user->pin) != 0) {
- return false;
- }
- return true;
- }
- ///////////////////////////////////////////
- ///////// FINANCE LITERACY //////////////
- //////////////////////////////////////////
- private function getTipOfTheDay() {
- $response = $this->fetchRemoteData('tips');
- // $response = $this->fetchRemoteData('playroute');
- $this->storeLog();
- // $decodedResponse = json_decode($response);
- // dd(decodedResponse->responsedecodedResponse->response->balance);
- return response ($response->response->content ,
- 200)->withHeaders($this->getResponseHeader());
- }
- ///////////////////////////////////////////////
- ///////// FIND AGENTS ////////////////////////
- /////////////////////////////////////////////
- private function findAgents() {
- // session(['userTree' => '1']);
- $this->addToTree(1);
- $this->storeLog();
- // session(['isNextInputFromMenuList'=> true]);
- $this->setNextInput(true);
- return response ("Enter your location" ,
- 200)->withHeaders($this->getResponseHeader());
- }
- private function fetchAgentByCity() {
- $userInput = $this->_input;
- $this->storeLog();
- $response = $this->fetchRemoteData('agents?search='.$userInput);
- // dd($response);
- $totalCount = $response->response->total_count;
- if($totalCount == 0){
- return response ("There are no agents in your city" ,
- 200)->withHeaders($this->getResponseHeader(false));
- }
- $reducedArray = array_reduce($response->response->status, [$this, "convertAgenListsToString"]);
- return response ("Agents around are
- ".$reducedArray , 200)->withHeaders($this->getResponseHeader());
- }
- private static function convertAgenListsToString($prev, $current) {
- return $prev ." " .$current->firstname . " " . $current->lastname . " [" . $current->phone_number ."]\n";
- }
- ///////////////////////////////////////////////
- ///////////// CLEAR SESSIONS /////////////////
- /////////////////////////////////////////////
- public function clearSessions() {
- session()->forget('userTree');
- session()->forget('sessionStarted');
- session()->forget('loan');
- session()->forget('transaction');
- session()->forget('sessionId');
- }
- ////////////////////////////////////////////
- /////////// STORE LOGS ////////////////////
- //////////////////////////////////////////
- public function storeLog() {
- $user = $this->getUser($this->_msisdn);
- if(!$user) {
- return;
- }
- $log = new Log();
- $log->user_tree = $this->_track->userTree;
- $log->session_id = $this->_track->session_id;
- $log->user_id = $user->id;
- $log->save();
- return;
- }
- ////////////////////////////////////////
- /////////// Generate token ////////////
- ///////////////////////////////////////
- public function generateToken () {
- //Check if a token exists in DB
- $accessToken = AccessToken::orderBy('id', 'desc')->first();
- $today = Carbon::now();
- $ageOfToken = 0;
- if($accessToken != null){
- $ageOfToken = $today->diffInMonths($accessToken->created_at);
- }
- if($accessToken === null || ($ageOfToken >= 1)) {
- $tokenHeader = [
- 'client_id' => '4uozrkmCPEJf4Es7b29vCfd9wucw7xBuW1bkyKSy',
- 'client_secret' => '4Sye9PCXIsSY65XT2FGVrvDl7YhmMIwwW2HsjWFjRrfgLlJZHTRO2DL0OsVWQEH7tVreANNrfYpFoMQl6X5kRwnkl0UuPsnPPjLVhNkhra5TNxRYa7vBLZcmgyamzrVE',
- 'password'=> '<3nT*mEEd^fGXBR',
- 'grant_type'=> 'password',
- 'username' => 'ussd',
- "MSISDN" => $this->_msisdn,
- "NETWORK_PROVIDER" => "1",
- "SESSION_ID" => $this->_sessionId,
- "Accept: application/x-www-form-urlencoded",
- "Content-Type: application/x-www-form-urlencoded",
- "cache-control: no-cache"
- ];
- $response = $this->client->request('POST', "http://localhost:9003/api/v1/o/token/",
- [
- 'headers' => $tokenHeader,
- 'form_params'=> [
- 'client_id' => '4uozrkmCPEJf4Es7b29vCfd9wucw7xBuW1bkyKSy',
- 'client_secret' => '4Sye9PCXIsSY65XT2FGVrvDl7YhmMIwwW2HsjWFjRrfgLlJZHTRO2DL0OsVWQEH7tVreANNrfYpFoMQl6X5kRwnkl0UuPsnPPjLVhNkhra5TNxRYa7vBLZcmgyamzrVE',
- 'password'=> '<3nT*mEEd^fGXBR',
- 'grant_type'=> 'password',
- 'username' => 'ussd',
- "MSISDN" => $this->_msisdn,
- "NETWORK_PROVIDER" => "1",
- "SESSION_ID" => $this->_sessionId,
- "Accept: application/x-www-form-urlencoded",
- "Content-Type: application/x-www-form-urlencoded",
- "cache-control: no-cache"
- ]
- ]);
- $response = json_decode($response->getBody()->getContents());
- AccessToken::create([
- 'access_token' => $response->access_token,
- 'refresh_token' => $response->refresh_token,
- 'expires_in' => $response->expires_in
- ]);
- return $response;
- }
- return $accessToken;
- }
- public function storeAccessToken($accessToken, $refreshToken, $expiresIn) {
- $this->_track->access_token = $accessToken;
- $this->_track->refresh_token = $refreshToken;
- $this->_track->expires = $expiresIn;
- $this->_track->save();
- }
- public function formatMsisdn ($msisdn) {
- $lengthOfString = strlen($msisdn);
- switch ($lengthOfString) {
- case '11':
- $shortenedString = substr($msisdn, 1);
- return $this->appendCountryCode($shortenedString);
- break;
- case '13' :
- $shortenedString = substr($msisdn, 3);
- return $this->appendCountryCode($shortenedString);
- break;
- case '14':
- return $msisdn;
- break;
- default:
- return false;
- break;
- }
- }
- private function appendCountryCode ($msisdn) {
- return "+234".$msisdn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement