2023-07-14 09:27:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Actions\License;
|
|
|
|
|
|
|
|
use App\Models\InstanceSettings;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
2023-11-02 13:10:29 +00:00
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
|
2023-07-14 09:27:08 +00:00
|
|
|
class CheckResaleLicense
|
|
|
|
{
|
2023-11-02 13:10:29 +00:00
|
|
|
use AsAction;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-02 13:10:29 +00:00
|
|
|
public function handle()
|
2023-07-14 09:27:08 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$settings = InstanceSettings::get();
|
2023-08-27 13:23:47 +00:00
|
|
|
if (isDev()) {
|
2023-11-02 13:10:29 +00:00
|
|
|
$settings->update([
|
|
|
|
'is_resale_license_active' => true,
|
|
|
|
]);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-14 12:00:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-02 13:10:29 +00:00
|
|
|
// if (!$settings->resale_license) {
|
|
|
|
// return;
|
|
|
|
// }
|
2023-07-27 12:45:34 +00:00
|
|
|
$base_url = config('coolify.license_url');
|
|
|
|
$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,
|
|
|
|
]);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-07-27 12:45:34 +00:00
|
|
|
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,
|
|
|
|
]);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-07-14 09:27:08 +00:00
|
|
|
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-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
ray($e);
|
2023-07-14 09:27:08 +00:00
|
|
|
$settings->update([
|
|
|
|
'resale_license' => null,
|
|
|
|
'is_resale_license_active' => false,
|
|
|
|
]);
|
2023-09-11 15:36:30 +00:00
|
|
|
throw $e;
|
2023-07-14 09:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|