Viewing file: PlanValidate.php (1.8 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Middleware;
use App\Models\BecameReseller;
use App\Models\BillingRequest;
use App\Models\Plan;
use Closure;
use Illuminate\Http\Request;
class PlanValidate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$customer=auth('customer')->user();
$cache_in_seconds = env('CACHE_TIME');
$cacheCustomerPLan = cache('current_plan_'.$customer->id);
if (is_null($cacheCustomerPLan)) {
$customerPLan=$customer->plan;
$customerPLan = cache()->remember('current_plan_'.$customer->id, $cache_in_seconds, function () use ($customerPLan) {
return $customerPLan;
});
} else {
$customerPLan = $cacheCustomerPLan;
}
if(!$customerPLan){
$billingRequest=BillingRequest::where('customer_id', $customer->id)->where('status', 'pending')->first();
if($billingRequest){
$plan = Plan::find($billingRequest->plan_id);
return redirect()->route('customer.trigger.plan',['plan_id'=>$plan->id]);
}else{
return redirect()->route('customer.billing.index');
}
}else{
if(isset($customerPLan->expire_date) && $customerPLan->expire_date < now()){
return redirect()->route('customer.billing.index')->withErrors(['failed'=>'Your plan expired, please renew first']);
}
}
return $next($request);
}
}
|