/**
  * This function will update the refresh token by the specific user 
  */
 public function refreshAndUpdateAccessToken()
 {
     $service_bgl360 = new ServiceBgl360($this->wpdb, $this->user_id);
     $time = new Time();
     //Get specific access token
     $userCurrentAccessToken = $service_bgl360->getCurrentAccessTokenByUser($this->user_id);
     // $this->authentication_bgl360->clear();
     // print_r( $userCurrentAccessToken );
     // echo "Current logged in access token " . $userCurrentAccessToken[0]['access_token'] . '<br>';
     // echo "Current logged in refresh token " . $userCurrentAccessToken[0]['refresh_token'] . '<br>';
     // echo "Is access token expired?<br>" .  $userCurrentAccessToken[0]['id'] . '  <BR>';
     // @todo get access token by refresh token smsf database.
     // echo "Token is automatically refreshed <br>";
     // echo "refresh token " . $userCurrentAccessToken[0]['refresh_token'] . '<br>';
     // Set the refresh token
     $this->authentication_bgl360->setRefreshTokenUrl($userCurrentAccessToken[0]['refresh_token']);
     // Set the access token
     $this->authentication_bgl360->setAccessToken($userCurrentAccessToken[0]['access_token']);
     // Get the refresh token data composed by the set access and refresh tokens
     $response = $this->authentication_bgl360->getRefreshTokenData($this->authentication_bgl360->getRefreshTokenUrl());
     // print_r($response);
     // echo "This is refresh url => " . $this->authentication_bgl360->getRefreshTokenUrl() . '<br>';
     // If there is something wrong with the refresh token provided then response will include "invalid_token"
     if ($response['error'] == 'invalid_token') {
         echo "<span style='color:red' >Refresh token is already used to retrieved new access token </span><br>";
         //delete the current user's access tokens
         if ($service_bgl360->deleteAccessToken($this->user_id)) {
             echo "user id: " . $this->user_id . " <-- deleted <br>";
         }
     } else {
         echo "<span style='color:green' > New access token updated</span>";
         // Update access token and refresh token by the user in SMSF database
         $service_bgl360->updateAccessToken(array('access_token' => $response['access_token'], 'refresh_token' => $response['refresh_token'], 'expired_at' => $time->getAccessTokenExpireDateTime(), 'updated_at' => $time->getCurrentDateTime()));
     }
 }
/**
 * Instantiate classes
 */
$service_bgl360 = new ServiceBgl360($wpdb, $current_user->ID);
$resource_request_bgl360 = new ResourceRequestBgl360();
$document = new \App\Document();
//echo "<pre>";
$current_user = wp_get_current_user();
// get access status if access token is exist
$isExistAccessToken = $service_bgl360->isExistAccessToken();
//check access token status from bgl360
// if access token is expired then
/**
 * Get the users token information from smsf database
 */
$currentUser['bgl360_token'] = $service_bgl360->getCurrentAccessTokenByUser();
$queryData['access_token'] = $currentUser['bgl360_token'][0]['access_token'];
$queryData['token_type'] = $currentUser['bgl360_token'][0]['token_type'];
$queryData['refresh_token'] = $currentUser['bgl360_token'][0]['refresh_token'];
$queryData['expires_in'] = $currentUser['bgl360_token'][0]['expires_in'];
$queryData['scope'] = $currentUser['bgl360_token'][0]['scope'];
/**
 * Querry data to bgl360 application with current users logged in access token
 */
$currentUser['bgl360_query_data'] = $resource_request_bgl360->getData($queryData['access_token'], '/fund/list');
//$currentUser['bgl360_query_data'] = $resource_request_bgl360->getData($queryData['access_token'], '/fund/detal?fundId=8a009fab528515320152ae5f949d003a');
//$currentUser['bgl360_query_data'] = $resource_request_bgl360->getData($queryData['access_token'], '/fund/trustees ');
//echo print_r($currentUser['bgl360_query_data']);
$funds = $currentUser['bgl360_query_data']['funds'];
//echo 'fund list = ' . $_POST['fundList'] .' access token = '  . $queryData['access_token'] . '<br>';
/**
$basicAuthorizationHeader = 'ZjkzN2EwM2UtZGIzNy00MjEzLTlkMzctOTQ4NGU3ZWFiMzNkOjlhODhlNGJjLWFiMWMtNDFiMy1hOGIzLWE1ZjdiMzI1MDJkZQ==';
$authentication_bgl360->setRedirectUrlToAuthorizationPage("https://api.bgl360.com.au/oauth/authorize?response_type=code&client_id={$clientId}&scope=investment&redirect_uri=https://app.thesmsfacademy.com.au/wp-bgl360-authenticate.php");
$authentication_bgl360->setMainUri('https://api.bgl360.com.au/oauth/token');
$authentication_bgl360->setAuthorizationCode($authentication_bgl360->getAuthorizationCode());
$authentication_bgl360->setGrantType('authorization_code');
$authentication_bgl360->setScope('investment');
$authentication_bgl360->setClientId($clientId);
$authentication_bgl360->setClientSecret($clientSecret);
$authentication_bgl360->setRedirectUri('https://app.thesmsfacademy.com.au/wp-bgl360-authenticate.php');
$authentication_bgl360->setAccessTokenUri($authentication_bgl360->getAccessTokenUri());
//echo " auth code " . $authentication_bgl360->getAuthorizationCode() . '<br>';
//echo " access token url " . $authentication_bgl360->getAccessTokenUri();
//
if ($service_bgl360->isExistAccessToken()) {
    // Get access token from database
    $userCurrentAccessToken = $service_bgl360->getCurrentAccessTokenByUser();
    // If access token is expired then refresh user's current token
    if ($service_bgl360->isUserAccessTokenExpired($userCurrentAccessToken[0]['expires_in'])) {
        echo "<span class='red' >Access Token is expired! </span><br>";
        $authentication_bgl360->setRefreshTokenUrl($userCurrentAccessToken[0]['refresh_token']);
        $authentication_bgl360->setAccessToken($userCurrentAccessToken[0]['access_token']);
        $response = $authentication_bgl360->getRefreshTokenData();
        if ($response['error'] == 'invalid_token') {
            echo "<span class='red' >Refresh token is already used to retrieved new access token.</span> <br>";
        } else {
            echo "<span class='yellow' >Updating access token by refresh token...</span><br>";
            // Update access token, refresh token, expired_at and updated_at
            $status = $service_bgl360->updateAccessToken(array('access_token' => $response['access_token'], 'refresh_token' => $response['refresh_token'], 'expired_at' => $time->getAccessTokenExpireDateTime(), 'updated_at' => $time->getCurrentDateTime()));
            if ($status == TRUE) {
                echo "<span class='green' >Token successfully updated...</span><br>";
            } else {
$time = new Time();
/**
 * Set up authentication data
 */
