コード例 #1
0
 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;
 }
コード例 #2
0
 public function userTripDetail()
 {
     $id = Request::segment(3);
     $owner_id = Session::get('user_id');
     $request = Requests::find($id);
     $request_service = RequestServices::find($id);
     if ($request->owner_id == $owner_id) {
         $locations = WalkLocation::where('request_id', $id)->orderBy('id')->get();
         $start = WalkLocation::where('request_id', $id)->orderBy('id')->first();
         $end = WalkLocation::where('request_id', $id)->orderBy('id', 'desc')->first();
         $map = "https://maps-api-ssl.google.com/maps/api/staticmap?size=249x249&style=feature:landscape|visibility:off&style=feature:poi|visibility:off&style=feature:transit|visibility:off&style=feature:road.highway|element:geometry|lightness:39&style=feature:road.local|element:geometry|gamma:1.45&style=feature:road|element:labels|gamma:1.22&style=feature:administrative|visibility:off&style=feature:administrative.locality|visibility:on&style=feature:landscape.natural|visibility:on&scale=2&markers=shadow:false|scale:2|icon:http://d1a3f4spazzrp4.cloudfront.net/receipt-new/marker-start@2x.png|{$start->latitude},{$start->longitude}&markers=shadow:false|scale:2|icon:http://d1a3f4spazzrp4.cloudfront.net/receipt-new/marker-finish@2x.png|{$end->latitude},{$end->longitude}&path=color:0x2dbae4ff|weight:4";
         foreach ($locations as $location) {
             $map .= "|{$location->latitude},{$location->longitude}";
         }
         $start_location = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng={$start->latitude},{$start->longitude}"), TRUE);
         $start_address = $start_location['results'][0]['formatted_address'];
         $end_location = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng={$end->latitude},{$end->longitude}"), TRUE);
         $end_address = $end_location['results'][0]['formatted_address'];
         $walker = Walker::find($request->confirmed_walker);
         $walker_review = WalkerReview::where('request_id', $id)->first();
         if ($walker_review) {
             $rating = round($walker_review->rating);
         } else {
             $rating = 0;
         }
         /* $var = Keywords::where('id', 4)->first();
            $currency = Keywords::where('id', 5)->first(); */
         return View::make('web.userTripDetail')->with('title', 'My ' . Config::get('app.generic_keywords.Trip') . 's')->with('request', $request)->with('request_service', $request_service)->with('start_address', $start_address)->with('end_address', $end_address)->with('currency', Config::get('app.generic_keywords.Currency'))->with('start', $start)->with('end', $end)->with('map_url', $map)->with('walker', $walker)->with('rating', $rating);
     } else {
         echo "false";
     }
 }