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 @@
use App\Models\InstanceSettings; use App\Models\InstanceSettings;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Visus\Cuid2\Cuid2;
class CheckResaleLicense class CheckResaleLicense
{ {
@ -12,41 +11,49 @@ public function __invoke()
{ {
try { try {
$settings = InstanceSettings::get(); $settings = InstanceSettings::get();
$instance_id = config('app.id'); $settings->update([
'is_resale_license_active' => false,
]);
if (!$settings->resale_license) { if (!$settings->resale_license) {
return; 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([ $data = Http::withHeaders([
'Accept' => 'application/json', 'Accept' => 'application/json',
])->post('https://api.lemonsqueezy.com/v1/licenses/validate', [ ])->get("$base_url/lemon/validate", [
'license_key' => $settings->resale_license, 'license_key' => $settings->resale_license,
'instance_name' => $instance_id, 'instance_id' => $instance_id,
])->throw()->json(); ])->json();
$product_id = (int)data_get($data, 'meta.product_id'); if (data_get($data, 'valid') === true && data_get($data, 'license_key.status') === 'active') {
$valid_product_id = (int)config('coolify.lemon_squeezy_product_id'); ray('Valid & active license key');
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();
}
$settings->update([ $settings->update([
'is_resale_license_active' => true, 'is_resale_license_active' => true,
]); ]);
return; 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) { } catch (\Throwable $th) {
ray($th); ray($th);
$settings->update([ $settings->update([

View File

@ -1,26 +0,0 @@
<?php
namespace App\Http\Livewire;
use Illuminate\Support\Facades\Cache;
use Livewire\Component;
class License extends Component
{
public string $license;
public function submit()
{
ray('checking license');
$this->validate([
'license' => 'required'
]);
// Pretend we're checking the license
// if ($this->license === '123') {
// ray('license is valid');
// Cache::put('license_key', '123');
// return redirect()->to('/');
// } else {
// ray('license is invalid');
// }
}
}

View File

@ -2,8 +2,8 @@
return [ return [
'self_hosted' => env('SELF_HOSTED', true), 'self_hosted' => env('SELF_HOSTED', true),
'license_url' => 'https://license.coolify.io',
'lemon_squeezy_webhook_secret' => env('LEMON_SQUEEZY_WEBHOOK_SECRET'), 'lemon_squeezy_webhook_secret' => env('LEMON_SQUEEZY_WEBHOOK_SECRET'),
'lemon_squeezy_product_id' => env('LEMON_SQUEEZY_PRODUCT_ID'),
'lemon_squeezy_checkout_id' => env('LEMON_SQUEEZY_CHECKOUT_ID'), 'lemon_squeezy_checkout_id' => env('LEMON_SQUEEZY_CHECKOUT_ID'),
'mux_enabled' => env('MUX_ENABLED', true), 'mux_enabled' => env('MUX_ENABLED', true),
'dev_webhook' => env('SERVEO_URL'), 'dev_webhook' => env('SERVEO_URL'),

View File

@ -33,7 +33,6 @@ services:
- SELF_HOSTED - SELF_HOSTED
- LEMON_SQUEEZY_WEBHOOK_SECRET - LEMON_SQUEEZY_WEBHOOK_SECRET
- LEMON_SQUEEZY_CHECKOUT_ID - LEMON_SQUEEZY_CHECKOUT_ID
- LEMON_SQUEEZY_PRODUCT_ID
ports: ports:
- "${APP_PORT:-8000}:80" - "${APP_PORT:-8000}:80"
expose: expose:

View File

@ -18,7 +18,7 @@
</div> </div>
@if (session()->has('error')) @if (session()->has('error'))
<div class="text-error"> <div class="text-error">
{{ session('error') }} {!! session('error') !!}
</div> </div>
@endif @endif
</form> </form>

View File

@ -1,6 +0,0 @@
<div>
<form wire:submit.prevent='submit'>
<x-forms.input id="license" label="License Key" />
<x-forms.button type='submit'>Check</x-forms.button>
</form>
</div>

View File

@ -29,6 +29,6 @@
Subscription</a> Subscription</a>
</x-forms.button> </x-forms.button>
@else @else
<div>Resale license is not active. Please contact your instance admin.</div> <div class="px-10">Resale license is not active. Please contact your instance admin.</div>
@endif @endif
</x-layout-subscription> </x-layout-subscription>