$authentication_bgl360->setRedirectUrlToAuthorizationPage("https://api-staging.bgl360.com.au/oauth/authorize?response_type=code&client_id=5dbf9b2c-981f-44e4-8212-d3b5c74795a1&scope=investment&redirect_uri=https://app.thesmsfacademy.com.au/bgl360-authenticate.php");
$authentication_bgl360->setMainUri('https://api-staging.bgl360.com.au/oauth/token');
$authentication_bgl360->setAuthorizationCode($authentication_bgl360->getAuthorizationCode());
$authentication_bgl360->setGrantType('authorization_code');
$authentication_bgl360->setScope('investment');
$authentication_bgl360->setClientId('5dbf9b2c-981f-44e4-8212-d3b5c74795a1');
$authentication_bgl360->setClientSecret('b5a0ff39-ef93-4bc7-b5de-e0ace2d7a6fc');
$authentication_bgl360->setRedirectUri('https://app.thesmsfacademy.com.au/bgl360-authenticate.php');
$authentication_bgl360->setAccessTokenUri($authentication_bgl360->getAccessTokenUri());
if ($service_bgl360->isExistAccessToken()) {
    // Get access token from database
    $userCurrentAccessToken = $service_bgl360->getCurrentAccessTokenByUser();
    // If access token is expired then refresh user's current token
    if ($service_bgl360->isUserAccessTokenExpired($userCurrentAccessToken[0]['expires_in'])) {
        echo "Access Token is expired <br>";
        $authentication_bgl360->setRefreshTokenUrl($userCurrentAccessToken[0]['refresh_token']);
        $authentication_bgl360->setAccessToken($userCurrentAccessToken[0]['access_token']);
        $response = $authentication_bgl360->getRefreshTokenData();
        if ($response['error'] == 'invalid_token') {
            echo "Refresh token is already used to retrieved new access token <br>";
        } else {
            // Update access token, refresh token, expired_at and updated_at
            $service_bgl360->updateAccessToken(array('access_token' => $response['access_token'], 'refresh_token' => $response['refresh_token'], 'expired_at' => $time->getAccessTokenExpireDateTime(), 'updated_at' => $time->getCurrentDateTime()));
        }
    } else {
        // Do nothing
        echo "Access Token is not expired <br>";
/**
 * Declare local and global variables
 */
global $wpdb;
/**
 * Instantiate classes
 */
$current_user = wp_get_current_user();
$service_bgl360 = new ServiceBgl360($wpdb, $current_user->ID);
$resource_request_bgl360 = new ResourceRequestBgl360();
//echo "This is the post data";
//print_r($_REQUEST);
//echo "current user id " .  $current_user->ID . ' <br>';
$fundList = $_REQUEST['fundList'];
$accessToken = $_REQUEST['accessToken'];
$currentUser = $service_bgl360->getCurrentAccessTokenByUser();
//echo "<pre>";
//print_r($currentUser);
//echo "current token of the user " . $currentUser[0]['access_token'] . '<br>';
//echo "access token " . $currentUser[0]['bgl360_token'];
if ($_POST) {
    // echo "<pre>";
    $members = $resource_request_bgl360->getFundMembers($fundList, $currentUser[0]['access_token']);
    // print_r($members);
    echo '<content-label><label for="fundList">Choose Fund Member: </label><content-label>';
    echo '<content-dropdown><select name="pensionMemberRefKey" id="select-fund-member">';
    echo '<option value="">- Select One -</option>';
    for ($i = 0; $i < count($members); $i++) {
        $member = new \App\Member($members, $i);
        echo '<option value="' . $member->fundMemberMemberRefKey . '">' . $member->fundMemberFullName . '</option>';
    }