Edit File: CitcServices.php
<?php namespace App\Services; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use Illuminate\Support\Facades\Http; use Carbon\Carbon; use App\Models\Region; use App\Models\City; use App\Models\Cartype; use App\Models\DelegateJoinrequest; use App\Models\Order; use App\Models\User; use App\Models\Store; use DateTime; class CitcServices{ //credential public $name = 'jood-app'; public $password = ''; // generals in one function public function generals (){ //regions && cities $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/regions' ,[ 'credential' => $credential ]); $data =json_decode($response , true); foreach ($data['data'] as $once) { $region = Region::create([ 'name' => [ 'ar' => $once['nameAr'], 'en' => $once['nameEn'], ], 'citc_regionId' => $once['id'], 'country_id' => 1, ]); //cities $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/cities' ,[ 'credential' => $credential, "regionId"=>$region->citc_regionId ]); $data =json_decode($response , true); foreach ($data['data'] as $once) { City::create([ 'name' => [ 'ar' => $once['nameAr'], 'en' => $once['nameEn'], ], 'citc_cityId' => $once['id'], 'region_id' => $region->id, ]); } } return true; } public function carTypes() { $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; //cartypes $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/car-types' ,[ 'credential' => $credential ]); $data =json_decode($response , true); foreach ($data['data'] as $once) { $region = Cartype::create([ 'name' => [ 'ar' => $once['nameAr'], 'en' => $once['nameEn'], ], 'citc_carTypeId' => $once['id'], ]); } } //**************************************** start Drivers ***********************************// public function createDriver($id) { $headers = [ 'Content-Type' => 'application/json' ]; $userMeta = DelegateJoinrequest::where('user_id',$id)->first(); $user = $userMeta->user; if(!$user){ return true; } # Setup request to send json via POST. $driver = []; $identityTypeId = 'oIcaYzeDfQQ='; $driver['identityTypeId'] = $userMeta->nationality?->citc_identityTypeId ?? $identityTypeId; $driver['idNumber'] = $userMeta->identity_card; $driver_date_of_birth = (int) str_replace('-', '', $userMeta->driver_date_of_birth); $driver['dateOfBirth'] = $driver_date_of_birth; $datetime = new DateTime($user->created_at?$user->created_at:$userMeta->created_at); $created_at = $datetime->format(DateTime::ATOM); $driver['registrationDate'] = $created_at; $driver['mobile'] = '0'.$userMeta->phone; $region_id = $userMeta->region?$userMeta->region->citc_regionId:''; $driver['regionId'] = $region_id ?? "Nap4gA1tyeY="; $driver['carTypeId'] = $userMeta->cartype?->citc_carTypeId ; $city_id = $userMeta->city?$userMeta->city->citc_cityId:''; $driver['cityId'] = $city_id ?? 'Nap4gA1tyeY='; $driver['carNumber'] = $userMeta->car_numbers.str_replace('/', '', $userMeta->car_letters); $driver['vehicleSequenceNumber'] = "123456789"; $credential= ['companyName' => 'Everything' , "password"=>"Cc7qQxyD40jEFARlC5H4KzHVzQ0zKhV7gZJKPcJVh9m0yrG9hOejUxsoNQOH5uAA"]; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/driver/create' , [ 'credential' =>$credential , 'driver' => $driver, ]); $results =json_decode($response ); if($results && $results->status == true){ if($results->data && $results->data->refrenceCode){ $userMeta->citc_refrenceCode = $results->data->refrenceCode; $userMeta->update(); /*send notification to provider with admin agree*/ $notify_title = setting('site_title'); $message_ar = 'تم إرسال معلوماتك للجهة المختصة وسنقوم بإشعارك عند الموافقة على التصريح.'; $message_en = 'Your information has been sent to the appropriate authority and we will notify you when the permit is approved.'; $data = ['title' => $notify_title,'message_en'=>$message_en,'message_ar'=>$message_ar,'key'=>'citcCreateDriverSent']; } }elseif($results && $results->status == false){ $userMeta->citc_errorCodes = json_encode($results->errorCodes); $userMeta->update(); if(in_array(61,$results->errorCodes)){ /*send notification to provider with admin agree*/ $notify_title = setting('site_title'); $message_ar = 'نعتذر لعدم قبول طلب التصريح بسبب إدخالك لتاريخ ميلادٍ خاطئ.'; $message_en = 'We apologize for not accepting the permission request because you entered the wrong birthday.'; $data = ['title' => $notify_title,'message_en'=>$message_en,'message_ar'=>$message_ar,'key'=>'citcCreateDriverFailed61']; } if(in_array(44,$results->errorCodes)){ /*send notification to provider with admin agree*/ $notify_title = setting('site_title'); $message_ar = 'نعتذر لعدم قبول طلب التصريح من الجهات المختصة.'; $message_en = 'We apologize for not accepting the permit request from the competent authorities.'; $data = ['title' => $notify_title,'message_en'=>$message_en,'message_ar'=>$message_ar,'key'=>'citcCreateDriverFailed44']; } if(in_array(76,$results->errorCodes)){ /*send notification to provider with admin agree*/ $notify_title = setting('site_title'); $message_ar = 'مندوب '.setting('site_title').' العزيز، طلب التصريح الخاص بك في طور قبولِه من قِبل الجهات المختصة.'; $message_en = 'Dear '.setting('site_title').'delegate, Your permit application is in the process of being accepted by the competent authorities.'; $data = ['title' => $notify_title,'message_en'=>$message_en,'message_ar'=>$message_ar,'key'=>'citcCreateDriverFailed76']; } } return true; } public function editDriver($id) { $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $userMeta = DelegateJoinrequest::where('user_id',$id)->first(); $user = $userMeta->user; if(!$user){ return true; } # Setup request to send json via POST. $driver = []; $identityTypeId = 'oIcaYzeDfQQ='; $driver['identityTypeId'] = $userMeta->nationality?->citc_identityTypeId ?? $identityTypeId; $driver['idNumber'] = $userMeta->identity_card; $driver_date_of_birth = (int) str_replace('-', '', $userMeta->driver_date_of_birth); $driver['dateOfBirth'] = $driver_date_of_birth; $datetime = new DateTime($user->created_at?$user->created_at:$userMeta->created_at); $created_at = $datetime->format(DateTime::ATOM); $driver['registrationDate'] = $created_at; $driver['mobile'] = '0'.$userMeta->phone; $region_id = $userMeta->region?$userMeta->region->citc_regionId:''; $driver['regionId'] = $region_id ?? "Nap4gA1tyeY="; $driver['carTypeId'] = $userMeta->cartype?->citc_carTypeId ; $city_id = $userMeta->city?$userMeta->city->citc_cityId:''; $driver['cityId'] = $city_id ?? 'Nap4gA1tyeY='; $driver['carNumber'] = $userMeta->car_numbers.str_replace('/', '', $userMeta->car_letters); $driver['vehicleSequenceNumber'] = "123456789"; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/driver/edit' , [ 'credential' =>$credential , 'driver' => $driver, ]); $results =json_decode($response ); if($results && $results->status == true){ if($results->data && $results->data->refrenceCode){ $userMeta->citc_refrenceCode = $results->data->refrenceCode; $userMeta->update(); /*send notification to provider with admin agree*/ $notify_title = setting('site_title'); $message_ar = 'تم إرسال معلوماتك للجهة المختصة وسنقوم بإشعارك عند الموافقة على التصريح.'; $message_en = 'Your information has been sent to the appropriate authority and we will notify you when the permit is approved.'; $data = ['title' => $notify_title,'message_en'=>$message_en,'message_ar'=>$message_ar,'key'=>'citcCreateDriverSent']; } }elseif($results && $results->status == false){ $userMeta->citc_errorCodes = json_encode($results->errorCodes); $userMeta->update(); if(in_array(61,$results->errorCodes)){ /*send notification to provider with admin agree*/ $notify_title = setting('site_title'); $message_ar = 'نعتذر لعدم قبول طلب التصريح بسبب إدخالك لتاريخ ميلادٍ خاطئ.'; $message_en = 'We apologize for not accepting the permission request because you entered the wrong birthday.'; $data = ['title' => $notify_title,'message_en'=>$message_en,'message_ar'=>$message_ar,'key'=>'citcCreateDriverFailed61']; } if(in_array(44,$results->errorCodes)){ /*send notification to provider with admin agree*/ $notify_title = setting('site_title'); $message_ar = 'نعتذر لعدم قبول طلب التصريح من الجهات المختصة.'; $message_en = 'We apologize for not accepting the permit request from the competent authorities.'; $data = ['title' => $notify_title,'message_en'=>$message_en,'message_ar'=>$message_ar,'key'=>'citcCreateDriverFailed44']; } if(in_array(76,$results->errorCodes)){ /*send notification to provider with admin agree*/ $notify_title = setting('site_title'); $message_ar = 'مندوب '.setting('site_title').' العزيز، طلب التصريح الخاص بك في طور قبولِه من قِبل الجهات المختصة.'; $message_en = 'Dear '.setting('site_title').'delegate, Your permit application is in the process of being accepted by the competent authorities.'; $data = ['title' => $notify_title,'message_en'=>$message_en,'message_ar'=>$message_ar,'key'=>'citcCreateDriverFailed76']; } } return true; } public function getDriver($id) { $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $userMeta = DelegateJoinrequest::where('user_id',$id)->first(); $user = $userMeta->user; if(!$user){ return true; } $idNumber = $userMeta->identity_card; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/driver/get' , [ 'credential' =>$credential , 'idNumber' => $idNumber, ]); $results =json_decode($response ); } public function deactiveDriver($id) { $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $userMeta = DelegateJoinrequest::where('user_id',$id)->first(); $user = $userMeta->user; if(!$user){ return true; } $idNumber = $userMeta->identity_card; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/driver/deactive' , [ 'credential' =>$credential , 'idNumber' => $idNumber, ]); $results =json_decode($response ); } //**************************************** End Drivers ***********************************// //**************************************** start Orders ***********************************// public function createOrder($id) { $order = Order::find($id); $user = User::find($order->user_id); $orderarr = []; if(isset($order->store_id)){ if ($store = Store::find($order->store_id)) { $storetName = $store->name; } } else { $storetName = $order->store_name; } $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $orderarr['storetName'] = $storetName; $orderarr['authorityId'] = "K752JTDp22o="; $orderarr['categoryId'] = "w+mTCW1569Y="; $orderarr['orderNumber'] = (string)$order->id; $coordinates = $order->deliver_lat . ',' . $order->deliver_long; $storeLocation = $order->receive_lat . ',' . $order->receive_long; $datetime = new DateTime(Carbon::parse($order->created_at)->addHour($order->deliver_time)); $created_at = $datetime->format(DateTime::ATOM); $orderarr['deliveryTime'] = $created_at; $city = City::first(); $cityId = $city->citc_cityId; if ($region = Region::find($city->region_id)) { $regionId = $region->citc_regionId; } $orderarr['regionId'] = $regionId; $orderarr['cityId'] = $cityId; $orderarr['coordinates'] = $coordinates; $orderarr['storeLocation'] = $storeLocation; $orderarr['orderDate'] = Carbon::now(); // $orderarr['recipientMobileNumber'] = '966533336485'; // incase live add '966'.$user->phone $orderarr['recipientMobileNumber'] = '966'.$user->phone; // incase live add '966'.$user->phone $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/order/create' , [ 'credential' =>$credential , 'order' => $orderarr, ]); $results =json_decode($response ); if ($results && $results->status == true) { $order->citc_referenceCode = $results->data->referenceCode; $order->citc_accept_order = 'true'; $order->update(); } return true; } public function acceptOrder($id) { $order = Order::find($id); $user = User::find($order->user_id); $orderarr = []; if ($store = Store::find($order->store_id)) { $storetName = $store->name; } $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/order/accept' , [ 'credential' =>$credential , 'order' => $order, ]); $results =json_decode($response ); } public function rejectOrder($id) { $order = Order::find($id); $user = User::find($order->user_id); $orderarr = []; if ($store = Store::find($order->store_id)) { $storetName = $store->name; } $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/order/reject' , [ 'credential' =>$credential , 'order' => $order, ]); $results =json_decode($response ); } public function executeOrder($id) { $order = Order::find($id); $user = User::find($order->user_id); $orderarr = []; if ($store = Store::find($order->store_id)) { $storetName = $store->name; } $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $orderExecutionData = [ "referenceCode" => $order->citc_referenceCode, "executionTime" => Carbon::now(), "paymentMethodId" => "NV25GlPuOnQ=", "price" => (int)$order->total_price, "priceWithoutDelivery" => (int)$order->price, "deliveryPrice"=> (int) ($order->delivery_price + $order->added_value + $order->app_percentage), "driverIncome" => (int) $order->delivery_price, ]; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/order/execute' , [ 'credential' =>$credential , 'orderExecutionData' => $orderExecutionData, ]); $results =json_decode($response ); } public function assignDriverToOrder($id) { $order = Order::find($id); $idNumber = ""; if ($delegate = User::find($order->delegate_id)) { $join = DelegateJoinrequest::where('user_id' , $order->delegate_id)->first(); $idNumber = $join->identity_card; } $coordinates = $order->deliver_lat . ',' . $order->deliver_long; $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $referenceCode = $order->citc_referenceCode; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/order/assign-driver-to-order' , [ 'credential' =>$credential , 'referenceCode' => $referenceCode, 'idNumber' => $idNumber, ]); $results =json_decode($response ); } public function cancelOrder($id , $cancel_id = null) { $order = Order::find($id); $headers = [ 'Content-Type' => 'application/json' ]; $credential= ['companyName' => $this->name, "password"=> $this->password, ]; $orderId = $order->id; $cancelationReasonId = "NV25GlPuOnQ="; $response = Http::withoutVerifying()->withHeaders($headers)->post('https://citc.4hoste.com/api/order/cancel' , [ 'credential' =>$credential , 'orderId ' => $orderId, 'cancelationReasonId ' => $cancelationReasonId, ]); $results =json_decode($response ); } //**************************************** End Orders ***********************************// }
Back to File Manager