<?php
require_once('Quickbooks.class.php');

$client_id = 'Q0yOeLTME3ebJQrDcmgAWFqnkwZpuiQVoF6js77iUeW41sdMcj';
$client_secret = '11cZVDfZlycl4zDAtfOfBwLEKi0TaImOdaGtQVa7';
$authorizationRequestUrl = 'https://appcenter.intuit.com/connect/oauth2';
$scope = 'com.intuit.quickbooks.accounting';
$response_type = 'code';
$redirect_uri = 'https://routely.net/quickbooks/authorize.php';
$state = 'security_token';
$grant_type= 'authorization_code';
$certFilePath = 'Certificate/cacert.pem';
$certFilePathOpenID = 'Certificate/cacert.pem';
$tokenEndPointUrl = 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer';

$client = new Quickbooks($client_id, $client_secret, $certFilePath);

if (!isset($_GET['code'])) {

    $authUrl = $client->getAuthorizationURL($authorizationRequestUrl, $scope, $redirect_uri, $response_type, $state);
    header('Location: ' . $authUrl);
    exit();
	
} else {
	
	$code = $_GET['code'];
	$result = $client->getAccessToken($tokenEndPointUrl, $code, $redirect_uri, $grant_type);
	
?>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
	var s = window.opener.angular.element('#form').scope();
	s.vm.setting.access_token = "<?php echo $result['access_token']; ?>";
	s.vm.setting.refresh_token = "<?php echo $result['refresh_token']; ?>";
	s.vm.setting.realm_id = "<?php echo $_GET['realmId']; ?>";
	s.vm._save(s.vm.setting);
	s.$apply();
	window.close();
</script>	
<?php
}
?>