You've already forked Epicnabbo-Catalogus-Updated-Daily
🆙 Add fixed cms 🆙
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Shop;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\AccountTopupFormRequest;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Srmklive\PayPal\Services\PayPal as PayPalClient;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class PaypalController extends Controller
|
||||
{
|
||||
private const STATUS_CANCELLED = 'CANCELLED';
|
||||
|
||||
private const STATUS_COMPLETED = 'COMPLETED';
|
||||
|
||||
public function __construct(private PayPalClient $provider)
|
||||
{
|
||||
$this->provider = new PayPalClient;
|
||||
$this->provider->setApiCredentials(config('habbo.paypal'));
|
||||
$this->provider->getAccessToken();
|
||||
}
|
||||
|
||||
public function process(AccountTopupFormRequest $request): Response|RedirectResponse
|
||||
{
|
||||
$amount = $request->integer('amount');
|
||||
$orderData = [
|
||||
'intent' => 'CAPTURE',
|
||||
'application_context' => [
|
||||
'return_url' => route('paypal.successful-transaction'),
|
||||
'cancel_url' => route('paypal.cancelled-transaction'),
|
||||
'brand_name' => setting('hotel_name'),
|
||||
'landing_page' => 'BILLING',
|
||||
'shipping_preference' => 'NO_SHIPPING',
|
||||
'user_action' => 'CONTINUE',
|
||||
],
|
||||
'purchase_units' => [
|
||||
0 => [
|
||||
'amount' => [
|
||||
'currency_code' => config('habbo.paypal.currency'),
|
||||
'value' => (string) $amount,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->provider->createOrder($orderData);
|
||||
|
||||
if (isset($response['id']) === false) {
|
||||
Log::error('Error creating order', ['response' => $response]);
|
||||
|
||||
return to_route('shop.index')->withErrors(
|
||||
['message' => $response['message'] ?? __('Something went wrong')],
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($response['links'] as $links) {
|
||||
if ($links['rel'] === 'approve') {
|
||||
$request->user()->transactions()->create([
|
||||
'transaction_id' => $response['id'],
|
||||
'amount' => 0,
|
||||
]);
|
||||
|
||||
return redirect()->away($links['href']);
|
||||
}
|
||||
}
|
||||
|
||||
return to_route('shop.index')->withErrors(
|
||||
['message' => $response['message'] ?? __('Something went wrong')],
|
||||
);
|
||||
}
|
||||
|
||||
public function successful(Request $request): Response
|
||||
{
|
||||
$request->validate([
|
||||
'token' => 'required',
|
||||
]);
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
$transaction = $user->transactions()->where('transaction_id', $request['token'])->first();
|
||||
if ($transaction === null) {
|
||||
return to_route('shop.index')->withErrors(['message' => __('Something went wrong, please try again later')]);
|
||||
}
|
||||
|
||||
$response = $this->provider->capturePaymentOrder($request['token']);
|
||||
$paymentDetails = $response['purchase_units'][0]['payments']['captures'][0];
|
||||
|
||||
if (! isset($response['status'], $paymentDetails)) {
|
||||
Log::error('Invalid response from PayPal', ['response' => $response]);
|
||||
|
||||
return to_route('shop.index')->withErrors(['message' => __('Something went wrong, please try again later')]);
|
||||
}
|
||||
|
||||
// Process successful payment
|
||||
$paymentDetails = $response['purchase_units'][0]['payments']['captures'][0];
|
||||
|
||||
$transaction->update([
|
||||
'status' => $paymentDetails['status'],
|
||||
'amount' => $paymentDetails['amount']['value'],
|
||||
'currency' => $paymentDetails['amount']['currency_code'],
|
||||
]);
|
||||
|
||||
if ($response['status'] !== self::STATUS_COMPLETED) {
|
||||
return to_route('shop.index')->withErrors(
|
||||
['message' => $response['message'] ?? __('Something went wrong')],
|
||||
);
|
||||
}
|
||||
|
||||
$user->increment('website_balance', $paymentDetails['amount']['value']);
|
||||
|
||||
return to_route('shop.index')->with('success', __('Transaction successful'));
|
||||
}
|
||||
|
||||
public function cancelled(Request $request): Response
|
||||
{
|
||||
$request->validate([
|
||||
'token' => 'required',
|
||||
]);
|
||||
|
||||
$transaction = $request->user()->transactions()->where('transaction_id', $request['token'])->first();
|
||||
if ($transaction !== null) {
|
||||
$transaction->update([
|
||||
'status' => self::STATUS_CANCELLED,
|
||||
'description' => 'The user cancelled the transaction',
|
||||
]);
|
||||
}
|
||||
|
||||
return to_route('shop.index')->withErrors(
|
||||
['message' => __('You have canceled the transaction')],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Shop;
|
||||
|
||||
use App\Actions\SendCurrency;
|
||||
use App\Actions\SendFurniture;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Shop\WebsiteShopArticle;
|
||||
use App\Models\Shop\WebsiteShopCategory;
|
||||
use App\Models\User;
|
||||
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) {}
|
||||
|
||||
public function __invoke(?WebsiteShopCategory $category)
|
||||
{
|
||||
$packages = WebsiteShopArticle::orderBy('position');
|
||||
|
||||
if ($category && $category->exists) {
|
||||
$packages = $category->articles()->orderBy('position');
|
||||
}
|
||||
|
||||
return view('shop.shop', [
|
||||
'articles' => $packages->with(['rank:id,rank_name', 'features'])->get(),
|
||||
'categories' => WebsiteShopCategory::whereHas('articles')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function giveBadges(User $user, string $badges): void
|
||||
{
|
||||
$badgeList = explode(';', $badges);
|
||||
$ownedBadges = $user->badges()->pluck('badge_code')->toArray();
|
||||
|
||||
foreach ($badgeList as $badge) {
|
||||
if (in_array($badge, $ownedBadges)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->rconService->isConnected) {
|
||||
$this->rconService->giveBadge($user, $badge);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$user->badges()->updateOrCreate([
|
||||
'user_id' => $user->id,
|
||||
'badge_code' => $badge,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function purchase(WebsiteShopArticle $package, Request $request, SendCurrency $sendCurrency): Response
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($request->has('receiver')) {
|
||||
if (! $package->is_giftable) {
|
||||
return to_route('shop.index')->withErrors(
|
||||
['message' => __('This package is not giftable')],
|
||||
);
|
||||
}
|
||||
|
||||
$user = User::where('username', $request->input('receiver'))->first();
|
||||
|
||||
if (! $user) {
|
||||
return to_route('shop.index')->withErrors(
|
||||
['message' => __('Recipient not found')],
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** @var User $currentUser */
|
||||
$currentUser = Auth::user();
|
||||
|
||||
if ($package->give_rank && $user->rank >= $package->give_rank) {
|
||||
$message = __('You are already this or a higher rank');
|
||||
|
||||
if ($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 && $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)])],
|
||||
);
|
||||
}
|
||||
|
||||
$currentUser->decrement('website_balance', $package->price());
|
||||
|
||||
$sendCurrency->execute($user, 'credits', $package->credits);
|
||||
$sendCurrency->execute($user, 'duckets', $package->duckets);
|
||||
$sendCurrency->execute($user, 'diamonds', $package->diamonds);
|
||||
|
||||
if ($package->give_rank) {
|
||||
if ($this->rconService->isConnected) {
|
||||
$this->rconService->setRank($user, $package->give_rank);
|
||||
$this->rconService->disconnectUser($user);
|
||||
} else {
|
||||
$user->update([
|
||||
'rank' => $package->give_rank,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($package->badges) {
|
||||
$this->giveBadges($user, $package->badges);
|
||||
}
|
||||
|
||||
if ($package->furniture) {
|
||||
$this->handleFurniture(json_decode($package->furniture, true));
|
||||
}
|
||||
|
||||
$message = __('You have successfully purchased the package :name', ['name' => $package->name]);
|
||||
|
||||
if ($user->username !== $currentUser->username) {
|
||||
$message = __('You have successfully purchased the package :name for :username', ['name' => $package->name, 'username' => $user->username]);
|
||||
}
|
||||
|
||||
return to_route('shop.index')->with('success', $message);
|
||||
}
|
||||
|
||||
public function handleFurniture(array $furniture): void
|
||||
{
|
||||
$sendFurniture = app(SendFurniture::class);
|
||||
|
||||
/** @var User $currentUser */
|
||||
$currentUser = Auth::user();
|
||||
|
||||
$sendFurniture->execute($currentUser, $furniture);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Shop;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\ShopVoucherFormRequest;
|
||||
use App\Models\Shop\WebsiteShopVoucher;
|
||||
|
||||
class ShopVoucherController extends Controller
|
||||
{
|
||||
public function __invoke(ShopVoucherFormRequest $request)
|
||||
{
|
||||
$user = $request->user();
|
||||
$voucher = WebsiteShopVoucher::where('code', $request->string('code'))->first();
|
||||
|
||||
if (is_null($voucher) || ($voucher->expires_at && $voucher->expires_at->lte(now()))) {
|
||||
return redirect()->back()->withErrors([
|
||||
'message' => __('No active voucher with the given code was found'),
|
||||
]);
|
||||
}
|
||||
|
||||
if ($user->usedShopVouchers()->where('voucher_id', $voucher->id)->exists()) {
|
||||
return redirect()->back()->withErrors([
|
||||
'message' => __('You can only use each shop voucher once'),
|
||||
]);
|
||||
}
|
||||
|
||||
$user->usedShopVouchers()->create([
|
||||
'voucher_id' => $voucher->id,
|
||||
]);
|
||||
|
||||
$user->increment('website_balance', $voucher->amount);
|
||||
|
||||
$voucher->increment('use_count');
|
||||
|
||||
if ($voucher->max_uses && $voucher->use_count >= $voucher->max_uses) {
|
||||
$voucher->update([
|
||||
'expires_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', __('Your balance has been increased by $:amount', ['amount' => $voucher->amount]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user