Commit b04e7b12 authored by abkrim's avatar abkrim

Tests con integracion de base de datos produccion 2018-12-11-16-44-04

parent 22eae3c2
......@@ -7,6 +7,7 @@ use GuzzleHttp\ClientInterface;
use Abkrim\ApiSage\Models\ExtToken;
use Carbon\Carbon;
use Log;
use DB;
class SageProvider extends AbstractProvider implements ProviderInterface
{
......@@ -180,6 +181,8 @@ class SageProvider extends AbstractProvider implements ProviderInterface
{
// Tenemos token y token de refresco activo ?
$tokenInfo = ExtToken::find('sage');
//$tokenInfo = DB::table('ext_tokens')->where('driver', 'sage')->get();
if (is_null($tokenInfo)) {
if (\App::runningInConsole()) {
// Running from console
......@@ -207,6 +210,34 @@ class SageProvider extends AbstractProvider implements ProviderInterface
}
public function setActiveTokenTest()
{
// Tenemos token y token de refresco activo ?
$tokenInfo = DB::connection(config('database.connections.mysql.driver'))
->table('ext_tokens')->where('driver', 'sage')->first();
if (is_null($tokenInfo)) {
exit('Necesita usar el interface web para activar el token');
}
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => ['Accept' => 'application/json'],
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => $tokenInfo->refresh,
'client_secret' => $this->clientSecret,
'client_id' => $this->clientId
]
]);
$auth_token = json_decode($response->getBody(), true);
$access_token = $this->storeToken($auth_token);
return $access_token;
}
private function verifyActiveToken($tokenInfo)
{
$now = Carbon::now();
......@@ -216,7 +247,7 @@ class SageProvider extends AbstractProvider implements ProviderInterface
// Necesitamos un nuevo token
// https://oauth.accounting.sage.com/token
//dd( $tokenInfo->refresh, $this->clientSecret, $this->clientId);
Log::debug('We need refresh token');
Log::debug('We need refresh token '.$tokenInfo->refresh);
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => ['Accept' => 'application/json'],
'form_params' => [
......@@ -271,18 +302,34 @@ class SageProvider extends AbstractProvider implements ProviderInterface
private function storeToken($auth_token)
{
ExtToken::updateOrCreate(
[ 'driver' => 'sage' ],
//['driver', 'type', 'scope', 'access','refresh', 'access_expires', 'refresh_expires']
[
'type' => $auth_token['token_type'],
'scope' => $auth_token['scope'],
'access' => $auth_token['access_token'],
'refresh' => $auth_token['refresh_token'],
'access_expires' => Carbon::now()->addSeconds($auth_token['expires_in']),
'refresh_expires' => Carbon::now()->addSeconds($auth_token['refresh_token_expires_in'])
]
);
if ((config('app.env') == 'testing')) {
DB::connection(config('database.connections.mysql.driver'))
->table('ext_tokens')->where('driver', 'sage')
->update(
[
'type' => $auth_token['token_type'],
'scope' => $auth_token['scope'],
'access' => $auth_token['access_token'],
'refresh' => $auth_token['refresh_token'],
'access_expires' => Carbon::now()->addSeconds($auth_token['expires_in']),
'refresh_expires' => Carbon::now()->addSeconds($auth_token['refresh_token_expires_in'])
]
);
} else {
ExtToken::updateOrCreate(
[ 'driver' => 'sage' ],
//['driver', 'type', 'scope', 'access','refresh', 'access_expires', 'refresh_expires']
[
'type' => $auth_token['token_type'],
'scope' => $auth_token['scope'],
'access' => $auth_token['access_token'],
'refresh' => $auth_token['refresh_token'],
'access_expires' => Carbon::now()->addSeconds($auth_token['expires_in']),
'refresh_expires' => Carbon::now()->addSeconds($auth_token['refresh_token_expires_in'])
]
);
}
return $auth_token['access_token'];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment