?>
				<h2>Account creation error</h2>
				<p><?php 
        echo $account_creation_error;
        ?>
</p>
				<p><a href="<?php 
        echo BASE_URL;
        ?>
create-account.php">Return to account creation page</a></p>

			<?php 
    } else {
        ?>
				<?php 
        if (!does_user_have_paypal_id($_SESSION['email'])) {
            ?>
					<script>
						alert( "Email confirmation step: <?php 
            echo STORE_NAME;
            ?>
 would send a verification email which would include a link to this page." );
					</script>
				<?php 
        }
        ?>

				<h2>Account created</h2>
				<p>Thank you for creating your <?php 
        echo STORE_NAME;
        ?>
        $access_token = acquire_access_token($_GET['code']);
        if (!isset($access_token)) {
            throw new Exception('Failed to get access token');
        }
        // For use with "Log In and Checkout", when we just want the access token and not a full user account
        $_SESSION['access_token'] = $access_token;
        $profile = acquire_paypal_user_profile($access_token);
        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;
?>
        echo $_SESSION['nonce'];
        ?>
" />
							<button class="btn btn-success">Proceed to checkout</button>
						</form>
					<?php 
    } else {
        ?>
						<button class="login-trigger btn btn-success" data-toggle="modal" data-target="#myModal">Log in and checkout</button>
					<?php 
    }
    ?>
				</td>
				<td>
					<?php 
    if (!isset($_SESSION['email']) || !does_user_have_paypal_id($_SESSION['email'])) {
        ?>
						<a href="<?php 
        echo BASE_URL;
        ?>
checkout.php?paymentType=paypal&nonce=<?php 
        echo $_SESSION['nonce'];
        ?>
" class="tiny"><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" align="left" style="margin-right:7px;"></a>
					<?php 
    }
    ?>
				</td>
			</tr>
		</table>