Update
This commit is contained in:
93
app/Http/Controllers/SocialAuthController.php
Normal file
93
app/Http/Controllers/SocialAuthController.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
|
||||
class SocialAuthController extends Controller
|
||||
{
|
||||
public function redirectToGoogle()
|
||||
{
|
||||
return Socialite::driver('google')
|
||||
->with(['prompt' => 'consent'])
|
||||
->redirect();
|
||||
}
|
||||
|
||||
public function handleGoogleCallback()
|
||||
{
|
||||
try {
|
||||
if (App::environment('production')) {
|
||||
$googleUser = Socialite::driver('google')->user();
|
||||
} else {
|
||||
$googleUser = Socialite::driver('google')->user();
|
||||
|
||||
// /$googleUser = $this->getMockGoogleUser();
|
||||
}
|
||||
|
||||
// First, check if the user exists by google_id
|
||||
$user = User::where('google_id', $googleUser->id)->whereNotNull('google_id')->first();
|
||||
|
||||
if ($user) {
|
||||
$this->setupUser($user);
|
||||
|
||||
Auth::login($user);
|
||||
} else {
|
||||
// If no user found by google_id, check by email
|
||||
$user = User::where('email', $googleUser->email)->first();
|
||||
|
||||
if ($user) {
|
||||
// If the google_id is empty, update it
|
||||
if (empty($user->google_id)) {
|
||||
$user->google_id = $googleUser->id;
|
||||
$user->save();
|
||||
}
|
||||
$this->setupUser($user);
|
||||
Auth::login($user);
|
||||
} else {
|
||||
// Create a new user if neither exists
|
||||
$user = User::create([
|
||||
'email' => $googleUser->email,
|
||||
'google_id' => $googleUser->id,
|
||||
]);
|
||||
$this->setupUser($user);
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
$intended_route = route('home');
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->intended(route('home'));
|
||||
} catch (\Exception $e) {
|
||||
|
||||
throw $e;
|
||||
$error_message = 'Google login failed. Please try again.';
|
||||
if (config('app.debug')) {
|
||||
$error_message = $e->getMessage();
|
||||
}
|
||||
|
||||
return redirect()->route('home')->with('error', $error_message);
|
||||
}
|
||||
}
|
||||
|
||||
private function setupUser($user) {}
|
||||
|
||||
private function getMockGoogleUser()
|
||||
{
|
||||
// Create a mock user object that mimics Socialite's user structure
|
||||
return new class
|
||||
{
|
||||
public $email = 'memeaigen.com@gmail.com';
|
||||
|
||||
public $id = 'xxx';
|
||||
|
||||
// public $email = 'patrick.christener@gmail.com';
|
||||
|
||||
// public $id = '104771940181889934768';
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
use App\Helpers\FirstParty\AI\RunwareAI;
|
||||
use App\Helpers\FirstParty\AspectRatio;
|
||||
use App\Helpers\FirstParty\Meme\MemeGenerator;
|
||||
use App\Helpers\FirstParty\Purchase\PurchaseHelper;
|
||||
use App\Models\Category;
|
||||
use App\Models\Meme;
|
||||
use App\Models\MemeMedia;
|
||||
@@ -18,6 +19,16 @@ public function index()
|
||||
//
|
||||
}
|
||||
|
||||
public function testPurchase()
|
||||
{
|
||||
$subscriptions = PurchaseHelper::getPricingPageSubscriptions();
|
||||
|
||||
$one_time = PurchaseHelper::getPricingPageOneTime();
|
||||
|
||||
dump($subscriptions);
|
||||
dump($one_time);
|
||||
}
|
||||
|
||||
public function getSuitableMemeMedia()
|
||||
{
|
||||
$meme = Meme::inRandomOrder()->first();
|
||||
@@ -68,7 +79,7 @@ public function writeMeme()
|
||||
{
|
||||
$meme_response = OpenAI::getSingleMemeGenerator('Write me 1 meme about adult life');
|
||||
|
||||
//dump($meme_response);
|
||||
// dump($meme_response);
|
||||
|
||||
$meme_output = json_decode(OpenAI::getOpenAIOutput($meme_response));
|
||||
|
||||
|
||||
132
app/Http/Controllers/UserPurchaseController.php
Normal file
132
app/Http/Controllers/UserPurchaseController.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\FirstParty\Purchase\PurchaseHelper;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
|
||||
class UserPurchaseController extends Controller
|
||||
{
|
||||
public function pricingPage(Request $request)
|
||||
{
|
||||
|
||||
$subscriptions = PurchaseHelper::getPricingPageSubscriptions();
|
||||
|
||||
return response()->json([
|
||||
'success' => [
|
||||
'data' => [
|
||||
'subscription' => $subscriptions[0],
|
||||
'one_times' => PurchaseHelper::getPricingPageOneTime(),
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
// SUBSCRIBE (RECURRING)
|
||||
|
||||
public function subscribe(Request $request)
|
||||
{
|
||||
$price_id = $request->input('price_id');
|
||||
|
||||
$payload = [
|
||||
'mode' => 'subscription',
|
||||
'success_url' => route('subscribe.success').'?'.'session_id={CHECKOUT_SESSION_ID}',
|
||||
'cancel_url' => route('subscribe.cancelled').'?'.'session_id={CHECKOUT_SESSION_ID}',
|
||||
'line_items' => [[
|
||||
'price' => $price_id,
|
||||
]],
|
||||
];
|
||||
|
||||
$checkout_session = Auth::user()->checkout([$price_id => 1], $payload);
|
||||
|
||||
Session::put('checkout_session_id', $checkout_session->id);
|
||||
|
||||
return response()->json([
|
||||
'success' => [
|
||||
'data' => [
|
||||
'redirect' => $checkout_session->url,
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function subscribeSuccess(Request $request)
|
||||
{
|
||||
if (! Session::has('checkout_session_id')) {
|
||||
abort(401);
|
||||
}
|
||||
|
||||
Session::forget('checkout_session_id');
|
||||
|
||||
return redirect()->route('home')->with('success', [
|
||||
'message' => 'Thank you for subscribing! Your subscription should be active momentarily. Please refresh the page if you do not see your plan.',
|
||||
'action' => 'subscription_success',
|
||||
]);
|
||||
}
|
||||
|
||||
public function subscribeCancelled(Request $request)
|
||||
{
|
||||
|
||||
if (Session::has('checkout_session_id')) {
|
||||
Session::forget('checkout_session_id');
|
||||
}
|
||||
|
||||
return redirect()->route('home')->with('error', [
|
||||
'message' => "You've decided not to complete the payment at this time. No charges have been made to your account.",
|
||||
'action' => 'subscription_cancelled',
|
||||
]);
|
||||
}
|
||||
|
||||
// PURCHASE (ONE TIME)
|
||||
|
||||
public function purchase(Request $request)
|
||||
{
|
||||
$price_id = $request->input('price_id');
|
||||
|
||||
$payload = [
|
||||
'success_url' => route('subscribe.success').'?'.'session_id={CHECKOUT_SESSION_ID}',
|
||||
'cancel_url' => route('subscribe.cancelled').'?'.'session_id={CHECKOUT_SESSION_ID}',
|
||||
];
|
||||
|
||||
$checkout_session = Auth::user()->checkout([$price_id => 1], $payload);
|
||||
|
||||
Session::put('checkout_session_id', $checkout_session->id);
|
||||
|
||||
return response()->json([
|
||||
'success' => [
|
||||
'data' => [
|
||||
'redirect' => $checkout_session->url,
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function purchaseSuccess(Request $request)
|
||||
{
|
||||
|
||||
if (! Session::has('checkout_session_id')) {
|
||||
abort(401);
|
||||
}
|
||||
|
||||
Session::forget('checkout_session_id');
|
||||
|
||||
return redirect()->route('home')->with('success', [
|
||||
'message' => 'Thank you for purchasing! Your purchase should be active momentarily. Please refresh the page if you do not see your plan.',
|
||||
'action' => 'purchase_success',
|
||||
]);
|
||||
}
|
||||
|
||||
public function purchaseCancelled(Request $request)
|
||||
{
|
||||
if (Session::has('checkout_session_id')) {
|
||||
Session::forget('checkout_session_id');
|
||||
}
|
||||
|
||||
return redirect()->route('home')->with('error', [
|
||||
'message' => "You've decided not to complete the payment at this time. No charges have been made to your account.",
|
||||
'action' => 'purchase_cancelled',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user