add stripe subscription

This commit is contained in:
Andras Bacsai
2023-08-24 16:14:09 +02:00
parent 2d8f166e4a
commit 39890b319a
36 changed files with 753 additions and 121 deletions

View File

@@ -30,7 +30,7 @@ class Controller extends BaseController
if (!is_cloud()) {
abort(404);
}
return view('subscription', [
return view('subscription.show', [
'settings' => InstanceSettings::get(),
]);
}

View File

@@ -69,4 +69,8 @@ class Actions extends Component
return general_error_handler($e, $this);
}
}
public function stripeCustomerPortal() {
$session = getStripeCustomerPortalSession(currentTeam());
redirect($session->url);
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Http\Livewire\Subscription;
use Livewire\Component;
use Stripe\Stripe;
use Stripe\Checkout\Session;
class PricingPlans extends Component
{
public function subscribeStripe($type)
{
Stripe::setApiKey(config('subscription.stripe_api_key'));
switch ($type) {
case 'basic-monthly':
$priceId = config('subscription.stripe_price_id_basic_monthly');
break;
case 'basic-yearly':
$priceId = config('subscription.stripe_price_id_basic_yearly');
break;
case 'ultimate-monthly':
$priceId = config('subscription.stripe_price_id_ultimate_monthly');
break;
case 'pro-monthly':
$priceId = config('subscription.stripe_price_id_pro_monthly');
break;
case 'pro-yearly':
$priceId = config('subscription.stripe_price_id_pro_yearly');
break;
case 'ultimate-yearly':
$priceId = config('subscription.stripe_price_id_ultimate_yearly');
break;
default:
$priceId = config('subscription.stripe_price_id_basic_monthly');
break;
}
if (!$priceId) {
$this->emit('error', 'Price ID not found! Please contact the administrator.');
return;
}
$payload = [
'client_reference_id' => auth()->user()->id . ':' . currentTeam()->id,
'line_items' => [[
'price' => $priceId,
'quantity' => 1,
]],
'customer_update' =>[
'name' => 'auto'
],
'tax_id_collection' => [
'enabled' => true,
],
'mode' => 'subscription',
'success_url' => route('subscription.success'),
'cancel_url' => route('subscription.show',['cancelled' => true]),
];
$customer = currentTeam()->subscription?->stripe_customer_id ?? null;
if ($customer) {
$payload['customer'] = $customer;
} else {
$payload['customer_email'] = auth()->user()->email;
}
$session = Session::create($payload);
return redirect($session->url, 303);
}
}

View File

@@ -15,12 +15,8 @@ class IsBoardingFlow
*/
public function handle(Request $request, Closure $next): Response
{
$allowed_paths = [
'subscription',
'boarding',
'livewire/message/boarding'
];
if (showBoarding() && !in_array($request->path(), $allowed_paths)) {
ray('IsBoardingFlow Middleware');
if (showBoarding() && !in_array($request->path(), allowedPaths())) {
return redirect('boarding');
}
return $next($request);

View File

@@ -17,31 +17,17 @@ class SubscriptionValid
return $next($request);
}
}
if (isInstanceAdmin()) {
return $next($request);
}
if (is_subscription_active() && $request->path() === 'subscription') {
if (isSubscriptionActive() && $request->path() === 'subscription') {
// ray('active subscription Middleware');
return redirect('/');
}
if (is_subscription_in_grace_period()) {
if (isSubscriptionOnGracePeriod()) {
// ray('is_subscription_in_grace_period Middleware');
return $next($request);
}
if (!is_subscription_active() && !is_subscription_in_grace_period()) {
ray('SubscriptionValid Middleware');
$allowed_paths = [
'subscription',
'login',
'register',
'waitlist',
'force-password-reset',
'logout',
'livewire/message/force-password-reset',
'livewire/message/check-license',
'livewire/message/switch-team',
];
if (!in_array($request->path(), $allowed_paths)) {
if (!isSubscriptionActive() && !isSubscriptionOnGracePeriod()) {
// ray('SubscriptionValid Middleware');
if (!in_array($request->path(), allowedPaths())) {
return redirect('subscription');
} else {
return $next($request);

View File

@@ -22,6 +22,7 @@ class CheckResaleLicenseJob implements ShouldQueue
try {
resolve(CheckResaleLicense::class)();
} catch (\Throwable $th) {
send_internal_notification('CheckResaleLicenseJob failed with: ' . $th->getMessage());
ray($th);
}
}

View File

@@ -28,7 +28,6 @@ class CoolifyTask implements ShouldQueue
*/
public function handle(): void
{
$remote_process = resolve(RunRemoteProcess::class, [
'activity' => $this->activity,
'ignore_errors' => $this->ignore_errors,

View File

@@ -64,35 +64,42 @@ class DatabaseBackupJob implements ShouldQueue
public function handle(): void
{
if ($this->database_status !== 'running') {
ray('database not running');
return;
}
$this->container_name = $this->database->uuid;
$this->backup_dir = backup_dir() . "/databases/" . Str::of($this->team->name)->slug() . '-' . $this->team->id . '/' . $this->container_name;
try {
if ($this->database_status !== 'running') {
ray('database not running');
return;
}
$this->container_name = $this->database->uuid;
$this->backup_dir = backup_dir() . "/databases/" . Str::of($this->team->name)->slug() . '-' . $this->team->id . '/' . $this->container_name;
if ($this->database->name === 'coolify-db') {
$this->container_name = "coolify-db";
$ip = Str::slug($this->server->ip);
$this->backup_dir = backup_dir() . "/coolify" . "/coolify-db-$ip";
}
$this->backup_file = "/dumpall-" . Carbon::now()->timestamp . ".sql";
$this->backup_location = $this->backup_dir . $this->backup_file;
if ($this->database->name === 'coolify-db') {
$this->container_name = "coolify-db";
$ip = Str::slug($this->server->ip);
$this->backup_dir = backup_dir() . "/coolify" . "/coolify-db-$ip";
}
$this->backup_file = "/dumpall-" . Carbon::now()->timestamp . ".sql";
$this->backup_location = $this->backup_dir . $this->backup_file;
$this->backup_log = ScheduledDatabaseBackupExecution::create([
'filename' => $this->backup_location,
'scheduled_database_backup_id' => $this->backup->id,
]);
if ($this->database_type === 'standalone-postgresql') {
$this->backup_standalone_postgresql();
$this->backup_log = ScheduledDatabaseBackupExecution::create([
'filename' => $this->backup_location,
'scheduled_database_backup_id' => $this->backup->id,
]);
if ($this->database_type === 'standalone-postgresql') {
$this->backup_standalone_postgresql();
}
$this->calculate_size();
$this->remove_old_backups();
if ($this->backup->save_s3) {
$this->upload_to_s3();
}
$this->save_backup_logs();
// TODO: Notify user
} catch (\Throwable $th) {
ray($th->getMessage());
send_internal_notification('DatabaseBackupJob failed with: ' . $th->getMessage());
//throw $th;
}
$this->calculate_size();
$this->remove_old_backups();
if ($this->backup->save_s3) {
$this->upload_to_s3();
}
$this->save_backup_logs();
// TODO: Notify user
}
private function backup_standalone_postgresql(): void

View File

@@ -46,6 +46,7 @@ class DatabaseContainerStatusJob implements ShouldQueue, ShouldBeUnique
$this->database->save();
}
} catch (\Exception $e) {
send_internal_notification('DatabaseContainerStatusJob failed with: ' . $e->getMessage());
ray($e->getMessage());
}
}

View File

@@ -49,6 +49,7 @@ class DockerCleanupJob implements ShouldQueue
}
}
} catch (\Exception $e) {
send_internal_notification('DockerCleanupJob failed with: ' . $e->getMessage());
ray($e->getMessage());
}
}

View File

@@ -33,6 +33,7 @@ class ProxyCheckJob implements ShouldQueue
}
} catch (\Throwable $th) {
ray($th->getMessage());
send_internal_notification('ProxyCheckJob failed with: ' . $th->getMessage());
//throw $th;
}
}

