if (!isset($profile)) {
            throw new Exception('Failed to get user profile');
        }
        $_SESSION['username'] = $profile->given_name;
        $_SESSION['user'] = array("email" => $profile->email, "given_name" => $profile->given_name, "family_name" => $profile->family_name, "language" => $profile->language, "phone_number" => $profile->phone_number, "street_address" => $profile->address->street_address, "locality" => $profile->address->locality, "region" => $profile->address->region, "postal_code" => $profile->address->postal_code, "country" => $profile->address->country, "payer_id" => $profile->payer_id, "access_token" => $access_token);
        if (does_user_have_account($profile->email)) {
            set_user_logged_in($profile->given_name, $profile->email);
            store_access_token($profile->email, $access_token);
            if (!does_user_have_paypal_id($profile->email)) {
                $targetUrl = 'link-accounts.php?email=' . urlencode($profile->email) . '&payer_id=' . $profile->payer_id;
            }
        } else {
            $targetUrl = 'create-account.php';
        }
    } catch (Exception $e) {
        throw_error_in_console($e->getMessage());
    }
}
?>

<script>
	var endpoint = ( sessionStorage.intent ) ? "<?php 
echo BASE_URL;
?>
" + sessionStorage.intent : "<?php 
echo $targetUrl;
?>
";

	window.opener.location.href = endpoint;
/**
 * get PayPal access token
 * @param  string $code ?
 * @return string       access token
 */
function acquire_access_token($code)
{
    $accessToken = null;
    try {
        $postvals = sprintf("client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s", PP_SELLER_APP_ID, PP_SELLER_APP_SECRET, $code);
        $ch = curl_init(PPI_TOKEN_SERVICE_URL);
        $options = array(CURLOPT_POST => 1, CURLOPT_VERBOSE => 1, CURLOPT_POSTFIELDS => $postvals, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => FALSE);
        curl_setopt_array($ch, $options);
        $response = curl_exec($ch);
        $error = curl_error($ch);
        log_http_call($ch, $response, $postvals);
        curl_close($ch);
        if (!$response) {
            throw new Exception("Error retrieving access token: " . curl_error($ch));
        }
        $jsonResponse = json_decode($response);
        if (isset($jsonResponse->access_token)) {
            $accessToken = $jsonResponse->access_token;
        }
    } catch (Exception $e) {
        throw_error_in_console($e->getMessage());
    }
    return $accessToken;
}