You've already forked Atomcms-edit
90 lines
2.9 KiB
PHP
Executable File
90 lines
2.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Shop;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Shop\WebsiteShopArticle;
|
|
use App\Models\Shop\WebsiteShopCategory;
|
|
use App\Models\User;
|
|
use App\Services\PurchaseService;
|
|
use App\Services\RconService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class ShopController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly RconService $rconService,
|
|
private readonly PurchaseService $purchaseService,
|
|
) {}
|
|
|
|
public function __invoke(?WebsiteShopCategory $category)
|
|
{
|
|
$query = WebsiteShopArticle::query()->orderBy('position');
|
|
|
|
if ($category && $category->exists) {
|
|
$query = $category->articles()->orderBy('position');
|
|
}
|
|
|
|
return view('shop.shop', [
|
|
'articles' => $query->with(['rank:id,rank_name', 'features'])->get(),
|
|
'categories' => WebsiteShopCategory::whereHas('articles')->get(),
|
|
]);
|
|
}
|
|
|
|
public function purchase(WebsiteShopArticle $package, Request $request): Response
|
|
{
|
|
/** @var User $currentUser */
|
|
$currentUser = Auth::user();
|
|
|
|
$recipient = null;
|
|
|
|
if ($request->has('receiver')) {
|
|
if (! $package->is_giftable) {
|
|
return to_route('shop.index')->withErrors(
|
|
['message' => __('This package is not giftable')],
|
|
);
|
|
}
|
|
|
|
$recipient = User::where('username', $request->input('receiver'))->first();
|
|
|
|
if (! $recipient) {
|
|
return to_route('shop.index')->withErrors(
|
|
['message' => __('Recipient not found')],
|
|
);
|
|
}
|
|
}
|
|
|
|
$user = $recipient ?? $currentUser;
|
|
|
|
if ($package->give_rank && $user->rank >= $package->give_rank) {
|
|
$message = __('You are already this or a higher rank');
|
|
|
|
if ($recipient && $user->username !== $currentUser->username) {
|
|
$message = __('The recipient is already this or a higher rank');
|
|
}
|
|
|
|
return to_route('shop.index')->withErrors(
|
|
['message' => $message],
|
|
);
|
|
}
|
|
|
|
if (! $this->rconService->isConnected && (int) $user->online === 1) {
|
|
return to_route('shop.index')->withErrors(
|
|
['message' => __('Please logout before purchasing a package')],
|
|
);
|
|
}
|
|
|
|
if ($currentUser->website_balance < $package->price()) {
|
|
return to_route('shop.index')->withErrors(
|
|
['message' => __('You need to top-up your account with another $:amount to purchase this package', ['amount' => ($package->price() - $currentUser->website_balance)])],
|
|
);
|
|
}
|
|
|
|
$message = $this->purchaseService->processPurchase($currentUser, $package, $recipient);
|
|
|
|
return to_route('shop.index')->with('success', $message);
|
|
}
|
|
}
|