forked from XeroAPI/XeroOAuth-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic.php
127 lines (97 loc) · 4.17 KB
/
public.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
require 'lib/XeroOAuth.php';
/**
* Define for file includes
*/
define('BASE_PATH', '.');
/**
* Define which app type you are using:
* Private - private app method
* Public - standard public app method
* Public - partner app method
*/
define("XRO_APP_TYPE", "Public");
/**
* Set a user agent string that matches your application name as set in the Xero developer centre
*/
$useragent = "Xero-OAuth-PHP Public";
/**
* Set your callback url or set 'oob' if none required
*/
define("OAUTH_CALLBACK", 'http://localhost/XeroOAuth-PHP/public.php');
/**
* Application specific settings
* Not all are required for given application types
* consumer_key: required for all applications
* consumer_secret: for partner applications, set to: s (cannot be blank)
* rsa_private_key: application certificate private key - not needed for public applications
* rsa_public_key: application certificate public cert - not needed for public applications
*/
include 'tests/testRunner.php';
$signatures = array(
'consumer_key' => 'YOURCONSUMERKEY',
'shared_secret' => 'YOURSECRET',
// API versions
'core_version' => '2.0',
'payroll_version' => '1.0'
);
if (XRO_APP_TYPE == "Private" || XRO_APP_TYPE == "Partner") {
$signatures['rsa_private_key'] = BASE_PATH . '/certs/privatekey.pem';
$signatures['rsa_public_key'] = BASE_PATH . '/certs/publickey.cer';
}
if (XRO_APP_TYPE == "Partner") {
$signatures['curl_ssl_cert'] = BASE_PATH . '/certs/entrust-cert-RQ3.pem';
$signatures['curl_ssl_password'] = '1234';
$signatures['curl_ssl_key'] = BASE_PATH . '/certs/entrust-private-RQ3.pem';
}
$XeroOAuth = new XeroOAuth(array_merge(array(
'application_type' => XRO_APP_TYPE,
'oauth_callback' => OAUTH_CALLBACK,
'user_agent' => $useragent
), $signatures));
$initialCheck = $XeroOAuth->diagnostics();
$checkErrors = count($initialCheck);
if ($checkErrors > 0) {
// you could handle any config errors here, or keep on truckin if you like to live dangerously
foreach ($initialCheck as $check) {
echo 'Error: ' . $check . PHP_EOL;
}
} else {
$here = XeroOAuth::php_self();
session_start();
$oauthSession = retrieveSession();
include 'tests/tests.php';
if (isset($_REQUEST['oauth_verifier'])) {
$XeroOAuth->config['access_token'] = $_SESSION['oauth']['oauth_token'];
$XeroOAuth->config['access_token_secret'] = $_SESSION['oauth']['oauth_token_secret'];
$code = $XeroOAuth->request('GET', $XeroOAuth->url('AccessToken', ''), array(
'oauth_verifier' => $_REQUEST['oauth_verifier'],
'oauth_token' => $_REQUEST['oauth_token']
));
if ($XeroOAuth->response['code'] == 200) {
$response = $XeroOAuth->extract_params($XeroOAuth->response['response']);
$session = persistSession($response);
unset($_SESSION['oauth']);
header("Location: {$here}");
} else {
outputError($XeroOAuth);
}
// start the OAuth dance
} elseif (isset($_REQUEST['authenticate']) || isset($_REQUEST['authorize'])) {
$params = array(
'oauth_callback' => OAUTH_CALLBACK
);
$response = $XeroOAuth->request('GET', $XeroOAuth->url('RequestToken', ''), $params);
if ($XeroOAuth->response['code'] == 200) {
//$scope = 'payroll.payrollcalendars,payroll.superfunds,payroll.payruns,payroll.payslip,payroll.employees,payroll.TaxDeclaration';
if($_REQUEST['authenticate']>1) $scope = 'payroll.employees,payroll.payruns';
print_r($XeroOAuth->extract_params($XeroOAuth->response['response']));
$_SESSION['oauth'] = $XeroOAuth->extract_params($XeroOAuth->response['response']);
$authurl = $XeroOAuth->url("Authorize", '') . "?oauth_token={$_SESSION['oauth']['oauth_token']}&scope=" . $scope;
echo '<p>To complete the OAuth flow follow this URL: <a href="' . $authurl . '">' . $authurl . '</a></p>';
} else {
outputError($XeroOAuth);
}
}
testLinks();
}