Monday, October 16, 2017

sdf


Thursday, May 25, 2017

How to set Deep Linking in Laravel 5.4

How to set Deep Linking in Laravel 5.4:

For this

    public function redirectDeepLink(Request $request) {
        try {
            $device = $this->isMobileDevice();
            $id = $request->input('itemId');
            $app = config('constant.DEEPLINKING.APP') . $id;

            $data = array();
            if ($device == 'iPhone') {
                $data['primaryRedirection'] = $app;
                $data['secndaryRedirection'] = config('constant.DEEPLINKING.APPSTORE');
            } else {
                $redirect = config('constant.DEEPLINKING.WEBSITE');
                return redirect($redirect);
            }
            return View('deep-linking', $data);
        } catch (Exception $e) {
            Log::error(__METHOD__ . ' ' . $e->getMessage());
            return Utilities::responseError(__('messages.SOMETHING_WENT_WRONG') . $e->getMessage());
        }
    }

    private function isMobileDevice() {
        $aMobileUA = array(
            '/iphone/i' => 'iPhone',
            '/ipod/i' => 'iPod',
            '/ipad/i' => 'iPad',
            '/android/i' => 'Android',
            '/blackberry/i' => 'BlackBerry',
            '/webos/i' => 'Mobile'
        );
        //Return true if Mobile User Agent is detected
        foreach ($aMobileUA as $sMobileKey => $sMobileOS) {
            if (preg_match($sMobileKey, $_SERVER['HTTP_USER_AGENT'])) {
                return $sMobileOS;
            }
        }
        //Otherwise return false..
        return false;
    }


Here:
config('constant.DEEPLINKING.APP') is Path of App
config('constant.DEEPLINKING.APPSTORE') is Path of App in Appstore
config('constant.DEEPLINKING.WEBSITE') is website domain

in deep-linking.blade.php add this code

<html>
<head>
</head>
<body>
<script>

  window.location = "{{$primaryRedirection}}"; // will result in error message if app not installed
  setTimeout(function() {
     // Link to the App Store should go here -- only fires if deep link fails
     window.location = "{{$secndaryRedirection}}";
  }, 500);

</script>
</body>
</html>


Thanks

Stripe Connect Account with Laravel 5.4

Stripe Connect Account with Laravel 5.4:

In laravel 5.4, I want to use Stripe Connect & payment to this connected account from an another user's card.

For this, firstly connect Stripe Account:

    public function getConnect() {
        try {
            if (isset($_GET['code']) && isset($_GET['state'])) {
                $userId = UtilitiesModels::getUserId($_GET['state']);
                if ($userId == config('constant.NUMERIC_ZERO')) {
                    return Utilities::responseError(__('messages.UNAUTHORISED'), config('constant.FOUR_HUNDRED'));
                }

                $token_request_body = array(
                    'grant_type' => 'authorization_code',
                    'client_id' => env('STRIPE_CLIENT_ID'),
                    'code' => $_GET['code'],
                    'client_secret' => env('STRIPE_SECRET_KEY')
                );
                $req = curl_init(env('STRIPE_TOKEN_URI'));
                curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($req, CURLOPT_POST, true);
                curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($token_request_body));
                curl_getinfo($req, CURLINFO_HTTP_CODE);
                $stripe = json_decode(curl_exec($req), true);
                curl_close($req);

                if (isset($stripe['stripe_user_id']) && !empty($stripe['stripe_user_id'])) {
                    UtilitiesModels::updateSellerStripeAcc($userId, $stripe['stripe_user_id']);
                    $response = redirect()->away(config('constant.HTTP_SUCCESS'));
                } else {
                    $response = redirect()->away(config('constant.HTTP_ERROR'));
                }
            } else {
                $response = redirect()->away(config('constant.HTTP_ERROR'));
            }
        } catch (Exception $e) {
            Log::error(__METHOD__ . ' ' . $e->getMessage());
            $response = redirect()->away(config('constant.HTTP_ERROR'));
        }
        return $response;
    }


Now i want to charge amount from first stripe token (which we get from card details) & then transfer to this connected stripe account

Stripe::setApiKey(env('STRIPE_SECRET_KEY'));
            $charge = Charge::create(array(
                        'source' => $request->buyerStripeToken,
                        'amount' => $request->amount * config('constant.HUNDRED'),
                        'currency' => 'aud',
                        'description' => __('messages.PAYMENT_FROM_BUYER'),
                        'destination' => array(
                            "amount" => $sellerEntity->price * config('constant.HUNDRED'),
                            "account" => $user->seller_stripe_account
                        ),
            ));


Here:
$_GET['code'] is the code which we get from Stripe after Stripe Auth.
$_GET['state'] is access token which we get from front end for getting user id.
env('STRIPE_CLIENT_ID') is stripe client id.
env('STRIPE_SECRET_KEY') is stripe secret key.
env('STRIPE_TOKEN_URI') is https://connect.stripe.com/oauth/token
config('constant.HTTP_SUCCESS') is http://success
config('constant.HTTP_ERROR') is http://error
$request->buyerStripeToken is Stripe token which comes from stripe card
$request->amount is amount which we deduct
$sellerEntity->price is amount which we transfer to Stripe Account
$user->seller_stripe_account is connected Stripe Account

