public function providerTripChangeState() { $date = date("Y-m-d H:i:s"); $time_limit = date("Y-m-d H:i:s", strtotime($date) - 3 * 60 * 60); $walker_id = Session::get('walker_id'); $state = $request_id = Request::segment(4); $current_request = Requests::where('confirmed_walker', $walker_id)->where('is_cancelled', 0)->where('is_dog_rated', 0)->where('created_at', '>', $time_limit)->orderBy('created_at', 'desc')->where(function ($query) { $query->where('status', 0)->orWhere(function ($query_inner) { $query_inner->where('status', 1)->where('is_dog_rated', 0); }); })->first(); if ($current_request && $state) { if ($state == 2) { $current_request->is_walker_started = 1; $owner = Owner::find($current_request->owner_id); $walker = Walker::find($walker_id); $location = get_location($owner->latitude, $owner->longitude); $latitude = $location['lat']; $longitude = $location['long']; $angle = get_angle($walker->latitude, $walker->longitude, $latitude, $longitude); $walker->old_latitude = $walker->latitude; $walker->old_longitude = $walker->longitude; $walker->latitude = $latitude; $walker->longitude = $longitude; $walker->bearing = $angle; $walker->save(); $walk_location = new WalkLocation(); $walk_location->request_id = $current_request->id; $walk_location->latitude = $latitude; $walk_location->longitude = $longitude; $walk_location->distance = 0; $walk_location->save(); } if ($state == 3) { $current_request->is_walker_arrived = 1; } if ($state == 4) { $current_request->is_started = 1; } if ($state == 6) { $rating = 0; if (Input::has('rating')) { $rating = Input::get('rating'); } $current_request->is_dog_rated = 1; $current_request->save(); $review_dog = new DogReview(); $review_dog->walker_id = $current_request->confirmed_walker; $review_dog->comment = Input::get('review'); $review_dog->rating = $rating; $review_dog->owner_id = $current_request->owner_id; $review_dog->request_id = $current_request->id; $review_dog->save(); if ($rating) { if ($owner = Owner::find($current_request->owner_id)) { $old_rate = $owner->rate; $old_rate_count = $owner->rate_count; $new_rate_counter = $owner->rate_count + 1; $new_rate = ($owner->rate * $owner->rate_count + $rating) / $new_rate_counter; $owner->rate_count = $new_rate_counter; $owner->rate = $new_rate; $owner->save(); } } $message = "You has successfully rated the owner."; $type = "success"; return Redirect::to('/provider/trips')->with('message', $message)->with('type', $type); } if ($state == 5) { $request_services = RequestServices::where('request_id', $current_request->id)->first(); $request_typ = ProviderType::where('id', '=', $request_services->req_typ)->first(); $address = urlencode(Input::get('address')); $end_address = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}"), TRUE); $end_location = $end_address['results'][0]['geometry']; $latitude = $end_location['location']['lat']; $longitude = $end_location['location']['lng']; $location = get_location($latitude, $longitude); $latitude = $location['lat']; $longitude = $location['long']; $request_id = $current_request->id; $walk_location_last = WalkLocation::where('request_id', $request_id)->orderBy('created_at', 'desc')->first(); if ($walk_location_last) { $distance_old = $walk_location_last->distance; $distance_new = distanceGeoPoints($walk_location_last->latitude, $walk_location_last->longitude, $latitude, $longitude); $distance = $distance_old + $distance_new; $settings = Settings::where('key', 'default_distance_unit')->first(); //$unit = $settings->value; $distance = $distance; } else { $distance = 0; } $walker = Walker::find($walker_id); $angle = get_angle($walker->latitude, $walker->longitude, $latitude, $longitude); $walker->old_latitude = $walker->latitude; $walker->old_longitude = $walker->longitude; $walker->latitude = $latitude; $walker->longitude = $longitude; $walker->bearing = $angle; $walker->save(); $walk_location = new WalkLocation(); $walk_location->request_id = $request_id; $walk_location->latitude = $latitude; $walk_location->longitude = $longitude; $walk_location->distance = $distance; $walk_location->save(); Walker::where('id', '=', $walker_id)->update(array('is_available' => 1)); // Calculate Rerquest Stats $time = 0; $time_query = "SELECT TIMESTAMPDIFF(SECOND,MIN(created_at),MAX(created_at)) as diff\n\t\t\t\tFROM walk_location where request_id = {$current_request->id}\n\t\t\t\tGROUP BY request_id limit 1 "; $time_data = DB::select(DB::raw($time_query)); foreach ($time_data as $time_diff) { $time = $time_diff->diff; } $time = $time / 60; /* TIME CALCULATION REDIRECTED */ $time = 0; /* TIME CALCULATION REDIRECTED END */ $walker_data = Walker::find($current_request->confirmed_walker); $provider_type = ProviderServices::where('type', $walker_data->type)->where('provider_id', $walker_id)->first(); if ($provider_type == NULL) { /* $settings = Settings::where('key', 'price_per_unit_distance')->first(); $price_per_unit_distance = $settings->value; $settings = Settings::where('key', 'price_per_unit_time')->first(); $price_per_unit_time = $settings->value; $settings = Settings::where('key', 'base_price')->first(); $base_price = $settings->value; */ $setbase_distance = $request_typ->base_distance; $base_price = $request_typ->base_price; $price_per_unit_distance = $request_typ->price_per_unit_distance; $price_per_unit_time = $request_typ->price_per_unit_time; } else { // $setbase_distance = $request_typ->base_distance; $setbase_distance = ""; $provider_type = ProviderServices::where('type', $walker_data->type)->where('provider_id', $walker_id)->first(); $base_price = $provider_type->base_price; $price_per_unit_distance = $provider_type->price_per_unit_distance; $price_per_unit_time = $provider_type->price_per_unit_time; } $settings = Settings::where('key', 'default_charging_method_for_users')->first(); $pricing_type = $settings->value; $settings = Settings::where('key', 'default_distance_unit')->first(); //$unit = $settings->value; $distance = convert($distance); if ($pricing_type == 1) { if ($distance <= $setbase_distance) { $distance_cost = 0; } else { $distance_cost = $price_per_unit_distance * ($distance - $setbase_distance); } $time_cost = $price_per_unit_time * $time; $total = $base_price + $distance_cost + $time_cost; } else { $distance_cost = 0; $time_cost = 0; $total = $base_price; } $current_request->is_completed = 1; $current_request->distance = $distance; $current_request->time = $time; $request_services->base_price = $base_price; $request_services->distance_cost = $distance_cost; $request_services->time_cost = $time_cost; $request_services->total = $total; $current_request->total = $total; $request_services->save(); // charge client // charge client $ledger = Ledger::where('owner_id', $current_request->owner_id)->first(); if ($ledger) { $balance = $ledger->amount_earned - $ledger->amount_spent; if ($balance > 0) { if ($total > $balance) { $ledger_temp = Ledger::find($ledger->id); $ledger_temp->amount_spent = $ledger_temp->amount_spent + $balance; $ledger_temp->save(); $total = $total - $balance; } else { $ledger_temp = Ledger::find($ledger->id); $ledger_temp->amount_spent = $ledger_temp->amount_spent + $total; $ledger_temp->save(); $total = 0; } } } $promo_discount = 0; if ($pcode = PromoCodes::where('id', $current_request->promo_code)->where('type', 1)->first()) { $discount = $pcode->value / 100; $promo_discount = $total * $discount; $total = $total - $promo_discount; if ($total < 0) { $total = 0; } } $current_request->total = $total; $current_request->save(); $cod_sett = Settings::where('key', 'cod')->first(); $allow_cod = $cod_sett->value; if ($current_request->payment_mode == 1 and $allow_cod == 1) { // Pay by Cash $current_request->is_paid = 1; Log::info('allow_cod'); } elseif ($current_request->payment_mode == 2) { // paypal Log::info('paypal payment'); } else { Log::info('normal payment. Stored cards'); // stored cards if ($total == 0) { $current_request->is_paid = 1; } else { $payment_data = Payment::where('owner_id', $current_request->owner_id)->where('is_default', 1)->first(); if (!$payment_data) { $payment_data = Payment::where('owner_id', $current_request->owner_id)->first(); } if ($payment_data) { $customer_id = $payment_data->customer_id; $setransfer = Settings::where('key', 'transfer')->first(); $transfer_allow = $setransfer->value; if (Config::get('app.default_payment') == 'stripe') { //dd($customer_id); Stripe::setApiKey(Config::get('app.stripe_secret_key')); try { $charge = Stripe_Charge::create(array("amount" => ceil($total * 100), "currency" => "usd", "customer" => $customer_id)); Log::info($charge); } catch (Stripe_InvalidRequestError $e) { // Invalid parameters were supplied to Stripe's API $ownr = Owner::find($current_request->owner_id); $ownr->debt = $total; $ownr->save(); $message = array('error' => $e->getMessage()); $type = "success"; Log::info($message); return Redirect::to('/provider/tripinprogress')->with('message', $message)->with('type', $type); } $current_request->is_paid = 1; $settng = Settings::where('key', 'service_fee')->first(); $settng_mode = Settings::where('key', 'payment_mode')->first(); if ($settng_mode->value == 2 and $transfer_allow == 1) { $transfer = Stripe_Transfer::create(array("amount" => ($total - $settng->value * $total / 100) * 100, "currency" => "usd", "recipient" => $walker_data->merchant_id)); $current_request->transfer_amount = $total - $settng->value * $total / 100; } } else { try { Braintree_Configuration::environment(Config::get('app.braintree_environment')); Braintree_Configuration::merchantId(Config::get('app.braintree_merchant_id')); Braintree_Configuration::publicKey(Config::get('app.braintree_public_key')); Braintree_Configuration::privateKey(Config::get('app.braintree_private_key')); if ($settng_mode->value == 2 and $transfer_allow == 1) { $sevisett = Settings::where('key', 'service_fee')->first(); $service_fee = $sevisett->value * $total / 100; $result = Braintree_Transaction::sale(array('amount' => $total - $service_fee, 'paymentMethodNonce' => $customer_id, 'merchantAccountId' => $walker_data->merchant_id, 'options' => array('submitForSettlement' => true, 'holdInEscrow' => true), 'serviceFeeAmount' => $service_fee)); } else { $result = Braintree_Transaction::sale(array('amount' => $total, 'paymentMethodNonce' => $customer_id)); } if ($result->success) { $request->is_paid = 1; } else { $request->is_paid = 0; } } catch (Exception $e) { $message = "Something went wrong in the payment. Please try again."; $type = "success"; return Redirect::to('/provider/tripinprogress')->with('message', $message)->with('type', $type); } } $current_request->card_payment = $total; $current_request->ledger_payment = $current_request->total - $total; } } } $current_request->save(); } $current_request->save(); } return Redirect::to('/provider/tripinprogress'); }
public function set_dog_rating() { if (Request::isMethod('post')) { $comment = ""; if (Input::has('comment')) { $comment = Input::get('comment'); } $request_id = Input::get('request_id'); $rating = 0; if (Input::has('rating')) { $rating = Input::get('rating'); } $token = Input::get('token'); $walker_id = Input::get('id'); $validator = Validator::make(array('request_id' => $request_id, 'token' => $token, 'walker_id' => $walker_id), array('request_id' => 'required|integer', 'token' => 'required', 'walker_id' => 'required|integer')); /* $var = Keywords::where('id', 1)->first(); */ if ($validator->fails()) { $error_messages = $validator->messages()->all(); $response_array = array('success' => false, 'error' => 'Invalid Input', 'error_code' => 401, 'error_messages' => $error_messages); $response_code = 200; } else { $is_admin = $this->isAdmin($token); if ($walker_data = $this->getWalkerData($walker_id, $token, $is_admin)) { // check for token validity if (is_token_active($walker_data->token_expiry) || $is_admin) { // Do necessary operations if ($request = Requests::find($request_id)) { if ($request->confirmed_walker == $walker_id) { if ($request->is_dog_rated == 0) { $owner = Owner::find($request->owner_id); $dog_review = new DogReview(); $dog_review->request_id = $request_id; $dog_review->walker_id = $walker_id; $dog_review->rating = $rating; $dog_review->owner_id = $owner->id; $dog_review->comment = $comment; $dog_review->save(); $request->is_dog_rated = 1; $request->save(); if ($rating) { if ($owner = Owner::find($request->owner_id)) { $old_rate = $owner->rate; $old_rate_count = $owner->rate_count; $new_rate_counter = $owner->rate_count + 1; $new_rate = ($owner->rate * $owner->rate_count + $rating) / $new_rate_counter; $owner->rate_count = $new_rate_counter; $owner->rate = $new_rate; $owner->save(); } } $response_array = array('success' => true); $response_code = 200; } else { $response_array = array('success' => false, 'error' => 'Already Rated', 'error_code' => 409); $response_code = 200; } } else { /* $response_array = array('success' => false, 'error' => 'Service ID doesnot matches with ' . $var->keyword . ' ID', 'error_code' => 407); */ $response_array = array('success' => false, 'error' => 'Service ID doesnot matches with ' . Config::get('app.generic_keywords.Provider') . ' ID', 'error_code' => 407); $response_code = 200; } } else { $response_array = array('success' => false, 'error' => 'Service ID Not Found', 'error_code' => 408); $response_code = 200; } } else { $response_array = array('success' => false, 'error' => 'Token Expired', 'error_code' => 405); $response_code = 200; } } else { if ($is_admin) { /* $response_array = array('success' => false, 'error' => '' . $var->keyword . ' ID not Found', 'error_code' => 410); */ $response_array = array('success' => false, 'error' => '' . Config::get('app.generic_keywords.Provider') . ' ID not Found', 'error_code' => 410); } else { $response_array = array('success' => false, 'error' => 'Not a valid token', 'error_code' => 406); } $response_code = 200; } } } $response = Response::json($response_array, $response_code); return $response; }
public function set_dog_rating() { if (Request::isMethod('post')) { $comment = Input::get('comment'); $request_id = Input::get('request_id'); $rating = Input::get('rating'); $token = Input::get('token'); $walker_id = Input::get('id'); $validator = Validator::make(array('comment' => $comment, 'request_id' => $request_id, 'rating' => $rating, 'token' => $token, 'walker_id' => $walker_id), array('comment' => 'required', 'request_id' => 'required|integer', 'rating' => 'required|integer', 'token' => 'required', 'walker_id' => 'required|integer')); if ($validator->fails()) { $error_messages = $validator->messages()->all(); $response_array = array('success' => false, 'error' => 'Invalid Input', 'error_code' => 401, 'error_messages' => $error_messages); $response_code = 200; } else { $is_admin = $this->isAdmin($token); if ($walker_data = $this->getWalkerData($walker_id, $token, $is_admin)) { // check for token validity if (is_token_active($walker_data->token_expiry) || $is_admin) { // Do necessary operations if ($request = Requests::find($request_id)) { if ($request->confirmed_walker == $walker_id) { if ($request->is_dog_rated == 0) { $owner = Owner::find($request->owner_id); $dog_review = new DogReview(); $dog_review->request_id = $request_id; $dog_review->walker_id = $walker_id; $dog_review->rating = $rating; $dog_review->owner_id = $owner->id; $dog_review->comment = $comment; $dog_review->save(); $request->is_dog_rated = 1; $request->save(); $response_array = array('success' => true); $response_code = 200; } else { $response_array = array('success' => false, 'error' => 'Already Rated', 'error_code' => 409); $response_code = 200; } } else { $response_array = array('success' => false, 'error' => 'Walk ID doesnot matches with Walker ID', 'error_code' => 407); $response_code = 200; } } else { $response_array = array('success' => false, 'error' => 'Walk ID Not Found', 'error_code' => 408); $response_code = 200; } } else { $response_array = array('success' => false, 'error' => 'Token Expired', 'error_code' => 405); $response_code = 200; } } else { if ($is_admin) { $response_array = array('success' => false, 'error' => 'Walker ID not Found', 'error_code' => 410); } else { $response_array = array('success' => false, 'error' => 'Not a valid token', 'error_code' => 406); } $response_code = 200; } } } $response = Response::json($response_array, $response_code); return $response; }
public function providerTripChangeState() { $date = date("Y-m-d H:i:s"); $time_limit = date("Y-m-d H:i:s", strtotime($date) - 3 * 60 * 60); $walker_id = Session::get('walker_id'); $state = $request_id = Request::segment(4); $current_request = Requests::where('confirmed_walker', $walker_id)->where('is_cancelled', 0)->where('created_at', '>', $time_limit)->orderBy('created_at', 'desc')->where(function ($query) { $query->where('status', 0)->orWhere(function ($query_inner) { $query_inner->where('status', 1)->where('is_dog_rated', 0); }); })->first(); if ($current_request && $state) { if ($state == 2) { $current_request->is_walker_started = 1; $owner = Owner::find($current_request->owner_id); $walk_location = new WalkLocation(); $walk_location->request_id = $current_request->id; $walk_location->latitude = $owner->latitude; $walk_location->longitude = $owner->longitude; $walk_location->distance = 0; $walk_location->save(); } if ($state == 3) { $current_request->is_walker_arrived = 1; } if ($state == 4) { $current_request->is_started = 1; } if ($state == 6) { $current_request->is_dog_rated = 1; $current_request->save(); $review_dog = new DogReview(); $review_dog->walker_id = $current_request->confirmed_walker; $review_dog->comment = Input::get('review'); $review_dog->rating = Input::get('rating'); $review_dog->owner_id = $current_request->owner_id; $review_dog->request_id = $current_request->id; $review_dog->save(); $message = "You has successfully rated the owner."; $type = "success"; return Redirect::to('/provider/trips')->with('message', $message)->with('type', $type); } if ($state == 5) { $address = urlencode(Input::get('address')); $end_address = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$address}"), TRUE); $end_location = $end_address['results'][0]['geometry']; $latitude = $end_location['location']['lat']; $longitude = $end_location['location']['lng']; $request_id = $current_request->id; $walk_location_last = WalkLocation::where('request_id', $request_id)->orderBy('created_at', 'desc')->first(); if ($walk_location_last) { $distance_old = $walk_location_last->distance; $distance_new = distanceGeoPoints($walk_location_last->latitude, $walk_location_last->longitude, $latitude, $longitude); $distance = $distance_old + $distance_new; $settings = Settings::where('key', 'default_distance_unit')->first(); $unit = $settings->value; $distance = $distance; } else { $distance = 0; } $walk_location = new WalkLocation(); $walk_location->request_id = $request_id; $walk_location->latitude = $latitude; $walk_location->longitude = $longitude; $walk_location->distance = $distance; $walk_location->save(); Walker::where('id', '=', $walker_id)->update(array('is_available' => 1)); // Calculate Rerquest Stats $time = 0; $time_query = "SELECT TIMESTAMPDIFF(SECOND,MIN(created_at),MAX(created_at)) as diff\n\t\t\t\tFROM walk_location where request_id = {$current_request->id}\n\t\t\t\tGROUP BY request_id limit 1 "; $time_data = DB::select(DB::raw($time_query)); foreach ($time_data as $time_diff) { $time = $time_diff->diff; } $time = $time / 60; $walker_data = Walker::find($current_request->confirmed_walker); if (!$walker_data->type) { $settings = Settings::where('key', 'price_per_unit_distance')->first(); $price_per_unit_distance = $settings->value; $settings = Settings::where('key', 'price_per_unit_time')->first(); $price_per_unit_time = $settings->value; $settings = Settings::where('key', 'base_price')->first(); $base_price = $settings->value; } else { $provider_type = ProviderType::find($walker_data->type); $base_price = $provider_type->base_price; $price_per_unit_distance = $provider_type->price_per_unit_distance; $price_per_unit_time = $provider_type->price_per_unit_time; } $settings = Settings::where('key', 'default_charging_method_for_users')->first(); $pricing_type = $settings->value; $settings = Settings::where('key', 'default_distance_unit')->first(); $unit = $settings->value; $distance = convert($distance, $unit); if ($pricing_type == 1) { $distance_cost = $price_per_unit_distance * $distance; $time_cost = $price_per_unit_time * $time; $total = $base_price + $distance_cost + $time_cost; } else { $distance_cost = 0; $time_cost = 0; $total = $base_price; } $current_request->is_completed = 1; $current_request->distance = $distance; $current_request->time = $time; $current_request->base_price = $base_price; $current_request->distance_cost = $distance_cost; $current_request->time_cost = $time_cost; $current_request->total = $total; // charge client // charge client $ledger = Ledger::where('owner_id', $current_request->owner_id)->first(); if ($ledger) { $balance = $ledger->amount_earned - $ledger->amount_spent; if ($balance > 0) { if ($total > $balance) { $ledger_temp = Ledger::find($ledger->id); $ledger_temp->amount_spent = $ledger_temp->amount_spent + $balance; $ledger_temp->save(); $total = $total - $balance; } else { $ledger_temp = Ledger::find($ledger->id); $ledger_temp->amount_spent = $ledger_temp->amount_spent + $total; $ledger_temp->save(); $total = 0; } } } if ($total == 0) { $current_request->is_paid = 1; } else { /* $payment_data = Payment::where('owner_id',$current_request->owner_id)->first(); if( $payment_data ) { $customer_id = $payment_data->customer_id; try{ if(Config::get('app.default_payment') == 'stripe') { Stripe::setApiKey(Config::get('app.stripe_secret_key')); Stripe_Charge::create(array( "amount" => $total * 100, "currency" => "usd", "customer" => $customer_id) ); $request->is_paid = 1; } else{ Braintree_Configuration::environment(Config::get('app.braintree_environment')); Braintree_Configuration::merchantId(Config::get('app.braintree_merchant_id')); Braintree_Configuration::publicKey(Config::get('app.braintree_public_key')); Braintree_Configuration::privateKey(Config::get('app.braintree_private_key')); $result = Braintree_Transaction::sale(array( 'amount' => round($total,2), 'paymentMethodNonce' => $customer_id )); if ($result->success) { $request->is_paid = 1; } else{ $request->is_paid = 0; } } } catch(Exception $e) { $response_array = array('success' => false , 'error' => $e , 'error_code' => 405); $response_code = 200; $response = Response::json($response_array, $response_code); return $response; } } */ } $current_request->card_payment = $total; $current_request->ledger_payment = $current_request->total - $total; $current_request->save(); } $current_request->save(); } return Redirect::to('/provider/tripinprogress'); }