added crypto pm and activation key system
This commit is contained in:
154
app/Filament/Pages/GenerateActivationKeys.php
Normal file
154
app/Filament/Pages/GenerateActivationKeys.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use App\Models\ActivationKey;
|
||||
use App\Models\Plan;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Pages\Page;
|
||||
use Filament\Tables\Actions\BulkAction;
|
||||
use Filament\Tables\Columns\BooleanColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Concerns\InteractsWithTable;
|
||||
use Filament\Tables\Contracts\HasTable;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Illuminate\Support\Collection;
|
||||
use Response;
|
||||
use Str;
|
||||
use Filament\Notifications\Notification;
|
||||
|
||||
class GenerateActivationKeys extends Page implements HasForms, HasTable
|
||||
{
|
||||
use InteractsWithForms, InteractsWithTable;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-key';
|
||||
protected static string $view = 'filament.pages.generate-activation-keys';
|
||||
protected static ?string $navigationGroup = 'Admin';
|
||||
protected static ?string $title = 'Activation Keys';
|
||||
|
||||
public $plan_id;
|
||||
public $quantity = 1;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->form->fill();
|
||||
}
|
||||
|
||||
protected function getFormSchema(): array
|
||||
{
|
||||
return [
|
||||
Select::make('plan_id')
|
||||
->label('Select Plan')
|
||||
->options(Plan::all()->pluck('name', 'id'))
|
||||
->required(),
|
||||
|
||||
TextInput::make('quantity')
|
||||
->numeric()
|
||||
->minValue(1)
|
||||
->maxValue(100)
|
||||
->default(1)
|
||||
->required(),
|
||||
];
|
||||
}
|
||||
|
||||
public function generate()
|
||||
{
|
||||
$data = $this->form->getState();
|
||||
$plan = Plan::findOrFail($data['plan_id']);
|
||||
|
||||
for ($i = 0; $i < $data['quantity']; $i++) {
|
||||
ActivationKey::create([
|
||||
'price_id' => $plan->pricing_id,
|
||||
'activation_key' => strtoupper('Z'.Str::random(16)),
|
||||
'is_activated' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
Notification::make()
|
||||
->title("{$data['quantity']} activation key(s) generated.")
|
||||
->success()
|
||||
->send();
|
||||
$this->form->fill(); // Reset form
|
||||
}
|
||||
|
||||
// === Table Setup ===
|
||||
protected function getTableQuery(): \Illuminate\Database\Eloquent\Builder
|
||||
{
|
||||
return ActivationKey::query()->latest();
|
||||
}
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
TextColumn::make('activation_key')
|
||||
->label('Key')
|
||||
->copyable(),
|
||||
|
||||
BooleanColumn::make('is_activated'),
|
||||
|
||||
TextColumn::make('user.email')
|
||||
->label('Activated By'),
|
||||
|
||||
TextColumn::make('billing_interval')
|
||||
->label('Interval')
|
||||
->getStateUsing(function ($record) {
|
||||
$isMonthly = \App\Models\Plan::where('pricing_id', $record->price_id)->value('monthly_billing');
|
||||
return $isMonthly ? 'Monthly' : 'Yearly';
|
||||
}),
|
||||
|
||||
|
||||
TextColumn::make('created_at')
|
||||
->dateTime(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getTableFilters(): array
|
||||
{
|
||||
return [
|
||||
SelectFilter::make('is_activated')
|
||||
->options([
|
||||
true => 'Activated',
|
||||
false => 'Not Activated',
|
||||
]),
|
||||
SelectFilter::make('price_id')
|
||||
->label('Plan')
|
||||
->options(
|
||||
Plan::pluck('name', 'pricing_id')
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
protected function getTableBulkActions(): array
|
||||
{
|
||||
return [
|
||||
BulkAction::make('Download Keys')
|
||||
->action(fn (Collection $records) => $this->downloadKeys($records))
|
||||
->deselectRecordsAfterCompletion()
|
||||
->requiresConfirmation(),
|
||||
];
|
||||
}
|
||||
|
||||
public function downloadKeys(Collection $records)
|
||||
{
|
||||
$text = $records->pluck('activation_key')->implode("\n");
|
||||
|
||||
$filename = 'activation_keys_' . now()->timestamp . '.txt';
|
||||
// Store the file in the 'public' directory or a subdirectory within 'public'
|
||||
$path = public_path("activation/{$filename}");
|
||||
|
||||
// Make sure the 'activation' folder exists, create it if it doesn't
|
||||
if (!file_exists(public_path('activation'))) {
|
||||
mkdir(public_path('activation'), 0777, true);
|
||||
}
|
||||
|
||||
// Write the contents to the file
|
||||
file_put_contents($path, $text);
|
||||
|
||||
// Return the response that allows users to download the file directly
|
||||
return response()->download($path)->deleteFileAfterSend(true);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user