calcinai / xero-php

A php library for the Xero API, with a cleaner OAuth interface and ORM-like abstraction.
MIT License
359 stars 262 forks source link

Site not connecting to Xero after upgrading from 1.8 to 2.2.4 #785

Open Pavikam opened 3 years ago

Pavikam commented 3 years ago

Hi,

My PHP site has been using Xero oAuth1.0 with calcinai/xero-php ^1.8 for a long time now and have had no issues. Now that we need to upgrade to oAuth2, I upgraded my package to ^2.2. And after that, the site does not connect to Xero, nor does it even go into the function code in the first place. It just goes inactive after it hits the code.

File name: xerofunction.php

session_start();

$provider = new \Calcinai\OAuth2\Client\Provider\Xero([ 'clientId' => '{xero-client-id}', 'clientSecret' => '{xero-client-secret}', 'redirectUri' => 'https://example.com/callback-url', ]);

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

// If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl([
    'scope' => 'openid email profile accounting.transactions'
]);

$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authUrl);
exit;

// Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

unset($_SESSION['oauth2state']);
exit('Invalid state');

} else {

// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
    'code' => $_GET['code']
]);

//If you added the openid/profile scopes you can access the authorizing user's identity.
$identity = $provider->getResourceOwner($token);
print_r($identity);

//Get the tenants that this user is authorized to access
$tenants = $provider->getTenants($token);
print_r($tenants);

}

Just had the above code given by you to start with. Of course, I substituted the client, secret and redirect url. After a purchase, the site went inactive, whereas it should have posted the data into Xero. I tried commenting the whole code above, and just doing an echo. Still the same.

The only way I could resolve was by reverting to the older ^1.8 version and the code for the same.

Can you please help me get it upgraded and working? Thanks in advance.

DPineault commented 3 years ago

I was having issues as well. I ended up completely removing calcinai/xero-php

composer remove calcinai/xero-php

and then reinstalled it

composer require calcinai/xero-php

that seemed to resolve my immediate issue. Also be sure that your provider info is set properly, including the redirect url. They have to match the defined OAuth2 app that you've setup through the Xero developer portal.