2024-01-01 01:03:38 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2024-01-01 01:03:38 +01:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Helpers\Chorus;
|
|
|
|
|
|
|
|
use Http;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Piste.
|
|
|
|
*/
|
|
|
|
class Piste
|
|
|
|
{
|
|
|
|
private string $oauth_sandbox_url = 'https://sandbox-oauth.piste.gouv.fr/api/oauth/token';
|
|
|
|
private string $oauth_production_url = 'https://oauth.piste.gouv.fr/api/oauth/token';
|
|
|
|
private string $sandbox_url = 'https://sandbox-api.piste.gouv.fr';
|
|
|
|
private string $production_url = 'https://api.piste.gouv.fr';
|
|
|
|
|
|
|
|
private bool $test_mode = false;
|
|
|
|
|
2024-01-01 07:21:52 +01:00
|
|
|
public function __construct(private string $username, private string $password)
|
2024-01-01 01:03:38 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMode($testmode = true): self
|
|
|
|
{
|
|
|
|
$this->test_mode = $testmode;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function oauthHeaders(): array
|
|
|
|
{
|
|
|
|
return [
|
2024-01-01 07:21:52 +01:00
|
|
|
'grant_type' => 'client_credentials',
|
|
|
|
'client_id' => config('services.chorus.client_id'),
|
|
|
|
'client_secret' => config('services.chorus.secret'),
|
|
|
|
'scope' => 'openid profile'
|
|
|
|
];
|
2024-01-01 01:03:38 +01:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-01-01 01:03:38 +01:00
|
|
|
private function oauthUrl(): string
|
|
|
|
{
|
|
|
|
return $this->test_mode ? $this->oauth_sandbox_url : $this->oauth_production_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function apiUrl(): string
|
|
|
|
{
|
|
|
|
return $this->test_mode ? $this->sandbox_url : $this->production_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOauthAccessToken(): ?string
|
|
|
|
{
|
|
|
|
$response = Http::asForm()->post($this->oauthUrl(), $this->oauthHeaders());
|
|
|
|
|
|
|
|
if($response->successful()) {
|
|
|
|
return $response->json()['access_token'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-01-01 07:21:52 +01:00
|
|
|
public function execute(string $uri, array $data = [])
|
2024-01-01 01:03:38 +01:00
|
|
|
{
|
|
|
|
$access_token = $this->getOauthAccessToken();
|
|
|
|
|
2024-01-01 07:21:52 +01:00
|
|
|
nlog($access_token);
|
|
|
|
nlog($this->username);
|
|
|
|
nlog($this->password);
|
|
|
|
nlog(base64_encode($this->username . ':' . $this->password));
|
|
|
|
|
|
|
|
$r = Http::withToken($access_token)
|
|
|
|
->withHeaders([
|
|
|
|
'cpro-account' => base64_encode($this->username . ':' . $this->password),
|
|
|
|
'Content-Type' => 'application/json;charset=utf-8',
|
|
|
|
'Accept' => 'application/json;charset=utf-8'
|
|
|
|
])
|
|
|
|
->post($this->apiUrl() . '/cpro/factures/'. $uri, $data);
|
|
|
|
|
|
|
|
nlog($r);
|
|
|
|
nlog($r->json());
|
|
|
|
nlog($r->successful());
|
|
|
|
nlog($r->body());
|
|
|
|
nlog($r->collect());
|
|
|
|
return $r;
|
2024-01-01 01:03:38 +01:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
}
|