first commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\RedirectLinkHelper;
|
||||
use App\Models\Transaction;
|
||||
use App\Modules\Gateway\AsanPardakht\AsanPardakhtApi;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AsanPardakhtController extends Controller
|
||||
{
|
||||
public function pay(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'token' => 'string|required',
|
||||
'mobile' => 'string',
|
||||
]);
|
||||
|
||||
return view('ap-form', ['token' => $data['token'], 'mobile' => $data['mobile']]);
|
||||
}
|
||||
|
||||
public function checkTransaction(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'localInvoiceId' => 'string|required',
|
||||
'platform' => 'string'
|
||||
]);
|
||||
|
||||
if (!$transaction = Transaction::with(['product.packageName', 'user'])->where('status', Transaction::STATUSES['pending'])->where('id', $data['localInvoiceId'])->first()) {
|
||||
return view('status', [
|
||||
'status' => false,
|
||||
'redirect_link' => '#'
|
||||
]);
|
||||
}
|
||||
$redirectLink = RedirectLinkHelper::get($transaction->product->packageName->name, $data['platform'] ?? null);
|
||||
$response = AsanPardakhtApi::checkTransaction($transaction);
|
||||
|
||||
if (!$response['status']) {
|
||||
return view('status', [
|
||||
'status' => false,
|
||||
'redirect_link' => $redirectLink
|
||||
]);
|
||||
}
|
||||
|
||||
$transaction->update(['status' => Transaction::STATUSES['success']]);
|
||||
|
||||
$transaction->product->buy($transaction->user, $transaction['authority'], Transaction::GATEWAYS['asanpardakht']);
|
||||
|
||||
return view('status', [
|
||||
'status' => true,
|
||||
'redirect_link' => $redirectLink
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\CafeConfig;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CafeController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
return CafeConfig::get();
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'client_id' => 'required|string',
|
||||
'client_secret' => 'required|string',
|
||||
'access_token' => 'required|string',
|
||||
'refresh_token' => 'required|string',
|
||||
]);
|
||||
|
||||
$CafeConfig = CafeConfig::create($data);
|
||||
|
||||
return $CafeConfig;
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'client_id' => 'required|string',
|
||||
'client_secret' => 'required|string',
|
||||
'access_token' => 'required|string',
|
||||
'refresh_token' => 'required|string',
|
||||
]);
|
||||
|
||||
if (!$cafeConfig = CafeConfig::where('id', $id)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'cafe config not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$cafeConfig->update($data);
|
||||
|
||||
return $cafeConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\CafeConfig;
|
||||
use App\Models\PackageName;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class PackageNameController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$packageNames = PackageName::get();
|
||||
return $packageNames;
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => 'string|required',
|
||||
'title' => 'string|required',
|
||||
'avatar' => 'image',
|
||||
'v1_identifier' => 'string|nullable',
|
||||
'myket_access_token' => 'string|nullable',
|
||||
'cafe_config_id' => 'integer|nullable',
|
||||
'web_app_url' => 'string|nullable',
|
||||
'tries' => 'integer'
|
||||
]);
|
||||
|
||||
if ($packageName = PackageName::where('name', $data['name'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package exists'
|
||||
], 400);
|
||||
}
|
||||
|
||||
if (isset($data['avatar'])) {
|
||||
Storage::disk('public')->put("package-names/{$data['name']}.png", file_get_contents($data['avatar']->path()));
|
||||
$data['image'] = "storage/package-names/{$data['name']}.png";
|
||||
unset($data['avatar']);
|
||||
}
|
||||
|
||||
if (isset($data['cafe_config_id']) && !$CafeConfig = CafeConfig::where('id', $data['cafe_config_id'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'cafe config not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$packageName = PackageName::create($data);
|
||||
return $packageName;
|
||||
}
|
||||
|
||||
public function update(Request $request, $name)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'title' => 'string|required',
|
||||
'avatar' => 'image',
|
||||
'v1_identifier' => 'string|nullable',
|
||||
'myket_access_token' => 'string|nullable',
|
||||
'cafe_config_id' => 'integer|nullable',
|
||||
'web_app_url' => 'string|nullable',
|
||||
'tries' => 'integer'
|
||||
]);
|
||||
|
||||
if (isset($data['avatar'])) {
|
||||
Storage::disk('public')->put("package-names/{$name}.png", file_get_contents($data['avatar']->path()));
|
||||
$data['image'] = "storage/package-names/{$name}.png";
|
||||
unset($data['avatar']);
|
||||
}
|
||||
|
||||
|
||||
if (isset($data['cafe_config_id']) && !$CafeConfig = CafeConfig::where('id', $data['cafe_config_id'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'cafe config not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!$packageName = PackageName::where('name', $name)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package name not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$packageName->update($data);
|
||||
|
||||
return $packageName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\PackageName;
|
||||
use App\Models\Product;
|
||||
use App\Models\Transaction;
|
||||
use App\Services\CafeService;
|
||||
use App\Services\MyketService;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function index(Request $request, $packageName)
|
||||
{
|
||||
if (!$packageName = PackageName::with('products')->where('name', $packageName)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package name not found'
|
||||
], 404);
|
||||
}
|
||||
$products = $packageName->products;
|
||||
return $products;
|
||||
}
|
||||
|
||||
public function store(Request $request, $packageName)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'title' => 'string|required',
|
||||
'price' => 'integer|required',
|
||||
'type' => ['integer', Rule::in(Product::TYPES)],
|
||||
]);
|
||||
|
||||
if (!$packageName = PackageName::with('products')->where('name', $packageName)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package name not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$product = $packageName->products()->create($data);
|
||||
return $product;
|
||||
}
|
||||
|
||||
public function update(Request $request, $packageName, $productId)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'title' => 'string|required',
|
||||
'price' => 'integer|required',
|
||||
'type' => ['integer', Rule::in(Product::TYPES)],
|
||||
]);
|
||||
|
||||
if (!$packageName = PackageName::with('products')->where('name', $packageName)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package name not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!$product = Product::where('id', $productId)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'product not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$product->update($data);
|
||||
return $product;
|
||||
}
|
||||
|
||||
public function delete($packageName, $productId)
|
||||
{
|
||||
if (!$packageName = PackageName::with('products')->where('name', $packageName)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package name not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!$product = Product::where('id', $productId)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'product not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$product->delete();
|
||||
return response()->json([
|
||||
'message' => 'product deleted'
|
||||
]);
|
||||
}
|
||||
|
||||
public function subscribe(Request $request, $packageName, $productId)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'gateway' => 'string|required|in:cafe,myket',
|
||||
'purchase_token' => 'string|required',
|
||||
]);
|
||||
|
||||
if (!$packageName = PackageName::where('name', $packageName)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package name not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
if (!$product = Product::with(['packageName.cafeConfig'])->where('id', $productId)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'product not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if ($data['gateway'] == 'zarinpal') {
|
||||
if (!$transaction = Transaction::where('authority', $data['authority'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'transaction not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if ($transaction->status == Transaction::STATUSES['consumed']) {
|
||||
return response()->json([
|
||||
'message' => 'already consumed'
|
||||
], 400);
|
||||
}
|
||||
$merchantId = config('zarinpal.merchant_id');
|
||||
$response = Http::retry(3, 100, throw: false)->post(config('zarinpal.url.verify'), [
|
||||
'merchant_id' => $merchantId,
|
||||
'amount' => $product['price'] * 10,
|
||||
'authority' => $data['authority'],
|
||||
]);
|
||||
if ($response['data'] == [] || $response['data']['code'] == 101) {
|
||||
return response()->json([
|
||||
"message" => "failed",
|
||||
"code" => strval($response['errors']['code'] ?? 101),
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
if ($data['gateway'] == 'cafe') {
|
||||
// $service = new CafeService($product->packageName->cafeConfig);
|
||||
// $purchaseStatus = $service->checkPurchase($product, $data['purchase_token']);
|
||||
// if (!$purchaseStatus['status']) {
|
||||
// return response()->json([
|
||||
// 'message' => $purchaseStatus['message'],
|
||||
// ], 400);
|
||||
// }
|
||||
// $service->consumePurchase($product, $data['purchase_token']);
|
||||
$user->transactions()->create([
|
||||
'amount' => $product->price,
|
||||
'authority' => $data['purchase_token'],
|
||||
'product_id' => $product->id,
|
||||
'status' => Transaction::STATUSES['consumed'],
|
||||
'gateway' => Transaction::GATEWAYS[$data['gateway']],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($data['gateway'] == 'myket') {
|
||||
$service = new MyketService($product->packageName->cafeConfig);
|
||||
$purchaseStatus = $service->checkPurchase($product, $data['purchase_token']);
|
||||
if (!$purchaseStatus['status']) {
|
||||
return response()->json([
|
||||
'message' => $purchaseStatus['message'],
|
||||
], 400);
|
||||
}
|
||||
$user->transactions()->create([
|
||||
'amount' => $product->price,
|
||||
'authority' => $data['purchase_token'],
|
||||
'product_id' => $product->id,
|
||||
'status' => Transaction::STATUSES['consumed'],
|
||||
'gateway' => Transaction::GATEWAYS[$data['gateway']],
|
||||
]);
|
||||
}
|
||||
|
||||
$product->buy($user, $data['purchase_token'], Transaction::GATEWAYS[$data['gateway']]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'subscribed successfuly'
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\Transaction;
|
||||
use App\Modules\Gateway\AsanPardakht\AsanPardakhtApi;
|
||||
use App\Modules\Gateway\Digipay\DigiPayApi;
|
||||
use App\Modules\Gateway\Zarinpal\ZarinpalApi;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class TransactionController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'per_page' => 'integer'
|
||||
]);
|
||||
|
||||
$transactions = auth()->user()->transactions()->paginate($data['per_page'] ?? 30);
|
||||
|
||||
return $transactions;
|
||||
}
|
||||
|
||||
public function getGateway(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'description' => 'required|string',
|
||||
'product_id' => 'nullable|string',
|
||||
'platform' => 'in:mobile,web'
|
||||
]);
|
||||
|
||||
if (isset($data['product_id']) && !$product = Product::where('id', $data['product_id'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'product not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
if ($user->products()->where('products.id', $data['product_id'])->where('type', Product::TYPES['permanent'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'already have product'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$transaction = $user->transactions()->create([
|
||||
'amount' => floor($product->price * 1.1),
|
||||
'product_id' => $data['product_id'],
|
||||
'uuid' => Str::uuid()->toString()
|
||||
]);
|
||||
|
||||
$gateway = Transaction::GATEWAYS['asanpardakht'];
|
||||
$response = AsanPardakhtApi::getGateway($transaction);
|
||||
if (!($response['status'] ?? false)) {
|
||||
$response = ZarinpalApi::getGateway($transaction, $data['description']);
|
||||
$gateway = Transaction::GATEWAYS['zarinpal'];
|
||||
if (!($response['status'] ?? false)) {
|
||||
$response = DigiPayApi::getGateway($transaction);
|
||||
$gateway = Transaction::GATEWAYS['digipay'];
|
||||
if (!($response['status'] ?? false)) {
|
||||
return response()->json(["message" => "failed"], 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$transaction->gateway = $gateway;
|
||||
$transaction->authority = $response['data']['authority'];
|
||||
$transaction->save();
|
||||
|
||||
return response()->json([
|
||||
'url' => $response['data']['url']
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Helpers\MobileNumberHelper;
|
||||
use App\Jobs\SendSMSJob;
|
||||
use App\Models\PackageName;
|
||||
use App\Models\Product;
|
||||
use App\Models\Transaction;
|
||||
use App\Models\User;
|
||||
use App\Rules\MobileNumber;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function login(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'auth' => 'string|required',
|
||||
'password' => 'string|required'
|
||||
]);
|
||||
|
||||
if (filter_var($data['auth'], FILTER_VALIDATE_EMAIL)) {
|
||||
$authType = 'email';
|
||||
} elseif (MobileNumberHelper::checkMobileNumber($data['auth'])) {
|
||||
$authType = 'mobile';
|
||||
$data['auth'] = MobileNumberHelper::formatMobile($data['auth']);
|
||||
} else {
|
||||
return response()->json([
|
||||
'message' => 'wrong credentials'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!$user = User::where($authType, $data['auth'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'wrong credentials'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!Hash::check($data['password'], $user->password)) {
|
||||
return response()->json([
|
||||
'message' => 'wrong credentials'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$token = '';
|
||||
if ($user->is_admin) {
|
||||
$token = $user->createToken('token', ['admin'])->plainTextToken;
|
||||
} else {
|
||||
$token = $user->createToken('token', ['user'])->plainTextToken;
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'user' => $user,
|
||||
'token' => $token,
|
||||
]);
|
||||
}
|
||||
|
||||
public function register(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'first_name' => 'string',
|
||||
'last_name' => 'string',
|
||||
'email' => 'email|unique:users|required_without:mobile',
|
||||
'password' => 'string|nullable|required_with:email',
|
||||
'mobile' => ['string', new MobileNumber, 'nullable', 'unique:users', 'required_without:email'],
|
||||
]);
|
||||
|
||||
$data['mobile'] = MobileNumberHelper::formatMobile($data['mobile']);
|
||||
if (!isset($data['password'])) {
|
||||
$data['password'] = Hash::make(config('app.default_password')); // Hash default password
|
||||
} else {
|
||||
$data['password'] = Hash::make($data['password']); // Hash provided password
|
||||
}
|
||||
// return response()->json([
|
||||
// "salam"
|
||||
// ]);
|
||||
$user = User::create($data);
|
||||
|
||||
$token = $user->createToken('token', ['user'])->plainTextToken;
|
||||
|
||||
return response()->json([
|
||||
'user' => $user,
|
||||
'token' => $token
|
||||
]);
|
||||
}
|
||||
|
||||
public function loginOTP(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'mobile' => ['string', new MobileNumber, 'required'],
|
||||
'package_name' => 'string|required'
|
||||
]);
|
||||
|
||||
if (!$packageName = PackageName::where('name', $data['package_name'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package name not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!$user = User::where('mobile', $data['mobile'])->first()) {
|
||||
$user = User::create([
|
||||
'mobile' => MobileNumberHelper::formatMobile($data['mobile']),
|
||||
'password' => config('app.default_password'),
|
||||
]);
|
||||
}
|
||||
if ($data['mobile'] != config('approo.admin_mobile')) {
|
||||
$otpToken = rand(pow(10, 4), pow(10, 5) - 1);
|
||||
$user->otpTokens()->create([
|
||||
'token' => $otpToken
|
||||
]);
|
||||
|
||||
$verify = Str::random(8);
|
||||
// SendSMSJob::dispatch([
|
||||
// 'mobile' => $data['mobile'],
|
||||
// 'message' => "کد ورود به برنامه: $otpToken
|
||||
// @{$packageName->web_app_url} #$otpToken
|
||||
// لغو 11"
|
||||
// ]);
|
||||
SendSMSJob::dispatch([
|
||||
'mobile' => $data['mobile'],
|
||||
'token' => "$otpToken"
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
if (!$user->packageNames()->where('package_names.id', $packageName->id)->exists()) {
|
||||
$user->packageNames()->attach($packageName->id, ['tries' => $packageName->tries]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'otp sent',
|
||||
'verify_code' => $verify ?? null
|
||||
]);
|
||||
}
|
||||
|
||||
public function checkOTP(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'mobile' => ['string', new MobileNumber, 'required'],
|
||||
'token' => 'string|required',
|
||||
]);
|
||||
|
||||
if (!$user = User::where('mobile', $data['mobile'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'user not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$otpToken = $user->otpTokens()->where('token', $data['token'])->where('created_at', '>', now()->subMinutes(15))->first();
|
||||
if (!$otpToken && !($data['mobile'] == config('approo.admin_mobile') && $data['token'] == config('approo.admin_otp'))) {
|
||||
return response()->json([
|
||||
'message' => 'token not valid'
|
||||
], 400);
|
||||
}
|
||||
$otpToken?->delete();
|
||||
|
||||
$token = '';
|
||||
if ($user->is_admin) {
|
||||
$token = $user->createToken('token', ['admin'])->plainTextToken;
|
||||
} else {
|
||||
$token = $user->createToken('token', ['admin'])->plainTextToken;
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'user' => $user,
|
||||
'token' => $token,
|
||||
]);
|
||||
}
|
||||
public function updateProfile(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'first_name' => 'string|nullable',
|
||||
'last_name' => 'string|nullable',
|
||||
'birthday' => 'date|nullable',
|
||||
'gender' => ['integer', 'nullable', Rule::in(User::GENDERS)],
|
||||
'email' => 'email|nullable',
|
||||
'mobile' => [new MobileNumber, 'string'],
|
||||
'avatar' => 'image'
|
||||
]);
|
||||
|
||||
if (isset($data['email'])) {
|
||||
$user = User::where('email', $data['email'])->first();
|
||||
if ($user && $data['email'] != auth()->user()->email) {
|
||||
return response()->json([
|
||||
'message' => 'user with given email exists'
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($data['mobile'])) {
|
||||
$user = User::where('mobile', $data['mobile'])->first();
|
||||
if ($user && $data['mobile'] != auth()->user()->mobile) {
|
||||
return response()->json([
|
||||
'message' => 'user with given email exists'
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
if (isset($data['avatar'])) {
|
||||
Storage::disk('public')->put("avatars/$user->uuid.png", file_get_contents($data['avatar']->path()));
|
||||
$data['avatar'] = "storage/avatars/$user->uuid.png";
|
||||
}
|
||||
|
||||
$user->update($data);
|
||||
|
||||
return response()->json([
|
||||
'user' => $user,
|
||||
'message' => 'user updated'
|
||||
]);
|
||||
}
|
||||
public function status(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'package_name' => 'string|required'
|
||||
]);
|
||||
|
||||
if (!$packageName = PackageName::with('cafeConfig')->where('name', $data['package_name'])->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package name not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
$user->load(['response', 'plans']);
|
||||
|
||||
if ($user->mobile == config('approo.admin_mobile')) {
|
||||
$user['products'] = Product::where('package_name_id', $packageName->id)->get();
|
||||
} else {
|
||||
// if ($packageName->cafeConfig) {
|
||||
// $user['products'] = $user->products()->where('package_name_id', $packageName->id)->get();
|
||||
// foreach ($user['products'] as $key => $product) {
|
||||
// $service = new CafeService($packageName->cafeConfig);
|
||||
// if ($product->pivot->gateway == Transaction::GATEWAYS['cafe'] && $product->pivot->purchase_token && $product->type != Product::TYPES['permanent']) {
|
||||
// $activeSubscriptions = $service->checkSubscription($product, $product->pivot->purchase_token);
|
||||
// if ($activeSubscriptions) {
|
||||
// $expire = Carbon::createFromTimestamp($activeSubscriptions[0]['validUntilTimestampMsec'] / 1000);
|
||||
// $user->products()->updateExistingPivot($product, ['expire_at' => $expire->format('Y-m-d H:i:s')]);
|
||||
// } else {
|
||||
// $user->products()->updateExistingPivot($product, ['expire_at' => now()->format('Y-m-d H:i:s')]);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
$user['products'] = $user->products()
|
||||
->where('package_name_id', $packageName->id)
|
||||
->where(fn ($q) => $q->where('user_product.expire_at', '>', now()->addHour())->orWhere('user_product.expire_at', null))
|
||||
->get();
|
||||
|
||||
$user['package_name'] = $user->packageNames()
|
||||
->where('package_names.id', $packageName->id)
|
||||
->first();
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
public function try(Request $request, $name) {
|
||||
$user = auth()->user();
|
||||
|
||||
if (!$packageName = $user->packageNames()->where('name', $name)->first()) {
|
||||
return response()->json([
|
||||
'message' => 'package name not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if ($packageName->pivot->tries <= 0) {
|
||||
return response()->json([
|
||||
'message' => 'no more tries'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$packageName->pivot->tries->decrement();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'success'
|
||||
]);
|
||||
}
|
||||
|
||||
public function oauthCallback(Request $request)
|
||||
{
|
||||
return dd(Socialite::driver('google')->stateless()->user());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http;
|
||||
|
||||
use App\Http\Middleware\AutoBan;
|
||||
use App\Http\Middleware\BanBot;
|
||||
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
||||
|
||||
class Kernel extends HttpKernel
|
||||
{
|
||||
/**
|
||||
* The application's global HTTP middleware stack.
|
||||
*
|
||||
* These middleware are run during every request to your application.
|
||||
*
|
||||
* @var array<int, class-string|string>
|
||||
*/
|
||||
protected $middleware = [
|
||||
// \App\Http\Middleware\TrustHosts::class,
|
||||
\App\Http\Middleware\TrustProxies::class,
|
||||
\Illuminate\Http\Middleware\HandleCors::class,
|
||||
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
|
||||
\App\Http\Middleware\TrimStrings::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* The application's route middleware groups.
|
||||
*
|
||||
* @var array<string, array<int, class-string|string>>
|
||||
*/
|
||||
protected $middlewareGroups = [
|
||||
'web' => [
|
||||
\App\Http\Middleware\EncryptCookies::class,
|
||||
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
|
||||
\Illuminate\Session\Middleware\StartSession::class,
|
||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
],
|
||||
|
||||
'api' => [
|
||||
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
||||
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* The application's middleware aliases.
|
||||
*
|
||||
* Aliases may be used instead of class names to conveniently assign middleware to routes and groups.
|
||||
*
|
||||
* @var array<string, class-string|string>
|
||||
*/
|
||||
protected $middlewareAliases = [
|
||||
'auth' => \App\Http\Middleware\Authenticate::class,
|
||||
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
|
||||
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
|
||||
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
|
||||
'can' => \Illuminate\Auth\Middleware\Authorize::class,
|
||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
|
||||
'signed' => \App\Http\Middleware\ValidateSignature::class,
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||
'abilities' => \Laravel\Sanctum\Http\Middleware\CheckAbilities::class,
|
||||
'ability' => \Laravel\Sanctum\Http\Middleware\CheckForAnyAbility::class,
|
||||
'ban-bot' => BanBot::class,
|
||||
'auto-ban' => AutoBan::class,
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Auth\Middleware\Authenticate as Middleware;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class Authenticate extends Middleware
|
||||
{
|
||||
/**
|
||||
* Get the path the user should be redirected to when they are not authenticated.
|
||||
*/
|
||||
protected function redirectTo(Request $request): ?string
|
||||
{
|
||||
return $request->expectsJson() ? null : route('login');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class AutoBan
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if ($request->ip() == '91.92.190.63') {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$bannedIps = Cache::get('banned-ips', ['62.212.88.47', '5.182.44.142', '185.244.36.217', '185.142.158.194', '188.229.86.48', '108.181.126.147',
|
||||
'94.182.91.226', '188.212.22.252', '185.55.225.6', '176.9.35.126', '93.115.223.250', '88.198.230.56',
|
||||
'185.94.98.252', '168.119.213.43', '193.151.150.73', '46.4.97.122', '192.42.116.176', '31.40.216.202']);
|
||||
|
||||
|
||||
if (in_array($request->ip(), [...$bannedIps])) {
|
||||
abort(403, ".|.");
|
||||
}
|
||||
|
||||
$ipTries = Cache::get($request->ip() . '-tries', 0);
|
||||
Cache::set($request->ip() . '-tries', $ipTries + 1, 600);
|
||||
|
||||
if ($ipTries > 30) {
|
||||
Cache::set('banned-ips', [...$bannedIps, $request->ip()]);
|
||||
}
|
||||
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class BanBot
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if ($request->header('cf-worker')) {
|
||||
return response()->json(['message' => 'unauthorized'], 403);
|
||||
}
|
||||
|
||||
if (in_array($request->ip(), ['23.95.132.45'])) {
|
||||
return response()->json(['message' => 'unauthorized'], 403);
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
|
||||
|
||||
class EncryptCookies extends Middleware
|
||||
{
|
||||
/**
|
||||
* The names of the cookies that should not be encrypted.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
//
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as Middleware;
|
||||
|
||||
class PreventRequestsDuringMaintenance extends Middleware
|
||||
{
|
||||
/**
|
||||
* The URIs that should be reachable while maintenance mode is enabled.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
//
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class RedirectIfAuthenticated
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next, string ...$guards): Response
|
||||
{
|
||||
$guards = empty($guards) ? [null] : $guards;
|
||||
|
||||
foreach ($guards as $guard) {
|
||||
if (Auth::guard($guard)->check()) {
|
||||
return redirect(RouteServiceProvider::HOME);
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
|
||||
|
||||
class TrimStrings extends Middleware
|
||||
{
|
||||
/**
|
||||
* The names of the attributes that should not be trimmed.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
'current_password',
|
||||
'password',
|
||||
'password_confirmation',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Middleware\TrustHosts as Middleware;
|
||||
|
||||
class TrustHosts extends Middleware
|
||||
{
|
||||
/**
|
||||
* Get the host patterns that should be trusted.
|
||||
*
|
||||
* @return array<int, string|null>
|
||||
*/
|
||||
public function hosts(): array
|
||||
{
|
||||
return [
|
||||
$this->allSubdomainsOfApplicationUrl(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Middleware\TrustProxies as Middleware;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class TrustProxies extends Middleware
|
||||
{
|
||||
/**
|
||||
* The trusted proxies for this application.
|
||||
*
|
||||
* @var array<int, string>|string|null
|
||||
*/
|
||||
protected $proxies;
|
||||
|
||||
/**
|
||||
* The headers that should be used to detect proxies.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $headers =
|
||||
Request::HEADER_X_FORWARDED_FOR |
|
||||
Request::HEADER_X_FORWARDED_HOST |
|
||||
Request::HEADER_X_FORWARDED_PORT |
|
||||
Request::HEADER_X_FORWARDED_PROTO |
|
||||
Request::HEADER_X_FORWARDED_AWS_ELB;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Routing\Middleware\ValidateSignature as Middleware;
|
||||
|
||||
class ValidateSignature extends Middleware
|
||||
{
|
||||
/**
|
||||
* The names of the query string parameters that should be ignored.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
// 'fbclid',
|
||||
// 'utm_campaign',
|
||||
// 'utm_content',
|
||||
// 'utm_medium',
|
||||
// 'utm_source',
|
||||
// 'utm_term',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
|
||||
|
||||
class VerifyCsrfToken extends Middleware
|
||||
{
|
||||
/**
|
||||
* The URIs that should be excluded from CSRF verification.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $except = [
|
||||
//
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user