View File

@@ -57,6 +57,7 @@ class ProxyContainerStatusJob implements ShouldQueue, ShouldBeUnique
$this->server->proxy->status = 'exited';
$this->server->save();
}
send_internal_notification('ProxyContainerStatusJob failed with: ' . $e->getMessage());
}
}
}

View File

@@ -29,8 +29,8 @@ class ProxyStartJob implements ShouldQueue
}
resolve(StartProxy::class)($this->server);
} catch (\Throwable $th) {
send_internal_notification('ProxyStartJob failed with: ' . $th->getMessage());
ray($th->getMessage());
//throw $th;
}
}
}

View File

@@ -37,8 +37,8 @@ class ResourceStatusJob implements ShouldQueue, ShouldBeUnique
database: $postgresql,
));
}
} catch (\Exception $e) {
ray($e->getMessage());
} catch (\Exception $th) {
send_internal_notification('ResourceStatusJob failed with: ' . $th->getMessage());
}
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Jobs;
use App\Models\Team;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Stripe\Stripe;
class SubscriptionInvoiceFailedJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(protected Team $team)
{
}
public function handle()
{
try {
$session = getStripeCustomerPortalSession($this->team);
$mail = new MailMessage();
$mail->view('emails.subscription-invoice-failed', [
'stripeCustomerPortal' => $session->url,
]);
$mail->subject('Your last payment was failed for Coolify Cloud.');
$this->team->members()->each(function ($member) use ($mail) {
ray($member);
if ($member->isAdmin()) {
send_user_an_email($mail, $member->email);
}
});
} catch (\Throwable $th) {
send_internal_notification('SubscriptionInvoiceFailedJob failed with: ' . $th->getMessage());
}
}
}

View File

@@ -8,6 +8,7 @@ class Webhook extends Model
{
protected $guarded = [];
protected $casts = [
'type' => 'string',
'payload' => 'encrypted',
];
}