Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/Actions/SubLicenses/DeleteSubLicense.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Actions\SubLicenses;

use App\Models\SubLicense;
use App\Services\Anystack\Anystack;

class DeleteSubLicense
{
/**
* Handle the deletion of a sub-license.
*/
public function handle(SubLicense $subLicense): bool
{
Anystack::api()
->license($subLicense->anystack_id, $subLicense->parentLicense->subscriptionType->anystackProductId())
->delete();

return $subLicense->delete();
}
}
25 changes: 25 additions & 0 deletions app/Actions/SubLicenses/SuspendSubLicense.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Actions\SubLicenses;

use App\Models\SubLicense;
use App\Services\Anystack\Anystack;

class SuspendSubLicense
{
/**
* Handle the suspension of a sub-license.
*/
public function handle(SubLicense $subLicense): SubLicense
{
Anystack::api()
->license($subLicense->anystack_id, $subLicense->parentLicense->subscriptionType->anystackProductId())
->suspend();

$subLicense->update([
'is_suspended' => true,
]);

return $subLicense;
}
}
25 changes: 25 additions & 0 deletions app/Actions/SubLicenses/UnsuspendSubLicense.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Actions\SubLicenses;

use App\Models\SubLicense;
use App\Services\Anystack\Anystack;

class UnsuspendSubLicense
{
/**
* Handle the un-suspension of a sub-license.
*/
public function handle(SubLicense $subLicense): SubLicense
{
Anystack::api()
->license($subLicense->anystack_id, $subLicense->parentLicense->subscriptionType->anystackProductId())
->suspend(false);

$subLicense->update([
'is_suspended' => false,
]);

return $subLicense;
}
}
47 changes: 47 additions & 0 deletions app/Console/Commands/ExtendLicenseExpiryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Console\Commands;

use App\Jobs\UpdateAnystackLicenseExpiryJob;
use App\Models\License;
use Illuminate\Console\Command;

class ExtendLicenseExpiryCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'licenses:renew {license_id}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Extend the expiry date of a license';

/**
* Execute the console command.
*/
public function handle(): int
{
$licenseId = $this->argument('license_id');

// Find the license
$license = License::find($licenseId);
if (! $license) {
$this->error("License with ID {$licenseId} not found");

return Command::FAILURE;
}

// Dispatch the job to update the license expiry
UpdateAnystackLicenseExpiryJob::dispatch($license);

$this->info("License expiry updated to {$license->expires_at->format('Y-m-d')}");

return Command::SUCCESS;
}
}
69 changes: 69 additions & 0 deletions app/Console/Commands/SendLicenseExpiryWarnings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Console\Commands;

use App\Models\License;
use App\Notifications\LicenseExpiryWarning;
use Illuminate\Console\Command;

class SendLicenseExpiryWarnings extends Command
{
protected $signature = 'licenses:send-expiry-warnings';

protected $description = 'Send expiry warning emails for licenses that are expiring soon';

public function handle(): int
{
$warningDays = [30, 7, 1];
$totalSent = 0;

foreach ($warningDays as $days) {
$sent = $this->sendWarningsForDays($days);
$totalSent += $sent;

$this->info("Sent {$sent} warning emails for licenses expiring in {$days} day(s)");
}

$this->info("Total warning emails sent: {$totalSent}");

return Command::SUCCESS;
}

private function sendWarningsForDays(int $days): int
{
$targetDate = now()->addDays($days)->startOfDay();
$sent = 0;

// Find licenses that:
// 1. Expire on the target date
// 2. Don't have an active subscription (legacy licenses)
// 3. Haven't been sent a warning for this specific day count recently
$licenses = License::query()
->whereDate('expires_at', $targetDate)
->whereNull('subscription_item_id') // Legacy licenses without subscriptions
->whereDoesntHave('expiryWarnings', function ($query) use ($days) {
$query->where('warning_days', $days)
->where('sent_at', '>=', now()->subHours(23)); // Prevent duplicate emails within 23 hours
})
->with('user')
->get();

foreach ($licenses as $license) {
if ($license->user) {
$license->user->notify(new LicenseExpiryWarning($license, $days));

// Track that we sent this warning
$license->expiryWarnings()->create([
'warning_days' => $days,
'sent_at' => now(),
]);

$sent++;

$this->line("Sent {$days}-day warning to {$license->user->email} for license {$license->key}");
}
}

return $sent;
}
}
6 changes: 5 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
// Send license expiry warnings daily at 9 AM UTC
$schedule->command('licenses:send-expiry-warnings')
->dailyAt('09:00')
->onOneServer()
->runInBackground();
}

