fix: licensing

This commit is contained in:
Andras Bacsai
2023-07-27 14:45:34 +02:00
parent e7d019028a
commit 97e2a5d30b
7 changed files with 35 additions and 61 deletions

View File

@@ -4,7 +4,6 @@ namespace App\Actions\License;
use App\Models\InstanceSettings;
use Illuminate\Support\Facades\Http;
use Visus\Cuid2\Cuid2;
class CheckResaleLicense
{
@@ -12,41 +11,49 @@ class CheckResaleLicense
{
try {
$settings = InstanceSettings::get();
$instance_id = config('app.id');
$settings->update([
'is_resale_license_active' => false,
]);
if (!$settings->resale_license) {
return;
}
ray('Checking license key');
$base_url = config('coolify.license_url');
if (isDev()) {
$base_url = 'http://host.docker.internal:8787';
}
$instance_id = config('app.id');
ray("Checking license key against $base_url/lemon/validate");
$data = Http::withHeaders([
'Accept' => 'application/json',
])->post('https://api.lemonsqueezy.com/v1/licenses/validate', [
])->get("$base_url/lemon/validate", [
'license_key' => $settings->resale_license,
'instance_name' => $instance_id,
])->throw()->json();
$product_id = (int)data_get($data, 'meta.product_id');
$valid_product_id = (int)config('coolify.lemon_squeezy_product_id');
if ($product_id !== $valid_product_id) {
throw new \Exception('Invalid product id');
}
ray('Valid Product Id');
['valid' => $valid, 'license_key' => $license_key] = $data;
if ($valid) {
if (data_get($license_key, 'status') === 'inactive') {
Http::withHeaders([
'Accept' => 'application/json',
])->post('https://api.lemonsqueezy.com/v1/licenses/activate', [
'license_key' => $settings->resale_license,
'instance_name' => $instance_id,
])->throw()->json();
}
'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;
}
throw new \Exception('Invalid license key');
$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');
$settings->update([
'is_resale_license_active' => true,
]);
return;
}
if (data_get($data, 'license_key.status') === 'active') {
throw new \Exception('Invalid license key.');
}
throw new \Exception('Cannot activate license key.');
} catch (\Throwable $th) {
ray($th);
$settings->update([