lasthourcloud/app/Actions/License/CheckResaleLicense.php

70 lines
2.3 KiB
PHP
Raw Normal View History

2023-07-14 09:27:08 +00:00
<?php
namespace App\Actions\License;
use App\Models\InstanceSettings;
use Illuminate\Support\Facades\Http;
class CheckResaleLicense
{
public function __invoke()
{
try {
$settings = InstanceSettings::get();
2023-07-27 12:45:34 +00:00
$settings->update([
'is_resale_license_active' => false,
]);
if (isDev()) {
2023-08-14 12:00:10 +00:00
return;
}
2023-07-14 09:27:08 +00:00
if (!$settings->resale_license) {
return;
}
2023-07-27 12:45:34 +00:00
$base_url = config('coolify.license_url');
if (isDev()) {
2023-07-27 12:45:34 +00:00
$base_url = 'http://host.docker.internal:8787';
}
$instance_id = config('app.id');
ray("Checking license key against $base_url/lemon/validate");
2023-07-14 09:27:08 +00:00
$data = Http::withHeaders([
'Accept' => 'application/json',
2023-07-27 12:45:34 +00:00
])->get("$base_url/lemon/validate", [
2023-07-14 09:27:08 +00:00
'license_key' => $settings->resale_license,
2023-07-27 12:45:34 +00:00
'instance_id' => $instance_id,
])->json();
if (data_get($data, 'valid') === true && data_get($data, 'license_key.status') === 'active') {
ray('Valid & active license key');
$settings->update([
'is_resale_license_active' => true,
]);
return;
2023-07-14 09:27:08 +00:00
}
2023-07-27 12:45:34 +00:00
$data = Http::withHeaders([
'Accept' => 'application/json',
])->get("$base_url/lemon/activate", [
'license_key' => $settings->resale_license,
'instance_id' => $instance_id,
])->json();
if (data_get($data, 'activated') === true) {
ray('Activated license key');
2023-07-14 09:27:08 +00:00
$settings->update([
'is_resale_license_active' => true,
]);
return;
}
2023-07-27 12:45:34 +00:00
if (data_get($data, 'license_key.status') === 'active') {
throw new \Exception('Invalid license key.');
}
throw new \Exception('Cannot activate license key.');
2023-07-14 09:27:08 +00:00
} catch (\Throwable $th) {
ray($th);
$settings->update([
'resale_license' => null,
'is_resale_license_active' => false,
]);
throw $th;
}
}
}