coolify/app/Actions/License/CheckResaleLicense.php

70 lines
2.3 KiB
PHP
Raw Normal View History

2023-07-14 11:27:08 +02: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 14:45:34 +02:00
$settings->update([
'is_resale_license_active' => false,
]);
if (isDev()) {
2023-08-14 14:00:10 +02:00
return;
}
2023-07-14 11:27:08 +02:00
if (!$settings->resale_license) {
return;
}
2023-07-27 14:45:34 +02:00
$base_url = config('coolify.license_url');
if (isDev()) {
2023-07-27 14:45:34 +02: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 11:27:08 +02:00
$data = Http::withHeaders([
'Accept' => 'application/json',
2023-07-27 14:45:34 +02:00
])->get("$base_url/lemon/validate", [
2023-07-14 11:27:08 +02:00
'license_key' => $settings->resale_license,
2023-07-27 14:45:34 +02: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 11:27:08 +02:00
}
2023-07-27 14:45:34 +02: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 11:27:08 +02:00
$settings->update([
'is_resale_license_active' => true,
]);
return;
}
2023-07-27 14:45:34 +02: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 17:36:30 +02:00
} catch (\Throwable $e) {
ray($e);
2023-07-14 11:27:08 +02:00
$settings->update([
'resale_license' => null,
'is_resale_license_active' => false,
]);
2023-09-11 17:36:30 +02:00
throw $e;
2023-07-14 11:27:08 +02:00
}
}
}