/**
Expand Down
18 changes: 16 additions & 2 deletions app/Enums/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function name(): string
return config("subscriptions.plans.{$this->value}.name");
}

public function stripePriceId(): string
public function stripePriceId(bool $forceEap = false): string
{
// EAP ends June 1st at midnight UTC
return now()->isBefore('2025-06-01 00:00:00')
return now()->isBefore('2025-06-01 00:00:00') || $forceEap
? config("subscriptions.plans.{$this->value}.stripe_price_id_eap")
: config("subscriptions.plans.{$this->value}.stripe_price_id");
}
Expand All @@ -75,4 +75,18 @@ public function anystackPolicyId(): string
{
return config("subscriptions.plans.{$this->value}.anystack_policy_id");
}

public function supportsSubLicenses(): bool
{
return in_array($this, [self::Pro, self::Max, self::Forever]);
}

public function subLicenseLimit(): ?int
{
return match ($this) {
self::Pro => 9,
self::Max, self::Forever => null, // Unlimited
default => 0,
};
}
}
20 changes: 20 additions & 0 deletions app/Features/ShowAuthButtons.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Features;

use Laravel\Pennant\Feature;

class ShowAuthButtons
{
/**
* Resolve the feature's initial value.
*/
public function resolve(mixed $scope): bool
{
if ($scope) {
return Feature::for(null)->active(static::class);
}

return false;
}
}
86 changes: 86 additions & 0 deletions app/Http/Controllers/Auth/CustomerAuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;

class CustomerAuthController extends Controller
{
public function showLogin(): View
{
return view('auth.login');
}

public function login(LoginRequest $request): RedirectResponse
{
$request->authenticate();

$request->session()->regenerate();

return redirect()->intended(route('customer.licenses'));
}

public function logout(Request $request): RedirectResponse
{
Auth::logout();

$request->session()->invalidate();
$request->session()->regenerateToken();

return redirect()->route('customer.login');
}

public function showForgotPassword(): View
{
return view('auth.forgot-password');
}

public function sendPasswordResetLink(Request $request): RedirectResponse
{
$request->validate([
'email' => ['required', 'email:rfc,dns'],
]);

$status = \Illuminate\Support\Facades\Password::sendResetLink(
$request->only('email')
);

return $status === \Illuminate\Auth\Passwords\PasswordBroker::RESET_LINK_SENT
? back()->with(['status' => __($status)])
: back()->withErrors(['email' => __($status)]);
}

public function showResetPassword(string $token): View
{
return view('auth.reset-password', ['token' => $token]);
}

public function resetPassword(Request $request): RedirectResponse
{
$request->validate([
'token' => ['required'],
'email' => ['required', 'email:rfc,dns'],
'password' => ['required', 'min:8', 'confirmed'],
]);

$status = \Illuminate\Support\Facades\Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password) {
$user->forceFill([
'password' => $password,
]);

$user->save();
}
);

return $status === \Illuminate\Auth\Passwords\PasswordBroker::PASSWORD_RESET
? redirect()->route('customer.login')->with('status', __($status))
: back()->withErrors(['email' => [__($status)]]);
}
}
52 changes: 52 additions & 0 deletions app/Http/Controllers/CustomerLicenseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;

class CustomerLicenseController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}

public function index(): View
{
$user = Auth::user();
$licenses = $user->licenses()->orderBy('created_at', 'desc')->get();

return view('customer.licenses.index', compact('licenses'));
}

public function show(string $licenseKey): View
{
$user = Auth::user();
$license = $user->licenses()
->with('subLicenses')
->where('key', $licenseKey)
->firstOrFail();

return view('customer.licenses.show', compact('license'));
}

public function update(Request $request, string $licenseKey): RedirectResponse
{
$user = Auth::user();
$license = $user->licenses()->where('key', $licenseKey)->firstOrFail();

$request->validate([
'name' => ['nullable', 'string', 'max:255'],
]);

$license->update([
'name' => $request->name,
]);

return redirect()->route('customer.licenses.show', $licenseKey)
->with('success', 'License name updated successfully!');
}
}
Loading