Thanks

Saturday, February 25, 2017

Convert to camel case and snake case in php

Convert to camel case and snake case in php:

In php, if we want to convert camel case to snake case and vise versa then we have to use following methods:

public static function convertToCamelCase($array) {
        $converted_array = [];
        foreach ($array as $old_key => $value) {
            if (is_array($value)) {
                $value = static::convertToCamelCase($value);
            } else if (is_object($value)) {
                if (method_exists($value, 'toArray')) {
                    $value = $value->toArray();
                } else {
                    $value = (array) $value;
                }
                $value = static::convertToCamelCase($value);
            }
            $converted_array[camel_case($old_key)] = $value;
        }
        return $converted_array;
    }


public static function convertToSnakeCase($array) {
        $converted_array = [];
        foreach ($array as $old_key => $value) {
            if (is_array($value)) {
                $value = static::convertToSnakeCase($value);
            } else if (is_object($value)) {
                if (method_exists($value, 'toArray')) {
                    $value = $value->toArray();
                } else {
                    $value = (array) $value;
                }
                $value = static::convertToSnakeCase($value);
            }
            $new_key = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', $old_key)), '_');
            $converted_array[$new_key] = $value;
        }
        return $converted_array;
    }



This will help for this.

Laravel 5.4 validation for age lesser than 18

Laravel 5.4 validation for age lesser than 18:

In laravel 5.4 validation we have only min & max value , which counts the number.

If in such case we have to validate age then this will not help.

For this we have to use regex like this:

            'age' => ['required','numeric','regex:/^(?:[1-9]\d{2,}+|[2-9]\d|1[89])$/i'],

It will not allow age lesser than 18 

Thanks

Laravel multi select box get old value

Laravel multi select box get old value:

Hi, Some time we want to get old values in laravel (when there is some validation apply on a form.).

In this case, genrally we are unable to get old value for a multiple select box.

For this use this code:

<select name="tribe[]" id="tags" class="form-control" multiple>
                                    @if (is_array(old('tribe')))
                                    @foreach ($tribes as $tribe)
                                    <option value="{{ $tribe->id }}" <?php if(in_array($tribe->id, old('tribe'))) {echo 'selected';} ?> >{{ $tribe->name }}</option>
                                    @endforeach
                                    @else
                                    @foreach ($tribes as $tribe)
                                    <option value="{{ $tribe->id }}" >{{ $tribe->name }}</option>
                                    @endforeach
                                    @endif
                                </select>


This will help for get old values in multiple select box.

Thanks

Saturday, November 19, 2016

Laravel 5.3 search bar using jquery Autocomplete

Laravel 5.3 search bar using jquery Autocomplete:

For set up search bar in Laravel 5.3, we have to do the folloing things:

In your blade file write this code:
Also add jquery library in your code which is mentioned in this code.

<input  id="surgeon-name"  type="text" class="form-control pd-l-50" placeholder="SEARCH BY SURGEON NAME">

<script src="{{asset('js/jquery-1.12.4.js')}}"></script>
<script src="{{asset('js/jquery-ui.js')}}"></script>
<script>
    (function () {
    src = "/prefcard/maker-search-surgeon";
    $("#surgeon-name").autocomplete({
    source: function (request, response) {
    $.ajax({
    url: src,
            dataType: "json",
            data: {
            term: request.term
            },
            success: function (data) {
            response(data);
            }
    });
    },
            min_length: 3,
            select: function (event, ui)
            {
//                console.log(ui.item.value);return false;
            var test = ui.item.value ? ui.item.value : '';
            if (test != '')
            {
            var url = '/prefcard/maker-search-surgeon';
            var formAutocomplete = $('<form action="' + url + '" method="post">' +
                    '<input type="hidden" name="_token" value="{{ csrf_token() }}">' +
                    '<input type="text" name="term" value="' + ui.item.value + '" />' +
                    '</form>');
            $('body').append(formAutocomplete);
            formAutocomplete.submit();
            }
            }

    });
    })();
</script>


In your routes file write this code

                Route::get('maker-search-surgeon', 'SearchController@searchSurgeon');
                Route::post('maker-search-surgeon', 'SearchController@postSearchSurgeon');


In your SearchController create two method

    public function searchSurgeon(Request $request) {
        $query = $request->get('term', '');

        $results = DB::table('surgeon')
                        ->where('firstname', 'LIKE', '%' . $query . '%')
                        ->orWhere('lastname', 'LIKE', '%' . $query . '%')
                        ->take(5)->get();

        $data = array();
        foreach ($results as $result) {
            $data[] = array('value' => $result->firstname . ' ' . $result->lastname, 'id' => $result->id);
        }
        if (count($data))
            return $data;
        else
            return ['value' => 'No Result Found', 'id' => ''];
    }

    public function postSearchSurgeon(Request $request) {
        //Do whatever you want to search accordingly name and then return
        return view('dashboard')->with('surgeon', $surgeon);
    }


Thanks