Merge pull request #10525 from beganovich/INV2-1776
E-invoicing: Healthchecks & regenerating tokens
This commit is contained in:
commit
7574af6c3a
|
|
@ -11,11 +11,13 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\EInvoice\HealthcheckRequest;
|
||||
use App\Http\Requests\EInvoice\ShowQuotaRequest;
|
||||
use App\Http\Requests\EInvoice\ValidateEInvoiceRequest;
|
||||
use App\Http\Requests\EInvoice\UpdateEInvoiceConfiguration;
|
||||
use App\Services\EDocument\Standards\Validation\Peppol\EntityLevel;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use InvoiceNinja\EInvoice\Models\Peppol\BranchType\FinancialInstitutionBranch;
|
||||
use InvoiceNinja\EInvoice\Models\Peppol\FinancialInstitutionType\FinancialInstitution;
|
||||
use InvoiceNinja\EInvoice\Models\Peppol\FinancialAccountType\PayeeFinancialAccount;
|
||||
|
|
@ -43,7 +45,7 @@ class EInvoiceController extends BaseController
|
|||
|
||||
$data = [];
|
||||
|
||||
match($request->entity) {
|
||||
match ($request->entity) {
|
||||
'invoices' => $data = $el->checkInvoice($request->getEntity()),
|
||||
'clients' => $data = $el->checkClient($request->getEntity()),
|
||||
'companies' => $data = $el->checkCompany($request->getEntity()),
|
||||
|
|
@ -130,8 +132,8 @@ class EInvoiceController extends BaseController
|
|||
{
|
||||
nlog(["quota" => $request->all()]);
|
||||
/**
|
||||
* @var \App\Models\Company
|
||||
*/
|
||||
* @var \App\Models\Company
|
||||
*/
|
||||
$company = auth()->user()->company();
|
||||
|
||||
$response = \Illuminate\Support\Facades\Http::baseUrl(config('ninja.hosted_ninja_url'))
|
||||
|
|
@ -162,4 +164,35 @@ class EInvoiceController extends BaseController
|
|||
'quota' => $account->e_invoice_quota,
|
||||
]);
|
||||
}
|
||||
|
||||
public function healthcheck(HealthcheckRequest $request): JsonResponse
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
$response = Http::baseUrl(config('ninja.hosted_ninja_url'))
|
||||
->withHeaders([
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
'X-EInvoice-Token' => $user->account->e_invoicing_token
|
||||
])
|
||||
->post('/api/einvoice/health_check', data: [
|
||||
'license' => config('ninja.license_key'),
|
||||
'account_key' => $user->account->key,
|
||||
]);
|
||||
|
||||
if ($response->status() === 403) {
|
||||
return response()->json(['message' => ctrans('texts.einvoice_token_not_found')], status: 422);
|
||||
}
|
||||
|
||||
if ($response->status() == 422) {
|
||||
return response()->json(['message' => $response->json('message')], 422);
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() === 400) {
|
||||
return response()->json(['message' => $response->json('message')], 400);
|
||||
}
|
||||
|
||||
return response()->json([]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Http\Requests\EInvoice;
|
||||
|
||||
use App\Utils\Ninja;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
|
||||
class HealthcheckRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
if (config('ninja.app_env') == 'local') {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
return Ninja::isSelfHost() && $user->account->isPaid();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function failedAuthorization(): void
|
||||
{
|
||||
throw new AuthorizationException(
|
||||
message: ctrans('texts.peppol_not_paid_message'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -5487,6 +5487,8 @@ $lang = array(
|
|||
'enable_public_notifications_help' => 'Enable real-time notifications from Invoice Ninja.',
|
||||
'navigate' => 'Navigate',
|
||||
'calculate_taxes_warning' => 'This action will enable line item taxes and disable total taxes. Any open invoices may be recalculated with the new settings!',
|
||||
'einvoice_token_not_found' => 'E-invoicing token not found. Please go to Settings > E-invoice and regenerate token.',
|
||||
'regenerate' => 'Regenerate',
|
||||
);
|
||||
|
||||
return $lang;
|
||||
|
|
|
|||
|
|
@ -245,6 +245,7 @@ Route::group(['middleware' => ['throttle:api', 'api_db', 'token_auth', 'locale']
|
|||
|
||||
Route::post('einvoice/token/update', EInvoiceTokenController::class)->name('einvoice.token.update');
|
||||
Route::get('einvoice/quota', [EInvoiceController::class, 'quota'])->name('einvoice.quota');
|
||||
Route::get('einvoice/health_check', [EInvoiceController::class, 'healthcheck'])->name('einvoice.healthcheck');
|
||||
|
||||
Route::post('emails', [EmailController::class, 'send'])->name('email.send')->middleware('user_verified');
|
||||
Route::post('emails/clientHistory/{client}', [EmailHistoryController::class, 'clientHistory'])->name('email.clientHistory');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace Tests\Feature\EInvoice;
|
||||
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||
use Tests\MockAccountData;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class PeppolApiTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use MockAccountData;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
if (!config('ninja.storecove_api_key')) {
|
||||
$this->markTestSkipped('Storecove API key not set');
|
||||
}
|
||||
|
||||
$this->makeTestData();
|
||||
|
||||
$this->withoutMiddleware(
|
||||
ThrottleRequests::class,
|
||||
);
|
||||
}
|
||||
|
||||
public function testGeneratingToken()
|
||||
{
|
||||
if (! class_exists(\Modules\Admin\Http\Controllers\EInvoiceTokenController::class)) {
|
||||
$this->markTestSkipped('Admin module not installed');
|
||||
}
|
||||
|
||||
config(['ninja.environment' => 'selfhost']);
|
||||
|
||||
/**
|
||||
* @var \App\Models\CompanyUser $user
|
||||
*/
|
||||
$user = $this->user;
|
||||
|
||||
$current = $user->account->e_invoicing_token;
|
||||
|
||||
$this->assertNull($current);
|
||||
|
||||
$this
|
||||
->withHeaders([
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])
|
||||
->post('/api/v1/einvoice/token/update')
|
||||
->assertSuccessful()
|
||||
;
|
||||
|
||||
$user->refresh();
|
||||
|
||||
$this->assertNotEquals($current, $user->account->e_invoicing_token);
|
||||
}
|
||||
|
||||
public function testHealthCheck()
|
||||
{
|
||||
if (! class_exists(\Modules\Admin\Http\Controllers\EInvoiceTokenController::class)) {
|
||||
$this->markTestSkipped('Admin module not installed');
|
||||
}
|
||||
|
||||
config(['ninja.environment' => 'selfhost']);
|
||||
|
||||
$this
|
||||
->withHeaders([
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])
|
||||
->get('/api/v1/einvoice/health_check')
|
||||
->assertStatus(status: 422)
|
||||
;
|
||||
|
||||
$this
|
||||
->withHeaders([
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])
|
||||
->post('/api/v1/einvoice/token/update')
|
||||
->assertSuccessful()
|
||||
;
|
||||
|
||||
$this
|
||||
->withHeaders([
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])
|
||||
->get('/api/v1/einvoice/health_check')
|
||||
->assertSuccessful()
|
||||
;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue