init
This commit is contained in:
232 files changed
+25416
No files matched your search
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Jobs\SyncCafeBazaarApp;
|
||||
use App\Models\MobileApp;
|
||||
use App\Services\CafeBazaar\SyncCafeBazaarAppAction;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
#[Signature('bazaar:sync {--app= : Synchronize one app by package name} {--now : Run immediately instead of queueing} {--force : Ignore normal freshness intervals}')]
|
||||
#[Description('Synchronize Cafe Bazaar purchases, subscriptions, reviews, and release states')]
|
||||
class SyncCafeBazaarAppsCommand extends Command
|
||||
{
|
||||
public function handle(SyncCafeBazaarAppAction $sync): int
|
||||
{
|
||||
$packageName = $this->option('app');
|
||||
$query = MobileApp::query()->where('is_active', true);
|
||||
|
||||
if (is_string($packageName) && $packageName !== '') {
|
||||
$query->where('package_name', $packageName);
|
||||
} else {
|
||||
$query->where('bazaar_sync_enabled', true);
|
||||
}
|
||||
|
||||
$apps = $query->get()->filter(function (MobileApp $app) use ($packageName): bool {
|
||||
if (is_string($packageName) && $packageName !== '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $app->bazaar_last_synced_at === null
|
||||
|| $app->bazaar_last_synced_at->lte(now()->subMinutes($app->bazaar_sync_interval_minutes));
|
||||
});
|
||||
|
||||
if ($apps->isEmpty()) {
|
||||
$this->components->info('No Cafe Bazaar app synchronization is due.');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
foreach ($apps as $app) {
|
||||
if ($this->option('now')) {
|
||||
$result = $sync->execute($app, (bool) $this->option('force'));
|
||||
$this->line("{$app->name}: {$result['status']}");
|
||||
} else {
|
||||
SyncCafeBazaarApp::dispatch($app->id, (bool) $this->option('force'));
|
||||
$this->line("{$app->name}: queued");
|
||||
}
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Pages\Tenancy;
|
||||
|
||||
use App\Jobs\SyncCafeBazaarApp;
|
||||
use App\Models\MobileApp;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Forms\Components\KeyValue;
|
||||
use Filament\Forms\Components\Placeholder;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Pages\Tenancy\EditTenantProfile;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
|
||||
class EditMobileAppProfile extends EditTenantProfile
|
||||
{
|
||||
public static function getLabel(): string
|
||||
{
|
||||
return 'App settings';
|
||||
}
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
Section::make('App identity and availability')->schema([
|
||||
TextInput::make('name')->required()->maxLength(255),
|
||||
TextInput::make('package_name')->required()->regex('/^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+$/')->unique(MobileApp::class, ignoreRecord: true)->maxLength(255),
|
||||
Toggle::make('is_active')->required(),
|
||||
Toggle::make('maintenance_mode')->required(),
|
||||
TextInput::make('latest_version_name')->maxLength(255),
|
||||
TextInput::make('latest_version_code')->numeric()->minValue(1),
|
||||
TextInput::make('minimum_version_code')->numeric()->minValue(1),
|
||||
TextInput::make('store_url')->url(),
|
||||
TextInput::make('privacy_url')->url(),
|
||||
TextInput::make('support_email')->email(),
|
||||
])->columns(2)->columnSpanFull(),
|
||||
Section::make('API access')->description('Use the client key in Android requests. Keep the management key only on trusted servers and in this panel.')->schema([
|
||||
TextInput::make('client_key')->password()->revealable()->required(),
|
||||
TextInput::make('management_key')->password()->revealable()->required(),
|
||||
])->columns(2)->columnSpanFull(),
|
||||
Section::make('Remote configuration')->schema([
|
||||
KeyValue::make('configuration')->keyLabel('Key')->valueLabel('Value')->columnSpanFull(),
|
||||
KeyValue::make('feature_flags')->keyLabel('Feature')->valueLabel('Enabled or value')->columnSpanFull(),
|
||||
])->columnSpanFull(),
|
||||
Section::make('Cafe Bazaar server API')->description('Per-app tokens from Pishkhan. Secrets are encrypted in the database.')->schema([
|
||||
TextInput::make('bazaar_api_secret')->password()->revealable()->label('Billing and Pishkhan API secret'),
|
||||
TextInput::make('bazaar_comments_header')->label('Comments API header name')->default('CAFEBAZAAR-PISHKHAN-API-SECRET'),
|
||||
TextInput::make('bazaar_comments_secret')->password()->revealable()->label('Comments API secret'),
|
||||
Textarea::make('bazaar_billing_public_key')->rows(5)->label('RSA billing public key')->columnSpanFull(),
|
||||
])->columns(2)->columnSpanFull(),
|
||||
Section::make('Cafe Bazaar synchronization')->description('Refreshes known purchases and subscriptions, imports reviews, and reconciles release state in the background.')->schema([
|
||||
Toggle::make('bazaar_sync_enabled')->label('Automatic synchronization')->required(),
|
||||
TextInput::make('bazaar_sync_interval_minutes')->label('Subscription sync interval (minutes)')->numeric()->minValue(5)->maxValue(1440)->required(),
|
||||
Placeholder::make('bazaar_sync_status')->label('Last result')->content(fn (?MobileApp $record): string => ucfirst($record?->bazaar_sync_status ?? 'idle')),
|
||||
Placeholder::make('bazaar_last_synced_at')->label('Last completed')->content(fn (?MobileApp $record): string => $record?->bazaar_last_synced_at?->diffForHumans() ?? 'Never'),
|
||||
Placeholder::make('bazaar_sync_error')->label('Last error')->content(fn (?MobileApp $record): string => $record?->bazaar_sync_error ?? 'None')->columnSpanFull(),
|
||||
])->columns(2)->columnSpanFull(),
|
||||
Section::make('Login with Bazaar')->schema([
|
||||
TextInput::make('bazaar_login_client_id')->label('Client ID'),
|
||||
TextInput::make('bazaar_login_client_secret')->password()->revealable()->label('Client secret'),
|
||||
TextInput::make('bazaar_login_redirect_url')->url()->label('Registered redirect URL')->columnSpanFull(),
|
||||
])->columns(2)->columnSpanFull(),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Action::make('syncBazaar')
|
||||
->label('Sync Bazaar now')
|
||||
->icon(Heroicon::ArrowPath)
|
||||
->requiresConfirmation()
|
||||
->action(function (): void {
|
||||
/** @var MobileApp $app */
|
||||
$app = Filament::getTenant();
|
||||
SyncCafeBazaarApp::dispatch($app->id, true);
|
||||
|
||||
Notification::make()
|
||||
->title('Cafe Bazaar synchronization queued')
|
||||
->body('The dashboard status will update when the background worker finishes.')
|
||||
->success()
|
||||
->send();
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Pages\Tenancy;
|
||||
|
||||
use App\Models\MobileApp;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Pages\Tenancy\RegisterTenant;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class RegisterMobileApp extends RegisterTenant
|
||||
{
|
||||
public static function getLabel(): string
|
||||
{
|
||||
return 'Add Android app';
|
||||
}
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
TextInput::make('name')->required()->maxLength(255),
|
||||
TextInput::make('package_name')->required()->regex('/^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+$/')->unique(MobileApp::class)->maxLength(255),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function handleRegistration(array $data): MobileApp
|
||||
{
|
||||
$mobileApp = MobileApp::create([
|
||||
...$data,
|
||||
'client_key' => Str::random(48),
|
||||
'management_key' => Str::random(64),
|
||||
'store_url' => 'https://cafebazaar.ir/app/'.$data['package_name'],
|
||||
]);
|
||||
|
||||
$mobileApp->users()->attach(auth()->user(), ['role' => 'owner']);
|
||||
|
||||
return $mobileApp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarApiRequests;
|
||||
|
||||
use App\Filament\Resources\BazaarApiRequests\Pages\ListBazaarApiRequests;
|
||||
use App\Filament\Resources\BazaarApiRequests\Pages\ViewBazaarApiRequest;
|
||||
use App\Filament\Resources\BazaarApiRequests\Schemas\BazaarApiRequestForm;
|
||||
use App\Filament\Resources\BazaarApiRequests\Schemas\BazaarApiRequestInfolist;
|
||||
use App\Filament\Resources\BazaarApiRequests\Tables\BazaarApiRequestsTable;
|
||||
use App\Models\BazaarApiRequest;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class BazaarApiRequestResource extends Resource
|
||||
{
|
||||
protected static ?string $model = BazaarApiRequest::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Cafe Bazaar';
|
||||
|
||||
protected static ?string $navigationLabel = 'API activity';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return BazaarApiRequestForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function infolist(Schema $schema): Schema
|
||||
{
|
||||
return BazaarApiRequestInfolist::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return BazaarApiRequestsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListBazaarApiRequests::route('/'),
|
||||
'view' => ViewBazaarApiRequest::route('/{record}'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarApiRequests\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarApiRequests\BazaarApiRequestResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateBazaarApiRequest extends CreateRecord
|
||||
{
|
||||
protected static string $resource = BazaarApiRequestResource::class;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarApiRequests\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarApiRequests\BazaarApiRequestResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditBazaarApiRequest extends EditRecord
|
||||
{
|
||||
protected static string $resource = BazaarApiRequestResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
ViewAction::make(),
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarApiRequests\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarApiRequests\BazaarApiRequestResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListBazaarApiRequests extends ListRecords
|
||||
{
|
||||
protected static string $resource = BazaarApiRequestResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarApiRequests\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarApiRequests\BazaarApiRequestResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewBazaarApiRequest extends ViewRecord
|
||||
{
|
||||
protected static string $resource = BazaarApiRequestResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarApiRequests\Schemas;
|
||||
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarApiRequestForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
//
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarApiRequests\Schemas;
|
||||
|
||||
use Filament\Infolists\Components\IconEntry;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarApiRequestInfolist
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextEntry::make('operation')->badge(),
|
||||
TextEntry::make('method')->badge(),
|
||||
TextEntry::make('url')->columnSpanFull(),
|
||||
TextEntry::make('status_code'),
|
||||
IconEntry::make('successful')->boolean(),
|
||||
TextEntry::make('duration_ms')->suffix(' ms'),
|
||||
TextEntry::make('request_metadata')->formatStateUsing(fn (?array $state): string => json_encode($state, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))->columnSpanFull(),
|
||||
TextEntry::make('response_body')->formatStateUsing(fn (?array $state): string => json_encode($state, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))->columnSpanFull(),
|
||||
TextEntry::make('error_message')->columnSpanFull(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarApiRequests\Tables;
|
||||
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class BazaarApiRequestsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('operation')->badge()->searchable()->sortable(),
|
||||
TextColumn::make('method')->badge(),
|
||||
TextColumn::make('status_code')->label('Status')->sortable(),
|
||||
IconColumn::make('successful')->boolean(),
|
||||
TextColumn::make('duration_ms')->label('Duration')->suffix(' ms')->sortable(),
|
||||
TextColumn::make('created_at')->since()->sortable(),
|
||||
])
|
||||
->filters([
|
||||
TernaryFilter::make('successful'),
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
])
|
||||
->defaultSort('created_at', 'desc');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarPurchases;
|
||||
|
||||
use App\Filament\Resources\BazaarPurchases\Pages\ListBazaarPurchases;
|
||||
use App\Filament\Resources\BazaarPurchases\Pages\ViewBazaarPurchase;
|
||||
use App\Filament\Resources\BazaarPurchases\Schemas\BazaarPurchaseForm;
|
||||
use App\Filament\Resources\BazaarPurchases\Schemas\BazaarPurchaseInfolist;
|
||||
use App\Filament\Resources\BazaarPurchases\Tables\BazaarPurchasesTable;
|
||||
use App\Models\BazaarPurchase;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class BazaarPurchaseResource extends Resource
|
||||
{
|
||||
protected static ?string $model = BazaarPurchase::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Cafe Bazaar';
|
||||
|
||||
protected static ?string $navigationLabel = 'Purchases';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return BazaarPurchaseForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function infolist(Schema $schema): Schema
|
||||
{
|
||||
return BazaarPurchaseInfolist::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return BazaarPurchasesTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListBazaarPurchases::route('/'),
|
||||
'view' => ViewBazaarPurchase::route('/{record}'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarPurchases\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarPurchases\BazaarPurchaseResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateBazaarPurchase extends CreateRecord
|
||||
{
|
||||
protected static string $resource = BazaarPurchaseResource::class;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarPurchases\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarPurchases\BazaarPurchaseResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditBazaarPurchase extends EditRecord
|
||||
{
|
||||
protected static string $resource = BazaarPurchaseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
ViewAction::make(),
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarPurchases\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarPurchases\BazaarPurchaseResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListBazaarPurchases extends ListRecords
|
||||
{
|
||||
protected static string $resource = BazaarPurchaseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarPurchases\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarPurchases\BazaarPurchaseResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewBazaarPurchase extends ViewRecord
|
||||
{
|
||||
protected static string $resource = BazaarPurchaseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarPurchases\Schemas;
|
||||
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarPurchaseForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
//
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarPurchases\Schemas;
|
||||
|
||||
use Filament\Infolists\Components\IconEntry;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarPurchaseInfolist
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextEntry::make('product_id'),
|
||||
TextEntry::make('purchase_token')->copyable()->columnSpanFull(),
|
||||
TextEntry::make('status')->badge(),
|
||||
IconEntry::make('is_consumed')->boolean(),
|
||||
TextEntry::make('purchased_at')->dateTime(),
|
||||
TextEntry::make('validated_at')->dateTime(),
|
||||
TextEntry::make('bazaar_response')->formatStateUsing(fn (?array $state): string => json_encode($state, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))->columnSpanFull(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarPurchases\Tables;
|
||||
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class BazaarPurchasesTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('product_id')->searchable()->sortable(),
|
||||
TextColumn::make('status')->badge()->sortable(),
|
||||
IconColumn::make('is_consumed')->boolean(),
|
||||
TextColumn::make('purchased_at')->dateTime()->sortable(),
|
||||
TextColumn::make('validated_at')->since()->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('status')->options([
|
||||
'purchased' => 'Purchased',
|
||||
'refunded' => 'Refunded',
|
||||
'unknown' => 'Unknown',
|
||||
]),
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
])
|
||||
->defaultSort('created_at', 'desc');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReleases;
|
||||
|
||||
use App\Filament\Resources\BazaarReleases\Pages\ListBazaarReleases;
|
||||
use App\Filament\Resources\BazaarReleases\Pages\ViewBazaarRelease;
|
||||
use App\Filament\Resources\BazaarReleases\Schemas\BazaarReleaseForm;
|
||||
use App\Filament\Resources\BazaarReleases\Schemas\BazaarReleaseInfolist;
|
||||
use App\Filament\Resources\BazaarReleases\Tables\BazaarReleasesTable;
|
||||
use App\Models\BazaarRelease;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class BazaarReleaseResource extends Resource
|
||||
{
|
||||
protected static ?string $model = BazaarRelease::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Cafe Bazaar';
|
||||
|
||||
protected static ?string $navigationLabel = 'Publishing';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return BazaarReleaseForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function infolist(Schema $schema): Schema
|
||||
{
|
||||
return BazaarReleaseInfolist::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return BazaarReleasesTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListBazaarReleases::route('/'),
|
||||
'view' => ViewBazaarRelease::route('/{record}'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReleases\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarReleases\BazaarReleaseResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateBazaarRelease extends CreateRecord
|
||||
{
|
||||
protected static string $resource = BazaarReleaseResource::class;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReleases\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarReleases\BazaarReleaseResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditBazaarRelease extends EditRecord
|
||||
{
|
||||
protected static string $resource = BazaarReleaseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
ViewAction::make(),
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReleases\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarReleases\BazaarReleaseResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListBazaarReleases extends ListRecords
|
||||
{
|
||||
protected static string $resource = BazaarReleaseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReleases\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarReleases\BazaarReleaseResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewBazaarRelease extends ViewRecord
|
||||
{
|
||||
protected static string $resource = BazaarReleaseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReleases\Schemas;
|
||||
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarReleaseForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
//
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReleases\Schemas;
|
||||
|
||||
use Filament\Infolists\Components\IconEntry;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarReleaseInfolist
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextEntry::make('bazaar_release_id')->label('Bazaar release ID'),
|
||||
TextEntry::make('status')->badge(),
|
||||
TextEntry::make('artifact_type')->badge(),
|
||||
TextEntry::make('staged_rollout_percentage')->suffix('%'),
|
||||
IconEntry::make('auto_publish')->boolean(),
|
||||
TextEntry::make('changelog_fa')->columnSpanFull(),
|
||||
TextEntry::make('changelog_en')->columnSpanFull(),
|
||||
TextEntry::make('developer_note')->columnSpanFull(),
|
||||
TextEntry::make('bazaar_response')->formatStateUsing(fn (?array $state): string => json_encode($state, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))->columnSpanFull(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReleases\Tables;
|
||||
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class BazaarReleasesTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('bazaar_release_id')->label('Bazaar ID')->sortable(),
|
||||
TextColumn::make('status')->badge()->sortable(),
|
||||
TextColumn::make('artifact_type')->badge(),
|
||||
TextColumn::make('staged_rollout_percentage')->suffix('%')->sortable(),
|
||||
IconColumn::make('auto_publish')->boolean(),
|
||||
TextColumn::make('committed_at')->dateTime()->sortable(),
|
||||
TextColumn::make('published_at')->dateTime()->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('status')->options([
|
||||
'draft' => 'Draft',
|
||||
'awaiting_bin' => 'Awaiting BIN',
|
||||
'processing_bundle' => 'Processing bundle',
|
||||
'artifact_uploaded' => 'Artifact uploaded',
|
||||
'committed' => 'Committed',
|
||||
'published' => 'Published',
|
||||
'deleted' => 'Deleted',
|
||||
]),
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
])
|
||||
->defaultSort('created_at', 'desc');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReviews;
|
||||
|
||||
use App\Filament\Resources\BazaarReviews\Pages\ListBazaarReviews;
|
||||
use App\Filament\Resources\BazaarReviews\Pages\ViewBazaarReview;
|
||||
use App\Filament\Resources\BazaarReviews\Schemas\BazaarReviewForm;
|
||||
use App\Filament\Resources\BazaarReviews\Schemas\BazaarReviewInfolist;
|
||||
use App\Filament\Resources\BazaarReviews\Tables\BazaarReviewsTable;
|
||||
use App\Models\BazaarReview;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class BazaarReviewResource extends Resource
|
||||
{
|
||||
protected static ?string $model = BazaarReview::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Cafe Bazaar';
|
||||
|
||||
protected static ?string $navigationLabel = 'Reviews';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return BazaarReviewForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function infolist(Schema $schema): Schema
|
||||
{
|
||||
return BazaarReviewInfolist::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return BazaarReviewsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListBazaarReviews::route('/'),
|
||||
'view' => ViewBazaarReview::route('/{record}'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReviews\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarReviews\BazaarReviewResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateBazaarReview extends CreateRecord
|
||||
{
|
||||
protected static string $resource = BazaarReviewResource::class;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReviews\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarReviews\BazaarReviewResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditBazaarReview extends EditRecord
|
||||
{
|
||||
protected static string $resource = BazaarReviewResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
ViewAction::make(),
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReviews\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarReviews\BazaarReviewResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListBazaarReviews extends ListRecords
|
||||
{
|
||||
protected static string $resource = BazaarReviewResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReviews\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarReviews\BazaarReviewResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewBazaarReview extends ViewRecord
|
||||
{
|
||||
protected static string $resource = BazaarReviewResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReviews\Schemas;
|
||||
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarReviewForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
//
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReviews\Schemas;
|
||||
|
||||
use Filament\Infolists\Components\IconEntry;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarReviewInfolist
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextEntry::make('bazaar_review_id')->label('Bazaar review ID'),
|
||||
TextEntry::make('rate')->badge(),
|
||||
TextEntry::make('text')->columnSpanFull(),
|
||||
TextEntry::make('app_version_code'),
|
||||
IconEntry::make('is_edited')->boolean(),
|
||||
TextEntry::make('published_at')->dateTime(),
|
||||
TextEntry::make('developer_reply_text')->columnSpanFull(),
|
||||
TextEntry::make('developer_reply_email'),
|
||||
TextEntry::make('developer_replied_at')->dateTime(),
|
||||
TextEntry::make('bazaar_response')->formatStateUsing(fn (?array $state): string => json_encode($state, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))->columnSpanFull(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarReviews\Tables;
|
||||
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class BazaarReviewsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('rate')->badge()->sortable(),
|
||||
TextColumn::make('text')->limit(70)->searchable()->wrap(),
|
||||
TextColumn::make('app_version_code')->label('App version')->sortable(),
|
||||
IconColumn::make('replied')->label('Replied')->state(fn ($record): bool => filled($record->developer_reply_text))->boolean(),
|
||||
TextColumn::make('published_at')->dateTime()->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('rate')->options([1 => '1 star', 2 => '2 stars', 3 => '3 stars', 4 => '4 stars', 5 => '5 stars']),
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
])
|
||||
->defaultSort('published_at', 'desc');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarSubscriptions;
|
||||
|
||||
use App\Filament\Resources\BazaarSubscriptions\Pages\ListBazaarSubscriptions;
|
||||
use App\Filament\Resources\BazaarSubscriptions\Pages\ViewBazaarSubscription;
|
||||
use App\Filament\Resources\BazaarSubscriptions\Schemas\BazaarSubscriptionForm;
|
||||
use App\Filament\Resources\BazaarSubscriptions\Schemas\BazaarSubscriptionInfolist;
|
||||
use App\Filament\Resources\BazaarSubscriptions\Tables\BazaarSubscriptionsTable;
|
||||
use App\Models\BazaarSubscription;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class BazaarSubscriptionResource extends Resource
|
||||
{
|
||||
protected static ?string $model = BazaarSubscription::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Cafe Bazaar';
|
||||
|
||||
protected static ?string $navigationLabel = 'Subscriptions';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return BazaarSubscriptionForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function infolist(Schema $schema): Schema
|
||||
{
|
||||
return BazaarSubscriptionInfolist::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return BazaarSubscriptionsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListBazaarSubscriptions::route('/'),
|
||||
'view' => ViewBazaarSubscription::route('/{record}'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarSubscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarSubscriptions\BazaarSubscriptionResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateBazaarSubscription extends CreateRecord
|
||||
{
|
||||
protected static string $resource = BazaarSubscriptionResource::class;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarSubscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarSubscriptions\BazaarSubscriptionResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditBazaarSubscription extends EditRecord
|
||||
{
|
||||
protected static string $resource = BazaarSubscriptionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
ViewAction::make(),
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarSubscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarSubscriptions\BazaarSubscriptionResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListBazaarSubscriptions extends ListRecords
|
||||
{
|
||||
protected static string $resource = BazaarSubscriptionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarSubscriptions\Pages;
|
||||
|
||||
use App\Filament\Resources\BazaarSubscriptions\BazaarSubscriptionResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewBazaarSubscription extends ViewRecord
|
||||
{
|
||||
protected static string $resource = BazaarSubscriptionResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarSubscriptions\Schemas;
|
||||
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarSubscriptionForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
//
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarSubscriptions\Schemas;
|
||||
|
||||
use Filament\Infolists\Components\IconEntry;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class BazaarSubscriptionInfolist
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextEntry::make('subscription_id'),
|
||||
TextEntry::make('purchase_token')->copyable()->columnSpanFull(),
|
||||
TextEntry::make('linked_subscription_token')->copyable()->columnSpanFull(),
|
||||
TextEntry::make('status')->badge(),
|
||||
IconEntry::make('auto_renewing')->boolean(),
|
||||
TextEntry::make('starts_at')->dateTime(),
|
||||
TextEntry::make('valid_until')->dateTime(),
|
||||
TextEntry::make('bazaar_response')->formatStateUsing(fn (?array $state): string => json_encode($state, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))->columnSpanFull(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\BazaarSubscriptions\Tables;
|
||||
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class BazaarSubscriptionsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('subscription_id')->searchable()->sortable(),
|
||||
TextColumn::make('status')->badge()->sortable(),
|
||||
IconColumn::make('auto_renewing')->boolean(),
|
||||
TextColumn::make('starts_at')->dateTime()->sortable(),
|
||||
TextColumn::make('valid_until')->dateTime()->sortable(),
|
||||
TextColumn::make('validated_at')->since()->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('status')->options([
|
||||
'active' => 'Active',
|
||||
'expired' => 'Expired',
|
||||
'cancelled' => 'Cancelled',
|
||||
'refunded' => 'Refunded',
|
||||
'unknown' => 'Unknown',
|
||||
]),
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
])
|
||||
->defaultSort('created_at', 'desc');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\MobileApp;
|
||||
use Filament\Facades\Filament;
|
||||
use Filament\Widgets\StatsOverviewWidget;
|
||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||
|
||||
class AppOverview extends StatsOverviewWidget
|
||||
{
|
||||
protected function getStats(): array
|
||||
{
|
||||
/** @var MobileApp $app */
|
||||
$app = Filament::getTenant();
|
||||
$apiRequestCount = $app->bazaarApiRequests()->count();
|
||||
$successRate = $apiRequestCount === 0
|
||||
? '—'
|
||||
: round($app->bazaarApiRequests()->where('successful', true)->count() / $apiRequestCount * 100, 1).'%';
|
||||
|
||||
return [
|
||||
Stat::make('Bazaar sync', ucfirst($app->bazaar_sync_status))
|
||||
->description($app->bazaar_last_synced_at?->diffForHumans() ?? 'Never synchronized'),
|
||||
Stat::make('Validated purchases', $app->bazaarPurchases()->count()),
|
||||
Stat::make('Active subscriptions', $app->bazaarSubscriptions()->where('status', 'active')->count()),
|
||||
Stat::make('Reviews', $app->bazaarReviews()->count()),
|
||||
Stat::make('Bazaar API success rate', $successRate),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\MobileApp;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AppConfigurationController extends Controller
|
||||
{
|
||||
public function show(Request $request): JsonResponse
|
||||
{
|
||||
/** @var MobileApp $mobileApp */
|
||||
$mobileApp = $request->attributes->get('mobileApp');
|
||||
|
||||
return response()->json(['data' => [
|
||||
'name' => $mobileApp->name,
|
||||
'package_name' => $mobileApp->package_name,
|
||||
'maintenance_mode' => $mobileApp->maintenance_mode,
|
||||
'version' => [
|
||||
'latest_name' => $mobileApp->latest_version_name,
|
||||
'latest_code' => $mobileApp->latest_version_code,
|
||||
'minimum_code' => $mobileApp->minimum_version_code,
|
||||
'store_url' => $mobileApp->store_url ?: 'https://cafebazaar.ir/app/'.$mobileApp->package_name,
|
||||
],
|
||||
'support' => [
|
||||
'email' => $mobileApp->support_email,
|
||||
'privacy_url' => $mobileApp->privacy_url,
|
||||
],
|
||||
'configuration' => $mobileApp->configuration ?? [],
|
||||
'feature_flags' => $mobileApp->feature_flags ?? [],
|
||||
'bazaar' => [
|
||||
'package_name' => $mobileApp->package_name,
|
||||
'store_url' => $mobileApp->store_url ?: 'https://cafebazaar.ir/app/'.$mobileApp->package_name,
|
||||
'login_enabled' => filled($mobileApp->bazaar_login_client_id),
|
||||
'billing_enabled' => filled($mobileApp->bazaar_api_secret),
|
||||
'updater_intent' => 'bazaar://details?id='.$mobileApp->package_name,
|
||||
],
|
||||
]]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\BazaarActiveSubscriptionsRequest;
|
||||
use App\Http\Requests\BazaarSubscriptionRequest;
|
||||
use App\Http\Requests\ConsumeBazaarPurchaseRequest;
|
||||
use App\Http\Requests\ValidateBazaarPurchaseRequest;
|
||||
use App\Http\Requests\VerifyBazaarSignatureRequest;
|
||||
use App\Models\BazaarPurchase;
|
||||
use App\Models\BazaarSubscription;
|
||||
use App\Models\MobileApp;
|
||||
use App\Services\CafeBazaar\BazaarPurchaseRecorder;
|
||||
use App\Services\CafeBazaar\BazaarSignatureVerifier;
|
||||
use App\Services\CafeBazaar\BazaarSubscriptionRecorder;
|
||||
use App\Services\CafeBazaar\CafeBazaarClient;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class BazaarBillingController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CafeBazaarClient $bazaar,
|
||||
private readonly BazaarPurchaseRecorder $purchases,
|
||||
private readonly BazaarSubscriptionRecorder $subscriptions,
|
||||
private readonly BazaarSignatureVerifier $signatures,
|
||||
) {}
|
||||
|
||||
public function validatePurchase(ValidateBazaarPurchaseRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->validatePurchase($app, $request->string('product_id')->toString(), $request->string('purchase_token')->toString());
|
||||
$this->purchases->record($app, $request->string('product_id')->toString(), $request->string('purchase_token')->toString(), $response);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function verifySignature(VerifyBazaarSignatureRequest $request): JsonResponse
|
||||
{
|
||||
$app = $request->attributes->get('mobileApp');
|
||||
|
||||
if (blank($app->bazaar_billing_public_key)) {
|
||||
return response()->json(['error' => [
|
||||
'code' => 'bazaar_public_key_missing',
|
||||
'message' => 'Configure the Bazaar billing public key in the app panel.',
|
||||
]], 422);
|
||||
}
|
||||
|
||||
return response()->json(['data' => [
|
||||
'valid' => $this->signatures->verify(
|
||||
$app->bazaar_billing_public_key,
|
||||
$request->string('original_json')->toString(),
|
||||
$request->string('signature')->toString(),
|
||||
),
|
||||
]]);
|
||||
}
|
||||
|
||||
public function consumePurchase(ConsumeBazaarPurchaseRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->consumePurchase($app, $request->string('purchase_token')->toString());
|
||||
|
||||
if ($response->successful()) {
|
||||
BazaarPurchase::query()->whereBelongsTo($app)->where('purchase_token_hash', hash('sha256', $request->string('purchase_token')->toString()))->update(['is_consumed' => true]);
|
||||
}
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function refundPurchase(ConsumeBazaarPurchaseRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->refundPurchase($app, $request->string('purchase_token')->toString());
|
||||
|
||||
if ($response->successful()) {
|
||||
BazaarPurchase::query()->whereBelongsTo($app)->where('purchase_token_hash', hash('sha256', $request->string('purchase_token')->toString()))->update(['status' => 'refunded']);
|
||||
BazaarSubscription::query()->whereBelongsTo($app)->where('purchase_token_hash', hash('sha256', $request->string('purchase_token')->toString()))->update(['status' => 'refunded']);
|
||||
}
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function subscription(BazaarSubscriptionRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->subscription($app, $request->string('subscription_id')->toString(), $request->string('purchase_token')->toString());
|
||||
$this->subscriptions->record($app, $request->string('subscription_id')->toString(), $request->string('purchase_token')->toString(), $response);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function activeSubscriptions(BazaarActiveSubscriptionsRequest $request): JsonResponse
|
||||
{
|
||||
return $this->bazaar
|
||||
->activeSubscriptions($this->app($request), $request->string('purchase_token')->toString())
|
||||
->toJsonResponse();
|
||||
}
|
||||
|
||||
public function cancelSubscription(BazaarSubscriptionRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->cancelSubscription($app, $request->string('subscription_id')->toString(), $request->string('purchase_token')->toString());
|
||||
|
||||
if ($response->successful()) {
|
||||
BazaarSubscription::query()->whereBelongsTo($app)->where('purchase_token_hash', hash('sha256', $request->string('purchase_token')->toString()))->update(['status' => 'cancelled']);
|
||||
}
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
private function app(ValidateBazaarPurchaseRequest|ConsumeBazaarPurchaseRequest|BazaarSubscriptionRequest|BazaarActiveSubscriptionsRequest $request): MobileApp
|
||||
{
|
||||
return $request->attributes->get('mobileApp');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\BazaarLoginExchangeRequest;
|
||||
use App\Http\Requests\BazaarLoginRefreshRequest;
|
||||
use App\Models\MobileApp;
|
||||
use App\Services\CafeBazaar\CafeBazaarClient;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Uri;
|
||||
|
||||
class BazaarLoginController extends Controller
|
||||
{
|
||||
public function __construct(private readonly CafeBazaarClient $bazaar) {}
|
||||
|
||||
public function authorizationUrl(Request $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$state = $request->string('state')->trim()->toString();
|
||||
|
||||
if ($state === '' || mb_strlen($state) > 255) {
|
||||
return response()->json(['error' => ['code' => 'invalid_state', 'message' => 'A state value of at most 255 characters is required.']], 422);
|
||||
}
|
||||
|
||||
$url = Uri::of('https://cafebazaar.ir/user/oauth')->withQuery([
|
||||
'redirect_url' => $app->bazaar_login_redirect_url,
|
||||
'client_id' => $app->bazaar_login_client_id,
|
||||
'state' => $state,
|
||||
]);
|
||||
|
||||
return response()->json(['data' => ['authorization_url' => (string) $url, 'state' => $state]]);
|
||||
}
|
||||
|
||||
public function exchange(BazaarLoginExchangeRequest $request): JsonResponse
|
||||
{
|
||||
return $this->bazaar->exchangeLoginCode($this->app($request), $request->string('code')->toString())->toJsonResponse();
|
||||
}
|
||||
|
||||
public function refresh(BazaarLoginRefreshRequest $request): JsonResponse
|
||||
{
|
||||
return $this->bazaar->refreshLoginToken($this->app($request), $request->string('refresh_token')->toString())->toJsonResponse();
|
||||
}
|
||||
|
||||
public function userInfo(Request $request): JsonResponse
|
||||
{
|
||||
$accessToken = $request->bearerToken();
|
||||
|
||||
if (! $accessToken) {
|
||||
return response()->json(['error' => ['code' => 'missing_bazaar_token', 'message' => 'Send the Bazaar access token as a Bearer token.']], 422);
|
||||
}
|
||||
|
||||
return $this->bazaar->userInfo($this->app($request), $accessToken)->toJsonResponse();
|
||||
}
|
||||
|
||||
private function app(Request $request): MobileApp
|
||||
{
|
||||
return $request->attributes->get('mobileApp');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\CommitBazaarReleaseRequest;
|
||||
use App\Http\Requests\UploadBazaarAabRequest;
|
||||
use App\Http\Requests\UploadBazaarApkRequest;
|
||||
use App\Http\Requests\UploadBazaarBinRequest;
|
||||
use App\Models\MobileApp;
|
||||
use App\Services\CafeBazaar\BazaarReleaseRecorder;
|
||||
use App\Services\CafeBazaar\CafeBazaarClient;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class BazaarReleaseController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CafeBazaarClient $bazaar,
|
||||
private readonly BazaarReleaseRecorder $releases,
|
||||
) {}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->createRelease($app);
|
||||
$this->releases->record($app, 'draft', $response);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function destroy(Request $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->deleteRelease($app);
|
||||
$this->releases->record($app, 'deleted', $response);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function uploadApk(UploadBazaarApkRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->uploadApk($app, $request->file('apk'), $request->string('architecture')->toString());
|
||||
$this->releases->record($app, 'artifact_uploaded', $response, ['artifact_type' => 'apk']);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function uploadAab(UploadBazaarAabRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->uploadAab($app, $request->file('aab'));
|
||||
$this->releases->record($app, 'awaiting_bin', $response, ['artifact_type' => 'aab']);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function uploadBin(UploadBazaarBinRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->uploadBin($app, $request->file('bin'));
|
||||
$this->releases->record($app, 'processing_bundle', $response, ['artifact_type' => 'aab']);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function bundleStatus(Request $request): JsonResponse
|
||||
{
|
||||
return $this->bazaar->bundleStatus($this->app($request))->toJsonResponse();
|
||||
}
|
||||
|
||||
public function commit(CommitBazaarReleaseRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->commitRelease($app, $request->validated());
|
||||
$this->releases->record($app, 'committed', $response, [...$request->validated(), 'committed_at' => now()]);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function last(Request $request, string $mobileApp, string $state): JsonResponse
|
||||
{
|
||||
abort_unless(in_array($state, ['committed', 'uncommitted', 'published'], true), 404);
|
||||
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->lastRelease($app, $state);
|
||||
$this->releases->record($app, $state, $response, $state === 'published' ? ['published_at' => now()] : []);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
private function app(Request $request): MobileApp
|
||||
{
|
||||
return $request->attributes->get('mobileApp');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\ListBazaarReviewsRequest;
|
||||
use App\Http\Requests\ReplyBazaarReviewRequest;
|
||||
use App\Models\MobileApp;
|
||||
use App\Services\CafeBazaar\BazaarReviewRecorder;
|
||||
use App\Services\CafeBazaar\CafeBazaarClient;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class BazaarReviewController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CafeBazaarClient $bazaar,
|
||||
private readonly BazaarReviewRecorder $reviews,
|
||||
) {}
|
||||
|
||||
public function index(ListBazaarReviewsRequest $request): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->reviews($app, $request->validated());
|
||||
$this->reviews->recordPage($app, $response);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
public function reply(ReplyBazaarReviewRequest $request, string $mobileApp, int $reviewId): JsonResponse
|
||||
{
|
||||
$app = $this->app($request);
|
||||
$response = $this->bazaar->replyToReview($app, $reviewId, $request->validated());
|
||||
$this->reviews->recordReply($app, $reviewId, $response);
|
||||
|
||||
return $response->toJsonResponse();
|
||||
}
|
||||
|
||||
private function app(ListBazaarReviewsRequest|ReplyBazaarReviewRequest $request): MobileApp
|
||||
{
|
||||
return $request->attributes->get('mobileApp');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\MobileApp;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class EnsureBazaarConfigured
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Closure(Request): (Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next, string $integration): Response
|
||||
{
|
||||
/** @var MobileApp $mobileApp */
|
||||
$mobileApp = $request->attributes->get('mobileApp');
|
||||
$requiredFields = match ($integration) {
|
||||
'billing', 'publishing' => ['bazaar_api_secret'],
|
||||
'reviews' => ['bazaar_comments_header', 'bazaar_comments_secret'],
|
||||
'login' => ['bazaar_login_client_id', 'bazaar_login_client_secret', 'bazaar_login_redirect_url'],
|
||||
default => [],
|
||||
};
|
||||
|
||||
$missingFields = collect($requiredFields)
|
||||
->filter(fn (string $field): bool => blank(Arr::get($mobileApp->getAttributes(), $field)) && blank($mobileApp->{$field}))
|
||||
->values()
|
||||
->all();
|
||||
|
||||
if ($missingFields !== []) {
|
||||
return response()->json(['error' => [
|
||||
'code' => 'bazaar_not_configured',
|
||||
'message' => "The Bazaar {$integration} integration is not configured for this app.",
|
||||
'missing_fields' => $missingFields,
|
||||
]], 422);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\MobileApp;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class RequireManagementKey
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Closure(Request): (Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
/** @var MobileApp $mobileApp */
|
||||
$mobileApp = $request->attributes->get('mobileApp');
|
||||
$providedKey = (string) $request->header('X-Management-Key');
|
||||
|
||||
if ($providedKey === '' || ! hash_equals($mobileApp->management_key, $providedKey)) {
|
||||
return response()->json(['error' => ['code' => 'invalid_management_key', 'message' => 'A valid X-Management-Key header is required.']], 403);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\MobileApp;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class ResolveMobileApp
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Closure(Request): (Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$mobileApp = MobileApp::query()
|
||||
->where('package_name', $request->route('packageName'))
|
||||
->first();
|
||||
|
||||
if (! $mobileApp) {
|
||||
return response()->json(['error' => ['code' => 'app_not_found', 'message' => 'The requested app does not exist.']], 404);
|
||||
}
|
||||
|
||||
if (! $mobileApp->is_active) {
|
||||
return response()->json(['error' => ['code' => 'app_disabled', 'message' => 'This app API is disabled.']], 403);
|
||||
}
|
||||
|
||||
$providedKey = (string) $request->header('X-App-Key');
|
||||
|
||||
if ($providedKey === '' || ! hash_equals($mobileApp->client_key, $providedKey)) {
|
||||
return response()->json(['error' => ['code' => 'invalid_app_key', 'message' => 'A valid X-App-Key header is required.']], 401);
|
||||
}
|
||||
|
||||
$request->attributes->set('mobileApp', $mobileApp);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class BazaarActiveSubscriptionsRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'purchase_token' => ['required', 'string', 'max:2048'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class BazaarLoginExchangeRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'code' => ['required', 'string', 'max:4096'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class BazaarLoginRefreshRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'refresh_token' => ['required', 'string', 'max:4096'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class BazaarSubscriptionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'subscription_id' => ['required', 'string', 'max:255'],
|
||||
'purchase_token' => ['required', 'string', 'max:2048'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class CommitBazaarReleaseRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'changelog_fa' => ['nullable', 'string', 'max:5000'],
|
||||
'changelog_en' => ['nullable', 'string', 'max:5000'],
|
||||
'developer_note' => ['nullable', 'string', 'max:5000'],
|
||||
'staged_rollout_percentage' => ['required', 'integer', 'between:1,100'],
|
||||
'auto_publish' => ['required', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ConsumeBazaarPurchaseRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'purchase_token' => ['required', 'string', 'max:2048'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ListBazaarReviewsRequest extends FormRequest
|
||||
{
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('is_replied')) {
|
||||
$this->merge([
|
||||
'is_replied' => filter_var($this->input('is_replied'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'publish_date_after' => ['nullable', 'date'],
|
||||
'publish_date_before' => ['nullable', 'date', 'after_or_equal:publish_date_after'],
|
||||
'rate_min' => ['nullable', 'integer', 'between:1,5'],
|
||||
'rate_max' => ['nullable', 'integer', 'between:1,5'],
|
||||
'app_version_code' => ['nullable', 'integer', 'min:1'],
|
||||
'sdk_version' => ['nullable', 'integer', 'min:1'],
|
||||
'is_replied' => ['nullable', 'boolean'],
|
||||
'cursor' => ['nullable', 'string', 'max:2048'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ReplyBazaarReviewRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'text' => ['required', 'string', 'max:1000'],
|
||||
'email' => ['nullable', 'email', 'max:255'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UploadBazaarAabRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'aab' => ['required', 'file', 'mimes:aab,zip', 'max:512000'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UploadBazaarApkRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'architecture' => ['required', Rule::in(['all', 'armeabi-v7a', 'arm64-v8a'])],
|
||||
'apk' => ['required', 'file', 'mimes:apk,zip', 'max:512000'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UploadBazaarBinRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'bin' => ['required', 'file', 'mimes:bin', 'max:51200'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ValidateBazaarPurchaseRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'product_id' => ['required', 'string', 'max:255'],
|
||||
'purchase_token' => ['required', 'string', 'max:2048'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class VerifyBazaarSignatureRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'original_json' => ['required', 'string', 'max:65535'],
|
||||
'signature' => ['required', 'string', 'max:8192'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\MobileApp;
|
||||
use App\Services\CafeBazaar\SyncCafeBazaarAppAction;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
use Illuminate\Queue\Middleware\RateLimited;
|
||||
use Throwable;
|
||||
|
||||
class SyncCafeBazaarApp implements ShouldBeUnique, ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public int $tries = 8;
|
||||
|
||||
public int $timeout = 300;
|
||||
|
||||
public int $uniqueFor = 600;
|
||||
|
||||
public function __construct(
|
||||
public readonly int $mobileAppId,
|
||||
public readonly bool $force = false,
|
||||
) {}
|
||||
|
||||
public function uniqueId(): string
|
||||
{
|
||||
return (string) $this->mobileAppId;
|
||||
}
|
||||
|
||||
/** @return array<int, int> */
|
||||
public function backoff(): array
|
||||
{
|
||||
return [60, 300, 900];
|
||||
}
|
||||
|
||||
/** @return array<int, object> */
|
||||
public function middleware(): array
|
||||
{
|
||||
return [(new RateLimited('cafe-bazaar-sync'))->releaseAfter(60)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(SyncCafeBazaarAppAction $sync): void
|
||||
{
|
||||
$app = MobileApp::query()->find($this->mobileAppId);
|
||||
|
||||
if ($app === null || (! $app->bazaar_sync_enabled && ! $this->force)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sync->execute($app, $this->force);
|
||||
}
|
||||
|
||||
public function failed(?Throwable $exception): void
|
||||
{
|
||||
MobileApp::query()->whereKey($this->mobileAppId)->update([
|
||||
'bazaar_sync_status' => 'failed',
|
||||
'bazaar_last_synced_at' => now(),
|
||||
'bazaar_sync_error' => $exception?->getMessage() ?? 'The Bazaar synchronization job failed.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\BazaarApiRequestFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable(['mobile_app_id', 'operation', 'method', 'url', 'status_code', 'successful', 'duration_ms', 'request_metadata', 'response_body', 'error_message'])]
|
||||
class BazaarApiRequest extends Model
|
||||
{
|
||||
/** @use HasFactory<BazaarApiRequestFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $attributes = ['successful' => false];
|
||||
|
||||
public function mobileApp(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MobileApp::class);
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'successful' => 'boolean',
|
||||
'request_metadata' => 'array',
|
||||
'response_body' => 'array',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\BazaarPurchaseFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable(['mobile_app_id', 'product_id', 'purchase_token', 'purchase_token_hash', 'status', 'is_consumed', 'purchased_at', 'validated_at', 'bazaar_response'])]
|
||||
#[Hidden(['purchase_token', 'purchase_token_hash'])]
|
||||
class BazaarPurchase extends Model
|
||||
{
|
||||
/** @use HasFactory<BazaarPurchaseFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $attributes = ['status' => 'unknown'];
|
||||
|
||||
public function mobileApp(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MobileApp::class);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (BazaarPurchase $purchase): void {
|
||||
if ($purchase->isDirty('purchase_token')) {
|
||||
$purchase->purchase_token_hash = hash('sha256', $purchase->purchase_token);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'is_consumed' => 'boolean',
|
||||
'purchase_token' => 'encrypted',
|
||||
'purchased_at' => 'datetime',
|
||||
'validated_at' => 'datetime',
|
||||
'bazaar_response' => 'array',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\BazaarReleaseFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable(['mobile_app_id', 'bazaar_release_id', 'status', 'artifact_type', 'staged_rollout_percentage', 'auto_publish', 'changelog_fa', 'changelog_en', 'developer_note', 'bazaar_response', 'committed_at', 'published_at'])]
|
||||
class BazaarRelease extends Model
|
||||
{
|
||||
/** @use HasFactory<BazaarReleaseFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $attributes = [
|
||||
'status' => 'draft',
|
||||
'staged_rollout_percentage' => 100,
|
||||
'auto_publish' => false,
|
||||
];
|
||||
|
||||
public function mobileApp(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MobileApp::class);
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'auto_publish' => 'boolean',
|
||||
'bazaar_response' => 'array',
|
||||
'committed_at' => 'datetime',
|
||||
'published_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\BazaarReviewFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable(['mobile_app_id', 'bazaar_review_id', 'user_account_id', 'rate', 'text', 'app_version_code', 'is_edited', 'published_at', 'developer_reply_text', 'developer_reply_email', 'developer_replied_at', 'bazaar_response'])]
|
||||
class BazaarReview extends Model
|
||||
{
|
||||
/** @use HasFactory<BazaarReviewFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $attributes = ['is_edited' => false];
|
||||
|
||||
public function mobileApp(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MobileApp::class);
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'is_edited' => 'boolean',
|
||||
'published_at' => 'datetime',
|
||||
'developer_replied_at' => 'datetime',
|
||||
'bazaar_response' => 'array',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\BazaarSubscriptionFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable(['mobile_app_id', 'subscription_id', 'purchase_token', 'purchase_token_hash', 'linked_subscription_token', 'status', 'auto_renewing', 'starts_at', 'valid_until', 'validated_at', 'bazaar_response'])]
|
||||
#[Hidden(['purchase_token', 'purchase_token_hash', 'linked_subscription_token'])]
|
||||
class BazaarSubscription extends Model
|
||||
{
|
||||
/** @use HasFactory<BazaarSubscriptionFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $attributes = ['status' => 'unknown'];
|
||||
|
||||
public function mobileApp(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MobileApp::class);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (BazaarSubscription $subscription): void {
|
||||
if ($subscription->isDirty('purchase_token')) {
|
||||
$subscription->purchase_token_hash = hash('sha256', $subscription->purchase_token);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'auto_renewing' => 'boolean',
|
||||
'purchase_token' => 'encrypted',
|
||||
'starts_at' => 'datetime',
|
||||
'valid_until' => 'datetime',
|
||||
'validated_at' => 'datetime',
|
||||
'bazaar_response' => 'array',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\MobileAppFactory;
|
||||
use Filament\Models\Contracts\HasName;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable([
|
||||
'name',
|
||||
'package_name',
|
||||
'client_key',
|
||||
'management_key',
|
||||
'is_active',
|
||||
'maintenance_mode',
|
||||
'latest_version_name',
|
||||
'latest_version_code',
|
||||
'minimum_version_code',
|
||||
'store_url',
|
||||
'privacy_url',
|
||||
'support_email',
|
||||
'configuration',
|
||||
'feature_flags',
|
||||
'bazaar_api_secret',
|
||||
'bazaar_comments_header',
|
||||
'bazaar_comments_secret',
|
||||
'bazaar_login_client_id',
|
||||
'bazaar_login_client_secret',
|
||||
'bazaar_login_redirect_url',
|
||||
'bazaar_billing_public_key',
|
||||
'bazaar_sync_enabled',
|
||||
'bazaar_sync_interval_minutes',
|
||||
'bazaar_sync_status',
|
||||
'bazaar_sync_started_at',
|
||||
'bazaar_last_synced_at',
|
||||
'bazaar_sync_error',
|
||||
'bazaar_sync_summary',
|
||||
])]
|
||||
#[Hidden([
|
||||
'client_key',
|
||||
'management_key',
|
||||
'bazaar_api_secret',
|
||||
'bazaar_comments_secret',
|
||||
'bazaar_login_client_secret',
|
||||
'bazaar_billing_public_key',
|
||||
])]
|
||||
class MobileApp extends Model implements HasName
|
||||
{
|
||||
/** @use HasFactory<MobileAppFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $attributes = [
|
||||
'is_active' => true,
|
||||
'maintenance_mode' => false,
|
||||
'bazaar_sync_enabled' => true,
|
||||
'bazaar_sync_interval_minutes' => 15,
|
||||
'bazaar_sync_status' => 'idle',
|
||||
];
|
||||
|
||||
public function getFilamentName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getRouteKeyName(): string
|
||||
{
|
||||
return 'package_name';
|
||||
}
|
||||
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class)->withPivot('role')->withTimestamps();
|
||||
}
|
||||
|
||||
public function bazaarPurchases(): HasMany
|
||||
{
|
||||
return $this->hasMany(BazaarPurchase::class);
|
||||
}
|
||||
|
||||
public function bazaarSubscriptions(): HasMany
|
||||
{
|
||||
return $this->hasMany(BazaarSubscription::class);
|
||||
}
|
||||
|
||||
public function bazaarReleases(): HasMany
|
||||
{
|
||||
return $this->hasMany(BazaarRelease::class);
|
||||
}
|
||||
|
||||
public function bazaarReviews(): HasMany
|
||||
{
|
||||
return $this->hasMany(BazaarReview::class);
|
||||
}
|
||||
|
||||
public function bazaarApiRequests(): HasMany
|
||||
{
|
||||
return $this->hasMany(BazaarApiRequest::class);
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'client_key' => 'encrypted',
|
||||
'management_key' => 'encrypted',
|
||||
'is_active' => 'boolean',
|
||||
'maintenance_mode' => 'boolean',
|
||||
'configuration' => 'array',
|
||||
'feature_flags' => 'array',
|
||||
'bazaar_api_secret' => 'encrypted',
|
||||
'bazaar_comments_secret' => 'encrypted',
|
||||
'bazaar_login_client_secret' => 'encrypted',
|
||||
'bazaar_billing_public_key' => 'encrypted',
|
||||
'bazaar_sync_enabled' => 'boolean',
|
||||
'bazaar_sync_interval_minutes' => 'integer',
|
||||
'bazaar_sync_started_at' => 'datetime',
|
||||
'bazaar_last_synced_at' => 'datetime',
|
||||
'bazaar_sync_summary' => 'array',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Filament\Models\Contracts\FilamentUser;
|
||||
use Filament\Models\Contracts\HasTenants;
|
||||
use Filament\Panel;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable implements FilamentUser, HasTenants
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
public function canAccessPanel(Panel $panel): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function mobileApps(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(MobileApp::class)->withPivot('role')->withTimestamps();
|
||||
}
|
||||
|
||||
public function getTenants(Panel $panel): Collection
|
||||
{
|
||||
return $this->mobileApps;
|
||||
}
|
||||
|
||||
public function canAccessTenant(Model $tenant): bool
|
||||
{
|
||||
return $tenant instanceof MobileApp && $this->mobileApps()->whereKey($tenant)->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
RateLimiter::for('app_api', function (Request $request): Limit {
|
||||
return Limit::perMinute(120)->by($request->route('packageName').':'.$request->ip());
|
||||
});
|
||||
|
||||
RateLimiter::for('cafe-bazaar-sync', function (object $job): Limit {
|
||||
return Limit::perMinute(4)->by((string) $job->mobileAppId);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers\Filament;
|
||||
|
||||
use App\Filament\Pages\Tenancy\EditMobileAppProfile;
|
||||
use App\Filament\Pages\Tenancy\RegisterMobileApp;
|
||||
use App\Models\MobileApp;
|
||||
use Filament\Http\Middleware\Authenticate;
|
||||
use Filament\Http\Middleware\AuthenticateSession;
|
||||
use Filament\Http\Middleware\DisableBladeIconComponents;
|
||||
use Filament\Http\Middleware\DispatchServingFilamentEvent;
|
||||
use Filament\Pages\Dashboard;
|
||||
use Filament\Panel;
|
||||
use Filament\PanelProvider;
|
||||
use Filament\Support\Colors\Color;
|
||||
use Filament\Widgets\AccountWidget;
|
||||
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
|
||||
use Illuminate\Cookie\Middleware\EncryptCookies;
|
||||
use Illuminate\Foundation\Http\Middleware\PreventRequestForgery;
|
||||
use Illuminate\Routing\Middleware\SubstituteBindings;
|
||||
use Illuminate\Session\Middleware\StartSession;
|
||||
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
||||
|
||||
class AdminPanelProvider extends PanelProvider
|
||||
{
|
||||
public function panel(Panel $panel): Panel
|
||||
{
|
||||
return $panel
|
||||
->default()
|
||||
->id('admin')
|
||||
->path('admin')
|
||||
->login()
|
||||
->brandName('Android App Manager')
|
||||
->tenant(MobileApp::class, slugAttribute: 'package_name')
|
||||
->tenantRoutePrefix('apps')
|
||||
->tenantRegistration(RegisterMobileApp::class)
|
||||
->tenantProfile(EditMobileAppProfile::class)
|
||||
->searchableTenantMenu()
|
||||
->colors([
|
||||
'primary' => Color::Amber,
|
||||
])
|
||||
->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources')
|
||||
->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages')
|
||||
->pages([
|
||||
Dashboard::class,
|
||||
])
|
||||
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\Filament\Widgets')
|
||||
->widgets([
|
||||
AccountWidget::class,
|
||||
])
|
||||
->middleware([
|
||||
EncryptCookies::class,
|
||||
AddQueuedCookiesToResponse::class,
|
||||
StartSession::class,
|
||||
AuthenticateSession::class,
|
||||
ShareErrorsFromSession::class,
|
||||
PreventRequestForgery::class,
|
||||
SubstituteBindings::class,
|
||||
DisableBladeIconComponents::class,
|
||||
DispatchServingFilamentEvent::class,
|
||||
])
|
||||
->authMiddleware([
|
||||
Authenticate::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CafeBazaar;
|
||||
|
||||
use App\Models\BazaarPurchase;
|
||||
use App\Models\MobileApp;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class BazaarPurchaseRecorder
|
||||
{
|
||||
public function record(MobileApp $app, string $productId, string $purchaseToken, BazaarResponse $response): ?BazaarPurchase
|
||||
{
|
||||
if (! $response->successful()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$purchaseState = (int) Arr::get($response->body, 'purchaseState', 0);
|
||||
$purchaseTime = Arr::get($response->body, 'purchaseTime');
|
||||
|
||||
return BazaarPurchase::updateOrCreate(
|
||||
['mobile_app_id' => $app->id, 'purchase_token_hash' => hash('sha256', $purchaseToken)],
|
||||
[
|
||||
'purchase_token' => $purchaseToken,
|
||||
'product_id' => $productId,
|
||||
'status' => $purchaseState === 1 ? 'refunded' : 'purchased',
|
||||
'is_consumed' => (int) Arr::get($response->body, 'consumptionState', 1) === 0,
|
||||
'purchased_at' => is_numeric($purchaseTime) ? CarbonImmutable::createFromTimestampMs((int) $purchaseTime) : null,
|
||||
'validated_at' => now(),
|
||||
'bazaar_response' => $response->body,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CafeBazaar;
|
||||
|
||||
use App\Models\BazaarRelease;
|
||||
use App\Models\MobileApp;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class BazaarReleaseRecorder
|
||||
{
|
||||
public function record(MobileApp $app, string $status, BazaarResponse $response, array $attributes = []): ?BazaarRelease
|
||||
{
|
||||
if (! $response->successful()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$release = BazaarRelease::query()
|
||||
->whereBelongsTo($app)
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
$release ??= new BazaarRelease(['mobile_app_id' => $app->id]);
|
||||
$release->fill([
|
||||
...$attributes,
|
||||
'bazaar_release_id' => Arr::get($response->body, 'release.id', $release->bazaar_release_id),
|
||||
'status' => $status,
|
||||
'bazaar_response' => $response->body,
|
||||
]);
|
||||
$release->save();
|
||||
|
||||
return $release;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CafeBazaar;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class BazaarResponse
|
||||
{
|
||||
public function __construct(
|
||||
public readonly int $status,
|
||||
public readonly array $body,
|
||||
) {}
|
||||
|
||||
public function successful(): bool
|
||||
{
|
||||
return $this->status >= 200 && $this->status < 300;
|
||||
}
|
||||
|
||||
public function toJsonResponse(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'data' => $this->successful() ? $this->body : null,
|
||||
'error' => $this->successful() ? null : $this->body,
|
||||
'meta' => ['bazaar_status' => $this->status],
|
||||
], $this->status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CafeBazaar;
|
||||
|
||||
use App\Models\BazaarReview;
|
||||
use App\Models\MobileApp;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class BazaarReviewRecorder
|
||||
{
|
||||
public function recordPage(MobileApp $app, BazaarResponse $response): void
|
||||
{
|
||||
if (! $response->successful()) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (Arr::get($response->body, 'results', []) as $review) {
|
||||
BazaarReview::updateOrCreate(
|
||||
['mobile_app_id' => $app->id, 'bazaar_review_id' => $review['id']],
|
||||
[
|
||||
'user_account_id' => Arr::get($review, 'user_account_id'),
|
||||
'rate' => Arr::get($review, 'rate'),
|
||||
'text' => Arr::get($review, 'text'),
|
||||
'app_version_code' => Arr::get($review, 'app_version_code'),
|
||||
'is_edited' => Arr::get($review, 'is_edited', false),
|
||||
'published_at' => Arr::get($review, 'publish_date'),
|
||||
'developer_reply_text' => Arr::get($review, 'developer_reply.text'),
|
||||
'developer_reply_email' => Arr::get($review, 'developer_reply.email'),
|
||||
'developer_replied_at' => Arr::get($review, 'developer_reply.publish_date'),
|
||||
'bazaar_response' => $review,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function recordReply(MobileApp $app, int $reviewId, BazaarResponse $response): void
|
||||
{
|
||||
if (! $response->successful()) {
|
||||
return;
|
||||
}
|
||||
|
||||
BazaarReview::query()
|
||||
->whereBelongsTo($app)
|
||||
->where('bazaar_review_id', $reviewId)
|
||||
->update([
|
||||
'developer_reply_text' => Arr::get($response->body, 'text'),
|
||||
'developer_reply_email' => Arr::get($response->body, 'email'),
|
||||
'developer_replied_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CafeBazaar;
|
||||
|
||||
class BazaarSignatureVerifier
|
||||
{
|
||||
public function verify(string $publicKey, string $originalJson, string $signature): bool
|
||||
{
|
||||
$pem = str_contains($publicKey, 'BEGIN PUBLIC KEY')
|
||||
? $publicKey
|
||||
: "-----BEGIN PUBLIC KEY-----\n".chunk_split(preg_replace('/\s+/', '', $publicKey) ?? '', 64, "\n")."-----END PUBLIC KEY-----\n";
|
||||
|
||||
$key = openssl_pkey_get_public($pem);
|
||||
$decodedSignature = base64_decode($signature, true);
|
||||
|
||||
if ($key === false || $decodedSignature === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return openssl_verify($originalJson, $decodedSignature, $key, OPENSSL_ALGO_SHA1) === 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CafeBazaar;
|
||||
|
||||
use App\Models\BazaarSubscription;
|
||||
use App\Models\MobileApp;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class BazaarSubscriptionRecorder
|
||||
{
|
||||
public function record(MobileApp $app, string $subscriptionId, string $purchaseToken, BazaarResponse $response): ?BazaarSubscription
|
||||
{
|
||||
if (! $response->successful()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$startsAt = CarbonImmutable::createFromTimestampMs((int) Arr::get($response->body, 'initiationTimestampMsec'));
|
||||
$validUntil = CarbonImmutable::createFromTimestampMs((int) Arr::get($response->body, 'validUntilTimestampMsec'));
|
||||
|
||||
return BazaarSubscription::updateOrCreate(
|
||||
['mobile_app_id' => $app->id, 'purchase_token_hash' => hash('sha256', $purchaseToken)],
|
||||
[
|
||||
'purchase_token' => $purchaseToken,
|
||||
'subscription_id' => $subscriptionId,
|
||||
'linked_subscription_token' => Arr::get($response->body, 'linkedSubscriptionToken'),
|
||||
'status' => $validUntil->isFuture() ? 'active' : 'expired',
|
||||
'auto_renewing' => (bool) Arr::get($response->body, 'autoRenewing', false),
|
||||
'starts_at' => $startsAt,
|
||||
'valid_until' => $validUntil,
|
||||
'validated_at' => now(),
|
||||
'bazaar_response' => $response->body,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CafeBazaar;
|
||||
|
||||
use App\Models\BazaarApiRequest;
|
||||
use App\Models\MobileApp;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
use InvalidArgumentException;
|
||||
use Throwable;
|
||||
|
||||
class CafeBazaarClient
|
||||
{
|
||||
private const BILLING_BASE_URL = 'https://pardakht.cafebazaar.ir/devapi/v2/api';
|
||||
|
||||
private const PISHKHAN_BASE_URL = 'https://api.pishkhan.cafebazaar.ir/v1/apps/releases';
|
||||
|
||||
private const COMMENTS_BASE_URL = 'https://sandoghche.cafebazaar.ir/api/v0/reviews';
|
||||
|
||||
private const ACCOUNT_BASE_URL = 'https://account.cafebazaar.ir/api/v0';
|
||||
|
||||
public function validatePurchase(MobileApp $app, string $productId, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'purchase.validate', 'GET', self::BILLING_BASE_URL.'/validate/'.$this->path($app->package_name).'/inapp/'.$this->path($productId).'/purchases/'.$this->path($purchaseToken).'/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function consumePurchase(MobileApp $app, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'purchase.consume', 'POST', self::BILLING_BASE_URL.'/consume/'.$this->path($app->package_name).'/purchases/', ['token' => $purchaseToken], $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function refundPurchase(MobileApp $app, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'purchase.refund', 'POST', self::BILLING_BASE_URL.'/refund/'.$this->path($app->package_name).'/purchases/'.$this->path($purchaseToken).'/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function subscription(MobileApp $app, string $subscriptionId, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'subscription.show', 'GET', self::BILLING_BASE_URL.'/applications/'.$this->path($app->package_name).'/subscriptions/'.$this->path($subscriptionId).'/purchases/'.$this->path($purchaseToken).'/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function activeSubscriptions(MobileApp $app, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'subscription.active', 'GET', self::BILLING_BASE_URL.'/applications/'.$this->path($app->package_name).'/active-subscriptions/'.$this->path($purchaseToken).'/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function cancelSubscription(MobileApp $app, string $subscriptionId, string $purchaseToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'subscription.cancel', 'POST', self::BILLING_BASE_URL.'/applications/'.$this->path($app->package_name).'/subscriptions/'.$this->path($subscriptionId).'/purchases/'.$this->path($purchaseToken).'/cancel/', request: $this->bazaarApiRequest($app));
|
||||
}
|
||||
|
||||
public function exchangeLoginCode(MobileApp $app, string $code): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'login.exchange', 'POST', self::ACCOUNT_BASE_URL.'/tokens', [
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => $code,
|
||||
'client_id' => $app->bazaar_login_client_id,
|
||||
'client_secret' => $app->bazaar_login_client_secret,
|
||||
], $this->request()->asForm());
|
||||
}
|
||||
|
||||
public function refreshLoginToken(MobileApp $app, string $refreshToken): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'login.refresh', 'POST', self::ACCOUNT_BASE_URL.'/tokens/refresh', [
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $refreshToken,
|
||||
], $this->request()->asForm());
|
||||
}
|
||||
|
||||
public function userInfo(MobileApp $app, string $accessToken, string $tokenType = 'Bearer'): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'login.userinfo', 'GET', self::ACCOUNT_BASE_URL.'/userinfo', request: $this->request()->withToken($accessToken, $tokenType));
|
||||
}
|
||||
|
||||
public function reviews(MobileApp $app, array $filters): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'reviews.list', 'GET', self::COMMENTS_BASE_URL.'/', $filters, $this->commentsRequest($app));
|
||||
}
|
||||
|
||||
public function replyToReview(MobileApp $app, int $reviewId, array $reply): BazaarResponse
|
||||
{
|
||||
return $this->send($app, 'reviews.reply', 'POST', self::COMMENTS_BASE_URL.'/'.$reviewId.'/', $reply, $this->commentsRequest($app));
|
||||
}
|
||||
|
||||
public function createRelease(MobileApp $app): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.create', 'POST', '/');
|
||||
}
|
||||
|
||||
public function deleteRelease(MobileApp $app): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.delete', 'DELETE', '/');
|
||||
}
|
||||
|
||||
public function uploadApk(MobileApp $app, UploadedFile $apk, string $architecture): BazaarResponse
|
||||
{
|
||||
$request = $this->pishkhanRequest($app)
|
||||
->attach('apk', $apk->getContent(), $apk->hashName(), ['Content-Type' => 'application/vnd.android.package-archive']);
|
||||
|
||||
return $this->send($app, 'release.upload_apk', 'POST', self::PISHKHAN_BASE_URL.'/upload/', ['architecture' => $architecture], $request);
|
||||
}
|
||||
|
||||
public function uploadAab(MobileApp $app, UploadedFile $aab): BazaarResponse
|
||||
{
|
||||
$request = $this->pishkhanRequest($app)->attach('aab', $aab->getContent(), $aab->hashName());
|
||||
|
||||
return $this->send($app, 'release.upload_aab', 'POST', self::PISHKHAN_BASE_URL.'/upload-aab/', request: $request);
|
||||
}
|
||||
|
||||
public function uploadBin(MobileApp $app, UploadedFile $bin): BazaarResponse
|
||||
{
|
||||
$request = $this->pishkhanRequest($app)->attach('file', $bin->getContent(), $bin->hashName());
|
||||
|
||||
return $this->send($app, 'release.upload_bin', 'POST', self::PISHKHAN_BASE_URL.'/upload-bin/', request: $request);
|
||||
}
|
||||
|
||||
public function bundleStatus(MobileApp $app): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.bundle_status', 'GET', '/bundle-status/');
|
||||
}
|
||||
|
||||
public function commitRelease(MobileApp $app, array $data): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.commit', 'POST', '/commit/', $data);
|
||||
}
|
||||
|
||||
public function lastRelease(MobileApp $app, string $state): BazaarResponse
|
||||
{
|
||||
return $this->pishkhan($app, 'release.last_'.$state, 'GET', '/last-'.$state.'/');
|
||||
}
|
||||
|
||||
private function pishkhan(MobileApp $app, string $operation, string $method, string $path, array $data = []): BazaarResponse
|
||||
{
|
||||
return $this->send($app, $operation, $method, self::PISHKHAN_BASE_URL.$path, $data, $this->pishkhanRequest($app));
|
||||
}
|
||||
|
||||
private function bazaarApiRequest(MobileApp $app): PendingRequest
|
||||
{
|
||||
return $this->request()->withHeaders(['CAFEBAZAAR-PISHKHAN-API-SECRET' => $app->bazaar_api_secret]);
|
||||
}
|
||||
|
||||
private function pishkhanRequest(MobileApp $app): PendingRequest
|
||||
{
|
||||
return $this->bazaarApiRequest($app);
|
||||
}
|
||||
|
||||
private function commentsRequest(MobileApp $app): PendingRequest
|
||||
{
|
||||
return $this->request()->withHeaders([$app->bazaar_comments_header => $app->bazaar_comments_secret]);
|
||||
}
|
||||
|
||||
private function request(): PendingRequest
|
||||
{
|
||||
return Http::acceptJson()->connectTimeout(5)->timeout(20)->retry([100, 300], throw: false);
|
||||
}
|
||||
|
||||
private function send(MobileApp $app, string $operation, string $method, string $url, array $data = [], ?PendingRequest $request = null): BazaarResponse
|
||||
{
|
||||
$startedAt = hrtime(true);
|
||||
$request ??= $this->request();
|
||||
|
||||
try {
|
||||
$response = $this->dispatch($request, $method, $url, $data);
|
||||
$body = $this->responseBody($response);
|
||||
$this->logRequest($app, $operation, $method, $url, $response->status(), $response->successful(), $startedAt, $data, $body);
|
||||
|
||||
return new BazaarResponse($response->status(), $body);
|
||||
} catch (ConnectionException $exception) {
|
||||
$body = ['error' => 'bazaar_unavailable', 'error_description' => 'Cafe Bazaar could not be reached.'];
|
||||
$this->logRequest($app, $operation, $method, $url, 502, false, $startedAt, $data, $body, $exception);
|
||||
|
||||
return new BazaarResponse(502, $body);
|
||||
}
|
||||
}
|
||||
|
||||
private function dispatch(PendingRequest $request, string $method, string $url, array $data): Response
|
||||
{
|
||||
return match ($method) {
|
||||
'GET' => $request->get($url, $data),
|
||||
'POST' => $request->post($url, $data),
|
||||
'DELETE' => $request->delete($url, $data),
|
||||
default => throw new InvalidArgumentException("Unsupported Bazaar method [{$method}]."),
|
||||
};
|
||||
}
|
||||
|
||||
private function responseBody(Response $response): array
|
||||
{
|
||||
$body = $response->json();
|
||||
|
||||
return is_array($body) ? $body : ['raw' => Str::limit($response->body(), 2000)];
|
||||
}
|
||||
|
||||
private function logRequest(MobileApp $app, string $operation, string $method, string $url, int $statusCode, bool $successful, int $startedAt, array $requestMetadata, array $responseBody, ?Throwable $exception = null): void
|
||||
{
|
||||
BazaarApiRequest::create([
|
||||
'mobile_app_id' => $app->id,
|
||||
'operation' => $operation,
|
||||
'method' => $method,
|
||||
'url' => $this->redactUrl($url),
|
||||
'status_code' => $statusCode,
|
||||
'successful' => $successful,
|
||||
'duration_ms' => (int) ((hrtime(true) - $startedAt) / 1_000_000),
|
||||
'request_metadata' => $this->redact($requestMetadata),
|
||||
'response_body' => $this->redact($responseBody),
|
||||
'error_message' => $exception?->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function redact(array $data): array
|
||||
{
|
||||
return collect($data)->mapWithKeys(function (mixed $value, string|int $key): array {
|
||||
if (is_string($key) && Str::contains(Str::lower($key), ['token', 'secret', 'code'])) {
|
||||
return [$key => '[REDACTED]'];
|
||||
}
|
||||
|
||||
return [$key => is_array($value) ? $this->redact($value) : $value];
|
||||
})->all();
|
||||
}
|
||||
|
||||
private function redactUrl(string $url): string
|
||||
{
|
||||
return preg_replace('#/(purchases|active-subscriptions)/[^/]+#', '/$1/[REDACTED]', $url) ?? $url;
|
||||
}
|
||||
|
||||
private function path(string $value): string
|
||||
{
|
||||
return rawurlencode($value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\CafeBazaar;
|
||||
|
||||
use App\Models\MobileApp;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Throwable;
|
||||
|
||||
class SyncCafeBazaarAppAction
|
||||
{
|
||||
private const MAX_RECORDS_PER_RUN = 500;
|
||||
|
||||
private const MAX_REVIEW_PAGES = 50;
|
||||
|
||||
public function __construct(
|
||||
private readonly CafeBazaarClient $bazaar,
|
||||
private readonly BazaarPurchaseRecorder $purchases,
|
||||
private readonly BazaarSubscriptionRecorder $subscriptions,
|
||||
private readonly BazaarReviewRecorder $reviews,
|
||||
private readonly BazaarReleaseRecorder $releases,
|
||||
) {}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function execute(MobileApp $app, bool $force = false): array
|
||||
{
|
||||
$result = Cache::lock("cafe-bazaar-sync:{$app->getKey()}", 330)->get(
|
||||
fn (): array => $this->synchronize($app->fresh(), $force),
|
||||
);
|
||||
|
||||
return $result === false
|
||||
? ['status' => 'skipped', 'reason' => 'A synchronization is already running.']
|
||||
: $result;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function synchronize(MobileApp $app, bool $force): array
|
||||
{
|
||||
$startedAt = now();
|
||||
$summary = [
|
||||
'purchases_checked' => 0,
|
||||
'subscriptions_checked' => 0,
|
||||
'reviews_imported' => 0,
|
||||
'review_pages_checked' => 0,
|
||||
'release_states_checked' => 0,
|
||||
'failures' => [],
|
||||
];
|
||||
|
||||
$app->update([
|
||||
'bazaar_sync_status' => 'running',
|
||||
'bazaar_sync_started_at' => $startedAt,
|
||||
'bazaar_sync_error' => null,
|
||||
]);
|
||||
|
||||
try {
|
||||
if (filled($app->bazaar_api_secret)) {
|
||||
$this->syncPurchases($app, $force, $summary);
|
||||
$this->syncSubscriptions($app, $force, $summary);
|
||||
$this->syncReleaseStates($app, $summary);
|
||||
}
|
||||
|
||||
if (filled($app->bazaar_comments_header) && filled($app->bazaar_comments_secret)) {
|
||||
$this->syncReviews($app, $force, $summary);
|
||||
}
|
||||
|
||||
$summary['duration_ms'] = $startedAt->diffInMilliseconds(now());
|
||||
$summary['status'] = $summary['failures'] === [] ? 'succeeded' : 'partial';
|
||||
|
||||
$app->update([
|
||||
'bazaar_sync_status' => $summary['status'],
|
||||
'bazaar_last_synced_at' => now(),
|
||||
'bazaar_sync_error' => $this->failureMessage($summary['failures']),
|
||||
'bazaar_sync_summary' => $summary,
|
||||
]);
|
||||
|
||||
return $summary;
|
||||
} catch (Throwable $exception) {
|
||||
$app->update([
|
||||
'bazaar_sync_status' => 'failed',
|
||||
'bazaar_last_synced_at' => now(),
|
||||
'bazaar_sync_error' => $exception->getMessage(),
|
||||
'bazaar_sync_summary' => [...$summary, 'status' => 'failed'],
|
||||
]);
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $summary */
|
||||
private function syncPurchases(MobileApp $app, bool $force, array &$summary): void
|
||||
{
|
||||
$query = $app->bazaarPurchases()->oldest('id');
|
||||
|
||||
if (! $force) {
|
||||
$query->where(fn ($query) => $query
|
||||
->whereNull('validated_at')
|
||||
->orWhere('validated_at', '<=', now()->subDay()));
|
||||
}
|
||||
|
||||
foreach ($query->limit(self::MAX_RECORDS_PER_RUN)->get() as $purchase) {
|
||||
$response = $this->bazaar->validatePurchase($app, $purchase->product_id, $purchase->purchase_token);
|
||||
$summary['purchases_checked']++;
|
||||
|
||||
if ($response->successful()) {
|
||||
$this->purchases->record($app, $purchase->product_id, $purchase->purchase_token, $response);
|
||||
} else {
|
||||
$this->addFailure($summary, 'purchase', $purchase->id, $response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $summary */
|
||||
private function syncSubscriptions(MobileApp $app, bool $force, array &$summary): void
|
||||
{
|
||||
$query = $app->bazaarSubscriptions()->oldest('id');
|
||||
|
||||
if (! $force) {
|
||||
$query->where(fn ($query) => $query
|
||||
->whereNull('validated_at')
|
||||
->orWhere('validated_at', '<=', now()->subMinutes($app->bazaar_sync_interval_minutes)));
|
||||
}
|
||||
|
||||
foreach ($query->limit(self::MAX_RECORDS_PER_RUN)->get() as $subscription) {
|
||||
$response = $this->bazaar->subscription($app, $subscription->subscription_id, $subscription->purchase_token);
|
||||
$summary['subscriptions_checked']++;
|
||||
|
||||
if ($response->successful()) {
|
||||
$this->subscriptions->record($app, $subscription->subscription_id, $subscription->purchase_token, $response);
|
||||
} else {
|
||||
$this->addFailure($summary, 'subscription', $subscription->id, $response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $summary */
|
||||
private function syncReviews(MobileApp $app, bool $force, array &$summary): void
|
||||
{
|
||||
$filters = [];
|
||||
$continuationCursor = $force ? null : Arr::get($app->bazaar_sync_summary, 'reviews_next_cursor');
|
||||
$continuationAfter = $force ? null : Arr::get($app->bazaar_sync_summary, 'reviews_filter_after');
|
||||
|
||||
if (is_string($continuationCursor) && $continuationCursor !== '') {
|
||||
$filters['cursor'] = $continuationCursor;
|
||||
|
||||
if (is_string($continuationAfter) && $continuationAfter !== '') {
|
||||
$filters['publish_date_after'] = $continuationAfter;
|
||||
}
|
||||
} elseif (! $force && ($latestPublishedAt = $app->bazaarReviews()->max('published_at')) !== null) {
|
||||
$filters['publish_date_after'] = now()->parse($latestPublishedAt)->subDay()->toIso8601String();
|
||||
}
|
||||
|
||||
$summary['reviews_filter_after'] = $filters['publish_date_after'] ?? null;
|
||||
|
||||
for ($page = 1; $page <= self::MAX_REVIEW_PAGES; $page++) {
|
||||
$summary['reviews_next_cursor'] = $filters['cursor'] ?? null;
|
||||
$response = $this->bazaar->reviews($app, $filters);
|
||||
$summary['review_pages_checked']++;
|
||||
|
||||
if (! $response->successful()) {
|
||||
$this->addFailure($summary, 'reviews', $page, $response);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$results = Arr::get($response->body, 'results', []);
|
||||
$this->reviews->recordPage($app, $response);
|
||||
$summary['reviews_imported'] += is_array($results) ? count($results) : 0;
|
||||
|
||||
$cursor = $this->nextCursor(Arr::get($response->body, 'next'));
|
||||
|
||||
if ($cursor === null) {
|
||||
$summary['reviews_next_cursor'] = null;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$filters['cursor'] = $cursor;
|
||||
$summary['reviews_next_cursor'] = $cursor;
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $summary */
|
||||
private function syncReleaseStates(MobileApp $app, array &$summary): void
|
||||
{
|
||||
foreach (['uncommitted', 'committed', 'published'] as $state) {
|
||||
$response = $this->bazaar->lastRelease($app, $state);
|
||||
$summary['release_states_checked']++;
|
||||
|
||||
if ($response->successful()) {
|
||||
$this->releases->record($app, $state, $response);
|
||||
} elseif ($response->status !== 404) {
|
||||
$this->addFailure($summary, 'release_'.$state, null, $response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function nextCursor(mixed $next): ?string
|
||||
{
|
||||
if (! is_string($next) || $next === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$query = parse_url($next, PHP_URL_QUERY);
|
||||
|
||||
if (! is_string($query)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
parse_str($query, $parameters);
|
||||
$cursor = $parameters['cursor'] ?? null;
|
||||
|
||||
return is_string($cursor) && $cursor !== '' ? $cursor : null;
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $summary */
|
||||
private function addFailure(array &$summary, string $operation, int|string|null $record, BazaarResponse $response): void
|
||||
{
|
||||
if (count($summary['failures']) >= 25) {
|
||||
return;
|
||||
}
|
||||
|
||||
$summary['failures'][] = array_filter([
|
||||
'operation' => $operation,
|
||||
'record' => $record,
|
||||
'status' => $response->status,
|
||||
'message' => Arr::get($response->body, 'error_description', Arr::get($response->body, 'error')),
|
||||
], fn (mixed $value): bool => $value !== null);
|
||||
}
|
||||
|
||||
/** @param array<int, array<string, mixed>> $failures */
|
||||
private function failureMessage(array $failures): ?string
|
||||
{
|
||||
if ($failures === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return count($failures).' Bazaar operation(s) failed. See the synchronization summary and API request log.';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user