fix free subscribe issue
This commit is contained in:
@@ -7,7 +7,7 @@ use App\Models\Law;
|
|||||||
use App\Models\Notification;
|
use App\Models\Notification;
|
||||||
use App\Models\Category;
|
use App\Models\Category;
|
||||||
use App\Models\RecentArt;
|
use App\Models\RecentArt;
|
||||||
use App\Models\SubscribePlan;
|
use App\Models\SubscribePlan; // <-- Added this import
|
||||||
use App\Models\UserSubscriber;
|
use App\Models\UserSubscriber;
|
||||||
use App\Services\AppMarketPurchaseVerifier;
|
use App\Services\AppMarketPurchaseVerifier;
|
||||||
use App\Traits\BaseApiResponse;
|
use App\Traits\BaseApiResponse;
|
||||||
@@ -25,7 +25,7 @@ class HomeController extends Controller
|
|||||||
{
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
// 1. Check and grant 30-day free subscription if eligible
|
// --- NEW: Check and grant free subscription for existing logged-in users ---
|
||||||
$this->checkAndGrantFreeSubscription($user);
|
$this->checkAndGrantFreeSubscription($user);
|
||||||
|
|
||||||
$this->refreshMarketSubscriptionAfterResponse($user);
|
$this->refreshMarketSubscriptionAfterResponse($user);
|
||||||
@@ -73,32 +73,41 @@ class HomeController extends Controller
|
|||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
// 2. Fetch Active Subscription & Calculate Remaining Days
|
$freeSubscription = $user->userSubscribers()
|
||||||
$activeSubscribers = $user->userSubscribers()
|
->whereHas('subscribe', function ($query) {
|
||||||
->with('subscribe')
|
$query->where('is_free', true);
|
||||||
|
})
|
||||||
->where('expired_at', '>=', now())
|
->where('expired_at', '>=', now())
|
||||||
->get();
|
->first();
|
||||||
|
|
||||||
$current_plan = null;
|
$current_plan = null;
|
||||||
|
|
||||||
if ($activeSubscribers->isNotEmpty()) {
|
if ($freeSubscription) {
|
||||||
$latestSubscription = $activeSubscribers->sortByDesc('expired_at')->first();
|
$current_plan = [
|
||||||
$expiredDays = $activeSubscribers->sum(function ($subscriber) {
|
'id' => $freeSubscription->id,
|
||||||
|
'name' => $freeSubscription->subscribe->name,
|
||||||
|
'price' => $freeSubscription->subscribe->price,
|
||||||
|
'expired_day' => $freeSubscription->expired_at->diffInDays(now()),
|
||||||
|
'is_free' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$latestSubscription = $user->userSubscribers()
|
||||||
|
->where('expired_at', '>=', now())
|
||||||
|
->latest('expired_at')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
$expiredDays = UserSubscriber::query()->where('user_id', $user->id)->where('expired_at', '>=', now())->get()->sum(function ($subscriber) {
|
||||||
return $subscriber->expired_at->diffInDays(now());
|
return $subscriber->expired_at->diffInDays(now());
|
||||||
});
|
});
|
||||||
|
|
||||||
// Accurately check if the active plan is free
|
if ($latestSubscription) {
|
||||||
$isFree = (bool) (
|
|
||||||
$latestSubscription->is_free ||
|
|
||||||
($latestSubscription->subscribe && $latestSubscription->subscribe->is_free)
|
|
||||||
);
|
|
||||||
|
|
||||||
$current_plan = [
|
$current_plan = [
|
||||||
'id' => $latestSubscription->id,
|
'id' => $latestSubscription->id,
|
||||||
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
|
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
|
||||||
'price' => $latestSubscription->subscribe->price ?? 0,
|
'price' => $latestSubscription->subscribe->price ?? 100,
|
||||||
'expired_day' => $expiredDays,
|
'expired_day' => $expiredDays,
|
||||||
'is_free' => $isFree,
|
'is_free' => (bool)$latestSubscription->is_free,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,8 +124,8 @@ class HomeController extends Controller
|
|||||||
|
|
||||||
return $this->success([
|
return $this->success([
|
||||||
'home' => $homeData,
|
'home' => $homeData,
|
||||||
'hoqoqi_list' => $this->getCategoryList(1),
|
'hoqoqi_list' => $this->getCategoryList(1), // Category 1
|
||||||
'keyfari_list' => $this->getCategoryList(2),
|
'keyfari_list' => $this->getCategoryList(2), // Category 2
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +161,7 @@ class HomeController extends Controller
|
|||||||
{
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
// 1. Check and grant 30-day free subscription if eligible
|
// --- NEW: Check and grant free subscription for existing logged-in users ---
|
||||||
$this->checkAndGrantFreeSubscription($user);
|
$this->checkAndGrantFreeSubscription($user);
|
||||||
|
|
||||||
$this->refreshMarketSubscriptionAfterResponse($user);
|
$this->refreshMarketSubscriptionAfterResponse($user);
|
||||||
@@ -200,32 +209,41 @@ class HomeController extends Controller
|
|||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
// 2. Fetch Active Subscription & Calculate Remaining Days
|
$freeSubscription = $user->userSubscribers()
|
||||||
$activeSubscribers = $user->userSubscribers()
|
->whereHas('subscribe', function ($query) {
|
||||||
->with('subscribe')
|
$query->where('is_free', true);
|
||||||
|
})
|
||||||
->where('expired_at', '>=', now())
|
->where('expired_at', '>=', now())
|
||||||
->get();
|
->first();
|
||||||
|
|
||||||
$current_plan = null;
|
$current_plan = null;
|
||||||
|
|
||||||
if ($activeSubscribers->isNotEmpty()) {
|
if ($freeSubscription) {
|
||||||
$latestSubscription = $activeSubscribers->sortByDesc('expired_at')->first();
|
$current_plan = [
|
||||||
$expiredDays = $activeSubscribers->sum(function ($subscriber) {
|
'id' => $freeSubscription->id,
|
||||||
|
'name' => $freeSubscription->subscribe->name,
|
||||||
|
'price' => $freeSubscription->subscribe->price,
|
||||||
|
'expired_day' => $freeSubscription->expired_at->diffInDays(now()),
|
||||||
|
'is_free' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$latestSubscription = $user->userSubscribers()
|
||||||
|
->where('expired_at', '>=', now())
|
||||||
|
->latest('expired_at')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
$expiredDays = UserSubscriber::query()->where('user_id', $user->id)->where('expired_at', '>=', now())->get()->sum(function ($subscriber) {
|
||||||
return $subscriber->expired_at->diffInDays(now());
|
return $subscriber->expired_at->diffInDays(now());
|
||||||
});
|
});
|
||||||
|
|
||||||
// Accurately check if the active plan is free
|
if ($latestSubscription) {
|
||||||
$isFree = (bool) (
|
|
||||||
$latestSubscription->is_free ||
|
|
||||||
($latestSubscription->subscribe && $latestSubscription->subscribe->is_free)
|
|
||||||
);
|
|
||||||
|
|
||||||
$current_plan = [
|
$current_plan = [
|
||||||
'id' => $latestSubscription->id,
|
'id' => $latestSubscription->id,
|
||||||
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
|
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
|
||||||
'price' => $latestSubscription->subscribe->price ?? 0,
|
'price' => $latestSubscription->subscribe->price ?? 100,
|
||||||
'expired_day' => $expiredDays,
|
'expired_day' => $expiredDays,
|
||||||
'is_free' => $isFree,
|
'is_free' => (bool)$latestSubscription->is_free,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,6 +273,7 @@ class HomeController extends Controller
|
|||||||
$freePlan = SubscribePlan::where('is_free', true)->where('is_active', true)->first();
|
$freePlan = SubscribePlan::where('is_free', true)->where('is_active', true)->first();
|
||||||
|
|
||||||
if ($freePlan) {
|
if ($freePlan) {
|
||||||
|
// Exactly matching the logic to keep the UI clean
|
||||||
$user->userSubscribers()->create([
|
$user->userSubscribers()->create([
|
||||||
'subscribe_plan_id' => $freePlan->id,
|
'subscribe_plan_id' => $freePlan->id,
|
||||||
'expired_at' => now()->addDays($freePlan->expired_day + 1),
|
'expired_at' => now()->addDays($freePlan->expired_day + 1),
|
||||||
|
|||||||
Reference in New Issue
Block a user