An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
wip
Andras Bacsaif3f8a62a1826
diff --git a/app/Http/Kernel.php b/app/Http/Kernel.phpindex 7beaad19e2..ef84b9abbc 100644--- a/app/Http/Kernel.php+++ b/app/Http/Kernel.php@@ -21,7 +21,7 @@ class Kernel extends HttpKernel\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,\App\Http\Middleware\TrimStrings::class,\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,- // \App\Http\Middleware\LicenseValid::class,+ \App\Http\Middleware\LicenseValid::class,];/**diff --git a/app/Http/Middleware/LicenseValid.php b/app/Http/Middleware/LicenseValid.phpindex 099092ff92..aeee9a5acd 100644--- a/app/Http/Middleware/LicenseValid.php+++ b/app/Http/Middleware/LicenseValid.php@@ -16,7 +16,10 @@ class LicenseValid*/public function handle(Request $request, Closure $next): Response{- if (!config('coolify.self_hosted')) {+ if (isCloud()) {+ if (isDev()) {+ return $next($request);+ }$value = Cache::get('license_key');if (!$value) {ray($request->path());diff --git a/app/Models/Subscription.php b/app/Models/Subscription.phpnew file mode 100644index 0000000000..380660b77f--- /dev/null+++ b/app/Models/Subscription.php@@ -0,0 +1,15 @@+<?php++namespace App\Models;++use Illuminate\Database\Eloquent\Factories\HasFactory;+use Illuminate\Database\Eloquent\Model;++class Subscription extends Model+{+ protected $guarded = [];+ public function team()+ {+ return $this->belongsTo(Team::class);+ }+}diff --git a/app/Models/Team.php b/app/Models/Team.phpindex cf9a1f5cbb..ad860dd721 100644--- a/app/Models/Team.php+++ b/app/Models/Team.php@@ -66,7 +66,10 @@ class Team extends Model implements SendsDiscord, SendsEmail}-+ public function subscription()+ {+ return $this->hasOne(Subscription::class);+ }public function projects(){return $this->hasMany(Project::class);diff --git a/app/Models/Webhook.php b/app/Models/Webhook.phpnew file mode 100644index 0000000000..09afb9898b--- /dev/null+++ b/app/Models/Webhook.php@@ -0,0 +1,11 @@+<?php++namespace App\Models;++use Illuminate\Database\Eloquent\Factories\HasFactory;+use Illuminate\Database\Eloquent\Model;++class Webhook extends Model+{+ protected $guarded = [];+}diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.phpindex 42359becdc..30b8f7d1be 100644--- a/bootstrap/helpers/shared.php+++ b/bootstrap/helpers/shared.php@@ -4,6 +4,7 @@ use App\Models\InstanceSettings;use Illuminate\Database\QueryException;use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Route;+use Illuminate\Support\Facades\URL;use Visus\Cuid2\Cuid2;use Illuminate\Support\Str;@@ -127,3 +128,26 @@ function isDev(){return config('app.env') === 'local';}+function isCloud()+{+ return !config('coolify.self_hosted');+}+function getSubscriptionLink()+{+ $user_id = auth()->user()->id;+ $email = auth()->user()->email ?? null;+ $name = auth()->user()->name ?? null;+ $url = "https://store.coollabs.io/checkout/buy/d0b28c6a-9b57-40bf-8b84-89fbafde6526?";+ if ($user_id) {+ $url .= "checkout[custom][user_id]={$user_id}";+ }+ if ($email) {+ $url .= "?checkout[email]={$email}";+ }+ if ($name) {+ $url .= "&checkout[name]={$name}";+ }+ $url = "?checkout[custom][user_id]={$user_id}&checkout[email]={$email}&checkout[name]={$name}";+ ray($url);+ return $url;+}diff --git a/config/coolify.php b/config/coolify.phpindex 4e25c87457..044bb45639 100644--- a/config/coolify.php+++ b/config/coolify.php@@ -2,6 +2,8 @@return ['self_hosted' => env('SELF_HOSTED', true),+ 'lemon_squeezy_webhook_secret' => env('LEMON_SQUEEZY_WEBHOOK_SECRET'),+ 'lemon_squeezy_product_id' => env('LEMON_SQUEEZY_PRODUCT_ID'),'mux_enabled' => env('MUX_ENABLED', true),'dev_webhook' => env('SERVEO_URL'),'base_config_path' => env('BASE_CONFIG_PATH', '/data/coolify'),diff --git a/database/migrations/2023_07_13_115117_create_subscriptions_table.php b/database/migrations/2023_07_13_115117_create_subscriptions_table.phpnew file mode 100644index 0000000000..e31fc1c3fa--- /dev/null+++ b/database/migrations/2023_07_13_115117_create_subscriptions_table.php@@ -0,0 +1,37 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration+{+ /**+ * Run the migrations.+ */+ public function up(): void+ {+ Schema::create('subscriptions', function (Blueprint $table) {+ $table->id();+ $table->string('lemon_order_id');+ $table->string('lemon_product_id');+ $table->string('lemon_variant_id');+ $table->string('lemon_customer_id');+ $table->string('lemon_status');+ $table->string('lemon_trial_ends_at');+ $table->string('lemon_renews_at');+ $table->string('lemon_ends_at');+ $table->string('lemon_update_payment_menthod_url');+ $table->foreignId('team_id');+ $table->timestamps();+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void+ {+ Schema::dropIfExists('subscriptions');+ }+};diff --git a/database/migrations/2023_07_13_120719_create_webhooks_table.php b/database/migrations/2023_07_13_120719_create_webhooks_table.phpnew file mode 100644index 0000000000..9376a10c92--- /dev/null+++ b/database/migrations/2023_07_13_120719_create_webhooks_table.php@@ -0,0 +1,31 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration+{+ /**+ * Run the migrations.+ */+ public function up(): void+ {+ Schema::create('webhooks', function (Blueprint $table) {+ $table->id();+ $table->enum('status', ['pending', 'success', 'failed'])->default('pending');+ $table->enum('type', ['github', 'gitlab', 'bitbucket', 'lemonsqueezy']);+ $table->longText('payload');+ $table->longText('failure_reason')->nullable();+ $table->timestamps();+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void+ {+ Schema::dropIfExists('webhooks');+ }+};diff --git a/database/seeders/SubscriptionSeeder.php b/database/seeders/SubscriptionSeeder.phpnew file mode 100644index 0000000000..e76d793a3e--- /dev/null+++ b/database/seeders/SubscriptionSeeder.php@@ -0,0 +1,17 @@+<?php++namespace Database\Seeders;++use Illuminate\Database\Console\Seeds\WithoutModelEvents;+use Illuminate\Database\Seeder;++class SubscriptionSeeder extends Seeder+{+ /**+ * Run the database seeds.+ */+ public function run(): void+ {+ //+ }+}diff --git a/database/seeders/WebhookSeeder.php b/database/seeders/WebhookSeeder.phpnew file mode 100644index 0000000000..f226c715a2--- /dev/null+++ b/database/seeders/WebhookSeeder.php@@ -0,0 +1,17 @@+<?php++namespace Database\Seeders;++use Illuminate\Database\Console\Seeds\WithoutModelEvents;+use Illuminate\Database\Seeder;++class WebhookSeeder extends Seeder+{+ /**+ * Run the database seeds.+ */+ public function run(): void+ {+ //+ }+}diff --git a/resources/views/components/layout.blade.php b/resources/views/components/layout.blade.phpindex b69b54fa2d..5610dfff2b 100644--- a/resources/views/components/layout.blade.php+++ b/resources/views/components/layout.blade.php@@ -22,6 +22,7 @@}</style>@livewireStyles+ <script src="https://app.lemonsqueezy.com/js/lemon.js" defer></script></head><body>diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.phpindex f9d5dd0430..f194e45291 100644--- a/resources/views/dashboard.blade.php+++ b/resources/views/dashboard.blade.php@@ -18,4 +18,6 @@<div class="stat-desc">Applications, databases, etc...</div></div></div>+ {{-- <a href="{{ getSubscriptionLink() }}">Subscribe</a> --}}+</x-layout>diff --git a/routes/webhooks.php b/routes/webhooks.phpindex f91f106569..5bfdbfde44 100644--- a/routes/webhooks.php+++ b/routes/webhooks.php@@ -4,6 +4,7 @@ use App\Models\Application;use App\Models\ApplicationPreview;use App\Models\PrivateKey;use App\Models\GithubApp;+use App\Models\Webhook;use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Route;use Illuminate\Support\Str;@@ -170,3 +171,42 @@ Route::post('/source/github/events', function () {return general_error_handler(err: $e);}});++Route::post('/subscriptions/events', function () {+ try {+ $secret = config('coolify.lemon_squeezy_webhook_secret');+ $payload = request()->collect();+ $hash = hash_hmac('sha256', $payload, $secret);+ $signature = '';++ if (!hash_equals($hash, $signature)) {+ return response('Invalid signature.', 400);+ }++ $webhook = Webhook::create([+ 'type' => 'lemonsqueezy',+ 'payload' => $payload+ ]);++ $event = data_get($payload, 'meta.event_name');+ $email = data_get($payload, 'data.attributes.user_email');+ $update_payment_method = data_get($payload, 'data.attributes.urls.update_payment_method');+ switch ($event) {+ case 'subscription_created':++ break;+ }++ ray($payload);+ $webhook->update([+ 'status' => 'success',+ ]);+ } catch (\Exception $e) {+ $webhook->update([+ 'status' => 'failed',+ 'failure_reason' => $e->getMessage()+ ]);+ } finally {+ return response('OK');+ }+});