$o=["candidate_admin"=>null,"subscription_attempt"=>null,"nfc_attempt"=>null,"logs"=>[],"errors"=>[]];try{$u=App\Models\User::whereHas("roles",function($q){$q->where("name","admin");})->first()?:App\Models\User::whereNotNull("tenant_id")->first();if(!$u)throw new Exception("No candidate user found");Illuminate\Support\Facades\Auth::login($u);$o["candidate_admin"]=["id"=>$u->id,"email"=>$u->email];try{$p=App\Models\Plan::where("status",App\Models\Plan::IS_ACTIVE)->where("price",">",0)->whereHas("currency")->first();if(!$p)throw new Exception("No paid active plan found");$m=(int)App\Models\Subscription::max("id");$r=Illuminate\Http\Request::create("/subscription/paymob/initiate","POST",["planId"=>$p->id,"name"=>(string)($u->full_name??$u->first_name??"Test User"),"email"=>(string)$u->email,"phone"=>"201000000000","address"=>"Test Address Cairo"]);$res=app(App\Http\Controllers\SubscriptionController::class)->paymobInitiate($r);$url=method_exists($res,"getTargetUrl")?$res->getTargetUrl():null;$s=App\Models\Subscription::where("id",">",$m)->latest("id")->first();$o["subscription_attempt"]=["result_type"=>is_object($res)?get_class($res):gettype($res),"redirect_url"=>$url,"contains_accept_paymob"=>is_string($url)&&str_contains($url,"accept.paymob.com"),"new_subscription"=>$s?["id"=>$s->id,"status"=>$s->status,"payment_type"=>$s->payment_type,"transaction_id"=>$s->transaction_id,"plan_id"=>$s->plan_id,"created_at"=>optional($s->created_at)->toDateTimeString()]:null];}catch(Throwable $e){$o["errors"][]="subscription_attempt: ".$e->getMessage();}try{$v=App\Models\Vcard::where("tenant_id",$u->tenant_id)->first();$c=App\Models\Nfc::first();if(!$v||!$c)throw new Exception("Missing vcard or nfc card");$m2=(int)App\Models\NfcOrders::max("id");$nr=App\Http\Requests\NfcOrderRequest::create("/nfc/orders","POST",["card_type"=>$c->id,"name"=>(string)($u->full_name??$u->first_name??"Test User"),"phone"=>2010000001,"region_code"=>"+20","email"=>(string)$u->email,"address"=>"Cairo, Egypt","quantity"=>1,"vcard_id"=>$v->id,"company_name"=>"Test Company","designation"=>"Manager","payment_method"=>18]);$nres=app(App\Http\Controllers\NfcOrdersController::class)->store($nr);$j=method_exists($nres,"getData")?$nres->getData(true):null;$nurl=data_get($j,"data.redirect_url");$ord=App\Models\NfcOrders::where("id",">",$m2)->latest("id")->first();$tx=$ord?App\Models\NfcOrderTransaction::where("nfc_order_id",$ord->id)->latest("id")->first():null;$o["nfc_attempt"]=["store_response_json"=>$j,"redirect_url"=>$nurl,"contains_accept_paymob"=>is_string($nurl)&&str_contains($nurl,"accept.paymob.com"),"new_nfc_order"=>$ord?["id"=>$ord->id,"user_id"=>$ord->user_id,"vcard_id"=>$ord->vcard_id,"card_type"=>$ord->card_type,"order_status"=>$ord->order_status,"created_at"=>optional($ord->created_at)->toDateTimeString()]:null,"nfc_transaction_for_order"=>$tx?["id"=>$tx->id,"nfc_order_id"=>$tx->nfc_order_id,"type"=>$tx->type,"status"=>$tx->status,"transaction_id"=>$tx->transaction_id,"created_at"=>optional($tx->created_at)->toDateTimeString()]:null];}catch(Throwable $e){$o["errors"][]="nfc_attempt: ".$e->getMessage();}try{$lp=storage_path("logs/laravel.log");$pat=["Paymob subscription initiate failed","Paymob callback resolved flow","NFC Paymob","payment_not_complete"];if(is_file($lp)){$ln=@file($lp,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[];$m=[];for($i=count($ln)-1;$i>=0;$i--){$line=$ln[$i];foreach($pat as $pp){if(stripos($line,$pp)!==false){$m[]=$line;break;}}if(count($m)>=20)break;}$o["logs"]=array_values(array_reverse($m));}}catch(Throwable $e){$o["errors"][]="logs: ".$e->getMessage();}}catch(Throwable $e){$o["errors"][]="fatal: ".$e->getMessage();}echo json_encode($o,JSON_UNESCAPED_SLASHES).PHP_EOL;