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 updateUserCode() { $owner_id = Session::get('user_id'); $code = Input::get('code'); $code_count = Ledger::where('referral_code', '=', $code)->where('owner_id', '!=', $owner_id)->count(); if ($code_count) { $message = "This referral code is already in use. Please choose a new one"; $type = "danger"; } else { $ledger = Ledger::where('owner_id', $owner_id)->first(); $ledger = Ledger::find($ledger->id); $ledger->referral_code = $code; $ledger->save(); $message = "Your referral code is successfully updated"; $type = "success"; } return Redirect::to('/user/payments')->with('message', $message)->with('type', $type); }
public function get_credits() { $token = Input::get('token'); $owner_id = Input::get('id'); $validator = Validator::make(array('token' => $token, 'owner_id' => $owner_id), array('token' => 'required', 'owner_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 ($owner_data = $this->getOwnerData($owner_id, $token, $is_admin)) { // check for token validity if (is_token_active($owner_data->token_expiry)) { /* $currency_selected = Keywords::find(5); */ $ledger = Ledger::where('owner_id', $owner_id)->first(); if ($ledger) { $credits['balance'] = currency_converted($ledger->amount_earned - $ledger->amount_spent); /* $credits['currency'] = $currency_selected->keyword; */ $credits['currency'] = Config::get('app.generic_keywords.Currency'); $response_array = array('success' => true, 'credits' => $credits); } else { $response_array = array('success' => false, 'error' => 'No Credit Found', 'error_code' => 475); } } else { $response_array = array('success' => false, 'error' => 'Not a valid token', 'error_code' => 406); } } else { $response_array = array('success' => false, 'error' => 'User Not Found', 'error_code' => 402); } $response_code = 200; } $response = Response::json($response_array, $response_code); return $response; }
public function referral_details() { $owner_id = Request::segment(4); $ledger = Ledger::where('owner_id', $owner_id)->first(); $owners = Owner::where('referred_by', $owner_id)->paginate(10); $title = ucwords(trans('customize.User') . 's' . " | Coupon Statistics"); /* 'Owner | Coupon Statistics' */ return View::make('referred')->with('page', 'owners')->with('title', $title)->with('owners', $owners)->with('ledger', $ledger); }
public function request_walk_completed() { if (Request::isMethod('post')) { $request_id = Input::get('request_id'); $token = Input::get('token'); $walker_id = Input::get('id'); $latitude = Input::get('latitude'); $longitude = Input::get('longitude'); $distance = Input::get('distance'); $time = Input::get('time'); $walker = Walker::find($walker_id); $validator = Validator::make(array('request_id' => $request_id, 'token' => $token, 'walker_id' => $walker_id, 'latitude' => $latitude, 'longitude' => $longitude, 'distance' => $distance, 'time' => $time), array('request_id' => 'required|integer', 'token' => 'required', 'walker_id' => 'required|integer', 'latitude' => 'required', 'longitude' => 'required', 'distance' => 'required', 'time' => 'required')); 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_started == 1) { $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); $pt = ProviderServices::where('provider_id', $walker_id)->get(); foreach ($pt as $key) { $reqserv = RequestServices::where('request_id', $request_id)->where('type', $key->type)->first(); $base_price = $key->base_price; $price_per_unit_distance = $key->price_per_unit_distance * $distance; $price_per_unit_time = $key->price_per_unit_time * $time; $reqserv->base_price = $base_price; $reqserv->distance_cost = $price_per_unit_distance; $reqserv->time_cost = $price_per_unit_time; $reqserv->total = $base_price + $price_per_unit_distance + $price_per_unit_time; $reqserv->save(); } $rs = RequestServices::where('request_id', $request_id)->get(); $total = 0; foreach ($rs as $key) { Log::info('total = ' . print_r($key->total, true)); $total = $total + $key->total; } $request->is_completed = 1; $request->distance = $distance; $request->time = $time; $request->total = $total; // charge client $ledger = Ledger::where('owner_id', $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) { $request->is_paid = 1; } else { $payment_data = Payment::where('owner_id', $request->owner_id)->first(); if ($payment_data) { $customer_id = $payment_data->customer_id; try { if (Config::get('app.default_payment') == 'stripe') { $am = round($total) * 100; $sett = Settings::where('key', 'service_fee')->first(); $serviceFee = $sett->value * 100; $amount = $am - $serviceFee; Stripe::setApiKey(Config::get('app.stripe_secret_key')); Stripe_Charge::create(array("amount" => $am, "currency" => "usd", "customer" => $customer_id)); if ($walker->merchant_id != NULL) { $transfer = Stripe_Transfer::create(array("amount" => $amount, "currency" => "usd", "recipient" => $walker->merchant_id, "statement_descriptor" => "UberFoxX")); Log::info('transfer = ' . print_r($transfer, true)); } $request->is_paid = 1; } else { $am = round($total, 2); $sett = Settings::where('key', 'service_fee')->first(); $serviceFee = $sett->value; $amount = $am - $serviceFee; 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')); $card_id = $payment_data->card_token; if ($walker->merchant_id == NULL) { $result = Braintree_Transaction::sale(array('amount' => $amount, 'paymentMethodToken' => $card_id)); } else { $result = Braintree_Transaction::sale(array('amount' => $amount, 'paymentMethodToken' => $card_id, 'merchantAccountId' => $walker->merchant_id, 'serviceFeeAmount' => $serviceFee)); } Log::info('result = ' . print_r($result, true)); 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; } } } $request->card_payment = $total; $request->ledger_payment = $request->total - $total; $request->save(); if ($request->is_paid == 1) { $owner = Owner::find($request->owner_id); $settings = Settings::where('key', 'sms_request_unanswered')->first(); $pattern = $settings->value; $pattern = str_replace('%user%', $owner->first_name . " " . $owner->last_name, $pattern); $pattern = str_replace('%id%', $request->id, $pattern); $pattern = str_replace('%user_mobile%', $owner->phone, $pattern); sms_notification(1, 'admin', $pattern); } $walker = Walker::find($walker_id); $walker->is_available = 1; $walker->save(); $walk_location = new WalkLocation(); $walk_location->latitude = $latitude; $walk_location->longitude = $longitude; $walk_location->request_id = $request_id; $walk_location->save(); // Send Notification $walker = Walker::find($request->confirmed_walker); $walker_data = array(); $walker_data['first_name'] = $walker->first_name; $walker_data['last_name'] = $walker->last_name; $walker_data['phone'] = $walker->phone; $walker_data['bio'] = $walker->bio; $walker_data['picture'] = $walker->picture; $walker_data['latitude'] = $walker->latitude; $walker_data['longitude'] = $walker->longitude; $walker_data['type'] = $walker->type; $walker_data['rating'] = DB::table('review_walker')->where('walker_id', '=', $walker->id)->avg('rating') ?: 0; $walker_data['num_rating'] = DB::table('review_walker')->where('walker_id', '=', $walker->id)->count(); $settings = Settings::where('key', 'default_distance_unit')->first(); $unit = $settings->value; $bill = array(); if ($request->is_completed == 1) { $bill['distance'] = convert($request->distance, $unit); $bill['time'] = $request->time; $bill['base_price'] = $request->base_price; $bill['distance_cost'] = $request->distance_cost; $bill['time_cost'] = $request->time_cost; $bill['total'] = $request->total; $bill['is_paid'] = $request->is_paid; } $response_array = array('success' => true, 'request_id' => $request_id, 'status' => $request->status, 'confirmed_walker' => $request->confirmed_walker, 'is_walker_started' => $request->is_walker_started, 'is_walker_arrived' => $request->is_walker_arrived, 'is_walk_started' => $request->is_started, 'is_completed' => $request->is_completed, 'is_walker_rated' => $request->is_walker_rated, 'walker' => $walker_data, 'bill' => $bill); $title = "Trip Completed"; $message = $response_array; send_notifications($request->owner_id, "owner", $title, $message); // Send SMS $owner = Owner::find($request->owner_id); $settings = Settings::where('key', 'sms_when_provider_completes_job')->first(); $pattern = $settings->value; $pattern = str_replace('%user%', $owner->first_name . " " . $owner->last_name, $pattern); $pattern = str_replace('%driver%', $walker->first_name . " " . $walker->last_name, $pattern); $pattern = str_replace('%driver_mobile%', $walker->phone, $pattern); $pattern = str_replace('%damount%', $request->total, $pattern); sms_notification($request->owner_id, 'owner', $pattern); // send email $settings = Settings::where('key', 'email_request_finished')->first(); $pattern = $settings->value; $pattern = str_replace('%id%', $request->id, $pattern); $pattern = str_replace('%url%', web_url() . "/admin/request/map/" . $request->id, $pattern); $subject = "Request Completed"; email_notification(1, 'admin', $pattern, $subject); $settings = Settings::where('key', 'email_invoice_generated_user')->first(); $pattern = $settings->value; $pattern = str_replace('%id%', $request->id, $pattern); $pattern = str_replace('%amount%', $request->total, $pattern); $subject = "Invoice Generated"; email_notification($request->owner_id, 'owner', $pattern, $subject); $settings = Settings::where('key', 'email_invoice_generated_provider')->first(); $pattern = $settings->value; $pattern = str_replace('%id%', $request->id, $pattern); $pattern = str_replace('%amount%', $request->total, $pattern); $subject = "Invoice Generated"; email_notification($request->confirmed_walker, 'walker', $pattern, $subject); if ($request->is_paid == 1) { // send email $settings = Settings::where('key', 'email_payment_charged')->first(); $pattern = $settings->value; $pattern = str_replace('%id%', $request->id, $pattern); $pattern = str_replace('%url%', web_url() . "/admin/request/map/" . $request->id, $pattern); $subject = "Payment Charged"; email_notification(1, 'admin', $pattern, $subject); } $response_array = array('success' => true, 'total' => $total, 'is_paid' => $request->is_paid); $response_code = 200; } else { $response_array = array('success' => false, 'error' => 'Walk not yet started', 'error_code' => 413); $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 pre_payment() { if (Request::isMethod('post')) { $request_id = Input::get('request_id'); $token = Input::get('token'); $walker_id = Input::get('id'); $time = Input::get('time'); $validator = Validator::make(array('request_id' => $request_id, 'token' => $token, 'walker_id' => $walker_id, 'time' => $time), array('request_id' => 'required|integer', 'token' => 'required', 'walker_id' => 'required|integer', 'time' => 'required')); 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) { $request_service = RequestServices::find($request_id); $request_typ = ProviderType::where('id', '=', $request_service->type)->first(); 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; */ $price_per_unit_distance = $request_typ->price_per_unit_distance; $price_per_unit_time = $request_typ->price_per_unit_time; $base_price = $request_typ->base_price; } else { $provider_type = ProviderServices::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; if ($pricing_type == 1) { $distance_cost = $price_per_unit_distance; $time_cost = $price_per_unit_time; $total = $base_price + $distance_cost + $time_cost; } else { $distance_cost = 0; $time_cost = 0; $total = $base_price; } Log::info('req'); $request_service = RequestServices::find($request_id); $request_service->base_price = $base_price; $request_service->distance_cost = $distance_cost; $request_service->time_cost = $time_cost; $request_service->total = $total; $request_service->save(); $request->distance = $distance_cost; $request->time = $time_cost; $request->total = $total; Log::info('in '); // charge client $ledger = Ledger::where('owner_id', $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; } } } Log::info('out'); if ($total == 0) { $request->is_paid = 1; } else { $payment_data = Payment::where('owner_id', $request->owner_id)->where('is_default', 1)->first(); if (!$payment_data) { $payment_data = Payment::where('owner_id', $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')); try { Stripe_Charge::create(array("amount" => floor($total) * 100, "currency" => "usd", "customer" => $customer_id)); } catch (Stripe_InvalidRequestError $e) { // Invalid parameters were supplied to Stripe's API $ownr = Owner::find($request->owner_id); $ownr->debt = $total; $ownr->save(); $response_array = array('error' => $e->getMessage()); $response_code = 200; $response = Response::json($response_array, $response_code); return $response; } $request->is_paid = 1; $setting = Settings::where('key', 'paypal')->first(); $settng1 = Settings::where('key', 'service_fee')->first(); if ($setting->value == 2 && $walker_data->merchant_id != NULL) { // dd($amount$request->transfer_amount); $transfer = Stripe_Transfer::create(array("amount" => ($total - $settng1->value) * 100, "currency" => "usd", "recipient" => $walker_data->merchant_id)); } } else { $amount = $total; 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')); $card_id = $payment_data->card_token; $setting = Settings::where('key', 'paypal')->first(); $settng1 = Settings::where('key', 'service_fee')->first(); if ($setting->value == 2 && $walker_data->merchant_id != NULL) { // escrow $result = Braintree_Transaction::sale(array('amount' => $amount, 'paymentMethodToken' => $card_id)); } else { $result = Braintree_Transaction::sale(array('amount' => $amount, 'paymentMethodToken' => $card_id)); } Log::info('result = ' . print_r($result, true)); 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; } } } $request->card_payment = $total; $request->ledger_payment = $request->total - $total; $request->save(); Log::info('Request = ' . print_r($request, true)); if ($request->is_paid == 1) { $owner = Owner::find($request->owner_id); $settings = Settings::where('key', 'sms_request_unanswered')->first(); $pattern = $settings->value; $pattern = str_replace('%user%', $owner->first_name . " " . $owner->last_name, $pattern); $pattern = str_replace('%id%', $request->id, $pattern); $pattern = str_replace('%user_mobile%', $owner->phone, $pattern); sms_notification(1, 'admin', $pattern); } $walker = Walker::find($walker_id); $walker->is_available = 1; $walker->save(); // Send Notification $walker = Walker::find($request->confirmed_walker); $walker_data = array(); $walker_data['first_name'] = $walker->first_name; $walker_data['last_name'] = $walker->last_name; $walker_data['phone'] = $walker->phone; $walker_data['bio'] = $walker->bio; $walker_data['picture'] = $walker->picture; $walker_data['type'] = $walker->type; $walker_data['rating'] = $walker->rate; $walker_data['num_rating'] = $walker->rate_count; $walker_data['car_model'] = $walker->car_model; $walker_data['car_number'] = $walker->car_number; /* $walker_data['rating'] = DB::table('review_walker')->where('walker_id', '=', $walker->id)->avg('rating') ? : 0; $walker_data['num_rating'] = DB::table('review_walker')->where('walker_id', '=', $walker->id)->count(); */ $settings = Settings::where('key', 'default_distance_unit')->first(); $unit = $settings->value; if ($unit == 0) { $unit_set = 'kms'; } elseif ($unit == 1) { $unit_set = 'miles'; } $bill = array(); if ($request->is_paid == 1) { $bill['distance'] = (string) convert($request->distance, $unit); $bill['unit'] = $unit_set; $bill['time'] = $request->time; $bill['base_price'] = currency_converted($base_price); $bill['distance_cost'] = currency_converted($distance_cost); $bill['time_cost'] = currency_converted($time_cost); $bill['total'] = currency_converted($request->total); $bill['is_paid'] = $request->is_paid; } $response_array = array('success' => true, 'request_id' => $request_id, 'status' => $request->status, 'confirmed_walker' => $request->confirmed_walker, 'walker' => $walker_data, 'bill' => $bill); $title = "Payment Has Made"; $message = $response_array; send_notifications($walker->id, "walker", $title, $message); $settings = Settings::where('key', 'email_notification')->first(); $condition = $settings->value; if ($condition == 1) { /* $settings = Settings::where('key', 'payment_made_client')->first(); $pattern = $settings->value; $pattern = str_replace('%id%', $request->id, $pattern); $pattern = str_replace('%amount%', $request->total, $pattern); $subject = "Payment Charged"; email_notification($walker->id, 'walker', $pattern, $subject); */ $settings = Settings::where('key', 'admin_email_address')->first(); $admin_email = $settings->value; $pattern = array('admin_eamil' => $admin_email, 'name' => ucwords($walker->first_name . " " . $walker->last_name), 'amount' => $total, 'req_id' => $request_id, 'web_url' => web_url()); $subject = "Payment Done With " . $request_id . ""; email_notification($walker->id, 'walker', $pattern, $subject, 'pre_payment', null); } // Send SMS $owner = Owner::find($request->owner_id); $settings = Settings::where('key', 'sms_when_provider_completes_job')->first(); $pattern = $settings->value; $pattern = str_replace('%user%', $owner->first_name . " " . $owner->last_name, $pattern); $pattern = str_replace('%driver%', $walker->first_name . " " . $walker->last_name, $pattern); $pattern = str_replace('%driver_mobile%', $walker->phone, $pattern); $pattern = str_replace('%amount%', $request->total, $pattern); sms_notification($request->owner_id, 'owner', $pattern); $email_data = array(); $email_data['name'] = $owner->first_name; $email_data['emailType'] = 'user'; $email_data['base_price'] = $bill['base_price']; $email_data['distance'] = $bill['distance']; $email_data['time'] = $bill['time']; $email_data['unit'] = $bill['unit']; $email_data['total'] = $bill['total']; if ($bill['payment_mode']) { $email_data['payment_mode'] = $bill['payment_mode']; } else { $email_data['payment_mode'] = '---'; } /* $subject = "Invoice Generated"; send_email($request->owner_id, 'owner', $email_data, $subject, 'invoice'); $subject = "Invoice Generated"; $email_data['emailType'] = 'walker'; send_email($request->confirmed_walker, 'walker', $email_data, $subject, 'invoice'); */ if ($request->is_paid == 1) { // send email /* $settings = Settings::where('key', 'email_payment_charged')->first(); $pattern = $settings->value; $pattern = str_replace('%id%', $request->id, $pattern); $pattern = str_replace('%url%', web_url() . "/admin/request/" . $request->id, $pattern); $subject = "Payment Charged"; email_notification(1, 'admin', $pattern, $subject); */ $settings = Settings::where('key', 'admin_email_address')->first(); $admin_email = $settings->value; $pattern = array('admin_eamil' => $admin_email, 'name' => 'Administrator', 'amount' => $total, 'req_id' => $request_id, 'web_url' => web_url()); $subject = "Payment Done With " . $request_id . ""; email_notification(1, 'admin', $pattern, $subject, 'pay_charged', null); } $response_array = array('success' => true, 'base_fare' => currency_converted($base_price), 'distance_cost' => currency_converted($distance_cost), 'time_cost' => currency_converted($time_cost), 'total' => currency_converted($total), 'is_paid' => $request->is_paid); $response_code = 200; } else { /* $var = Keywords::where('id', 1)->first(); $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) { /* $var = Keywords::where('id', 1)->first(); $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 get_request() { $request_id = Input::get('request_id'); $token = Input::get('token'); $owner_id = Input::get('id'); $validator = Validator::make(array('request_id' => $request_id, 'token' => $token, 'owner_id' => $owner_id), array('request_id' => 'required|integer', 'token' => 'required', 'owner_id' => 'required|integer')); /* $var = Keywords::where('id', 2)->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 ($owner_data = $this->getOwnerData($owner_id, $token, $is_admin)) { // check for token validity if (is_token_active($owner_data->token_expiry) || $is_admin) { /* SEND REFERRAL & PROMO INFO */ $settings = Settings::where('key', 'referral_code_activation')->first(); $referral_code_activation = $settings->value; if ($referral_code_activation) { $referral_code_activation_txt = "referral on"; } else { $referral_code_activation_txt = "referral off"; } $settings = Settings::where('key', 'promotional_code_activation')->first(); $promotional_code_activation = $settings->value; if ($promotional_code_activation) { $promotional_code_activation_txt = "promo on"; } else { $promotional_code_activation_txt = "promo off"; } /* SEND REFERRAL & PROMO INFO */ // Do necessary operations if ($request = Requests::find($request_id)) { if ($request->owner_id == $owner_data->id) { if ($request->current_walker != 0) { if ($request->confirmed_walker != 0) { $walker = Walker::where('id', $request->confirmed_walker)->first(); $walker_data = array(); $walker_data['unique_id'] = 1; $walker_data['id'] = $walker->id; $walker_data['first_name'] = $walker->first_name; $walker_data['last_name'] = $walker->last_name; $walker_data['phone'] = $walker->phone; $walker_data['bio'] = $walker->bio; $walker_data['picture'] = $walker->picture; $walker_data['latitude'] = $walker->latitude; $walker_data['longitude'] = $walker->longitude; if ($request->D_latitude != NULL) { $walker_data['d_latitude'] = $request->D_latitude; $walker_data['d_longitude'] = $request->D_longitude; } $walker_data['type'] = $walker->type; $walker_data['rating'] = $walker->rate; $walker_data['num_rating'] = $walker->rate_count; $walker_data['car_model'] = $walker->car_model; $walker_data['car_number'] = $walker->car_number; $walker_data['bearing'] = $walker->bearing; /* $walker_data['rating'] = DB::table('review_walker')->where('walker_id', '=', $walker->id)->avg('rating') ? : 0; $walker_data['num_rating'] = DB::table('review_walker')->where('walker_id', '=', $walker->id)->count(); */ $settings = Settings::where('key', 'default_distance_unit')->first(); $unit = $settings->value; if ($unit == 0) { $unit_set = 'kms'; } elseif ($unit == 1) { $unit_set = 'miles'; } $requestserv = RequestServices::where('request_id', $request->id)->first(); $bill = array(); $request_typ = ProviderType::where('id', '=', $requestserv->type)->first(); /* $currency_selected = Keywords::find(5); $cur_symb = $currency_selected->keyword; */ $cur_symb = Config::get('app.generic_keywords.Currency'); if ($request->is_completed == 1) { $bill['unit'] = $unit_set; $bill['payment_mode'] = $request->payment_mode; $bill['distance'] = (string) $request->distance; $bill['time'] = $request->time; if ($requestserv->base_price != 0) { $bill['base_distance'] = $request_typ->base_distance; $bill['base_price'] = currency_converted($requestserv->base_price); $bill['distance_cost'] = currency_converted($requestserv->distance_cost); $bill['time_cost'] = currency_converted($requestserv->time_cost); } else { /* $setbase_price = Settings::where('key', 'base_price')->first(); $bill['base_price'] = currency_converted($setbase_price->value); $setdistance_price = Settings::where('key', 'price_per_unit_distance')->first(); $bill['distance_cost'] = currency_converted($setdistance_price->value); $settime_price = Settings::where('key', 'price_per_unit_time')->first(); $bill['time_cost'] = currency_converted($settime_price->value); */ $bill['base_distance'] = $request_typ->base_distance; $bill['base_price'] = currency_converted($request_typ->base_price); $bill['distance_cost'] = currency_converted($request_typ->price_per_unit_distance); $bill['time_cost'] = currency_converted($request_typ->price_per_unit_time); } if ($request->payment_mode == 2) { $bill['walker']['email'] = $walker->email; $bill['walker']['amount'] = currency_converted($request->transfer_amount); $admins = Admin::first(); $bill['admin']['email'] = $admins->username; $bill['admin']['amount'] = currency_converted($request->total - $request->transfer_amount); } $bill['currency'] = $cur_symb; /* $bill['total'] = currency_converted($request->total); */ $bill['main_total'] = currency_converted($request->total); $bill['total'] = currency_converted($request->total - $request->ledger_payment - $request->promo_payment); $bill['referral_bonus'] = currency_converted($request->ledger_payment); $bill['promo_bonus'] = currency_converted($request->promo_payment); // $bill['payment_type'] = $owner_data->cash_or_card; $bill['payment_type'] = ""; $bill['is_paid'] = $request->is_paid; $discount = 0; if ($request->promo_code != "") { if ($request->promo_code != "") { $promo_code = PromoCodes::where('id', $request->promo_code)->first(); if ($promo_code) { $promo_value = $promo_code->value; $promo_type = $promo_code->type; if ($promo_type == 1) { // Percent Discount $discount = $request->total * $promo_value / 100; } elseif ($promo_type == 2) { // Absolute Discount $discount = $promo_value; } } } } $bill['promo_discount'] = currency_converted($discount); $bill['actual_total'] = currency_converted($request->total + $request->ledger_payment + $discount); } $cards = ""; /* $cards['none'] = ""; */ $dif_card = 0; $cardlist = Payment::where('owner_id', $owner_id)->where('is_default', 1)->first(); /* $cardlist = Payment::where('id', $owner_data->default_card_id)->first(); */ if (count($cardlist) >= 1) { $cards = array(); $default = $cardlist->is_default; if ($default == 1) { $dif_card = $cardlist->id; $cards['is_default_text'] = "default"; } else { $cards['is_default_text'] = "not_default"; } $cards['card_id'] = $cardlist->id; $cards['owner_id'] = $cardlist->owner_id; $cards['customer_id'] = $cardlist->customer_id; $cards['last_four'] = $cardlist->last_four; $cards['card_token'] = $cardlist->card_token; $cards['card_type'] = $cardlist->card_type; $cards['is_default'] = $default; } $code_data = Ledger::where('owner_id', '=', $owner_data->id)->first(); $owner = array(); $owner['owner_lat'] = $owner_data->latitude; $owner['owner_long'] = $owner_data->longitude; $owner['owner_dist_lat'] = $request->D_latitude; $owner['owner_dist_long'] = $request->D_longitude; $owner['payment_type'] = $request->payment_mode; $owner['default_card'] = $dif_card; $owner['dest_latitude'] = $request->D_latitude; $owner['dest_longitude'] = $request->D_longitude; $owner['referral_code'] = $code_data->referral_code; $owner['is_referee'] = $owner_data->is_referee; $owner['promo_count'] = $owner_data->promo_count; $charge = array(); $settings = Settings::where('key', 'default_distance_unit')->first(); $unit = $settings->value; if ($unit == 0) { $unit_set = 'kms'; } elseif ($unit == 1) { $unit_set = 'miles'; } $charge['unit'] = $unit_set; if ($requestserv->base_price != 0) { $charge['base_distance'] = $request_typ->base_distance; $charge['base_price'] = currency_converted($requestserv->base_price); $charge['distance_price'] = currency_converted($requestserv->distance_cost); $charge['price_per_unit_time'] = currency_converted($requestserv->time_cost); } else { /* $setbase_price = Settings::where('key', 'base_price')->first(); $charge['base_price'] = currency_converted($setbase_price->value); $setdistance_price = Settings::where('key', 'price_per_unit_distance')->first(); $charge['distance_price'] = currency_converted($setdistance_price->value); $settime_price = Settings::where('key', 'price_per_unit_time')->first(); $charge['price_per_unit_time'] = currency_converted($settime_price->value); */ $charge['base_distance'] = $request_typ->base_distance; $charge['base_price'] = currency_converted($request_typ->base_price); $charge['distance_price'] = currency_converted($request_typ->price_per_unit_distance); $charge['price_per_unit_time'] = currency_converted($request_typ->price_per_unit_time); } $charge['total'] = currency_converted($request->total); $charge['is_paid'] = $request->is_paid; $loc1 = WalkLocation::where('request_id', $request->id)->first(); $loc2 = WalkLocation::where('request_id', $request->id)->orderBy('id', 'desc')->first(); if ($loc1) { $time1 = strtotime($loc2->created_at); $time2 = strtotime($loc1->created_at); $difference = intval(($time1 - $time2) / 60); } else { $difference = 0; } $difference = $request->time; /* $rserv = RequestServices::where('request_id', $request_id)->get(); $typs = array(); $typi = array(); $typp = array(); $total_price = 0; foreach ($rserv as $typ) { $typ1 = ProviderType::where('id', $typ->type)->first(); $typ_price = ProviderServices::where('provider_id', $request->confirmed_walker)->where('type', $typ->type)->first(); if ($typ_price->base_price > 0) { $typp1 = 0.00; $typp1 = $typ_price->base_price; } elseif ($typ_price->price_per_unit_distance > 0) { $typp1 = 0.00; foreach ($rserv as $key) { $typp1 = $typp1 + $key->distance_cost; } } else { $typp1 = 0.00; } $typs['name'] = $typ1->name; $typs['price'] = currency_converted($typp1); $total_price = $total_price + $typp1; array_push($typi, $typs); } $bill['type'] = $typi;*/ $rserv = RequestServices::where('request_id', $request_id)->get(); $typs = array(); $typi = array(); $typp = array(); $total_price = 0; foreach ($rserv as $typ) { $typ1 = ProviderType::where('id', $typ->type)->first(); $typ_price = ProviderServices::where('provider_id', $request->confirmed_walker)->where('type', $typ->type)->first(); $typp1 = 0.0; if (isset($typ_price->base_price)) { if ($typ_price->base_price > 0) { $typp1 = 0.0; $typp1 = $typ_price->base_price; } elseif ($typ_price->price_per_unit_distance > 0) { $typp1 = 0.0; foreach ($rserv as $key) { $typp1 = $typp1 + $key->distance_cost; } } else { $typp1 = 0.0; } } $typs['name'] = $typ1->name; $typs['price'] = currency_converted($typp1); $total_price = $total_price + $typp1; array_push($typi, $typs); } $bill['type'] = $typi; $response_array = array('success' => true, 'unique_id' => 1, 'status' => $request->status, 'is_referral_active' => $referral_code_activation, 'is_referral_active_txt' => $referral_code_activation_txt, 'is_promo_active' => $promotional_code_activation, 'is_promo_active_txt' => $promotional_code_activation_txt, 'confirmed_walker' => $request->confirmed_walker, 'is_walker_started' => $request->is_walker_started, 'is_walker_arrived' => $request->is_walker_arrived, 'is_walk_started' => $request->is_started, 'is_completed' => $request->is_completed, 'is_walker_rated' => $request->is_walker_rated, 'is_cancelled' => $request->is_cancelled, 'dest_latitude' => $request->D_latitude, 'dest_longitude' => $request->D_longitude, 'promo_id' => $request->promo_id, 'promo_code' => $request->promo_code, 'walker' => $walker_data, 'time' => $difference, 'bill' => $bill, 'owner' => $owner, 'card_details' => $cards, 'charge_details' => $charge); $user_timezone = $walker->timezone; $default_timezone = Config::get('app.timezone'); $accepted_time = get_user_time($default_timezone, $user_timezone, $request->request_start_time); $time = DB::table('walk_location')->where('request_id', $request_id)->min('created_at'); $end_time = get_user_time($default_timezone, $user_timezone, $time); $response_array['accepted_time'] = $accepted_time; if ($request->is_started == 1) { $response_array['start_time'] = DB::table('walk_location')->where('request_id', $request_id)->min('created_at'); $settings = Settings::where('key', 'default_distance_unit')->first(); $unit = $settings->value; $response_array['distance'] = DB::table('walk_location')->where('request_id', $request_id)->max('distance'); $response_array['distance'] = (string) convert($response_array['distance'], $unit); if ($unit == 0) { $unit_set = 'kms'; } elseif ($unit == 1) { $unit_set = 'miles'; } $response_array['unit'] = $unit_set; } if ($request->is_completed == 1) { $response_array['end_time'] = $end_time; } } else { if ($request->current_walker != 0) { $walker = Walker::find($request->current_walker); $walker_data = array(); $walker_data['unique_id'] = 1; $walker_data['id'] = $walker->id; $walker_data['first_name'] = $walker->first_name; $walker_data['last_name'] = $walker->last_name; $walker_data['phone'] = $walker->phone; $walker_data['bio'] = $walker->bio; $walker_data['picture'] = $walker->picture; $walker_data['latitude'] = $walker->latitude; $walker_data['longitude'] = $walker->longitude; $walker_data['type'] = $walker->type; $walker_data['car_model'] = $walker->car_model; $walker_data['car_number'] = $walker->car_number; $walker_data['bearing'] = $walker->bearing; // $walker_data['payment_type'] = $request->cash_or_card; $walker_data['rating'] = $walker->rate; $walker_data['num_rating'] = $walker->rate_count; } $cards = ""; /* $cards['none'] = ""; */ $dif_card = 0; $cardlist = Payment::where('owner_id', $owner_id)->where('is_default', 1)->first(); /* $cardlist = Payment::where('id', $owner_data->default_card_id)->first(); */ if (count($cardlist) >= 1) { $cards = array(); $default = $cardlist->is_default; if ($default == 1) { $dif_card = $cardlist->id; $cards['is_default_text'] = "default"; } else { $cards['is_default_text'] = "not_default"; } $cards['card_id'] = $cardlist->id; $cards['owner_id'] = $cardlist->owner_id; $cards['customer_id'] = $cardlist->customer_id; $cards['last_four'] = $cardlist->last_four; $cards['card_token'] = $cardlist->card_token; $cards['card_type'] = $cardlist->card_type; $cards['is_default'] = $default; } $code_data = Ledger::where('owner_id', '=', $owner_data->id)->first(); $owner = array(); $owner['owner_lat'] = $owner_data->latitude; $owner['owner_long'] = $owner_data->longitude; $owner['owner_dist_lat'] = $request->D_latitude; $owner['owner_dist_long'] = $request->D_longitude; //$owner['payment_type'] = $owner_data->cash_or_card; $owner['payment_type'] = ""; $owner['default_card'] = $dif_card; $owner['dest_latitude'] = $request->D_latitude; $owner['dest_longitude'] = $request->D_longitude; $owner['referral_code'] = $code_data->referral_code; $owner['is_referee'] = $owner_data->is_referee; $owner['promo_count'] = $owner_data->promo_count; /* $driver = Keywords::where('id', 1)->first(); */ $requestserv = RequestServices::where('request_id', $request->id)->first(); $charge = array(); $request_typ = ProviderType::where('id', '=', $requestserv->type)->first(); $settings = Settings::where('key', 'default_distance_unit')->first(); $unit = $settings->value; if ($unit == 0) { $unit_set = 'kms'; } elseif ($unit == 1) { $unit_set = 'miles'; } $charge['unit'] = $unit_set; if ($requestserv->base_price != 0) { $charge['base_distance'] = $request_typ->base_distance; $charge['base_price'] = currency_converted($requestserv->base_price); $charge['distance_price'] = currency_converted($requestserv->distance_cost); $charge['price_per_unit_time'] = currency_converted($requestserv->time_cost); } else { /* $setbase_price = Settings::where('key', 'base_price')->first(); $charge['base_price'] = currency_converted($setbase_price->value); $setdistance_price = Settings::where('key', 'price_per_unit_distance')->first(); $charge['distance_price'] = currency_converted($setdistance_price->value); $settime_price = Settings::where('key', 'price_per_unit_time')->first(); $charge['price_per_unit_time'] = currency_converted($settime_price->value); */ $charge['base_distance'] = $request_typ->base_distance; $charge['base_price'] = currency_converted($request_typ->base_price); $charge['distance_price'] = currency_converted($request_typ->price_per_unit_distance); $charge['price_per_unit_time'] = currency_converted($request_typ->price_per_unit_time); } $charge['total'] = currency_converted($request->total); $charge['is_paid'] = $request->is_paid; $response_array = array('success' => true, 'unique_id' => 1, 'status' => $request->status, 'is_referral_active' => $referral_code_activation, 'is_referral_active_txt' => $referral_code_activation_txt, 'is_promo_active' => $promotional_code_activation, 'is_promo_active_txt' => $promotional_code_activation_txt, 'confirmed_walker' => 0, 'is_walker_started' => $request->is_walker_started, 'is_walker_arrived' => $request->is_walker_arrived, 'is_walk_started' => $request->is_started, 'is_completed' => $request->is_completed, 'is_walker_rated' => $request->is_walker_rated, 'is_cancelled' => $request->is_cancelled, 'dest_latitude' => $request->D_latitude, 'dest_longitude' => $request->D_longitude, 'promo_id' => $request->promo_id, 'promo_code' => $request->promo_code, 'walker' => $walker_data, 'bill' => "", 'owner' => $owner, 'card_details' => $cards, 'charge_details' => $charge, 'confirmed_walker' => 0, 'error_code' => 484, 'error' => 'Searching for ' . Config::get('app.generic_keywords.Provider') . 's.'); } } else { /* $driver = Keywords::where('id', 1)->first(); */ if ($request->current_walker != 0) { $walker = Walker::find($request->current_walker); $walker_data = array(); $walker_data['unique_id'] = 1; $walker_data['id'] = $walker->id; $walker_data['first_name'] = $walker->first_name; $walker_data['last_name'] = $walker->last_name; $walker_data['phone'] = $walker->phone; $walker_data['bio'] = $walker->bio; $walker_data['picture'] = $walker->picture; $walker_data['latitude'] = $walker->latitude; $walker_data['longitude'] = $walker->longitude; $walker_data['type'] = $walker->type; $walker_data['car_model'] = $walker->car_model; $walker_data['car_number'] = $walker->car_number; $walker_data['bearing'] = $walker->bearing; $walker_data['payment_type'] = $request->cash_or_card; $walker_data['rating'] = $walker->rate; $walker_data['num_rating'] = $walker->rate_count; } $cards = ""; /* $cards['none'] = ""; */ $dif_card = 0; $cardlist = Payment::where('owner_id', $owner_id)->where('is_default', 1)->first(); /* $cardlist = Payment::where('id', $owner_data->default_card_id)->first(); */ if (count($cardlist) >= 1) { $cards = array(); $default = $cardlist->is_default; if ($default == 1) { $dif_card = $cardlist->id; $cards['is_default_text'] = "default"; } else { $cards['is_default_text'] = "not_default"; } $cards['card_id'] = $cardlist->id; $cards['owner_id'] = $cardlist->owner_id; $cards['customer_id'] = $cardlist->customer_id; $cards['last_four'] = $cardlist->last_four; $cards['card_token'] = $cardlist->card_token; $cards['card_type'] = $cardlist->card_type; $cards['is_default'] = $default; } $code_data = Ledger::where('owner_id', '=', $owner_data->id)->first(); $owner = array(); $owner['owner_lat'] = $owner_data->latitude; $owner['owner_long'] = $owner_data->longitude; $owner['owner_dist_lat'] = $request->D_latitude; $owner['owner_dist_long'] = $request->D_longitude; // $owner['payment_type'] = $owner_data->cash_or_card; $owner['payment_type'] = ""; $owner['default_card'] = $dif_card; $owner['dest_latitude'] = $request->D_latitude; $owner['dest_longitude'] = $request->D_longitude; $owner['referral_code'] = $code_data->referral_code; $owner['is_referee'] = $owner_data->is_referee; $owner['promo_count'] = $owner_data->promo_count; /* $driver = Keywords::where('id', 1)->first(); */ $requestserv = RequestServices::where('request_id', $request->id)->first(); $charge = array(); $request_typ = ProviderType::where('id', '=', $requestserv->type)->first(); $settings = Settings::where('key', 'default_distance_unit')->first(); $unit = $settings->value; if ($unit == 0) { $unit_set = 'kms'; } elseif ($unit == 1) { $unit_set = 'miles'; } $charge['unit'] = $unit_set; if ($requestserv->base_price != 0) { $charge['base_distance'] = $request_typ->base_distance; $charge['base_price'] = currency_converted($requestserv->base_price); $charge['distance_price'] = currency_converted($requestserv->distance_cost); $charge['price_per_unit_time'] = currency_converted($requestserv->time_cost); } else { /* $setbase_price = Settings::where('key', 'base_price')->first(); $charge['base_price'] = currency_converted($setbase_price->value); $setdistance_price = Settings::where('key', 'price_per_unit_distance')->first(); $charge['distance_price'] = currency_converted($setdistance_price->value); $settime_price = Settings::where('key', 'price_per_unit_time')->first(); $charge['price_per_unit_time'] = currency_converted($settime_price->value); */ $charge['base_distance'] = $request_typ->base_distance; $charge['base_price'] = currency_converted($request_typ->base_price); $charge['distance_price'] = currency_converted($request_typ->price_per_unit_distance); $charge['price_per_unit_time'] = currency_converted($request_typ->price_per_unit_time); } $charge['total'] = currency_converted($request->total); $charge['is_paid'] = $request->is_paid; $response_array = array('success' => true, 'unique_id' => 1, 'status' => $request->status, 'is_referral_active' => $referral_code_activation, 'is_referral_active_txt' => $referral_code_activation_txt, 'is_promo_active' => $promotional_code_activation, 'is_promo_active_txt' => $promotional_code_activation_txt, 'confirmed_walker' => 0, 'is_walker_started' => $request->is_walker_started, 'is_walker_arrived' => $request->is_walker_arrived, 'is_walk_started' => $request->is_started, 'is_completed' => $request->is_completed, 'is_walker_rated' => $request->is_walker_rated, 'is_cancelled' => $request->is_cancelled, 'dest_latitude' => $request->D_latitude, 'dest_longitude' => $request->D_longitude, 'promo_id' => $request->promo_id, 'promo_code' => $request->promo_code, 'walker' => "", 'bill' => "", 'owner' => $owner, 'card_details' => $cards, 'charge_details' => $charge, 'current_walker' => 0, 'error_code' => 483, 'error' => 'No ' . Config::get('app.generic_keywords.Provider') . 's are available currently. Please try after sometime.'); } $response_code = 200; } else { /* $response_array = array('success' => false, 'error' => 'Request ID doesnot matches with ' . $var->keyword . ' ID', 'error_code' => 407); */ $response_array = array('success' => false, 'error' => 'Request ID doesnot matches with ' . Config::get('app.generic_keywords.User') . ' ID', 'error_code' => 407); $response_code = 200; } } else { $response_array = array('success' => false, 'error' => 'Request 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.User') . ' 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 get_referral_code() { $token = Input::get('token'); $owner_id = Input::get('id'); $validator = Validator::make(array('token' => $token, 'owner_id' => $owner_id), array('token' => 'required', 'owner_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 ($owner_data = $this->getOwnerData($owner_id, $token, $is_admin)) { // check for token validity if (is_token_active($owner_data->token_expiry) || $is_admin) { // Do necessary operations $ledger = Ledger::where('owner_id', $owner_id)->first(); if ($ledger) { $response_array = array('success' => true, 'referral_code' => $ledger->referral_code, 'total_referrals' => $ledger->total_referrals, 'amount_earned' => $ledger->amount_earned, 'amount_spent' => $ledger->amount_spent, 'balance_amount' => $ledger->amount_earned - $ledger->amount_spent); } else { $response_array = array('success' => false, 'error' => 'This user does not have a referral code'); } $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' => 'Owner 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'); }